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