]> git.tdb.fi Git - poefilter.git/commitdiff
Associate categories and appearances at filter level
authorMikko Rasa <tdb@tdb.fi>
Tue, 14 Aug 2018 23:08:20 +0000 (02:08 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 15 Aug 2018 00:01:34 +0000 (03:01 +0300)
Categories can still define appearance properties but can no longer refer
to appearances in the theme.

Since categories now know their name, enable printing of category names
in the output.

source/appearance.cpp
source/appearance.h
source/category.cpp
source/category.h
source/filter.cpp
source/filter.h
source/poefilter.cpp
source/theme.cpp
source/theme.h

index bdb25bc97f6b2a5123eb30c04b6027c51aac7778..f1bb993c90544f65a2dcb758a36a54a98e4bf5c3 100644 (file)
@@ -1,16 +1,54 @@
+#include <msp/strings/format.h>
 #include "appearance.h"
+#include "filter.h"
 #include "theme.h"
 
 using namespace std;
 using namespace Msp;
 
 Appearance::Appearance():
-       font_size(32),
+       font_size(0),
        sound_type(0),
        sound_volume(100)
 { }
 
-Appearance::Loader::Loader(Appearance &a, const Theme &t):
+void Appearance::merge_from(const Appearance &other)
+{
+       if(other.font_size)
+               font_size = other.font_size;
+       if(other.border_color.defined)
+               border_color = other.border_color;
+       if(other.background_color.defined)
+               background_color = other.background_color;
+       if(other.text_color.defined)
+               text_color = other.text_color;
+       if(other.sound_type)
+       {
+               sound_type = other.sound_type;
+               sound_volume = other.sound_volume;
+       }
+}
+
+void Appearance::add_lines(FilterStatement &st) const
+{
+       if(font_size)
+               st.add_line(format("SetFontSize %d", font_size));
+
+       if(background_color.defined)
+               st.add_line(format("SetBackgroundColor %d %d %d", background_color.r, background_color.g, background_color.b));
+
+       if(border_color.defined)
+               st.add_line(format("SetBorderColor %d %d %d", border_color.r, border_color.g, border_color.b));
+
+       if(text_color.defined)
+               st.add_line(format("SetTextColor %d %d %d", text_color.r, text_color.g, text_color.b));
+
+       if(sound_type)
+               st.add_line(format("PlayAlertSound %d %d", sound_type, sound_volume));
+}
+
+
+Appearance::Loader::Loader(Appearance &a, const Theme *t):
        DataFile::ObjectLoader<Appearance>(a),
        theme(t)
 {
@@ -31,7 +69,9 @@ void Appearance::Loader::background_color(unsigned r, unsigned g, unsigned b)
 
 void Appearance::Loader::background_color_named(const string &name)
 {
-       obj.background_color = theme.get_color(name);
+       if(!theme)
+               throw logic_error("No theme");
+       obj.background_color = theme->get_color(name);
 }
 
 void Appearance::Loader::border_color(unsigned r, unsigned g, unsigned b)
@@ -41,12 +81,15 @@ void Appearance::Loader::border_color(unsigned r, unsigned g, unsigned b)
 
 void Appearance::Loader::border_color_named(const string &name)
 {
-       obj.border_color = theme.get_color(name);
+       if(!theme)
+               throw logic_error("No theme");
+       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;
+       float base_size = (theme ? theme->get_base_font_size() : 32);
+       obj.font_size = base_size*s+0.5f;
 }
 
 void Appearance::Loader::text_color(unsigned r, unsigned g, unsigned b)
@@ -56,5 +99,7 @@ void Appearance::Loader::text_color(unsigned r, unsigned g, unsigned b)
 
 void Appearance::Loader::text_color_named(const string &name)
 {
-       obj.text_color = theme.get_color(name);
+       if(!theme)
+               throw logic_error("No theme");
+       obj.text_color = theme->get_color(name);
 }
index a522796c663270469e4692a2bed9ecfa405797b0..41350744234f877b84b7d7142bc114792ee361f2 100644 (file)
@@ -1,9 +1,11 @@
 #ifndef APPEARANCE_H_
 #define APPEARANCE_H_
 
+#include <list>
 #include <msp/datafile/objectloader.h>
 #include "color.h"
 
+class FilterStatement;
 class Theme;
 
 class Appearance
@@ -12,10 +14,10 @@ public:
        class Loader: public Msp::DataFile::ObjectLoader<Appearance>
        {
        private:
-               const Theme &theme;
+               const Theme *theme;
 
        public:
-               Loader(Appearance &, const Theme &);
+               Loader(Appearance &, const Theme * = 0);
 
                void background_color(unsigned, unsigned, unsigned);
                void background_color_named(const std::string &);
@@ -37,12 +39,16 @@ private:
 public:
        Appearance();
 
+       void merge_from(const 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; }
+
+       void add_lines(FilterStatement &) const;
 };
 
 #endif
index 8b0d442ea8d572c016fe4f01fb9c891109b23ffd..4ceed1883df32f7e971bd8dcb594795f64fa831c 100644 (file)
@@ -8,12 +8,14 @@
 using namespace std;
 using namespace Msp;
 
-Category::Category():
+Category::Category(const string &n):
+       name(n),
        condition(0),
        order(0)
 { }
 
 Category::Category(const Category &other):
+       name(other.name),
        condition(other.condition ? other.condition->clone() : 0),
        order(other.order),
        appearance(other.appearance)
@@ -22,6 +24,7 @@ Category::Category(const Category &other):
 Category &Category::operator=(const Category &other)
 {
        delete condition;
+       name = other.name;
        condition = (other.condition ? other.condition->clone() : 0);
        order = other.order;
        appearance = other.appearance;
@@ -35,24 +38,8 @@ Category::~Category()
 
 void Category::create_statements(list<FilterStatement> &st) const
 {
-       st.clear();
-       st.push_back(FilterStatement());
-       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(st.empty())
+               st.push_back(FilterStatement());
 
        if(condition)
                condition->add_lines(st);
@@ -63,10 +50,9 @@ Category::Loader::Loader(Category &c, const PoeFilter &p, CompoundCondition *n):
        DataFile::ObjectLoader<Category>(c),
        poe(p),
        compound(n),
-       app_loader(c.appearance, p.get_theme())
+       app_loader(c.appearance)
 {
        add("and", &Loader::and_);
-       add("appearance", &Loader::appearance);
        add("base_type", &Loader::condition<BaseTypeCondition>);
        add("class", &Loader::condition<ClassCondition>);
        add_range<DropLevelCondition>("drop_level");
@@ -116,11 +102,6 @@ void Category::Loader::and_()
        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)
 {
index 0c549fa9811e54a72a8bcc4a9ad7c2be11033ddb..dd47191e7bcd9d88a0aa59bceceb3e1e43bd52f7 100644 (file)
@@ -33,7 +33,6 @@ public:
 
        private:
                void and_();
-               void appearance(const std::string &);
 
                template<typename T>
                void condition(typename T::Type);
@@ -52,17 +51,20 @@ public:
        };
 
 private:
+       std::string name;
        Condition *condition;
        unsigned order;
        Appearance appearance;
 
 public:
-       Category();
+       Category(const std::string & = std::string());
        Category(const Category &);
        Category &operator=(const Category &);
        ~Category();
 
+       const std::string &get_name() const { return name; }
        unsigned get_order() const { return order; }
+       const Appearance &get_appearance() const { return appearance; }
        void create_statements(std::list<FilterStatement> &) const;
 };
 
index 70a126b5c8eeaf5464c1b8540915fcea5c8054d5..80ca1932facfa455cc6ee2d0bd631958cf9513d4 100644 (file)
@@ -1,4 +1,4 @@
-#include <algorithm>
+#include <msp/core/algorithm.h>
 #include <msp/core/maputils.h>
 #include <msp/io/print.h>
 #include "category.h"
@@ -46,11 +46,14 @@ Filter::Filter():
 
 void Filter::write(IO::Base &out) const
 {
-       for(list<const Category *>::const_iterator i=categories.begin(); i!=categories.end(); ++i)
+       for(list<Block>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
        {
                list<FilterStatement> st;
-               //IO::print(out, "# %s\n", i->first);
-               (*i)->create_statements(st);
+               st.push_back(FilterStatement());
+               i->appearance.add_lines(st.back());
+               i->category->create_statements(st);
+
+               IO::print(out, "# %s\n", i->category->get_name());
                for(list<FilterStatement>::const_iterator j=st.begin(); j!=st.end(); ++j)
                        j->write(out);
        }
@@ -104,17 +107,18 @@ void Filter::Loader::hide(const string &name)
                categs.push_back(&poe.get_category(name));
 
        for(list<const Category *>::const_iterator i=categs.begin(); i!=categs.end(); ++i)
-       {
-               list<const Category *>::iterator j = find(obj.categories.begin(), obj.categories.end(), *i);
-               if(j!=obj.categories.end())
-                       obj.categories.erase(j);
-       }
+               for(list<Block>::iterator j=obj.blocks.begin(); j!=obj.blocks.end(); ++j)
+                       if(j->category==*i)
+                       {
+                               obj.blocks.erase(j);
+                               break;
+                       }
 }
 
 void Filter::Loader::include(const string &name)
 {
        const Filter &base = poe.get_filter(name);
-       obj.categories.insert(obj.categories.end(), base.categories.begin(), base.categories.end());
+       obj.blocks.insert(obj.blocks.end(), base.blocks.begin(), base.blocks.end());
 }
 
 void Filter::Loader::show(const string &name)
@@ -133,6 +137,19 @@ void Filter::Loader::show(const string &name)
                categs.push_back(&poe.get_category(name));
 
        for(list<const Category *>::const_iterator i=categs.begin(); i!=categs.end(); ++i)
-               if(find(obj.categories.begin(), obj.categories.end(), *i)==obj.categories.end())
-                       obj.categories.push_back(*i);
+       {
+               bool found = false;
+               for(list<Block>::const_iterator j=obj.blocks.begin(); (!found && j!=obj.blocks.end()); ++j)
+                       found = (j->category==*i);
+
+               if(!found)
+               {
+                       Block blk;
+                       blk.category = *i;
+                       blk.appearance = (*i)->get_appearance();
+                       if(const Appearance *app = poe.get_theme().find_appearance((*i)->get_name()))
+                               blk.appearance.merge_from(*app);
+                       obj.blocks.push_back(blk);
+               }
+       }
 }
index 40eb057ba8e895ffd34852eb178db4264225c2bc..492ff7d30ac9d7fa685b185a13b97b093be59a4f 100644 (file)
@@ -6,6 +6,7 @@
 #include <string>
 #include <msp/datafile/objectloader.h>
 #include <msp/io/base.h>
+#include "appearance.h"
 
 class Category;
 class PoeFilter;
@@ -50,7 +51,13 @@ public:
        };
 
 private:
-       std::list<const Category *> categories;
+       struct Block
+       {
+               const Category *category;
+               Appearance appearance;
+       };
+
+       std::list<Block> blocks;
        bool abstract;
 
 public:
index 20bf53cd24b115806aec05bf487d7139c0ca2deb..b6fc1475a1f96fc093234d1c0e1a4f71d5524547 100644 (file)
@@ -57,7 +57,7 @@ PoeFilter::Loader::Loader(PoeFilter &f):
 
 void PoeFilter::Loader::category(const string &name)
 {
-       Category cat;
+       Category cat(name);
        load_sub(cat, obj);
        obj.categories[name] = cat;
 }
index b531cb6351222360651075d19b832889b64dc923..7557caaa8c8f90083a323e39b9e0e7ffb168fd7f 100644 (file)
@@ -33,6 +33,12 @@ const Appearance &Theme::get_appearance(const string &name) const
        return get_item(appearances, name);
 }
 
+const Appearance *Theme::find_appearance(const string &name) const
+{
+       AppearanceMap::const_iterator i = appearances.find(name);
+       return (i!=appearances.end() ? &i->second : 0);
+}
+
 
 Theme::Loader::Loader(Theme &t):
        DataFile::ObjectLoader<Theme>(t)
@@ -45,7 +51,8 @@ Theme::Loader::Loader(Theme &t):
 void Theme::Loader::appearance(const string &name)
 {
        Appearance app;
-       load_sub(app, obj);
+       Appearance::Loader ldr(app, &obj);
+       load_sub_with(ldr);
        obj.set_appearance(name, app);
 }
 
index 2799c6b040bd15a253519a72ba068282e3b1e2bd..43584a2a0d28ce3c49097a0a789b318c020add61 100644 (file)
@@ -39,6 +39,7 @@ public:
 
        void set_appearance(const std::string &, const Appearance &);
        const Appearance &get_appearance(const std::string &) const;
+       const Appearance *find_appearance(const std::string &) const;
 };
 
 #endif