cgv
Loading...
Searching...
No Matches
theme_info.h
1#pragma once
2
3#include <cgv/media/color.h>
4#include <cgv/signal/signal.h>
5
6#include "lib_begin.h"
7
8namespace cgv {
9namespace gui {
10
11#define DEF_COLOR_MEMBER_METHODS(FIELD) \
12void FIELD(unsigned char r, unsigned char g, unsigned char b) { \
13 FIELD##_col = rgb( \
14 static_cast<float>(r) / 255.0f, \
15 static_cast<float>(g) / 255.0f, \
16 static_cast<float>(b) / 255.0f \
17 ); \
18} \
19rgb FIELD() const { \
20 return FIELD##_col; \
21}\
22std::string FIELD##_hex() const { \
23 return rgb_to_hex(FIELD##_col); \
24}
25
26class CGV_API theme_info {
27protected:
28 int theme_idx;
29
30 int spacing_ = 1;
31
32 rgb background_col;
33 rgb group_col;
34 rgb control_col;
35 rgb border_col;
36 rgb text_col;
37 rgb text_background_col;
38 rgb selection_col;
39 rgb highlight_col;
40 rgb warning_col;
41 rgb shadow_col;
42
43 static std::string char_to_hex(unsigned char c) {
44 static const char hex_chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
45 std::string s = "00";
46 s[0] = hex_chars[(c & 0xF0) >> 4];
47 s[1] = hex_chars[(c & 0x0F) >> 0];
48 return s;
49 }
50
51 static std::string rgb_to_hex(const rgb& c) {
52 unsigned char r = static_cast<unsigned char>(255.0f * c.R());
53 unsigned char g = static_cast<unsigned char>(255.0f * c.G());
54 unsigned char b = static_cast<unsigned char>(255.0f * c.B());
55 std::string s = "0x" + char_to_hex(r) + char_to_hex(g) + char_to_hex(b);
56 return s;
57 }
58
59public:
60 static theme_info& instance();
61
62 theme_info(const theme_info&) = delete;
63 void operator=(const theme_info&) = delete;
64
66 theme_info();
69
70 void set_index(int idx);
71 int get_index() const;
72 bool is_dark() const;
73
74 int spacing() const { return spacing_; }
75 void spacing(int i) { spacing_ = i; }
76
77 DEF_COLOR_MEMBER_METHODS(background)
78 DEF_COLOR_MEMBER_METHODS(group)
79 DEF_COLOR_MEMBER_METHODS(control)
80 DEF_COLOR_MEMBER_METHODS(border)
81 DEF_COLOR_MEMBER_METHODS(text)
82 DEF_COLOR_MEMBER_METHODS(text_background)
83 DEF_COLOR_MEMBER_METHODS(selection)
84 DEF_COLOR_MEMBER_METHODS(highlight)
85 DEF_COLOR_MEMBER_METHODS(warning)
86 DEF_COLOR_MEMBER_METHODS(shadow)
87
88 cgv::signal::signal<const theme_info&> on_change;
89};
90
91#undef DEF_COLOR_MEMBER_METHODS
92
93class CGV_API theme_observer : virtual public cgv::signal::tacker {
94public:
96 cgv::gui::theme_info& theme = cgv::gui::theme_info::instance();
97 cgv::signal::connect(theme.on_change, this, &theme_observer::handle_theme_change);
98 }
99
101 cgv::gui::theme_info& theme = cgv::gui::theme_info::instance();
102 cgv::signal::disconnect(theme.on_change, this, &theme_observer::handle_theme_change);
103 }
104
106 virtual void handle_theme_change(const cgv::gui::theme_info& theme) {}
107};
108
109}
110}
111
112#include <cgv/config/lib_end.h>
~theme_info()
destruct
Definition theme_info.h:68
virtual void handle_theme_change(const cgv::gui::theme_info &theme)
override in your class to handle theme changes
Definition theme_info.h:106
the cgv namespace
Definition print.h:11