Loading...
Searching...
No Matches
TextInputBase.h
Go to the documentation of this file.
1//
2// TextInputBase.h
3//
4
5#ifndef MALENA_TEXTINPUTBASE_H
6#define MALENA_TEXTINPUTBASE_H
7
8#pragma once
9
17#include <functional>
18#include <string>
19
20namespace ml
21{
22 // ── TextInputManifest ─────────────────────────────────────────────────────
23
28 class MALENA_API TextInputManifest : public ml::Manifest
29 {
30 public:
42 enum class Flag { READONLY };
43
53 enum class State { IDLE, FOCUSED, ERROR }; // disabled derived from ml::Flag::ENABLED
54 };
55
56 // ── TextInputBase ─────────────────────────────────────────────────────────
57
68 class MALENA_API TextInputBase : public ComponentWith<TextInputManifest>
69 {
70 public:
75
76 protected:
77 void onEnabledChanged(bool enabled) override; // refresh background colors
78
79 // ── Background ────────────────────────────────────────────────────────
81 sf::Vector2f _size = {200.f, 36.f};
82 sf::Vector2f _position = {0.f, 0.f};
83 float _padding = 8.f;
84
85 // ── Colors ────────────────────────────────────────────────────────────
96 float _borderThickness = 1.5f;
97
98 // ── Placeholder ───────────────────────────────────────────────────────
100 bool _showPlaceholder = true;
101
102 // ── Font ──────────────────────────────────────────────────────────────
104 unsigned int _charSize = 16;
105
106 // ── Callbacks ─────────────────────────────────────────────────────────
107 std::function<void(const std::string&)> _onChange;
108
109 // ── Internal helpers ──────────────────────────────────────────────────
112
113 public:
119
120 // ── Size ──────────────────────────────────────────────────────────────
121
126 virtual void setSize(const sf::Vector2f& size);
127
129 [[nodiscard]] sf::Vector2f getSize() const;
130
131 // ── Placeholder ───────────────────────────────────────────────────────
132
137 void setPlaceholder(const std::string& text);
138
140 [[nodiscard]] std::string getPlaceholder() const;
141
142 // ── Enabled / disabled / readonly ─────────────────────────────────────
143
144 // Enabled state is driven by Core::setEnabled (ml::Flag::ENABLED); react via
145 // the protected onEnabledChanged override — no setEnabled override needed.
146
155 void setReadOnly(bool readonly);
156
158 [[nodiscard]] bool isReadOnly() const;
159
160 // ── Error state ───────────────────────────────────────────────────────
161
170 void setError(bool error);
171
173 [[nodiscard]] bool hasError() const;
174
175 // ── Styling ───────────────────────────────────────────────────────────
176
178 void setBackgroundColor(const sf::Color& color);
179
182
184 void setBorderColor(const sf::Color& color);
185
188
190 void setBorderErrorColor(const sf::Color& color);
191
193 void setBorderThickness(float thickness);
194
196 void setTextColor(const sf::Color& color);
197
199 void setPlaceholderColor(const sf::Color& color);
200
202 void setPadding(float padding);
203
205 [[nodiscard]] float getPadding() const;
206
207 // ── Font ──────────────────────────────────────────────────────────────
208
213 virtual void setFont(const sf::Font& font);
214
216 void setFont(const sf::Font&&) = delete;
217
222 virtual void setCharacterSize(unsigned int size);
223
225 [[nodiscard]] unsigned int getCharacterSize() const;
226
227 // ── Callback ─────────────────────────────────────────────────────────
228
233 void onChange(std::function<void(const std::string&)> callback);
234
235 // ── Positionable overrides ────────────────────────────────────────────
236
237 void setPosition(const sf::Vector2f& position) override;
238 sf::Vector2f getPosition() const override;
240 };
241
242} // namespace ml
243
244#endif // MALENA_TEXTINPUTBASE_H
static const sf::Font & getDefault()
Return the built-in Arial font.
void setBorderColor(const sf::Color &color)
Set the border color in the idle state.
sf::Color _borderFocusedColor
void setBorderFocusedColor(const sf::Color &color)
Set the border color when focused.
void applyBackgroundState()
std::function< void(const std::string &)> _onChange
sf::Vector2f _position
unsigned int _charSize
bool hasError() const
Return true if the field is currently in error state.
void updatePlaceholderPosition()
unsigned int getCharacterSize() const
Return the current character size in points.
sf::Color _borderColor
sf::Vector2f getPosition() const override
virtual void setSize(const sf::Vector2f &size)
Set the width and height of the input field.
TextInputManifest::State State
Convenience alias for TextInputManifest::State.
sf::Color _borderDisabledColor
void setReadOnly(bool readonly)
Set the field to read-only mode.
sf::Color _placeholderColor
void setTextColor(const sf::Color &color)
Set the text color.
sf::Color _disabledTextColor
void setFont(const sf::Font &&)=delete
Deleted — rvalue font references are not supported.
void setBackgroundColor(const sf::Color &color)
Set the background fill color in the idle state.
float getPadding() const
Return the current padding in pixels.
void onEnabledChanged(bool enabled) override
sf::Color _borderErrorColor
sf::Vector2f _size
void setBorderErrorColor(const sf::Color &color)
Set the border color in the error state.
void setError(bool error)
Put the field into or out of the error visual state.
virtual void setFont(const sf::Font &font)
Set the font used for text and placeholder.
virtual void setCharacterSize(unsigned int size)
Set the character size in points.
void setBorderThickness(float thickness)
Set the border outline thickness in pixels.
void onChange(std::function< void(const std::string &)> callback)
Register a callback invoked whenever the text content changes.
void setPlaceholderColor(const sf::Color &color)
Set the placeholder text color.
sf::Vector2f getSize() const
Return the current size of the input field.
void setBackgroundFocusedColor(const sf::Color &color)
Set the background fill color when focused.
sf::RectangleShape _background
bool isReadOnly() const
Return true if the field is read-only.
std::string getPlaceholder() const
Return the current placeholder string.
sf::Color _bgDisabledColor
TextInputBase(const sf::Font &font=FontManager<>::getDefault())
Construct a text input base with an optional font.
void setPadding(float padding)
Set the padding between the border and the text.
sf::Color _bgFocusedColor
void setPosition(const sf::Vector2f &position) override
void setPlaceholder(const std::string &text)
Set the placeholder text shown when the field is empty.
sf::FloatRect getGlobalBounds() const override
const sf::Font * _font
TextInputManifest::Flag Flag
Convenience alias for TextInputManifest::Flag.
Shared manifest for TextInput and TextArea.
Definition TextInput.h:29
static const Color White
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:316
@ 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