Loading...
Searching...
No Matches
TextInput.h
Go to the documentation of this file.
1//
2// TextInput.h
3//
4
5#ifndef MALENA_TEXTINPUT_H
6#define MALENA_TEXTINPUT_H
7
8#pragma once
9
21#include <SFML/Graphics.hpp>
22#include <functional>
23#include <string>
24#include <type_traits>
25
26namespace ml
27{
29 {
30 public:
31 enum class Flag { DISABLED, READONLY };
32 enum class State { IDLE, FOCUSED, DISABLED, ERROR };
33 };
34
44 class MALENA_API TextInput : public ComponentWith<TextInputManifest>,
45 public TextInputSettings,
46 public TextInputTheme,
47 public Themeable
48 {
49 public:
52
53 protected:
57
59 sf::Vector2f _position = {0.f, 0.f};
60 float _scrollX = 0.f;
61
63 bool _showPlaceholder = true;
64
66 mutable bool _cursorVisible = false;
67
68 bool _dragging = false;
69 bool _prevMouseDown = false;
70 std::size_t _dragAnchor = 0;
73 bool _waitingDouble = false;
74
75 std::function<void(const std::string&)> _onChange;
76 std::function<void(const std::string&)> _onSubmit;
77
78 void rebuild();
79 virtual void onRebuildComplete() {}
80 virtual void reflow();
81 virtual void rebuildAndScroll();
82 void syncColors();
83 virtual void syncPlaceholder();
84 virtual std::size_t hitTest(const sf::Vector2f& worldPos) const;
85
86 virtual void handleKey(const sf::Event::KeyPressed& kp);
88
89 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
90 void onThemeApplied(const Theme& theme) override;
91
92 public:
94
95 // ── Apply ─────────────────────────────────────────────────────────────
96
97 template<typename S>
98 void applySettings(const S& s)
99 {
100 static_assert(std::is_base_of_v<TextInputSettings, S>,
101 "applySettings() requires a type derived from TextInputSettings");
102 static_cast<TextInputSettings&>(*this) = s;
103 syncColors();
105 rebuild();
106 }
107
108 template<typename T>
109 void applyTheme(const T& t)
110 {
111 static_assert(std::is_base_of_v<TextInputTheme, T>,
112 "applyTheme() requires a type derived from TextInputTheme");
113 static_cast<TextInputTheme&>(*this) = t;
114 syncColors();
115 }
116
117 template<typename St>
118 void applyStyle(const St& s)
119 {
120 static_assert(std::is_base_of_v<TextInputSettings, St> &&
121 std::is_base_of_v<TextInputTheme, St>,
122 "applyStyle() requires TextInputSettings and TextInputTheme");
123 static_cast<TextInputSettings&>(*this) = s;
124 static_cast<TextInputTheme&>(*this) = s;
125 syncColors();
127 rebuild();
128 }
129
130 // ── Value ─────────────────────────────────────────────────────────────
131
132 void setValue(const std::string& value);
133 [[nodiscard]] std::string getValue() const;
134 void clear();
135
136 // ── Selection styling ─────────────────────────────────────────────────
137
139 void setSelectionCharSize(unsigned int size);
140 void setSelectionColor(const sf::Color& color);
141 void setSelectionBold(bool bold);
142 void setSelectionItalic(bool italic);
143 void setSelectionUnderline(bool underline);
144
145 void selectAll();
146 void setSelection(std::size_t start, std::size_t end);
147 [[nodiscard]] std::string getSelectedText() const;
148
149 // ── Options ───────────────────────────────────────────────────────────
150
151 void setPlaceholder(const std::string& text);
152 [[nodiscard]] std::string getPlaceholder() const;
153
154 void setEnabled(bool enabled);
155 [[nodiscard]] bool isEnabled() const;
156 void setReadOnly(bool readonly);
157 [[nodiscard]] bool isReadOnly() const;
158
159 void setError(bool error);
160 [[nodiscard]] bool hasError() const;
161
162 // ── Size / font ───────────────────────────────────────────────────────
163
164 virtual void setSize(const sf::Vector2f& size);
165 [[nodiscard]] sf::Vector2f getSize() const;
166
167 virtual void setFont(const sf::Font& font);
168 void setFont(const sf::Font&&) = delete;
169 virtual void setCharacterSize(unsigned int size);
170 [[nodiscard]] unsigned int getCharacterSize() const;
171
172 // ── Callbacks ─────────────────────────────────────────────────────────
173
174 void onChange(std::function<void(const std::string&)> callback);
175 void onSubmit(std::function<void(const std::string&)> callback);
176
177 // ── Positionable ──────────────────────────────────────────────────────
178
179 virtual void setPosition(const sf::Vector2f& position) override;
180 sf::Vector2f getPosition() const override;
182 };
183
184 template<typename MANIFEST>
185 class TextInputWith : public TextInput, public Customizable<MANIFEST>
186 { public: using TextInput::TextInput; };
187
188} // namespace ml
189
190#endif // MALENA_TEXTINPUT_H
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
Pure data layer for rich text editing.
Rendering layer for RichTextBuffer.
void setSelectionFont(const sf::Font &font)
std::size_t _dragAnchor
Definition TextInput.h:70
std::string getValue() const
void applyStyle(const St &s)
Definition TextInput.h:118
virtual void onRebuildComplete()
Definition TextInput.h:79
bool isReadOnly() const
void setError(bool error)
void setSelection(std::size_t start, std::size_t end)
virtual void reflow()
void setSelectionBold(bool bold)
void selectAll()
RichTextBuffer _buffer
Definition TextInput.h:54
void handleChar(const sf::Event::TextEntered &te)
bool _waitingDouble
Definition TextInput.h:73
std::function< void(const std::string &)> _onSubmit
Definition TextInput.h:76
bool isEnabled() const
bool _showPlaceholder
Definition TextInput.h:63
void setReadOnly(bool readonly)
void setSelectionItalic(bool italic)
bool _prevMouseDown
Definition TextInput.h:69
sf::Vector2f getSize() const
void onSubmit(std::function< void(const std::string &)> callback)
void setValue(const std::string &value)
void applyTheme(const T &t)
Definition TextInput.h:109
sf::Vector2f _position
Definition TextInput.h:59
bool hasError() const
virtual void rebuildAndScroll()
RichTextRenderer _renderer
Definition TextInput.h:55
void onChange(std::function< void(const std::string &)> callback)
void setSelectionColor(const sf::Color &color)
virtual void setFont(const sf::Font &font)
std::string getPlaceholder() const
void syncColors()
std::function< void(const std::string &)> _onChange
Definition TextInput.h:75
virtual void handleKey(const sf::Event::KeyPressed &kp)
bool _cursorVisible
Definition TextInput.h:66
sf::Vector2f getPosition() const override
virtual void setSize(const sf::Vector2f &size)
void setFont(const sf::Font &&)=delete
sf::RectangleShape _background
Definition TextInput.h:58
TextInputManifest::State State
Definition TextInput.h:51
void setPlaceholder(const std::string &text)
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
TextInputManifest::Flag Flag
Definition TextInput.h:50
sf::RenderTexture _canvas
Definition TextInput.h:56
virtual void setPosition(const sf::Vector2f &position) override
sf::Vector2f _lastClick
Definition TextInput.h:72
sf::Clock _cursorClock
Definition TextInput.h:65
TextInput(const sf::Font &font=FontManager<>::getDefault())
virtual void syncPlaceholder()
float _scrollX
Definition TextInput.h:60
sf::Text _placeholder
Definition TextInput.h:62
std::string getSelectedText() const
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
unsigned int getCharacterSize() const
void setSelectionCharSize(unsigned int size)
void applySettings(const S &s)
Definition TextInput.h:98
virtual void setCharacterSize(unsigned int size)
void setEnabled(bool enabled)
sf::FloatRect getGlobalBounds() const override
sf::Clock _clickClock
Definition TextInput.h:71
virtual std::size_t hitTest(const sf::Vector2f &worldPos) const
void setSelectionUnderline(bool underline)
Shared manifest for TextInput and TextArea.
Definition TextInput.h:29
TextInput(const sf::Font &font=FontManager<>::getDefault())
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:299
@ FOCUSED
Component has keyboard focus (set by ClickableDispatcher).
Definition Flag.h:70
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
sf::Vector2f size
Layout and behaviour settings for TextInput and TextArea.
Color tokens for TextInput and TextArea.
Universal design token set applied across all Themeable components.
Definition Theme.h:70