]> git.tdb.fi Git - poefilter.git/commitdiff
Revamp appearance definitions
authorMikko Rasa <tdb@tdb.fi>
Mon, 13 Aug 2018 21:35:25 +0000 (00:35 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 13 Aug 2018 21:35:25 +0000 (00:35 +0300)
Appearance data is now stored in a separate class and whole appearances
can be defined in the theme.

12 files changed:
source/appearance.cpp [new file with mode: 0644]
source/appearance.h [new file with mode: 0644]
source/category.cpp
source/category.h
source/color.cpp [new file with mode: 0644]
source/color.h [new file with mode: 0644]
source/filter.cpp
source/filter.h
source/poefilter.cpp
source/poefilter.h
source/theme.cpp
source/theme.h

diff --git a/source/appearance.cpp b/source/appearance.cpp
new file mode 100644 (file)
index 0000000..bdb25bc
--- /dev/null
@@ -0,0 +1,60 @@
+#include "appearance.h"
+#include "theme.h"
+
+using namespace std;
+using namespace Msp;
+
+Appearance::Appearance():
+       font_size(32),
+       sound_type(0),
+       sound_volume(100)
+{ }
+
+Appearance::Loader::Loader(Appearance &a, const Theme &t):
+       DataFile::ObjectLoader<Appearance>(a),
+       theme(t)
+{
+       add("alert_sound", &Appearance::sound_type, &Appearance::sound_volume);
+       add("background_color", &Loader::background_color);
+       add("background_color", &Loader::background_color_named);
+       add("border_color", &Loader::border_color);
+       add("border_color", &Loader::border_color_named);
+       add("font_size", &Loader::font_size);
+       add("text_color", &Loader::text_color);
+       add("text_color", &Loader::text_color_named);
+}
+
+void Appearance::Loader::background_color(unsigned r, unsigned g, unsigned b)
+{
+       obj.background_color = Color(r, g, b);
+}
+
+void Appearance::Loader::background_color_named(const string &name)
+{
+       obj.background_color = theme.get_color(name);
+}
+
+void Appearance::Loader::border_color(unsigned r, unsigned g, unsigned b)
+{
+       obj.border_color = Color(r, g, b);
+}
+
+void Appearance::Loader::border_color_named(const string &name)
+{
+       obj.border_color = theme.get_color(name);
+}
+
+void Appearance::Loader::font_size(float s)
+{
+       obj.font_size = theme.get_base_font_size()*s+0.5f;
+}
+
+void Appearance::Loader::text_color(unsigned r, unsigned g, unsigned b)
+{
+       obj.text_color = Color(r, g, b);
+}
+
+void Appearance::Loader::text_color_named(const string &name)
+{
+       obj.text_color = theme.get_color(name);
+}
diff --git a/source/appearance.h b/source/appearance.h
new file mode 100644 (file)
index 0000000..a522796
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef APPEARANCE_H_
+#define APPEARANCE_H_
+
+#include <msp/datafile/objectloader.h>
+#include "color.h"
+
+class Theme;
+
+class Appearance
+{
+public:
+       class Loader: public Msp::DataFile::ObjectLoader<Appearance>
+       {
+       private:
+               const Theme &theme;
+
+       public:
+               Loader(Appearance &, const Theme &);
+
+               void background_color(unsigned, unsigned, unsigned);
+               void background_color_named(const std::string &);
+               void border_color(unsigned, unsigned, unsigned);
+               void border_color_named(const std::string &);
+               void font_size(float);
+               void text_color(unsigned, unsigned, unsigned);
+               void text_color_named(const std::string &);
+       };
+
+private:
+       unsigned font_size;
+       Color border_color;
+       Color background_color;
+       Color text_color;
+       unsigned sound_type;
+       unsigned sound_volume;
+
+public:
+       Appearance();
+
+       unsigned get_font_size() const { return font_size; }
+       const Color &get_border_color() const { return border_color; }
+       const Color &get_background_color() const { return background_color; }
+       const Color &get_text_color() const { return text_color; }
+       unsigned get_sound_type() const { return sound_type; }
+       unsigned get_sound_volume() const { return sound_volume; }
+};
+
+#endif
index 2ea4818b9ae460871b520fc84504fcd4f450b8e4..8b0d442ea8d572c016fe4f01fb9c891109b23ffd 100644 (file)
@@ -1,6 +1,7 @@
 #include "category.h"
 #include "choicecondition.h"
 #include "filter.h"
+#include "poefilter.h"
 #include "rangecondition.h"
 #include "theme.h"
 
@@ -9,30 +10,21 @@ using namespace Msp;
 
 Category::Category():
        condition(0),
-       font_size(1.0f),
-       order(0),
-       sound_type(0),
-       sound_volume(100)
+       order(0)
 { }
 
 Category::Category(const Category &other):
        condition(other.condition ? other.condition->clone() : 0),
-       font_size(other.font_size),
-       border_color(other.border_color),
        order(other.order),
-       sound_type(other.sound_type),
-       sound_volume(other.sound_volume)
+       appearance(other.appearance)
 { }
 
 Category &Category::operator=(const Category &other)
 {
        delete condition;
        condition = (other.condition ? other.condition->clone() : 0);
-       font_size = other.font_size;
-       border_color = other.border_color;
        order = other.order;
-       sound_type = other.sound_type;
-       sound_volume = other.sound_volume;
+       appearance = other.appearance;
        return *this;
 }
 
@@ -41,34 +33,43 @@ Category::~Category()
        delete condition;
 }
 
-void Category::create_statements(list<FilterStatement> &st, const Theme &theme) const
+void Category::create_statements(list<FilterStatement> &st) const
 {
        st.clear();
        st.push_back(FilterStatement());
-       st.back().add_line(format("SetFontSize %d", static_cast<int>(font_size*theme.get_base_font_size()+0.5)));
-       if(!border_color.empty())
-       {
-               const Color &color = theme.get_color(border_color);
-               st.back().add_line(format("SetBorderColor %d %d %d", color.r, color.g, color.b));
-       }
-       if(sound_type)
-               st.back().add_line(format("PlayAlertSound %d %d", sound_type, sound_volume));
+       st.back().add_line(format("SetFontSize %d", appearance.get_font_size()));
+
+       const Color &bg_color = appearance.get_background_color();
+       if(bg_color.defined)
+               st.back().add_line(format("SetBackgroundColor %d %d %d", bg_color.r, bg_color.g, bg_color.b));
+
+       const Color &brd_color = appearance.get_border_color();
+       if(brd_color.defined)
+               st.back().add_line(format("SetBorderColor %d %d %d", brd_color.r, brd_color.g, brd_color.b));
+
+       const Color &txt_color = appearance.get_text_color();
+       if(txt_color.defined)
+               st.back().add_line(format("SetTextColor %d %d %d", txt_color.r, txt_color.g, txt_color.b));
+
+       if(appearance.get_sound_type())
+               st.back().add_line(format("PlayAlertSound %d %d", appearance.get_sound_type(), appearance.get_sound_volume()));
+
        if(condition)
                condition->add_lines(st);
 }
 
 
-Category::Loader::Loader(Category &c, CompoundCondition *n):
+Category::Loader::Loader(Category &c, const PoeFilter &p, CompoundCondition *n):
        DataFile::ObjectLoader<Category>(c),
-       compound(n)
+       poe(p),
+       compound(n),
+       app_loader(c.appearance, p.get_theme())
 {
-       add("alert_sound", &Category::sound_type, &Category::sound_volume);
        add("and", &Loader::and_);
+       add("appearance", &Loader::appearance);
        add("base_type", &Loader::condition<BaseTypeCondition>);
-       add("border_color", &Category::border_color);
        add("class", &Loader::condition<ClassCondition>);
        add_range<DropLevelCondition>("drop_level");
-       add("font_size", &Category::font_size);
        add_range<HeightCondition>("height");
        add_range<ItemLevelCondition>("item_level");
        add("linked_colors", &Loader::linked_colors);
@@ -79,6 +80,8 @@ Category::Loader::Loader(Category &c, CompoundCondition *n):
        add_range<QualityCondition>("quality");
        add_range<RarityCondition>("rarity");
        add_range<WidthCondition>("width");
+
+       add_auxiliary_loader(app_loader);
 }
 
 template<typename T>
@@ -108,11 +111,16 @@ void Category::Loader::add_condition(Condition *cond)
 void Category::Loader::and_()
 {
        RefPtr<AndCondition> cond = new AndCondition;
-       Loader sub_ldr(obj, cond.get());
+       Loader sub_ldr(obj, poe, cond.get());
        load_sub_with(sub_ldr);
        add_condition(cond.release());
 }
 
+void Category::Loader::appearance(const std::string &name)
+{
+       obj.appearance = poe.get_theme().get_appearance(name);
+}
+
 template<typename T>
 void Category::Loader::condition(typename T::Type value)
 {
@@ -145,7 +153,7 @@ void Category::Loader::linked_colors(const LinkedColorsCondition::Colors &colors
 void Category::Loader::or_()
 {
        RefPtr<OrCondition> cond = new OrCondition;
-       Loader sub_ldr(obj, cond.get());
+       Loader sub_ldr(obj, poe, cond.get());
        load_sub_with(sub_ldr);
        add_condition(cond.release());
 }
index 53b1beb342fd09c5828d6e660f00f94053748bb5..0c549fa9811e54a72a8bcc4a9ad7c2be11033ddb 100644 (file)
@@ -4,10 +4,12 @@
 #include <list>
 #include <string>
 #include <msp/datafile/objectloader.h>
+#include "appearance.h"
 #include "condition.h"
 #include "rarity.h"
 
 class FilterStatement;
+class PoeFilter;
 class Theme;
 
 class Category
@@ -16,10 +18,12 @@ public:
        class Loader: public Msp::DataFile::ObjectLoader<Category>
        {
        private:
+               const PoeFilter &poe;
                CompoundCondition *compound;
+               Appearance::Loader app_loader;
 
        public:
-               Loader(Category &, CompoundCondition * = 0);
+               Loader(Category &, const PoeFilter &, CompoundCondition * = 0);
 
        protected:
                template<typename T>
@@ -29,6 +33,7 @@ public:
 
        private:
                void and_();
+               void appearance(const std::string &);
 
                template<typename T>
                void condition(typename T::Type);
@@ -48,11 +53,8 @@ public:
 
 private:
        Condition *condition;
-       float font_size;
-       std::string border_color;
        unsigned order;
-       unsigned sound_type;
-       unsigned sound_volume;
+       Appearance appearance;
 
 public:
        Category();
@@ -61,7 +63,7 @@ public:
        ~Category();
 
        unsigned get_order() const { return order; }
-       void create_statements(std::list<FilterStatement> &, const Theme &) const;
+       void create_statements(std::list<FilterStatement> &) const;
 };
 
 #endif
diff --git a/source/color.cpp b/source/color.cpp
new file mode 100644 (file)
index 0000000..a7d8330
--- /dev/null
@@ -0,0 +1,15 @@
+#include "color.h"
+
+Color::Color():
+       defined(false),
+       r(0),
+       g(0),
+       b(0)
+{ }
+
+Color::Color(unsigned r_, unsigned g_, unsigned b_):
+       defined(true),
+       r(r_),
+       g(g_),
+       b(b_)
+{ }
diff --git a/source/color.h b/source/color.h
new file mode 100644 (file)
index 0000000..5ade180
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef COLOR_H_
+#define COLOR_H_
+
+struct Color
+{
+       bool defined;
+       unsigned r;
+       unsigned g;
+       unsigned b;
+
+       Color();
+       Color(unsigned, unsigned, unsigned);
+};
+
+#endif
index c694ca67eb09814f38dcc563207beb940ad2d14a..70a126b5c8eeaf5464c1b8540915fcea5c8054d5 100644 (file)
@@ -44,13 +44,13 @@ Filter::Filter():
        abstract(false)
 { }
 
-void Filter::write(IO::Base &out, const Theme &theme) const
+void Filter::write(IO::Base &out) const
 {
        for(list<const Category *>::const_iterator i=categories.begin(); i!=categories.end(); ++i)
        {
                list<FilterStatement> st;
                //IO::print(out, "# %s\n", i->first);
-               (*i)->create_statements(st, theme);
+               (*i)->create_statements(st);
                for(list<FilterStatement>::const_iterator j=st.begin(); j!=st.end(); ++j)
                        j->write(out);
        }
index 46429431fe4293a2b43f938c80d92931eb525955..40eb057ba8e895ffd34852eb178db4264225c2bc 100644 (file)
@@ -57,7 +57,7 @@ public:
        Filter();
 
        bool is_abstract() const { return abstract; }
-       void write(Msp::IO::Base &, const Theme &) const;
+       void write(Msp::IO::Base &) const;
 };
 
 #endif
index 032b8716841ef75bc3b8a5f6637daea1be1b9e5e..20bf53cd24b115806aec05bf487d7139c0ca2deb 100644 (file)
@@ -23,7 +23,7 @@ int PoeFilter::main()
                if(!i->second.is_abstract())
                {
                        IO::BufferedFile out(i->first+".filter", IO::M_WRITE);
-                       i->second.write(out, theme);
+                       i->second.write(out);
                }
 
        return 0;
@@ -58,7 +58,7 @@ PoeFilter::Loader::Loader(PoeFilter &f):
 void PoeFilter::Loader::category(const string &name)
 {
        Category cat;
-       load_sub(cat);
+       load_sub(cat, obj);
        obj.categories[name] = cat;
 }
 
index 0e87d2dbe5e2ff5a8573c9db9a3dae3c66410367..666d0a60eb12912a031c2b66f401dd59100607ca 100644 (file)
@@ -35,6 +35,7 @@ public:
 
        virtual int main();
 
+       const Theme &get_theme() const { return theme; }
        const Category &get_category(const std::string &) const;
        void find_categories(const Msp::Regex &, std::list<const Category *> &) const;
        const Filter &get_filter(const std::string &) const;
index 3b09dc3f3fddd9540dcef643c25483f12c6c5151..b531cb6351222360651075d19b832889b64dc923 100644 (file)
@@ -4,19 +4,6 @@
 using namespace std;
 using namespace Msp;
 
-Color::Color():
-       r(200),
-       g(200),
-       b(200)
-{ }
-
-Color::Color(unsigned r_, unsigned g_, unsigned b_):
-       r(r_),
-       g(g_),
-       b(b_)
-{ }
-
-
 Theme::Theme():
        base_font_size(32)
 { }
@@ -36,14 +23,32 @@ void Theme::set_base_font_size(unsigned size)
        base_font_size = size;
 }
 
+void Theme::set_appearance(const string &name, const Appearance &app)
+{
+       appearances[name] = app;
+}
+
+const Appearance &Theme::get_appearance(const string &name) const
+{
+       return get_item(appearances, name);
+}
+
 
 Theme::Loader::Loader(Theme &t):
        DataFile::ObjectLoader<Theme>(t)
 {
+       add("appearance", &Loader::appearance);
        add("base_font_size", &Theme::base_font_size);
        add("color", &Loader::color);
 }
 
+void Theme::Loader::appearance(const string &name)
+{
+       Appearance app;
+       load_sub(app, obj);
+       obj.set_appearance(name, app);
+}
+
 void Theme::Loader::color(const string &name, unsigned r, unsigned g, unsigned b)
 {
        obj.set_color(name, Color(r, g, b));
index 7e296ae44c799027d949ebca64f98959b493cb64..2799c6b040bd15a253519a72ba068282e3b1e2bd 100644 (file)
@@ -4,17 +4,8 @@
 #include <map>
 #include <string>
 #include <msp/datafile/objectloader.h>
-
-struct Color
-{
-       unsigned r;
-       unsigned g;
-       unsigned b;
-
-       Color();
-       Color(unsigned, unsigned, unsigned);
-};
-
+#include "appearance.h"
+#include "color.h"
 
 class Theme
 {
@@ -25,14 +16,17 @@ public:
                Loader(Theme &);
 
        private:
+               void appearance(const std::string &);
                void color(const std::string &, unsigned, unsigned, unsigned);
        };
 
 private:
        typedef std::map<std::string, Color> ColorMap;
+       typedef std::map<std::string, Appearance> AppearanceMap;
 
        ColorMap colors;
        unsigned base_font_size;
+       AppearanceMap appearances;
 
 public:
        Theme();
@@ -42,6 +36,9 @@ public:
 
        void set_base_font_size(unsigned);
        unsigned get_base_font_size() const { return base_font_size; }
+
+       void set_appearance(const std::string &, const Appearance &);
+       const Appearance &get_appearance(const std::string &) const;
 };
 
 #endif