ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
input_box.h
Go to the documentation of this file.
1
8
9#ifndef ASW_MODULES_UI_INPUT_BOX_H
10#define ASW_MODULES_UI_INPUT_BOX_H
11
12#include <functional>
13#include <optional>
14#include <string>
15
16#include "../types.h"
17#include "context.h"
18#include "widget.h"
19
20namespace asw::ui {
21
24class InputBox : public Widget {
25public:
29 {
30 focusable = true;
31 }
32
35 ~InputBox() override;
36
37 InputBox(const InputBox&) = delete;
38 InputBox& operator=(const InputBox&) = delete;
39
41 std::function<void(const std::string&)> on_change;
42
48 void on_focus_changed(Context& ctx, bool focused) override;
49
56 bool on_event(Context& ctx, const UIEvent& e) override;
57
62 void draw(Context& ctx) override;
63
66
68 std::string value;
69
71 std::string placeholder;
72
74 std::optional<InputStyle> style;
75
81 const InputStyle& get_style(const Context& ctx) const
82 {
83 return style ? *style : ctx.theme.input;
84 }
85
86private:
87 std::size_t _cursor_pos = 0;
88
90 float _scroll = 0.0F;
91};
92
93} // namespace asw::ui
94
95#endif // ASW_MODULES_UI_INPUT_BOX_H
Shared state for the UI system.
Definition context.h:97
Theme theme
The current UI theme.
Definition context.h:104
A text input widget.
Definition input_box.h:24
asw::Font font
The font for the text. Uses the theme font when empty.
Definition input_box.h:65
InputBox & operator=(const InputBox &)=delete
std::optional< InputStyle > style
Style for this input box only. Uses the theme input style when empty.
Definition input_box.h:74
float _scroll
How far the text is scrolled left, so the caret stays in view.
Definition input_box.h:90
void draw(Context &ctx) override
Draw the input box.
std::size_t _cursor_pos
Definition input_box.h:87
void on_focus_changed(Context &ctx, bool focused) override
Called when focus state changes.
Definition input_box.cpp:51
InputBox()
Default constructor.
Definition input_box.h:28
std::function< void(const std::string &)> on_change
Callback invoked when the value changes.
Definition input_box.h:41
std::string placeholder
Placeholder text shown when value is empty.
Definition input_box.h:71
~InputBox() override
Stops text input if the box is destroyed while focused.
Definition input_box.cpp:44
InputBox(const InputBox &)=delete
bool on_event(Context &ctx, const UIEvent &e) override
Handle a UI event.
Definition input_box.cpp:63
std::string value
The current text value.
Definition input_box.h:68
const InputStyle & get_style(const Context &ctx) const
Get the style this input box draws with.
Definition input_box.h:81
Base class for all UI widgets.
Definition widget.h:43
bool focusable
Whether the widget can receive focus. Focusable widgets are also the ones the pointer can press.
Definition widget.h:88
UI context and focus management for the ASW UI module.
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:79
How an input box looks. Set on Theme::input for every input box, or on InputBox::style for one input ...
Definition theme.h:116
InputStyle input
Default input box style.
Definition theme.h:270
Event structure for UI interactions.
Definition event.h:21
Types used throughout the ASW library.
Base widget class for the ASW UI module.