-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathctextinput.cpp
86 lines (68 loc) · 1.66 KB
/
ctextinput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "ctextinput.h"
void CTextInput::init( int Id, float Width, float Height, float X, float Y, float Z ) {
id = Id;
width = Width; height = Height;
x = X; y = Y; z = Z;
font.fontWidth = 20;
font.fontHeight = 30;
enabled = false;
visible = false;
inputText = "";
return;
}
void CTextInput::setFont( SFont Font ) {
font.fontWidth = Font.fontWidth;
font.fontHeight = Font.fontHeight;
font.foreColor.r = Font.foreColor.r;
font.foreColor.g = Font.foreColor.g;
font.foreColor.b = Font.foreColor.b;
font.foreColor.a = Font.foreColor.a;
return;
}
void CTextInput::draw( ) {
if ( !enabled || !visible ) {
return;
}
// Draw the string now
fontPrinter.setFont( font );
fontPrinter.print( inputText, x, y, z );
return;
}
char keysym_to_char ( SDLKey keysym ) {
switch ( keysym ) {
case SDLK_0 : return '0';
case SDLK_1 : return '1';
case SDLK_2 : return '2';
case SDLK_3 : return '3';
case SDLK_4 : return '4';
case SDLK_5 : return '5';
case SDLK_6 : return '6';
case SDLK_7 : return '7';
case SDLK_8 : return '8';
case SDLK_9 : return '9';
case SDLK_PERIOD : return '.';
default : return '\0';
}
return '\0';
}
void CTextInput::keyPressHandler( SDLKey keysym ) {
if ( ( keysym >= SDLK_0 && \
keysym <= SDLK_9 ) || \
keysym == SDLK_PERIOD ) {
inputText.push_back( keysym_to_char( keysym ) );
} else if (keysym == SDLK_BACKSPACE ) {
if ( inputText.length() > 0 ) {
inputText.erase( inputText.length() - 1 );
}
} else if (keysym == SDLK_RETURN || \
keysym == SDLK_KP_ENTER ) {
inputDoneHandler( );
}
return;
}
void CTextInput::inputDoneHandler( ) {
if ( onInputDone ) {
onInputDone( );
}
return;
}