]> git.tdb.fi Git - libs/gltk.git/blob - source/style.cpp
Don't rebuild on state change if there are no visual changes
[libs/gltk.git] / source / style.cpp
1 #include "resources.h"
2 #include "style.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GLtk {
8
9 Style::Style():
10         font(0),
11         font_size(0)
12 { }
13
14 const GL::Font &Style::get_font() const
15 {
16         if(!font)
17                 throw logic_error("!font");
18         return *font;
19 }
20
21 const GL::Color &Style::get_font_color(State s) const
22 {
23         if(s>=N_STATES_)
24                 throw invalid_argument("Style::get_font_color");
25
26         return font_color[s];
27 }
28
29 const Part *Style::get_part(const string &name) const
30 {
31         for(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
32                 if(i->get_name()==name)
33                         return &*i;
34
35         return 0;
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(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
49                 if(i->get_graphic(s1)!=i->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
65         add("font",       &Loader::font);
66         add("font_color", &Loader::font_color_normal);
67         add("font_color", &Loader::font_color);
68         add("font_size",  &Style::font_size);
69         add("part",       &Loader::part);
70         add("part",       &Loader::unnamed_part);
71 }
72
73 void Style::Loader::font(const string &n)
74 {
75         obj.font = &get_collection().get<GL::Font>(n);
76         obj.font_size = obj.font->get_native_size();
77 }
78
79 void Style::Loader::font_color_normal(float r, float g, float b)
80 {
81         font_color(NORMAL, r, g, b);
82 }
83
84 void Style::Loader::font_color(State s, float r, float g, float b)
85 {
86         for(unsigned i=0; i<N_STATES_; ++i)
87                 if((i&s)==s)
88                         obj.font_color[i] = GL::Color(r, g, b);
89 }
90
91 void Style::Loader::part(const string &n)
92 {
93         Part p(n);
94         load_sub(p, get_collection());
95         obj.parts.push_back(p);
96 }
97
98 void Style::Loader::unnamed_part()
99 {
100         part(string());
101 }
102
103 } // namespace GLtk
104 } // namespace Msp