20 #include "sdl_input.hpp"
28 #include <SDL3_ttf/SDL_ttf.h>
30 #include "sdl_widget.hpp"
31 #include "sdl_button.hpp"
32 #include "sdl_buttons.hpp"
34 static const SDL_Color inputbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
35 static const SDL_Color inputhighlightcolor = { 0x80, 0, 0, 0x60 };
36 static const SDL_Color inputmouseovercolor = { 0, 0x80, 0, 0x60 };
37 static const SDL_Color inputfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
38 static const SDL_Color labelbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
39 static const SDL_Color labelfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
40 static const Uint32 vpadding = 5;
41 static const Uint32 hpadding = 10;
43 SdlInputWidget::SdlInputWidget(SDL_Renderer* renderer, std::string label, std::string initial,
44 Uint32 flags,
size_t offset,
size_t width,
size_t height)
45 : _flags(flags), _text(std::move(initial)), _text_label(std::move(label)),
47 { 0,
static_cast<float>(offset * (height + vpadding)),
static_cast<float>(width),
48 static_cast<float>(height) },
51 {
static_cast<float>(width + hpadding),
52 static_cast<float>(offset * (height + vpadding)),
static_cast<float>(width),
53 static_cast<float>(height) },
55 _highlight(
false), _mouseover(
false)
60 : _flags(other._flags), _text(std::move(other._text)),
61 _text_label(std::move(other._text_label)), _label(std::move(other._label)),
62 _input(std::move(other._input)), _highlight(other._highlight), _mouseover(other._mouseover)
66 bool SdlInputWidget::fill_label(SDL_Renderer* renderer, SDL_Color color)
68 if (!_label.fill(renderer, color))
70 return _label.update_text(renderer, _text_label, labelfontcolor);
73 bool SdlInputWidget::update_label(SDL_Renderer* renderer)
75 return _label.update_text(renderer, _text_label, labelfontcolor, labelbackgroundcolor);
78 bool SdlInputWidget::set_mouseover(SDL_Renderer* renderer,
bool mouseOver)
82 _mouseover = mouseOver;
83 return update_input(renderer);
86 bool SdlInputWidget::set_highlight(SDL_Renderer* renderer,
bool highlight)
90 _highlight = highlight;
91 return update_input(renderer);
94 bool SdlInputWidget::update_input(SDL_Renderer* renderer)
96 std::vector<SDL_Color> colors = { inputbackgroundcolor };
98 colors.push_back(inputhighlightcolor);
100 colors.push_back(inputmouseovercolor);
102 if (!_input.fill(renderer, colors))
104 return update_input(renderer, inputfontcolor);
107 bool SdlInputWidget::resize_input(
size_t size)
114 bool SdlInputWidget::set_str(SDL_Renderer* renderer,
const std::string& text)
119 if (!resize_input(_text.size()))
121 return update_input(renderer);
124 bool SdlInputWidget::remove_str(SDL_Renderer* renderer,
size_t count)
133 if (!resize_input(_text.size() - count))
135 return update_input(renderer);
138 bool SdlInputWidget::append_str(SDL_Renderer* renderer,
const std::string& text)
145 if (!resize_input(_text.size()))
147 return update_input(renderer);
150 const SDL_FRect& SdlInputWidget::input_rect()
const
152 return _input.rect();
155 std::string SdlInputWidget::value()
const
160 bool SdlInputWidget::readonly()
const
162 return (_flags & SDL_INPUT_READONLY) != 0;
165 bool SdlInputWidget::update_input(SDL_Renderer* renderer, SDL_Color fgcolor)
167 std::string text = _text;
170 if (_flags & SDL_INPUT_MASK)
177 return _input.update_text(renderer, text, fgcolor);