00001
00002
00003 #include "gui/textbox.h"
00004 using namespace VGUI;
00005
00006 TextBox::TextBox( Widget* w )
00007 {
00008 setting = "";
00009 accepts = "";
00010 center = w->GetCenter();
00011 name = w->GetName();
00012 text = w->GetText();
00013 type = w->GetType();
00014 tip = w->GetTip();
00015 rel_width = w->GetRelWidth();
00016 rel_height = w->GetRelHeight();
00017 enabled = w->GetEnabled();
00018 selected = w->GetSelected();
00019 is_default = w->GetDefault();
00020 is_cancel = w->GetCancel();
00021 has_icon = w->GetHasIcon();
00022 width_auto = w->GetWidthAuto();
00023 height_auto = w->GetHeightAuto();
00024 font_size = w->GetFontSize();
00025 color_r = w->GetColorR();
00026 color_g = w->GetColorG();
00027 color_b = w->GetColorB();
00028 spacing = 0.3;
00029 cursor_pos = 0;
00030 highlight_start = highlight_end = 10000;
00031
00032 string skin_path = settings.GetSkinPath();
00033 textbox_tex.Load( skin_path + "/textures/widgets/text_box.png", false );
00034 cursor_tex.Load( skin_path + "/textures/widgets/text_cursor.png", false );
00035 highlight_tex.Load( skin_path + "/textures/widgets/text_highlight.png", false );
00036
00037 tex_width = 0.0277;
00038 tex_height = 0.0370;
00039
00040
00041 if( width_auto )
00042 {
00043 rel_width = spacing + font.Width( "999.999.999.999", 1, 6 ) + 0.1;
00044 }
00045
00046
00047 if( height_auto )
00048 {
00049 rel_height = tex_height;
00050 }
00051
00052 l_down=r_down=bkspc_down=del_down=mbtn_down=cursor_on = false;
00053 }
00054
00055 TextBox::~TextBox()
00056 {
00057 }
00058
00059 void TextBox::SetSpacing( float new_spacing )
00060 {
00061 spacing = new_spacing;
00062 if( width_auto )
00063 {
00064 rel_width = spacing + font.Width( "999.999.999.999", 1, 6 ) + 0.1;
00065 }
00066 }
00067
00068 void TextBox::Save()
00069 {
00070 settings.Set( setting, value );
00071 }
00072
00073 void TextBox::ResetValue()
00074 {
00075 string default_val;
00076 settings.Get( setting, default_val );
00077 SetValue( default_val );
00078 }
00079
00080 void TextBox::Draw()
00081 {
00082 Draw( 1.0 );
00083 }
00084
00085 void TextBox::Draw( float opacity )
00086 {
00087
00088 float center_x = center.GetXPercent();
00089 float center_y = center.GetYPercent();
00090 float rel_width_half = rel_width / 2.0f;
00091 float rel_height_half = rel_height / 2.0f;
00092 float opacity_txt = selected ? 0.9 : enabled ? 0.75 : 0.4;
00093
00094
00095 utility.DrawButton( center_x - rel_width_half + spacing + 0.02777,
00096 center_y - rel_height_half,
00097 center_x + rel_width_half - 0.02777,
00098 center_y + rel_height_half,
00099 0.02777, &textbox_tex, opacity * opacity_txt );
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 font.Print( center_x - rel_width_half + spacing + 0.029,
00128 center_y - rel_height_half - 0.005,
00129 value.c_str(), 1, font_size, 0.0, 0.0, 0.0, opacity );
00130
00131 float blink_time = (float)( SDL_GetTicks() - last_blink ) / 1000.0;
00132 if( blink_time >= 0.5 )
00133 {
00134 cursor_on = !cursor_on;
00135 last_blink = SDL_GetTicks();
00136 }
00137
00138
00139 if( selected && cursor_on )
00140 {
00141 float cursor_x = center_x - rel_width_half + spacing + 0.02777 + font.Width( value.substr( 0, cursor_pos ).c_str(), 1, font_size );
00142
00143 utility.Draw2D( cursor_x,
00144 center_y - rel_height_half,
00145 cursor_x + tex_width,
00146 center_y + rel_height_half,
00147 &cursor_tex, 0.0, 0, opacity );
00148 }
00149
00150
00151 font.Print( center_x - rel_width_half,
00152 center_y - ( font.Height( text.c_str(), 1, font_size ) / 2.0f ),
00153 text.c_str(), 1, font_size, color_r, color_g, color_b, opacity_txt * opacity );
00154
00155
00156 if( selected && ( tip != "" ) )
00157 font.Print( 0.5 - ( font.Width( tip.c_str(), 1, 6 ) / 2.0f ), 0.90, tip.c_str(), 1, 6, 0.7, 0.7, 0.7, 0.9 );
00158 }
00159
00160 void TextBox::IncValuePress()
00161 {
00162 Release();
00163 r_down = true;
00164 }
00165
00166 void TextBox::IncValueRelease()
00167 {
00168 if( r_down && ( cursor_pos < value.size() ) )
00169 {
00170 cursor_pos++;
00171 }
00172 Release();
00173 }
00174
00175 void TextBox::DecValuePress()
00176 {
00177 Release();
00178 l_down = true;
00179 }
00180
00181 void TextBox::DecValueRelease()
00182 {
00183 if( l_down && ( cursor_pos > 0 ) )
00184 {
00185 cursor_pos--;
00186 }
00187 Release();
00188 }
00189
00190 void TextBox::BackspacePress()
00191 {
00192 Release();
00193 bkspc_down = true;
00194 }
00195
00196 void TextBox::BackspaceRelease()
00197 {
00198 if( bkspc_down && ( cursor_pos > 0 ) )
00199 {
00200 cursor_pos--;
00201 value.erase( cursor_pos, 1 );
00202 }
00203 Release();
00204 }
00205
00206 void TextBox::DeletePress()
00207 {
00208 Release();
00209 del_down = true;
00210 }
00211
00212 void TextBox::DeleteRelease()
00213 {
00214 if( del_down && ( cursor_pos < value.size() ) )
00215 {
00216 value.erase( cursor_pos, 1 );
00217 }
00218 Release();
00219 }
00220
00221 void TextBox::Release()
00222 {
00223 r_down = l_down = bkspc_down = del_down = false;
00224 }
00225
00226 void TextBox::EnterText( SDLKey key )
00227 {
00228 if( key == SDLK_END )
00229 {
00230 cursor_pos = value.size();
00231 return;
00232 }
00233 else if( key == SDLK_HOME )
00234 {
00235 cursor_pos = 0;
00236 return;
00237 }
00238
00239 for( unsigned int i = 0; i < accepts.size(); i++ )
00240 {
00241 if( accepts[i] == key )
00242 {
00243 string t;
00244 t.append((char *) &key);
00245 value.insert( cursor_pos, t );
00246 cursor_pos++;
00247 break;
00248 }
00249 }
00250 }
00251
00252 bool TextBox::MouseOver( float x, float y )
00253 {
00254
00255 float min_x, min_y, max_x, max_y;
00256 float center_x = center.GetXPercent();
00257 float center_y = center.GetYPercent();
00258 float rel_width_half = rel_width / 2.0f;
00259 float rel_height_half = rel_height / 2.0f;
00260 min_x = center_x - rel_width_half;
00261 min_y = center_y - rel_height_half;
00262 max_x = center_x + rel_width_half;
00263 max_y = center_y + rel_height_half;
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 return ( x > min_x ) && ( x < max_x ) && ( y > min_y ) && ( y < max_y );
00313 }
00314
00315 string TextBox::MousePress( float x, float y )
00316 {
00317 string action = "";
00318 string::size_type cursor_pos_click = 10000;
00319 float min_x, min_y, max_x, max_y;
00320 float center_x = center.GetXPercent();
00321 float center_y = center.GetYPercent();
00322 float rel_width_half = rel_width / 2.0f;
00323 float rel_height_half = rel_height / 2.0f;
00324 min_x = center_x - rel_width_half + spacing;
00325 min_y = center_y - rel_height_half;
00326 max_x = center_x + rel_width_half;
00327 max_y = center_y + rel_height_half;
00328
00329 if( ( x >= min_x ) && ( x <= max_x ) && ( y >= min_y ) && ( y <= max_y ) )
00330 {
00331 mbtn_down = true;
00332 float char_width = 0.0;
00333 float min_char_x, max_char_x;
00334 float half_char_width;
00335
00336 for( string::size_type i = 0; i < value.size(); i++ )
00337 {
00338
00339 min_char_x = min_x + 0.029 + ( i == 0 ? 0.0 : font.Width( value.substr( 0, i - 1 ).c_str(), 1, 6 ) );
00340 max_char_x = min_x + 0.029 + font.Width( value.substr( 0, i ).c_str(), 1, 6 );
00341 char_width = max_char_x - min_char_x;
00342 half_char_width = char_width / 2.0f;
00343
00344 if( ( x >= min_char_x ) && ( x <= ( max_char_x - half_char_width ) ) )
00345 {
00346
00347 cursor_pos_click = i - 1;
00348 break;
00349 }
00350 else if( ( x > ( max_char_x - half_char_width ) ) && ( x < max_char_x ) )
00351 {
00352
00353 cursor_pos_click = i;
00354 break;
00355 }
00356 }
00357
00358 if( cursor_pos_click == 10000 )
00359 {
00360 if( ( x >= min_x ) && ( x < min_x + 0.029 ) )
00361 {
00362 cursor_pos = 0;
00363 }
00364 else
00365 {
00366 cursor_pos = value.size();
00367 }
00368 }
00369 else
00370 {
00371 cursor_pos = cursor_pos_click;
00372 }
00373
00374 highlight_start = cursor_pos;
00375 }
00376
00377 return action;
00378 }
00379
00380 string TextBox::MouseRelease( float x, float y )
00381 {
00382 mbtn_down = false;
00383 string action = "";
00384 string::size_type cursor_pos_click = 10000;
00385 float min_x, min_y, max_x, max_y;
00386 float center_x = center.GetXPercent();
00387 float center_y = center.GetYPercent();
00388 float rel_width_half = rel_width / 2.0f;
00389 float rel_height_half = rel_height / 2.0f;
00390 min_x = center_x - rel_width_half + spacing;
00391 min_y = center_y - rel_height_half;
00392 max_x = center_x + rel_width_half;
00393 max_y = center_y + rel_height_half;
00394
00395
00396 if( ( x >= min_x ) && ( x <= max_x ) && ( y >= min_y ) && ( y <= max_y ) )
00397 {
00398
00399 float char_width = 0.0;
00400 float min_char_x, max_char_x;
00401 float half_char_width;
00402
00403 for( string::size_type i = 0; i < value.size(); i++ )
00404 {
00405
00406 min_char_x = min_x + 0.029 + ( i == 0 ? 0.0 : font.Width( value.substr( 0, i - 1 ).c_str(), 1, 6 ) );
00407 max_char_x = min_x + 0.029 + font.Width( value.substr( 0, i ).c_str(), 1, 6 );
00408 char_width = max_char_x - min_char_x;
00409 half_char_width = char_width / 2.0f;
00410
00411 if( ( x >= min_char_x ) && ( x <= ( max_char_x - half_char_width ) ) )
00412 {
00413
00414 cursor_pos_click = i - 1;
00415 break;
00416 }
00417 else if( ( x > ( max_char_x - half_char_width ) ) && ( x < max_char_x ) )
00418 {
00419
00420 cursor_pos_click = i;
00421 break;
00422 }
00423 }
00424
00425 if( cursor_pos_click == 10000 )
00426 {
00427 if( ( x >= min_x ) && ( x < min_x + 0.029 ) )
00428 {
00429 cursor_pos = 0;
00430 }
00431 else
00432 {
00433 cursor_pos = value.size();
00434 }
00435 }
00436 else
00437 {
00438 cursor_pos = cursor_pos_click;
00439 }
00440
00441
00442 if( ( cursor_pos != highlight_start ) && ( highlight_start != 10000 ) )
00443 highlight_end = cursor_pos;
00444 else
00445 highlight_start = highlight_end = 10000;
00446 }
00447
00448 return action;
00449 }