]> git.tdb.fi Git - libs/gltk.git/blob - source/style.cpp
acbdff54febbda18dce5df7a7cc265fb16e2a647
[libs/gltk.git] / source / style.cpp
1 #include <msp/core/algorithm.h>
2 #include "resources.h"
3 #include "style.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GLtk {
9
10 Style::Style():
11         font(0),
12         font_size(0),
13         sampler(0)
14 { }
15
16 const GL::Font &Style::get_font() const
17 {
18         if(!font)
19                 throw logic_error("!font");
20         return *font;
21 }
22
23 const GL::Color &Style::get_font_color(State s) const
24 {
25         if(s>=N_STATES_)
26                 throw invalid_argument("Style::get_font_color");
27
28         return font_color[s];
29 }
30
31 const GL::Sampler &Style::get_sampler() const
32 {
33         if(!sampler)
34                 throw logic_error("!sampler");
35         return *sampler;
36 }
37
38 const Part *Style::get_part(const string &name) const
39 {
40         auto i = find_if(parts, [&name](const Part &p){ return p.get_name()==name; });
41         return (i!=parts.end() ? &*i : 0);
42 }
43
44 bool Style::compare_states(State s1, State s2) const
45 {
46         if(s1>=N_STATES_ || s2>=N_STATES_)
47                 throw invalid_argument("Style::compare_states");
48
49         const GL::Color &c1 = font_color[s1];
50         const GL::Color &c2 = font_color[s2];
51         if(c1.r!=c2.r || c1.g!=c2.g || c1.b!=c2.b)
52                 return true;
53
54         for(const Part &p: parts)
55                 if(p.get_graphic(s1)!=p.get_graphic(s2))
56                         return true;
57
58         return false;
59 }
60
61
62 Style::Loader::Loader(Style &s, Resources &r):
63         DataFile::CollectionObjectLoader<Style, Resources>(s, &r)
64 {
65         if(!obj.font)
66         {
67                 obj.font = &get_collection().get_default_font();
68                 obj.font_size = obj.font->get_native_size();
69         }
70         if(!obj.sampler)
71                 obj.sampler = &get_collection().get<GL::Sampler>("linear_clamp.samp");
72
73         add("font",       &Loader::font);
74         add("font_color", &Loader::font_color_normal);
75         add("font_color", &Loader::font_color);
76         add("font_size",  &Style::font_size);
77         add("part",       &Loader::part);
78         add("part",       &Loader::unnamed_part);
79         add("sampler",    &Style::sampler);
80 }
81
82 void Style::Loader::font(const string &n)
83 {
84         obj.font = &get_collection().get<GL::Font>(n);
85         obj.font_size = obj.font->get_native_size();
86 }
87
88 void Style::Loader::font_color_normal(float r, float g, float b)
89 {
90         font_color(NORMAL, r, g, b);
91 }
92
93 void Style::Loader::font_color(State s, float r, float g, float b)
94 {
95         for(unsigned i=0; i<N_STATES_; ++i)
96                 if((i&s)==s)
97                         obj.font_color[i] = GL::Color(r, g, b);
98 }
99
100 void Style::Loader::part(const string &n)
101 {
102         Part p(n);
103         load_sub(p, get_collection());
104         obj.parts.push_back(p);
105 }
106
107 void Style::Loader::unnamed_part()
108 {
109         part(string());
110 }
111
112 } // namespace GLtk
113 } // namespace Msp