bwidgets/inc/basic_widgets/w/base/input.hpp

220 lines
7.1 KiB
C++
Raw Normal View History

#ifndef BWIDGETS_INPUT_HPP_
#define BWIDGETS_INPUT_HPP_
2021-06-11 13:30:33 +02:00
#include <exception>
#include <iomanip>
2021-06-16 13:50:34 +02:00
#include <sstream>
2021-07-10 19:55:53 +02:00
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_render.h>
2021-06-16 13:50:34 +02:00
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/type/concepts.hpp>
#include <basic_widgets/w/caption.hpp>
#include <basic_widgets/w/feat/keyboard_handler.hpp>
#include <basic_widgets/w/feat/mouse_handler.hpp>
2021-06-16 13:50:34 +02:00
namespace bwidgets::widget
2021-06-11 13:30:33 +02:00
{
2021-07-10 19:55:53 +02:00
template<typename T>
class Input : public Widget,
public FontHandler,
public KeyboardHandler,
public MouseHandler
2021-07-10 19:55:53 +02:00
{
protected:
const int _border_width {3};
Caption _input_caption;
Input(Widget* parent = nullptr) : Widget(parent)
{
FocusHandler::_focus_area = &_widget_area;
MouseHandler::_click_area = &_widget_area;
_input_caption.text(value_to_string(value));
}
void _handle_focus_change(bool focus) override
{
if (focus) {
SDL_StartTextInput();
}
else {
value = value_from_string(input_text());
input_text(value_to_string(value));
SDL_StopTextInput();
}
}
void _handle_font_change(core::Font* f) override
{
_input_caption.font(f);
_handle_geometry_change(_viewport);
}
void _handle_font_color_change(const SDL_Color& fg, const SDL_Color& bg) override
{
_input_caption.color_bg(bg)->color_fg(fg);
}
void _handle_geometry_change(const SDL_Rect& vp) noexcept override
{
const auto input_h = _input_caption.size().h + 2 * _border_width;
_widget_area = {0, core::center_line(vp.h, input_h), vp.w, input_h};
_input_caption.viewport(core::rect_offset(
{core::rect_margin(_widget_area, {_border_width, _border_width})}, vp));
}
void _handle_key(const SDL_KeyboardEvent& key) override
{
if (key.type == SDL_KEYDOWN) {
switch (key.keysym.sym) {
case SDLK_BACKSPACE: {
std::string txt = input_text();
if (txt.length() > 0) {
txt.pop_back();
input_text(txt);
}
break;
2021-06-20 00:42:40 +02:00
}
case SDLK_RETURN:
case SDLK_RETURN2: // what is return2 btw?
case SDLK_KP_ENTER:
value = value_from_string(input_text());
focus(false);
break;
2021-06-20 00:42:40 +02:00
}
}
}
void _handle_renderer_change(core::Renderer* r) override
{
_input_caption.renderer(r);
}
void _handle_rendering() override
{
// TODO: smoothstep
for (int i = _border_width - 1; i >= 0; i--) {
_renderer
->draw_color({(uint8_t)(color_border.r / (i + 1)),
(uint8_t)(color_border.g / (i + 1)),
(uint8_t)(color_border.b / (i + 1)), color_border.a})
->draw_rect({_widget_area.x + i, _widget_area.y + i,
_widget_area.w - 2 * i, _widget_area.h - 2 * i});
}
if (MouseHandler::_is_hovered || FocusHandler::_has_focus)
_input_caption.color_bg(color_bg_focused);
else _input_caption.color_bg(color_bg);
_input_caption.render();
}
void _handle_text_input(const SDL_TextInputEvent& input) override
{
if (is_valid_input(input.text)) {
input_text(input_text() + input.text);
}
}
public:
SDL_Color color_border {160, 160, 160, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
SDL_Color color_bg {200, 200, 200, // NOLINT(readability-magic-numbers)
SDL_ALPHA_OPAQUE};
SDL_Color color_bg_focused {
255, 255, 255, SDL_ALPHA_OPAQUE}; // NOLINT(readability-magic-numbers)
int float_precision {2};
int input_min_width {1};
char input_width_unit {'W'};
T value {};
virtual auto color_fg(const SDL_Color& c) noexcept -> Input<T>*
{
_input_caption.color_fg(c);
return this;
}
[[nodiscard]] virtual auto input_text() const noexcept -> const std::string&
{
return _input_caption.text();
}
virtual auto input_text(const std::string& txt) noexcept -> Input<T>*
{
_input_caption.text(txt);
return this;
}
[[nodiscard]] virtual auto
is_valid_input(const std::string& /*unused*/) const noexcept -> bool
{
return true;
}
virtual auto process_value(T x) const noexcept -> T
{
return x;
}
[[nodiscard]] inline auto size() const noexcept -> core::Size override
{
return {
_font->text_size(std::string(input_min_width, input_width_unit)).w
+ 2 * _border_width,
_font->line_skip + 4 * _border_width // _why_ 4 and not 2?…
};
}
auto value_from_string(std::string s) -> T
{
T v;
if constexpr (std::is_arithmetic_v<T>) {
s = s.length() > 0 ? s : "0";
if constexpr (std::is_floating_point_v<T>) v = (T)std::stold(s);
else if constexpr (std::is_integral_v<T>) v = (T)std::stoll(s);
}
else if constexpr (std::is_same_v<
T, std::string> || std::convertible_to<std::string, T>)
v = s;
else
static_assert((bool)sizeof(T)
&& false, // NOLINT(readability-simplify-boolean-expr)
"string cannot be converted to v type T.");
return process_value(v);
}
auto value_to_string(T value) -> std::string
{
std::string s;
if constexpr (std::is_same_v<std::string,
T> || std::convertible_to<T, std::string>)
s = std::move(value);
else if constexpr (core::CanToString<T>) {
std::stringstream ss;
ss << std::fixed << std::setprecision(float_precision) << value;
s = std::move(ss).str();
}
else if constexpr (core::Printable<T>) {
std::stringstream ss;
ss << value;
s = std::move(ss).str();
}
else
static_assert((bool)sizeof(T)
&& false, // NOLINT(readability-simplify-boolean-expr)
"value cannot be converted to string.");
return s;
}
2021-07-10 19:55:53 +02:00
};
}
2021-06-11 13:30:33 +02:00
#endif