-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbox.cpp
More file actions
94 lines (84 loc) · 1.7 KB
/
textbox.cpp
File metadata and controls
94 lines (84 loc) · 1.7 KB
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
86
87
88
89
90
91
92
93
94
#include "textbox.h"
TextBox::TextBox(const std::string& caption, int w = 200) : m_caption(caption), m_w(w), m_focussed(false), m_str("")
{
m_h = 25;
m_font = new Font("images/mono.ttf",18);
m_enabled = true;
m_audio = NULL;
}
void TextBox::addAudio(Audio *audio)
{
m_audio = audio;
}
void TextBox::render(int x, int y)
{
m_x = x;
m_y = y + 25;
glColor3f(0,1,0);
m_font->render(m_caption, m_x, m_y - 25 + 17);
glBegin(GL_LINE_LOOP);
glVertex2f(m_x, m_y);glVertex2f(m_x + m_w, m_y);
glVertex2f(m_x + m_w, m_y + m_h);glVertex2f(m_x, m_y + m_h);
glEnd();
std::string displayStr = m_str;
if(m_focussed == true)
{
displayStr = displayStr + "|";
}
m_font->render(displayStr.c_str(), m_x + 4, m_y + 17);
}
bool TextBox::isFocussed()
{
return m_focussed;
}
bool TextBox::isEnabled()
{
return m_enabled;
}
void TextBox::setEnabled(bool state)
{
m_enabled = state;
}
std::string TextBox::getText()
{
return m_str;
}
void TextBox::setText(std::string& text)
{
m_str = text;
}
void TextBox::handleEvents(SDL_Event event)
{
if(m_enabled == false) return;
if(event.type == SDL_MOUSEBUTTONDOWN)
{
int x = event.button.x;
int y = event.button.y;
if((x >= m_x && x <= m_x + m_w) && (y >= m_y && y <= m_y + m_h))
{
m_focussed = true;
}
else
{
m_focussed = false;
}
}
if(event.type == SDL_KEYDOWN && m_focussed == true)
{
int key = event.key.keysym.sym;
if((key >= 32 && key <= 126) && m_str.length() < 18)
{
if(m_audio) m_audio->playSound("keystroke");
m_str = m_str + char(key);
}
if((key == 8 || key == 127) && m_str.length() != 0)
{
if(m_audio) m_audio->playSound("keystroke");
m_str.erase(m_str.length() - 1, 1);
}
}
}
TextBox::~TextBox()
{
delete m_font;
}