Loading...
Searching...
No Matches
Modal.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_MODAL_H
5#define MALENA_MODAL_H
6
7#pragma once
8
19#include <functional>
20#include <string>
21
22namespace ml
23{
25 {
26 public:
27 enum class Flag { VISIBLE };
28 enum class State { HIDDEN, VISIBLE, ANIMATING };
29 };
30
70 class MALENA_API Modal : public ComponentWith<ModalManifest>,
71 public ModalSettings,
72 public ModalTheme,
73 public Themeable
74 {
75 public:
78
79 private:
80 // ── Shapes ───────────────────────────────────────────────────────────
81 sf::RectangleShape _backdrop;
82 sf::RectangleShape _panel;
83 sf::RectangleShape _separator;
84 sf::RectangleShape _confirmShape;
85 sf::RectangleShape _cancelShape;
86
87 // ── Text ─────────────────────────────────────────────────────────────
88 ml::Text _titleText;
89 ml::Text _confirmText;
90 ml::Text _cancelText;
91 ml::Text _closeText;
92
93 // ── Labels ────────────────────────────────────────────────────────────
94 std::string _titleStr;
95 std::string _confirmStr;
96 std::string _cancelStr;
97
98 // ── Content ───────────────────────────────────────────────────────────
99 ml::Core* _content = nullptr;
100
101 // ── Computed bounds (updated in applyLayout) ──────────────────────────
102 sf::FloatRect _panelBounds;
103 sf::FloatRect _confirmBounds;
104 sf::FloatRect _cancelBounds;
105 sf::FloatRect _closeBounds;
106
107 // ── Animation ─────────────────────────────────────────────────────────
108 float _alpha = 0.f;
109 bool _animating = false;
110 bool _fadingIn = false;
111 bool _prevDown = false;
112
113 // ── Callbacks ─────────────────────────────────────────────────────────
114 std::function<void()> _onConfirm;
115 std::function<void()> _onDismiss;
116
117 // ── Internal ──────────────────────────────────────────────────────────
118 sf::Vector2f panelOrigin() const;
119 bool hasButtons() const;
120 float contentTop() const;
121 float contentHeight() const;
122 void applyLayout();
123 void applyAlphaToColor(sf::Color& c, float alpha) const;
124 void drawWithAlpha(sf::RenderTarget& t,
125 const sf::RenderStates& s) const;
126
127 protected:
128 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
129 void onThemeApplied(const Theme& theme) override;
130
131 public:
133
134 // ── Content ───────────────────────────────────────────────────────────
135
137 void setTitle(const std::string& title);
138
140 void setContent(ml::Core& content);
141
143 void setConfirmLabel(const std::string& label);
144
146 void setCancelLabel(const std::string& label);
147
148 // ── Control ───────────────────────────────────────────────────────────
149
150 void show();
152 void hide();
153 void toggle();
154 [[nodiscard]] bool isVisible() const;
155
156 // ── Callbacks ─────────────────────────────────────────────────────────
157
159 void onConfirm(std::function<void()> cb);
160
162 void onDismiss(std::function<void()> cb);
163
164 // ── Theming ───────────────────────────────────────────────────────────
165
166 template<typename S>
167 void applySettings(const S& s)
168 {
169 static_assert(std::is_base_of_v<ModalSettings, S>,
170 "applySettings() requires a type derived from ModalSettings");
171 static_cast<ModalSettings&>(*this) = s;
172 applyLayout();
173 }
174
175 template<typename T>
176 void applyTheme(const T& t)
177 {
178 static_assert(std::is_base_of_v<ModalTheme, T>,
179 "applyTheme() requires a type derived from ModalTheme");
180 static_cast<ModalTheme&>(*this) = t;
181 }
182
183 template<typename St>
184 void applyStyle(const St& s)
185 {
186 static_assert(std::is_base_of_v<ModalSettings, St> &&
187 std::is_base_of_v<ModalTheme, St>,
188 "applyStyle() requires ModalSettings and ModalTheme");
189 static_cast<ModalSettings&>(*this) = s;
190 static_cast<ModalTheme&>(*this) = s;
191 applyLayout();
192 }
193
194 // ── Positionable ──────────────────────────────────────────────────────
195
196 void setPosition(const sf::Vector2f& pos) override;
197 sf::Vector2f getPosition() const override;
199 };
200
205 template<typename MANIFEST>
206 class ModalWith : public Modal, public Customizable<MANIFEST>
207 { public: using Modal::Modal; };
208
209} // namespace ml
210
211#endif // MALENA_MODAL_H
Virtual base class for all Malena framework objects.
Definition Core.h:97
static const sf::Font & getDefault()
Return the built-in Arial font.
Base class for all Malena manifests.
Definition Manifest.h:51
ModalManifest::Flag Flag
Definition Modal.h:76
void setConfirmLabel(const std::string &label)
Label for the confirm button. Pass empty to hide the button bar.
sf::Vector2f getPosition() const override
bool isVisible() const
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void onThemeApplied(const Theme &theme) override
Called by ThemeManager when the active theme changes.
void hide()
Modal(const sf::Font &font=FontManager<>::getDefault())
void setCancelLabel(const std::string &label)
Label for the cancel button. Pass empty to show only the confirm button.
void setPosition(const sf::Vector2f &pos) override
void applyStyle(const St &s)
Definition Modal.h:184
sf::FloatRect getGlobalBounds() const override
void applyTheme(const T &t)
Definition Modal.h:176
void setTitle(const std::string &title)
Text displayed in the title bar. Pass an empty string to hide it.
void showImmediate()
void onConfirm(std::function< void()> cb)
Fired when the confirm button is clicked.
void applySettings(const S &s)
Definition Modal.h:167
void show()
ModalManifest::State State
Definition Modal.h:77
void onDismiss(std::function< void()> cb)
Fired when dismissed — cancel button, close button, or backdrop tap.
void toggle()
void setContent(ml::Core &content)
Component drawn in the content area. The Modal positions it.
Modal with an attached manifest.
Definition Modal.h:207
Modal(const sf::Font &font=FontManager<>::getDefault())
A framework-integrated text object with word-wrap support.
Definition Text.h:57
Component< M, Traits... > ComponentWith
Alias for Component<M, Traits...>.
Definition Component.h:316
@ HIDDEN
Component should not be drawn.
Definition Flag.h:68
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f
const sf::Font * font
Universal design token set applied across all Themeable components.
Definition Theme.h:70