From 74086c211f082f6f47c3d038dd308a257a81e006 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 14 Aug 2018 00:35:25 +0300 Subject: [PATCH] Revamp appearance definitions Appearance data is now stored in a separate class and whole appearances can be defined in the theme. --- source/appearance.cpp | 60 ++++++++++++++++++++++++++++++++++++++++ source/appearance.h | 48 ++++++++++++++++++++++++++++++++ source/category.cpp | 64 ++++++++++++++++++++++++------------------- source/category.h | 14 ++++++---- source/color.cpp | 15 ++++++++++ source/color.h | 15 ++++++++++ source/filter.cpp | 4 +-- source/filter.h | 2 +- source/poefilter.cpp | 4 +-- source/poefilter.h | 1 + source/theme.cpp | 31 ++++++++++++--------- source/theme.h | 19 ++++++------- 12 files changed, 214 insertions(+), 63 deletions(-) create mode 100644 source/appearance.cpp create mode 100644 source/appearance.h create mode 100644 source/color.cpp create mode 100644 source/color.h diff --git a/source/appearance.cpp b/source/appearance.cpp new file mode 100644 index 0000000..bdb25bc --- /dev/null +++ b/source/appearance.cpp @@ -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(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 index 0000000..a522796 --- /dev/null +++ b/source/appearance.h @@ -0,0 +1,48 @@ +#ifndef APPEARANCE_H_ +#define APPEARANCE_H_ + +#include +#include "color.h" + +class Theme; + +class Appearance +{ +public: + class Loader: public Msp::DataFile::ObjectLoader + { + 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 diff --git a/source/category.cpp b/source/category.cpp index 2ea4818..8b0d442 100644 --- a/source/category.cpp +++ b/source/category.cpp @@ -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 &st, const Theme &theme) const +void Category::create_statements(list &st) const { st.clear(); st.push_back(FilterStatement()); - st.back().add_line(format("SetFontSize %d", static_cast(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(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); - add("border_color", &Category::border_color); add("class", &Loader::condition); add_range("drop_level"); - add("font_size", &Category::font_size); add_range("height"); add_range("item_level"); add("linked_colors", &Loader::linked_colors); @@ -79,6 +80,8 @@ Category::Loader::Loader(Category &c, CompoundCondition *n): add_range("quality"); add_range("rarity"); add_range("width"); + + add_auxiliary_loader(app_loader); } template @@ -108,11 +111,16 @@ void Category::Loader::add_condition(Condition *cond) void Category::Loader::and_() { RefPtr 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 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 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()); } diff --git a/source/category.h b/source/category.h index 53b1beb..0c549fa 100644 --- a/source/category.h +++ b/source/category.h @@ -4,10 +4,12 @@ #include #include #include +#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 { private: + const PoeFilter &poe; CompoundCondition *compound; + Appearance::Loader app_loader; public: - Loader(Category &, CompoundCondition * = 0); + Loader(Category &, const PoeFilter &, CompoundCondition * = 0); protected: template @@ -29,6 +33,7 @@ public: private: void and_(); + void appearance(const std::string &); template 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 &, const Theme &) const; + void create_statements(std::list &) const; }; #endif diff --git a/source/color.cpp b/source/color.cpp new file mode 100644 index 0000000..a7d8330 --- /dev/null +++ b/source/color.cpp @@ -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 index 0000000..5ade180 --- /dev/null +++ b/source/color.h @@ -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 diff --git a/source/filter.cpp b/source/filter.cpp index c694ca6..70a126b 100644 --- a/source/filter.cpp +++ b/source/filter.cpp @@ -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_iterator i=categories.begin(); i!=categories.end(); ++i) { list st; //IO::print(out, "# %s\n", i->first); - (*i)->create_statements(st, theme); + (*i)->create_statements(st); for(list::const_iterator j=st.begin(); j!=st.end(); ++j) j->write(out); } diff --git a/source/filter.h b/source/filter.h index 4642943..40eb057 100644 --- a/source/filter.h +++ b/source/filter.h @@ -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 diff --git a/source/poefilter.cpp b/source/poefilter.cpp index 032b871..20bf53c 100644 --- a/source/poefilter.cpp +++ b/source/poefilter.cpp @@ -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; } diff --git a/source/poefilter.h b/source/poefilter.h index 0e87d2d..666d0a6 100644 --- a/source/poefilter.h +++ b/source/poefilter.h @@ -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; const Filter &get_filter(const std::string &) const; diff --git a/source/theme.cpp b/source/theme.cpp index 3b09dc3..b531cb6 100644 --- a/source/theme.cpp +++ b/source/theme.cpp @@ -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(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)); diff --git a/source/theme.h b/source/theme.h index 7e296ae..2799c6b 100644 --- a/source/theme.h +++ b/source/theme.h @@ -4,17 +4,8 @@ #include #include #include - -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 ColorMap; + typedef std::map 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 -- 2.45.2