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