]> git.tdb.fi Git - libs/gltk.git/blob - source/style.cpp
369559d8095b358bac335e2fb2c5727eabbe0206
[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
39 Style::Loader::Loader(Style &s, Resources &r):
40         DataFile::CollectionObjectLoader<Style, Resources>(s, &r)
41 {
42         if(!obj.font)
43         {
44                 obj.font = &get_collection().get_default_font();
45                 obj.font_size = obj.font->get_native_size();
46         }
47
48         add("font",       &Loader::font);
49         add("font_color", &Loader::font_color_normal);
50         add("font_color", &Loader::font_color);
51         add("font_size",  &Style::font_size);
52         add("part",       &Loader::part);
53         add("part",       &Loader::unnamed_part);
54 }
55
56 void Style::Loader::font(const string &n)
57 {
58         obj.font = &get_collection().get<GL::Font>(n);
59         obj.font_size = obj.font->get_native_size();
60 }
61
62 void Style::Loader::font_color_normal(float r, float g, float b)
63 {
64         font_color(NORMAL, r, g, b);
65 }
66
67 void Style::Loader::font_color(State s, float r, float g, float b)
68 {
69         for(unsigned i=0; i<N_STATES_; ++i)
70                 if((i&s)==s)
71                         obj.font_color[i] = GL::Color(r, g, b);
72 }
73
74 void Style::Loader::part(const string &n)
75 {
76         Part p(n);
77         load_sub(p, get_collection());
78         obj.parts.push_back(p);
79 }
80
81 void Style::Loader::unnamed_part()
82 {
83         part(string());
84 }
85
86 } // namespace GLtk
87 } // namespace Msp