Loading...
Searching...
No Matches
TextArea.h
Go to the documentation of this file.
1//
2// TextArea.h
3//
4
5#ifndef MALENA_TEXTAREA_H
6#define MALENA_TEXTAREA_H
7
8#pragma once
9
14#include <type_traits>
15
16namespace ml
17{
35 public TextAreaTheme
36 {
37 private:
38 ScrollPane _scrollPane;
39
40 void handleTextAreaKeypress(const sf::Event::KeyPressed& kp);
41 void drawScrollbar(sf::RenderTarget& target, const sf::RenderStates& states) const;
42 void scrollToCursor();
43 void rebuildAndScroll() override;
44 std::size_t hitTest(const sf::Vector2f& worldPos) const override;
45
46 // ── Scroll / drag state ───────────────────────────────────────────
47 float _prevScrollY = 0.f;
48 bool _thumbDragging = false;
49 float _thumbDragStartMouseY = 0.f;
50 float _thumbDragStartScroll = 0.f;
51 bool _prevMouseDown = false;
52
53 protected:
54 // ── TextInput hooks ───────────────────────────────────────────────────
55
60 void onRebuildComplete() override;
61
65 void reflow() override;
66
67 void syncPlaceholder() override;
68
69 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
70 void onThemeApplied(const Theme& theme) override;
71
72 public:
74
75 // ── Apply ─────────────────────────────────────────────────────────────
76
77 template<typename T>
78 void applyTheme(const T& t)
79 {
80 static_assert(std::is_base_of_v<TextAreaTheme, T>,
81 "applyTheme() requires a type derived from TextAreaTheme");
82 static_cast<TextAreaTheme&>(*this) = t;
83 _scrollPane.setScrollBarColor(scrollBarColor);
84 _scrollPane.setBackgroundColor(scrollBarTrackColor);
85 _scrollPane.setScrollBarWidth(scrollBarWidth);
86 syncColors();
87 }
88
89 template<typename St>
90 void applyStyle(const St& s)
91 {
92 static_assert(std::is_base_of_v<TextInputSettings, St> &&
93 std::is_base_of_v<TextAreaTheme, St>,
94 "applyStyle() requires TextInputSettings and TextAreaTheme");
95 static_cast<TextInputSettings&>(*this) = s;
96 static_cast<TextAreaTheme&>(*this) = s;
98 _scrollPane.setBackgroundColor(scrollBarTrackColor);
99 _scrollPane.setScrollBarWidth(scrollBarWidth);
100 syncColors();
102 rebuild();
103 }
104
105 // ── Scrollbar styling ─────────────────────────────────────────────────
106
107 void setScrollBarColor(const sf::Color& color);
109 void setScrollBarWidth(float width);
110
111 // ── Overrides ─────────────────────────────────────────────────────────
112
113 void setSize(const sf::Vector2f& size) override;
114 void setPosition(const sf::Vector2f& position) override;
116 };
117
118 template<typename MANIFEST>
119 class TextAreaWith : public TextArea, public Customizable<MANIFEST>
120 {
121 public:
122 using TextArea::TextArea;
123 };
124
125} // namespace ml
126
127#endif // MALENA_TEXTAREA_H
static const sf::Font & getDefault()
Return the built-in Arial font.
void applyTheme(const T &t)
Definition TextArea.h:78
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void setScrollBarColor(const sf::Color &color)
void syncPlaceholder() override
void onRebuildComplete() override
Called after every rebuild(). Grows the canvas to full content height and updates ScrollPane's conten...
void applyStyle(const St &s)
Definition TextArea.h:90
TextArea(const sf::Font &font=FontManager<>::getDefault())
void setPosition(const sf::Vector2f &position) override
void setSize(const sf::Vector2f &size) override
void setScrollBarWidth(float width)
void reflow() override
Top-aligned reflow. Reads vertical scroll from ScrollPane.
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
sf::FloatRect getGlobalBounds() const override
void setScrollBarTrackColor(const sf::Color &color)
TextArea(const sf::Font &font=FontManager<>::getDefault())
void syncColors()
TextInput(const sf::Font &font=FontManager<>::getDefault())
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
sf::Vector2f size
Scrollbar color tokens for TextArea.
void setScrollBarColor(const sf::Color &c)
sf::Color scrollBarColor
sf::Color scrollBarTrackColor
Layout and behaviour settings for TextInput and TextArea.
Universal design token set applied across all Themeable components.
Definition Theme.h:70