Repository navigation
Expand file tree
/
Copy pathTextInput.cpp
More file actions
75 lines (66 loc) · 1.67 KB
/
Copy pathTextInput.cpp
File metadata and controls
75 lines (66 loc) · 1.67 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
#include "TextInput.h"
TextInput::TextInput(std::shared_ptr<Font> font, unsigned int x, unsigned int y, const std::string& label, float x_scale, float y_scale)
: TextLabel(font, x, y, "", x_scale, y_scale),
label(label)
{
UpdateLabel();
}
TextInput::TextInput(std::shared_ptr<Font> font, unsigned int x, unsigned int y, const std::string& label, float scale)
: TextLabel(font, x, y, "", scale, scale),
label(label)
{
UpdateLabel();
}
void TextInput::SetInputText(const std::string& text)
{
input_text = text;
UpdateLabel();
}
void TextInput::SetLabelText(const std::string& text)
{
label = text;
UpdateLabel();
}
const std::string& TextInput::GetInputText() const
{
return input_text;
}
void TextInput::OnKeyDown(SDL_Scancode scancode, SDL_Keymod mod)
{
SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, mod, false);
if(keycode == SDLK_BACKSPACE || keycode == SDLK_DELETE)
{
if(!input_text.empty())
{
input_text.pop_back();
UpdateLabel();
}
}
else if(keycode == SDLK_RETURN || keycode == SDLK_KP_ENTER)
{
if(font->IsValid('\n'))
{
input_text += '\n';
UpdateLabel();
}
}
else if(font->IsValid(static_cast<char>(keycode)))
{
char c = static_cast<char>(keycode);
input_text += c;
UpdateLabel();
}
}
void TextInput::OnKeyUp(SDL_Scancode scancode, SDL_Keymod mod)
{
// Handle key up events for text input
}
void TextInput::Draw(SDL_Renderer* renderer)
{
// Draw the text input field
TextLabel::Draw(renderer);
}
void TextInput::UpdateLabel()
{
TextLabel::SetLabelText(label + input_text);
}