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"
34static const SDL_Color inputbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
35static const SDL_Color inputhighlightcolor = { 0x80, 0, 0, 0x60 };
36static const SDL_Color inputmouseovercolor = { 0, 0x80, 0, 0x60 };
37static const SDL_Color inputfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
38static const SDL_Color labelbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
39static const SDL_Color labelfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
40static const Uint32 vpadding = 5;
41static const Uint32 hpadding = 10;
43SdlInputWidget::SdlInputWidget(std::shared_ptr<SDL_Renderer>& renderer, std::string label,
44 std::string initial, Uint32 flags,
size_t offset,
size_t width,
46 : _flags(flags), _text(std::move(initial)), _text_label(std::move(label)),
48 { 0,
static_cast<float>(offset * (height + vpadding)),
static_cast<float>(width),
49 static_cast<float>(height) },
52 {
static_cast<float>(width + hpadding),
53 static_cast<float>(offset * (height + vpadding)),
static_cast<float>(width),
54 static_cast<float>(height) },
56 _highlight(
false), _mouseover(
false)
61 : _flags(other._flags), _text(std::move(other._text)),
62 _text_label(std::move(other._text_label)), _label(std::move(other._label)),
63 _input(std::move(other._input)), _highlight(other._highlight), _mouseover(other._mouseover)
67bool SdlInputWidget::fill_label(std::shared_ptr<SDL_Renderer>& renderer, SDL_Color color)
69 if (!_label.fill(renderer, color))
71 return _label.update_text(renderer, _text_label, labelfontcolor);
74bool SdlInputWidget::update_label(std::shared_ptr<SDL_Renderer>& renderer)
76 return _label.update_text(renderer, _text_label, labelfontcolor, labelbackgroundcolor);
79bool SdlInputWidget::set_mouseover(std::shared_ptr<SDL_Renderer>& renderer,
bool mouseOver)
83 _mouseover = mouseOver;
84 return update_input(renderer);
87bool SdlInputWidget::set_highlight(std::shared_ptr<SDL_Renderer>& renderer,
bool highlight)
91 _highlight = highlight;
92 return update_input(renderer);
95bool SdlInputWidget::update_input(std::shared_ptr<SDL_Renderer>& renderer)
const
97 std::vector<SDL_Color> colors = { inputbackgroundcolor };
99 colors.push_back(inputhighlightcolor);
101 colors.push_back(inputmouseovercolor);
103 if (!_input.fill(renderer, colors))
105 return update_input(renderer, inputfontcolor);
108bool SdlInputWidget::set_str(std::shared_ptr<SDL_Renderer>& renderer,
const std::string& text)
113 return update_input(renderer);
116bool SdlInputWidget::remove_str(std::shared_ptr<SDL_Renderer>& renderer,
size_t count)
125 auto newsize = _text.size() - std::min<size_t>(_text.size(), count);
126 _text = _text.substr(0, newsize);
127 return update_input(renderer);
130bool SdlInputWidget::append_str(std::shared_ptr<SDL_Renderer>& renderer,
const std::string& text)
137 return update_input(renderer);
140const SDL_FRect& SdlInputWidget::input_rect()
const
142 return _input.rect();
145std::string SdlInputWidget::value()
const
150bool SdlInputWidget::readonly()
const
152 return (_flags & SDL_INPUT_READONLY) != 0;
155bool SdlInputWidget::update_input(std::shared_ptr<SDL_Renderer>& renderer, SDL_Color fgcolor)
const
157 std::string text = _text;
160 if (_flags & SDL_INPUT_MASK)
167 return _input.update_text(renderer, text, fgcolor);