1.10
theme.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_THEME_H
7#define EGT_THEME_H
8
14#include <egt/detail/enum.h>
15#include <egt/detail/meta.h>
16#include <egt/flags.h>
17#include <egt/font.h>
18#include <egt/geometry.h>
19#include <egt/palette.h>
20#include <functional>
21#include <memory>
22
23namespace egt
24{
25inline namespace v1
26{
27
28class Widget;
29class Painter;
30
37template<class T>
39{
40public:
41
45 using DrawFunction = std::function<void(T& widget, Painter& painter, const Rect& rect)>;
46
50 virtual void operator()(T& widget, Painter& painter, const Rect& rect) = 0;
51};
52
59class EGT_API DrawerReset
60{
61public:
63 using ResetFunction = std::function<void()>;
64
66 static inline void add(const ResetFunction& func)
67 {
68 m_reset_list.push_back(func);
69 }
70
72 static inline void reset()
73 {
74 for (auto& x : m_reset_list)
75 x();
76 m_reset_list.clear();
77 }
78
79private:
80
82 static std::vector<ResetFunction> m_reset_list;
83};
84
102template<class T>
104{
105public:
106
110 static void draw(typename Drawable<T>::DrawFunction d)
111 {
113 {
114 Drawer<T>::draw(T::default_draw);
115 });
116
117 m_drawable = d;
118 }
119
123 static void draw(T& widget, Painter& painter, const Rect& rect)
124 {
125 m_drawable(widget, painter, rect);
126 }
127
128private:
129 static typename Drawable<T>::DrawFunction m_drawable;
130};
131
132template<class T>
133// NOLINTNEXTLINE(cert-err58-cpp)
134typename Drawable<T>::DrawFunction Drawer<T>::m_drawable = T::default_draw;
135
136class Color;
137
145class EGT_API Theme
146{
147public:
148
156 enum class FillFlag : uint32_t
157 {
162 solid = detail::bit(0),
164 blend = detail::bit(1),
165 };
166
169
179 enum class BorderFlag : uint32_t
180 {
181 top = detail::bit(0),
182 right = detail::bit(1),
183 bottom = detail::bit(2),
184 left = detail::bit(3),
185 drop_shadow = detail::bit(4),
186 };
187
190
192
193 Theme(const Theme&) = default;
194 Theme& operator=(const Theme&) = default;
195 Theme(Theme&&) noexcept = default;
196 Theme& operator=(Theme&&) noexcept = default;
197
199 EGT_NODISCARD const std::string& name() const { return m_name; }
200
209 void name(const std::string& name) { m_name = name; }
210
215 {
216 return m_palette;
217 }
218
222 const Palette& palette() const
223 {
224 return m_palette;
225 }
226
230 void palette(const Palette& palette)
231 {
232 m_palette = palette;
233 }
234
239 {
240 return m_font;
241 }
242
246 const Font& font() const
247 {
248 return m_font;
249 }
250
254 void font(const Font& font)
255 {
256 m_font = font;
257 }
258
262 virtual void draw_box(Painter& painter,
263 const Widget& widget,
265 Palette::ColorId border) const;
266
270 virtual void draw_box(Painter& painter,
271 const FillFlags& type,
272 const Rect& rect,
273 const Pattern& border,
274 const Pattern& bg,
275 DefaultDim border_width = 0,
276 DefaultDim margin_width = 0,
277 float border_radius = 0.0,
278 const BorderFlags& border_flags = {},
279 Image* background = nullptr) const;
280
284 virtual void draw_circle(Painter& painter,
285 const Widget& widget,
287 Palette::ColorId border) const;
288
292 virtual void draw_circle(Painter& painter,
293 const FillFlags& type,
294 const Rect& rect,
295 const Pattern& border,
296 const Pattern& bg,
297 DefaultDim border_width = 0,
298 DefaultDim margin_width = 0) const;
299
302 {
303 return 2;
304 }
305
307 virtual float default_border_radius() const
308 {
309 return 4.0;
310 }
311
317 virtual void apply()
318 {
319 init_palette();
320 init_font();
321 init_draw();
322 }
323
324 virtual ~Theme() noexcept = default;
325
326protected:
327
328 explicit Theme(const std::string& name);
329
330 inline void rounded_box(Painter& painter, const Rect& box, float border_radius) const
331 {
332 rounded_box(painter, RectF(box.x(), box.y(), box.width(), box.height()), border_radius);
333 }
334
335 virtual void rounded_box(Painter& painter, const RectF& box, float border_radius) const;
336
339
341 std::string m_name;
342
345
351 virtual void init_palette();
352
358 virtual void init_font();
359
365 virtual void init_draw();
366};
367
370{
371 return {lhs, rhs};
372}
373
375template<>
376EGT_API const std::pair<Theme::FillFlag, char const*> detail::EnumStrings<Theme::FillFlag>::data[2];
377
379template<>
380EGT_API const std::pair<Theme::BorderFlag, char const*> detail::EnumStrings<Theme::BorderFlag>::data[5];
381
393
401EGT_API void global_theme(std::unique_ptr<Theme>&& theme);
402
403}
404}
405
406#endif
32 bit RGBA color.
Definition color.h:41
Drawable function object.
Definition theme.h:39
std::function< void(T &widget, Painter &painter, const Rect &rect)> DrawFunction
Definition of the draw function.
Definition theme.h:45
virtual void operator()(T &widget, Painter &painter, const Rect &rect)=0
Implementation of the actual draw function.
Keeps track of what's been changed with Drawer::draw() changes.
Definition theme.h:60
static void add(const ResetFunction &func)
Add a custom reset function.
Definition theme.h:66
std::function< void()> ResetFunction
Reset function type.
Definition theme.h:63
static void reset()
Execute reset functions.
Definition theme.h:72
Manager of the Drawable for each widget type.
Definition theme.h:104
static void draw(T &widget, Painter &painter, const Rect &rect)
Call the Drawable.
Definition theme.h:123
static void draw(typename Drawable< T >::DrawFunction d)
Set the default Drawable for all widgets of type T.
Definition theme.h:110
Manages a font and properties of a font.
Definition font.h:35
Raster image resource used for drawing or displaying.
Definition image.h:39
Drawing interface for 2D graphics.
Definition painter.h:45
Color palette that contains a 2 dimensional array of colors.
Definition palette.h:40
ColorId
The Pattern identifier in the Palette.
Definition palette.h:239
A Pattern which can store one or more colors at different offsets (steps) which can be used to create...
Definition pattern.h:55
Customizable characteristics for drawing widgets.
Definition theme.h:146
virtual void draw_box(Painter &painter, const FillFlags &type, const Rect &rect, const Pattern &border, const Pattern &bg, DefaultDim border_width=0, DefaultDim margin_width=0, float border_radius=0.0, const BorderFlags &border_flags={}, Image *background=nullptr) const
Draw a box specifying the properties directly.
Flags< BorderFlag > BorderFlags
Border flags.
Definition theme.h:189
void font(const Font &font)
Set the theme Font.
Definition theme.h:254
virtual void draw_circle(Painter &painter, const Widget &widget, Palette::ColorId bg, Palette::ColorId border) const
Draw a circle using properties directly from the widget.
Theme(const Theme &)=default
virtual ~Theme() noexcept=default
const Palette & palette() const
Get a const reference to the theme Palette.
Definition theme.h:222
Palette & palette()
Get a reference to the theme Palette.
Definition theme.h:214
virtual void apply()
Apply the Theme.
Definition theme.h:317
virtual void init_draw()
Setup for initializing drawing.
void palette(const Palette &palette)
Set the theme palette.
Definition theme.h:230
virtual void draw_box(Painter &painter, const Widget &widget, Palette::ColorId bg, Palette::ColorId border) const
Draw a box using properties directly from the widget.
const Font & font() const
Get a const reference to the theme Font.
Definition theme.h:246
Palette m_palette
Palette instance used by the theme.
Definition theme.h:338
FillFlag
Fill flags are used to characterize how a widget's background and border should be drawn.
Definition theme.h:157
virtual void init_palette()
Setup for initializing the palette.
BorderFlag
Border flags allow, when drawing a rectangle, control over what sides of the rectangle are drawn.
Definition theme.h:180
Theme(Theme &&) noexcept=default
virtual float default_border_radius() const
Get the default border radius.
Definition theme.h:307
Font m_font
Default font instance used by the theme.
Definition theme.h:344
Font & font()
Get a reference to the theme Font.
Definition theme.h:238
void name(const std::string &name)
Set the name of the Object.
Definition theme.h:209
virtual DefaultDim default_border() const
Get the default border width.
Definition theme.h:301
virtual void rounded_box(Painter &painter, const RectF &box, float border_radius) const
virtual void draw_circle(Painter &painter, const FillFlags &type, const Rect &rect, const Pattern &border, const Pattern &bg, DefaultDim border_width=0, DefaultDim margin_width=0) const
Draw a circle specifying the properties directly.
std::string m_name
A user defined name for the Object.
Definition theme.h:341
virtual void init_font()
Setup for initializing the font.
Theme & operator=(const Theme &)=default
Base Widget class.
Definition widget.h:53
T & top(T &widget)
Helper to set alignment of a widget.
Definition widgetflags.h:494
T & right(T &widget)
Helper to set alignment of a widget.
Definition widgetflags.h:468
T & left(T &widget)
Helper to set alignment of a widget.
Definition widgetflags.h:442
EGT_API Theme & global_theme()
Get the global theme.
T & bottom(T &widget)
Helper to set alignment of a widget.
Definition widgetflags.h:520
Theme::BorderFlags operator|(Theme::BorderFlag lhs, Theme::BorderFlag rhs)
BorderFlags operator.
Definition theme.h:369
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24
When using enum_to_string() and enum_from_string(), this type needs to be defined and specialized to ...
Definition enum.h:48