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

230 lines
7.9 KiB
C++
Raw Normal View History

2021-08-06 14:22:56 +02:00
#ifndef BWIDGETS_INPUT_HPP
#define BWIDGETS_INPUT_HPP
2021-06-11 13:30:33 +02:00
2021-08-06 14:22:56 +02:00
#include <cmath>
#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>
2021-08-17 11:00:18 +02:00
#include <basic_widgets/w/caption_impl.hpp>
2021-08-15 20:04:53 +02:00
#include <basic_widgets/w/feat/keyboard_handler_impl.hpp>
#include <basic_widgets/w/feat/mouse_handler_impl.hpp>
2021-06-16 13:50:34 +02:00
2021-08-06 14:22:56 +02:00
namespace bwidgets
2021-06-11 13:30:33 +02:00
{
2021-07-10 19:55:53 +02:00
template<typename T>
2021-08-17 11:00:18 +02:00
class Input : public virtual WidgetImpl,
public FontHandler,
2021-08-15 20:04:53 +02:00
public KeyboardHandlerImpl,
public MouseHandlerImpl
2021-07-10 19:55:53 +02:00
{
protected:
2021-08-17 11:00:18 +02:00
CaptionImpl _input_caption;
Input(Widget* parent = nullptr) : WidgetImpl {parent}, _input_caption {this}
{
2021-08-15 20:04:53 +02:00
FocusHandlerImpl::focus_handler([this](bool focus) {
if (focus) {
SDL_StartTextInput();
}
// focus loss.
else if (FocusHandlerImpl::focus()) {
value = value_from_string(input_text());
input_text(value_to_string(value));
SDL_StopTextInput();
_input_caption.text(value_to_string(value));
}
});
KeyboardHandlerImpl::key_handler([this](const SDL_KeyboardEvent& key) {
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;
}
case SDLK_RETURN:
case SDLK_RETURN2: // what is return2 btw?
case SDLK_KP_ENTER:
value = value_from_string(input_text());
FocusHandlerImpl::focus(false);
break;
}
}
});
KeyboardHandlerImpl::text_input_handler(
[this](const SDL_TextInputEvent& input) {
if (is_valid_input(input.text)) {
input_text(input_text() + input.text);
}
});
KeyboardHandlerImpl::enable_keyboard_handler();
MouseHandlerImpl::enable_mouse_handler(&(Input::_widget_area),
&(Input::_viewport));
}
2021-08-10 23:48:19 +02:00
void _handle_font_change(const std::shared_ptr<Font>& f) override
{
_input_caption.font(f);
_handle_geometry_change(_viewport);
}
2021-08-14 16:37:02 +02:00
void _handle_font_color_change(Color fg, Color bg) override
{
2021-08-06 14:22:56 +02:00
_input_caption.font_color_bg(bg)->font_color_fg(fg);
}
2021-08-08 16:00:22 +02:00
void _handle_geometry_change(const SDL_Rect& vp) override
{
2021-08-06 14:22:56 +02:00
const auto input_h = _input_caption.size().h + 2 * border_width;
_widget_area = {0, center_line(vp.h, input_h), vp.w, input_h};
2021-08-06 14:22:56 +02:00
_input_caption.viewport(rect_offset(
{rect_margin(_widget_area, {border_width, border_width})}, vp));
}
2021-08-10 23:48:19 +02:00
void _handle_renderer_change(const std::shared_ptr<Renderer>& r) override
{
_input_caption.renderer(r);
}
void _handle_rendering() override
{
2021-08-06 14:22:56 +02:00
for (int i = border_width - 1; i >= 0; i--) {
2021-08-15 20:04:53 +02:00
const auto factor = linear(i, 0, border_width);
const auto& color_end =
FocusHandlerImpl::focus() ? color_bg_focused : color_bg;
const auto color_start = color_end / 2;
2021-08-06 14:22:56 +02:00
_renderer->draw_color(lerp(color_start, color_end, factor))
->draw_rect(rect_margin(_widget_area, {i, i}));
}
2021-08-15 20:04:53 +02:00
if (hovered() || FocusHandlerImpl::focus())
2021-08-06 14:22:56 +02:00
_input_caption.font_color_bg(color_bg_focused);
else _input_caption.font_color_bg(color_bg);
_input_caption.render();
}
public:
2021-08-08 14:30:56 +02:00
static const int default_border_width = 3;
2021-08-06 14:22:56 +02:00
inline static const Color default_color_bg = {200, 200, 200, SDL_ALPHA_OPAQUE};
inline static const Color default_color_bg_focused = {255, 255, 255,
SDL_ALPHA_OPAQUE};
2021-08-08 14:30:56 +02:00
static const int default_float_precision = 2;
static const int default_min_width = 1;
2021-08-06 14:22:56 +02:00
2021-08-08 14:30:56 +02:00
int border_width = default_border_width;
2021-08-06 14:22:56 +02:00
Color color_bg = default_color_bg;
Color color_bg_focused = default_color_bg_focused;
2021-08-08 14:30:56 +02:00
int float_precision = default_float_precision;
int input_min_width = default_min_width;
2021-08-06 14:22:56 +02:00
char input_width_unit = 'W';
T value {};
Input(const Input&) = delete;
2021-08-14 09:25:28 +02:00
Input(Input&&) = delete;
~Input() override = default;
auto operator=(const Input&) = delete;
auto operator=(Input&&) = delete;
2021-08-14 16:37:02 +02:00
virtual auto color_fg(Color c) -> Input<T>*
2021-08-06 14:22:56 +02:00
{
_input_caption.font_color_fg(c);
return this;
}
2021-08-08 16:00:22 +02:00
[[nodiscard]] virtual auto input_text() const -> const std::string&
{
return _input_caption.text();
}
2021-08-08 16:00:22 +02:00
virtual auto input_text(const std::string& txt) -> Input<T>*
{
_input_caption.text(txt);
return this;
}
2021-08-06 14:22:56 +02:00
[[nodiscard]] virtual auto is_valid_input(const std::string&) const noexcept
-> bool
{
return true;
}
2021-08-06 14:22:56 +02:00
[[nodiscard]] virtual auto process_value(T x) const noexcept -> T
{
return x;
}
[[nodiscard]] auto size() const noexcept -> Size override
{
2021-08-06 14:22:56 +02:00
if (_font == nullptr) return {0, 0};
return {
_font->text_size(std::string(input_min_width, input_width_unit)).w
2021-08-06 14:22:56 +02:00
+ 2 * border_width,
_font->line_skip + 4 * border_width // _why_ 4 and not 2?…
};
}
2021-08-08 16:00:22 +02:00
[[nodiscard]] auto value_from_string(std::string s) noexcept
{
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
2021-08-06 14:22:56 +02:00
// NOLINTNEXTLINE(readability-simplify-boolean-expr)
static_assert((bool)sizeof(T) && false,
2021-08-14 16:37:02 +02:00
"string cannot be converted to type T.");
return process_value(v);
}
2021-08-08 16:00:22 +02:00
[[nodiscard]] auto value_to_string(T value) noexcept
{
std::string s;
if constexpr (std::is_same_v<std::string,
T> || std::convertible_to<T, std::string>)
s = std::move(value);
2021-08-06 14:22:56 +02:00
else if constexpr (CanToString<T>) {
std::stringstream ss;
ss << std::fixed << std::setprecision(float_precision) << value;
s = std::move(ss).str();
}
2021-08-06 14:22:56 +02:00
else if constexpr (Printable<T>) {
std::stringstream ss;
ss << value;
s = std::move(ss).str();
}
else
2021-08-06 14:22:56 +02:00
// NOLINTNEXTLINE(readability-simplify-boolean-expr)
static_assert((bool)sizeof(T) && false,
"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