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:
39 enum class Flag { DISABLED, READONLY };
40
51 enum class State { IDLE, FOCUSED, DISABLED, ERROR };
52 };
53
54 // ── TextInputBase ─────────────────────────────────────────────────────────
55
66 class MALENA_API TextInputBase : public ComponentWith<TextInputManifest>
67 {
68 public:
73
74 protected:
75 // ── Background ────────────────────────────────────────────────────────
77 sf::Vector2f _size = {200.f, 36.f};
78 sf::Vector2f _position = {0.f, 0.f};
79 float _padding = 8.f;
80
81 // ── Colors ────────────────────────────────────────────────────────────
92 float _borderThickness = 1.5f;
93
94 // ── Placeholder ───────────────────────────────────────────────────────
96 bool _showPlaceholder = true;
97
98 // ── Font ──────────────────────────────────────────────────────────────
100 unsigned int _charSize = 16;
101
102 // ── Callbacks ─────────────────────────────────────────────────────────
103 std::function<void(const std::string&)> _onChange;
104
105 // ── Internal helpers ──────────────────────────────────────────────────
108
109 public:
115
116 // ── Size ──────────────────────────────────────────────────────────────
117
122 virtual void setSize(const sf::Vector2f& size);
123
125 [[nodiscard]] sf::Vector2f getSize() const;
126
127 // ── Placeholder ───────────────────────────────────────────────────────
128
133 void setPlaceholder(const std::string& text);
134
136 [[nodiscard]] std::string getPlaceholder() const;
137
138 // ── Enabled / disabled / readonly ─────────────────────────────────────
139
144 void setEnabled(bool enabled);
145
147 [[nodiscard]] bool isEnabled() const;
148
157 void setReadOnly(bool readonly);
158
160 [[nodiscard]] bool isReadOnly() const;
161
162 // ── Error state ───────────────────────────────────────────────────────
163
172 void setError(bool error);
173
175 [[nodiscard]] bool hasError() const;
176
177 // ── Styling ───────────────────────────────────────────────────────────
178
180 void setBackgroundColor(const sf::Color& color);
181
184
186 void setBorderColor(const sf::Color& color);
187
190
192 void setBorderErrorColor(const sf::Color& color);
193
195 void setBorderThickness(float thickness);
196
198 void setTextColor(const sf::Color& color);
199
201 void setPlaceholderColor(const sf::Color& color);
202
204 void setPadding(float padding);
205
207 [[nodiscard]] float getPadding() const;
208
209 // ── Font ──────────────────────────────────────────────────────────────
210
215 virtual void setFont(const sf::Font& font);
216
218 void setFont(const sf::Font&&) = delete;
219
224 virtual void setCharacterSize(unsigned int size);
225
227 [[nodiscard]] unsigned int getCharacterSize() const;
228
229 // ── Callback ─────────────────────────────────────────────────────────
230
235 void onChange(std::function<void(const std::string&)> callback);
236
237 // ── Positionable overrides ────────────────────────────────────────────
238
239 void setPosition(const sf::Vector2f& position) override;
240 sf::Vector2f getPosition() const override;
242 };
243
244} // namespace ml
245
246#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 setEnabled(bool enabled)
Enable or disable the input field.
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.
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.
bool isEnabled() const
Return true if the field is currently enabled.
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: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