-
Notifications
You must be signed in to change notification settings - Fork 15
/
text_edit.h
78 lines (56 loc) · 1.84 KB
/
text_edit.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
#ifndef TEXT_EDIT_H_
#define TEXT_EDIT_H_
#include <cstdint>
#include <string>
#include <SDL.h>
#include "def.h"
#include "sdl_ptrs.h"
class TextEdit {
public:
explicit TextEdit(bool support_tabs = false)
: support_tabs_(support_tabs)
{
}
void setDimensions(int width, int height);
int width() const { return width_; }
int height() const { return height_; }
void blitBackground(SDL_Surface &out, int x, int y) const;
void blitForeground(SDL_Surface &out, int x, int y) const;
const std::string &text() const { return text_; }
void typeText(const std::string &text);
void typeText(char c);
bool backspace();
bool del();
bool isFocused() const { return focused_; }
void setFocused(bool val) { focused_ = val; }
bool moveCursorPrev();
bool moveCursorNext();
bool setCursorToStart();
bool setCursorToEnd();
private:
void blitFocus(SDL_Surface &out, int x, int y) const;
void prepareSurfaces();
void prepareColors();
void updateBackground();
void updateForeground() const;
std::string text_;
std::size_t cursor_pos_ = 0;
bool focused_ = false;
// Rendering composes background, foreground, and cursor.
//
// Cached surfaces:
mutable SDLSurfaceUniquePtr background_, foreground_;
mutable int cursor_x_, text_x_ = 0;
mutable bool update_foreground_ = false;
SDL_Rect foreground_rect_;
int width_, height_;
int border_width_x_, border_width_y_, padding_x_, padding_y_;
SDL_Color sdl_border_color_ = SDL_Color { COLOR_BORDER };
std::uint32_t border_color_;
SDL_Color sdl_focus_border_color_ = SDL_Color { COLOR_CURSOR_1 };
std::uint32_t focus_border_color_;
SDL_Color sdl_bg_color_ = SDL_Color { COLOR_BG_1 };
std::uint32_t bg_color_;
const bool support_tabs_;
};
#endif // TEXT_EDIT_H_