]> git.tdb.fi Git - poefilter.git/blob - source/appearance.cpp
Add inheritance for appearances
[poefilter.git] / source / appearance.cpp
1 #include <msp/strings/format.h>
2 #include "appearance.h"
3 #include "filter.h"
4 #include "theme.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 Appearance::Appearance():
10         font_size(0),
11         sound_type(0),
12         sound_volume(100)
13 { }
14
15 void Appearance::merge_from(const Appearance &other)
16 {
17         if(other.font_size)
18                 font_size = other.font_size;
19         if(other.border_color.defined)
20                 border_color = other.border_color;
21         if(other.background_color.defined)
22                 background_color = other.background_color;
23         if(other.text_color.defined)
24                 text_color = other.text_color;
25         if(other.sound_type)
26         {
27                 sound_type = other.sound_type;
28                 sound_volume = other.sound_volume;
29         }
30 }
31
32 void Appearance::add_lines(FilterStatement &st) const
33 {
34         if(font_size)
35                 st.add_line(format("SetFontSize %d", font_size));
36
37         if(background_color.defined)
38                 st.add_line(format("SetBackgroundColor %d %d %d %d", background_color.r, background_color.g, background_color.b, background_color.a));
39
40         if(border_color.defined)
41                 st.add_line(format("SetBorderColor %d %d %d %d", border_color.r, border_color.g, border_color.b, border_color.a));
42
43         if(text_color.defined)
44                 st.add_line(format("SetTextColor %d %d %d %d", text_color.r, text_color.g, text_color.b, text_color.a));
45
46         if(sound_type)
47                 st.add_line(format("PlayAlertSound %d %d", sound_type, sound_volume));
48 }
49
50
51 Appearance::Loader::Loader(Appearance &a, const Theme *t):
52         DataFile::ObjectLoader<Appearance>(a),
53         theme(t)
54 {
55         add("alert_sound", &Appearance::sound_type, &Appearance::sound_volume);
56         add("background_color", &Loader::background_color);
57         add("background_color", &Loader::background_color_alpha);
58         add("background_color", &Loader::background_color_named);
59         add("border_color", &Loader::border_color);
60         add("border_color", &Loader::border_color_alpha);
61         add("border_color", &Loader::border_color_named);
62         add("font_size", &Loader::font_size);
63         add("inherit", &Loader::inherit);
64         add("text_color", &Loader::text_color);
65         add("text_color", &Loader::text_color_alpha);
66         add("text_color", &Loader::text_color_named);
67 }
68
69 void Appearance::Loader::background_color(unsigned r, unsigned g, unsigned b)
70 {
71         obj.background_color = Color(r, g, b);
72 }
73
74 void Appearance::Loader::background_color_alpha(unsigned r, unsigned g, unsigned b, unsigned a)
75 {
76         obj.background_color = Color(r, g, b, a);
77 }
78
79 void Appearance::Loader::background_color_named(const string &name)
80 {
81         if(!theme)
82                 throw logic_error("No theme");
83         obj.background_color = theme->get_color(name);
84 }
85
86 void Appearance::Loader::border_color(unsigned r, unsigned g, unsigned b)
87 {
88         obj.border_color = Color(r, g, b);
89 }
90
91 void Appearance::Loader::border_color_alpha(unsigned r, unsigned g, unsigned b, unsigned a)
92 {
93         obj.border_color = Color(r, g, b, a);
94 }
95
96 void Appearance::Loader::border_color_named(const string &name)
97 {
98         if(!theme)
99                 throw logic_error("No theme");
100         obj.border_color = theme->get_color(name);
101 }
102
103 void Appearance::Loader::font_size(float s)
104 {
105         float base_size = (theme ? theme->get_base_font_size() : 32);
106         obj.font_size = base_size*s+0.5f;
107 }
108
109 void Appearance::Loader::inherit(const string &name)
110 {
111         if(!theme)
112                 throw logic_error("No theme");
113         obj = theme->get_appearance(name);
114 }
115
116 void Appearance::Loader::text_color(unsigned r, unsigned g, unsigned b)
117 {
118         obj.text_color = Color(r, g, b);
119 }
120
121 void Appearance::Loader::text_color_alpha(unsigned r, unsigned g, unsigned b, unsigned a)
122 {
123         obj.text_color = Color(r, g, b, a);
124 }
125
126 void Appearance::Loader::text_color_named(const string &name)
127 {
128         if(!theme)
129                 throw logic_error("No theme");
130         obj.text_color = theme->get_color(name);
131 }