]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/style.cpp
Don't rebuild on state change if there are no visual changes
[libs/gltk.git] / source / style.cpp
index b4ee82d10eff5d1e454ebf767430d0edd941ed17..d53ae240fcea168a1b4659e6d8ac4472ec79f69c 100644 (file)
@@ -7,29 +7,97 @@ namespace Msp {
 namespace GLtk {
 
 Style::Style():
-       font(0)
+       font(0),
+       font_size(0)
 { }
 
+const GL::Font &Style::get_font() const
+{
+       if(!font)
+               throw logic_error("!font");
+       return *font;
+}
+
+const GL::Color &Style::get_font_color(State s) const
+{
+       if(s>=N_STATES_)
+               throw invalid_argument("Style::get_font_color");
+
+       return font_color[s];
+}
+
+const Part *Style::get_part(const string &name) const
+{
+       for(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+               if(i->get_name()==name)
+                       return &*i;
+
+       return 0;
+}
+
+bool Style::compare_states(State s1, State s2) const
+{
+       if(s1>=N_STATES_ || s2>=N_STATES_)
+               throw invalid_argument("Style::compare_states");
+
+       const GL::Color &c1 = font_color[s1];
+       const GL::Color &c2 = font_color[s2];
+       if(c1.r!=c2.r || c1.g!=c2.g || c1.b!=c2.b)
+               return true;
+
+       for(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+               if(i->get_graphic(s1)!=i->get_graphic(s2))
+                       return true;
+
+       return false;
+}
+
 
 Style::Loader::Loader(Style &s, Resources &r):
-       style(s),
-       res(r)
+       DataFile::CollectionObjectLoader<Style, Resources>(s, &r)
 {
-       add("font",       &Style::font);
+       if(!obj.font)
+       {
+               obj.font = &get_collection().get_default_font();
+               obj.font_size = obj.font->get_native_size();
+       }
+
+       add("font",       &Loader::font);
+       add("font_color", &Loader::font_color_normal);
        add("font_color", &Loader::font_color);
+       add("font_size",  &Style::font_size);
        add("part",       &Loader::part);
+       add("part",       &Loader::unnamed_part);
+}
+
+void Style::Loader::font(const string &n)
+{
+       obj.font = &get_collection().get<GL::Font>(n);
+       obj.font_size = obj.font->get_native_size();
 }
 
-void Style::Loader::font_color(float r, float g, float b)
+void Style::Loader::font_color_normal(float r, float g, float b)
 {
-       style.font_color=GL::Color(r, g, b);
+       font_color(NORMAL, r, g, b);
+}
+
+void Style::Loader::font_color(State s, float r, float g, float b)
+{
+       for(unsigned i=0; i<N_STATES_; ++i)
+               if((i&s)==s)
+                       obj.font_color[i] = GL::Color(r, g, b);
 }
 
 void Style::Loader::part(const string &n)
 {
        Part p(n);
-       load_sub(p, res);
-       style.parts.push_back(p);
+       load_sub(p, get_collection());
+       obj.parts.push_back(p);
+}
+
+void Style::Loader::unnamed_part()
+{
+       part(string());
 }
 
 } // namespace GLtk