-
Notifications
You must be signed in to change notification settings - Fork 5
/
TextEdit.h
80 lines (77 loc) · 2.03 KB
/
TextEdit.h
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
#pragma once
#include "TtfWrapper.h"
#include "SDL_include.h"
#include <string>
class TextEdit : public TtfWrapper
{
public:
SDL_Rect click_area;
//Initializes variables
TextEdit(){}
TextEdit(const int &x, const int &y, const int &w, const int &h,
const Uint8 &red, const Uint8 &green, const Uint8 &blue,
const SDL_BlendMode &blending, const Uint8 &alpha,
const SDL_Color &_textColor, const std::string &font_name, const int &font_size,const bool &_visible){
click_area.x = x;
click_area.y = y;
click_area.w = w;
click_area.h = h;
renderQuad.x = x;
renderQuad.y = y;
textColor = _textColor;
setColor(red, green, blue);
setBlendMode(blending);
setAlpha(alpha);
font = TTF_OpenFont(font_name.c_str(), font_size);
visible = _visible;
}
~TextEdit(){}
bool isIn(const int &x, const int &y){
if (click_area.x <= x && x <= click_area.x + click_area.w && click_area.y <= y && y <= click_area.y + click_area.h){
return true;
}
return false;
}
void handleKey(SDL_Event *e){
//Special key input
if (e->type == SDL_KEYDOWN)
{
//Handle backspace
if (e->key.keysym.sym == SDLK_BACKSPACE && text.length() > 0)
{
//lop off character
text.pop_back();
}
//Handle copy
else if (e->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL)
{
text = SDL_SetClipboardText(text.c_str());
}
//Handle paste
else if (e->key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL)
{
text = SDL_GetClipboardText();
}
if (e->key.keysym.sym == SDLK_RETURN)
setFocus(false);
}
//Special text input event
else if (e->type == SDL_TEXTINPUT)
{
//Not copy or pasting
if (!((e->text.text[0] == 'c' || e->text.text[0] == 'C') && (e->text.text[0] == 'v' || e->text.text[0] == 'V') && SDL_GetModState() & KMOD_CTRL))
{
//Append character
text += e->text.text;
}
}
}
bool getFocus(){
return focus;
}
void setFocus(bool _focus){
focus = _focus;
}
private:
bool focus;
};