From 0388bdcffe5c2c3e720afa9aa911268eac9c32de Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 15 Aug 2018 02:08:20 +0300 Subject: [PATCH] Associate categories and appearances at filter level 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 | 57 ++++++++++++++++++++++++++++++++++++++----- source/appearance.h | 10 ++++++-- source/category.cpp | 33 ++++++------------------- source/category.h | 6 +++-- source/filter.cpp | 41 ++++++++++++++++++++++--------- source/filter.h | 9 ++++++- source/poefilter.cpp | 2 +- source/theme.cpp | 9 ++++++- source/theme.h | 1 + 9 files changed, 117 insertions(+), 51 deletions(-) diff --git a/source/appearance.cpp b/source/appearance.cpp index bdb25bc..f1bb993 100644 --- a/source/appearance.cpp +++ b/source/appearance.cpp @@ -1,16 +1,54 @@ +#include #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(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); } diff --git a/source/appearance.h b/source/appearance.h index a522796..4135074 100644 --- a/source/appearance.h +++ b/source/appearance.h @@ -1,9 +1,11 @@ #ifndef APPEARANCE_H_ #define APPEARANCE_H_ +#include #include #include "color.h" +class FilterStatement; class Theme; class Appearance @@ -12,10 +14,10 @@ public: class Loader: public Msp::DataFile::ObjectLoader { 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 diff --git a/source/category.cpp b/source/category.cpp index 8b0d442..4ceed18 100644 --- a/source/category.cpp +++ b/source/category.cpp @@ -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 &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(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); add("class", &Loader::condition); add_range("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 void Category::Loader::condition(typename T::Type value) { diff --git a/source/category.h b/source/category.h index 0c549fa..dd47191 100644 --- a/source/category.h +++ b/source/category.h @@ -33,7 +33,6 @@ public: private: void and_(); - void appearance(const std::string &); template 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 &) const; }; diff --git a/source/filter.cpp b/source/filter.cpp index 70a126b..80ca193 100644 --- a/source/filter.cpp +++ b/source/filter.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "category.h" @@ -46,11 +46,14 @@ Filter::Filter(): void Filter::write(IO::Base &out) const { - for(list::const_iterator i=categories.begin(); i!=categories.end(); ++i) + for(list::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) { list 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::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_iterator i=categs.begin(); i!=categs.end(); ++i) - { - list::iterator j = find(obj.categories.begin(), obj.categories.end(), *i); - if(j!=obj.categories.end()) - obj.categories.erase(j); - } + for(list::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_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::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); + } + } } diff --git a/source/filter.h b/source/filter.h index 40eb057..492ff7d 100644 --- a/source/filter.h +++ b/source/filter.h @@ -6,6 +6,7 @@ #include #include #include +#include "appearance.h" class Category; class PoeFilter; @@ -50,7 +51,13 @@ public: }; private: - std::list categories; + struct Block + { + const Category *category; + Appearance appearance; + }; + + std::list blocks; bool abstract; public: diff --git a/source/poefilter.cpp b/source/poefilter.cpp index 20bf53c..b6fc147 100644 --- a/source/poefilter.cpp +++ b/source/poefilter.cpp @@ -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; } diff --git a/source/theme.cpp b/source/theme.cpp index b531cb6..7557caa 100644 --- a/source/theme.cpp +++ b/source/theme.cpp @@ -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(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); } diff --git a/source/theme.h b/source/theme.h index 2799c6b..43584a2 100644 --- a/source/theme.h +++ b/source/theme.h @@ -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 -- 2.45.2