00001
00002
00003
00004
#include <ctype.h>
00005
#include "TextEntry.h"
00006
00007 TextEntry::TextEntry(
const string &label,
const string &name,
00008
Font *font, TextEntryType type,
const string &defaultValue,
int maxLength)
00009 {
00010 label_ = label;
00011 name_ = name;
00012 value_ = defaultValue;
00013 type_ = type;
00014 font_ = font;
00015 maxLength_ = maxLength;
00016 grabbingKey_ =
false;
00017 }
00018
00019 TextEntry::~TextEntry()
00020 {
00021 }
00022
00023 void TextEntry::load(ifstream &file)
00024 {
00025
if (findLoadPosition(name_, file)){
00026 string newValue;
00027
char c;
00028 file.read(&c, 1);
00029 file.read(&c, 1);
00030
while (file.good() && c!=
'"'){
00031 newValue += c;
00032 file.read(&c, 1);
00033 }
00034 value_ = newValue;
00035 }
00036 }
00037
00038 void TextEntry::save(ofstream &file)
00039 {
00040 file << name_ <<
"=\"" << value_ <<
"\"\n";
00041 }
00042
00043 string TextEntry::getVisualString()
00044 {
00045
if (grabbingKey_){
00046
return value_;
00047 }
00048
else {
00049
return label_ + value_;
00050 }
00051 }
00052
00053 void TextEntry::draw(
float x,
float y,
float zoom)
00054 {
00055
return font_->
printString(getVisualString(), x,y,zoom);
00056 }
00057
00058 float TextEntry::getWidth(
float zoom)
00059 {
00060
return font_->
getWidth(getVisualString(), zoom);
00061 }
00062
00063 float TextEntry::getHeight(
float zoom)
00064 {
00065
return font_->
getHeight(zoom);
00066 }
00067
00068 bool TextEntry::handleKeyEvent(SDL_Event *event)
00069 {
00070
if (event->type != SDL_KEYDOWN)
return false;
00071 SDLKey key = event->key.keysym.sym;
00072
if (
00073 key == SDLK_NUMLOCK ||
00074 key == SDLK_CAPSLOCK ||
00075 key == SDLK_SCROLLOCK ||
00076 key == SDLK_RSHIFT ||
00077 key == SDLK_LSHIFT ||
00078 key == SDLK_RCTRL ||
00079 key == SDLK_LCTRL ||
00080 key == SDLK_RALT ||
00081 key == SDLK_LALT ||
00082 key == SDLK_RMETA ||
00083 key == SDLK_LMETA ||
00084 key == SDLK_LSUPER ||
00085 key == SDLK_RSUPER ||
00086 key == SDLK_MODE){
00087
return false;
00088 }
00089
00090
switch (key){
00091
case SDLK_KP0:
00092 key = (SDLKey)
'0';
00093
break;
00094
case SDLK_KP1:
00095 key = (SDLKey)
'1';
00096
break;
00097
case SDLK_KP2:
00098 key = (SDLKey)
'2';
00099
break;
00100
case SDLK_KP3:
00101 key = (SDLKey)
'3';
00102
break;
00103
case SDLK_KP4:
00104 key = (SDLKey)
'4';
00105
break;
00106
case SDLK_KP5:
00107 key = (SDLKey)
'5';
00108
break;
00109
case SDLK_KP6:
00110 key = (SDLKey)
'6';
00111
break;
00112
case SDLK_KP7:
00113 key = (SDLKey)
'7';
00114
break;
00115
case SDLK_KP8:
00116 key = (SDLKey)
'8';
00117
break;
00118
case SDLK_KP9:
00119 key = (SDLKey)
'9';
00120
break;
00121
00122
case SDLK_KP_PERIOD:
00123 key = (SDLKey)
'.';
00124
break;
00125
case SDLK_KP_DIVIDE:
00126 key = (SDLKey)
'/';
00127
break;
00128
case SDLK_KP_MULTIPLY:
00129 key = (SDLKey)
'*';
00130
break;
00131
case SDLK_KP_MINUS:
00132 key = (SDLKey)
'-';
00133
break;
00134
case SDLK_KP_PLUS:
00135 key = (SDLKey)
'+';
00136
break;
00137
case SDLK_KP_EQUALS:
00138 key = (SDLKey)
'=';
00139
break;
00140
default:
00141
break;
00142 }
00143
00144
if (grabbingKey_){
00145
if (grabbingKey_ && (key == SDLK_RETURN || key == SDLK_KP_ENTER)){
00146 grabbingKey_ =
false;
00147
return true;
00148 }
00149
00150
bool shiftIsPressed;
00151
switch (type_) {
00152
case UPPERCASE:
00153 shiftIsPressed =
true;
00154
break;
00155
case LOWERCASE:
00156 shiftIsPressed =
false;
00157
break;
00158
default:
00159 SDLMod mod = event->key.keysym.mod;
00160 shiftIsPressed = mod & (KMOD_SHIFT);
00161
break;
00162 }
00163
00164
char c = (
char)(key & 255);
00165
if (type_ ==
UPPERCASE){
00166 c = (
char)toupper(c);
00167 }
00168
00169
00170
if (c !=
'\b'){
00171
char t[2];
00172 t[0] = c;
00173 t[1] = 0;
00174 string tmp = t;
00175
if (font_->
getWidth(tmp, 1)>0){
00176 value_ += c;
00177 }
00178 }
00179
else {
00180
if (value_.size() != 0){
00181 value_.erase(value_.size()-1 ,1);
00182 }
00183 }
00184
00185
return true;
00186 }
00187
else if (key == SDLK_RETURN || key == SDLK_KP_ENTER) {
00188 grabbingKey_ =
true;
00189
return true;
00190 }
00191
else {
00192
return false;
00193 }
00194 }