]> git.tdb.fi Git - poefilter.git/blob - source/category.cpp
Initial version from when I last played
[poefilter.git] / source / category.cpp
1 #include "category.h"
2 #include "choicecondition.h"
3 #include "filter.h"
4 #include "rangecondition.h"
5 #include "theme.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 Category::Category():
11         condition(0),
12         font_size(1.0f),
13         order(0),
14         sound_type(0),
15         sound_volume(100)
16 { }
17
18 Category::Category(const Category &other):
19         condition(other.condition ? other.condition->clone() : 0),
20         font_size(other.font_size),
21         border_color(other.border_color),
22         order(other.order),
23         sound_type(other.sound_type),
24         sound_volume(other.sound_volume)
25 { }
26
27 Category &Category::operator=(const Category &other)
28 {
29         delete condition;
30         condition = (other.condition ? other.condition->clone() : 0);
31         font_size = other.font_size;
32         border_color = other.border_color;
33         order = other.order;
34         sound_type = other.sound_type;
35         sound_volume = other.sound_volume;
36         return *this;
37 }
38
39 Category::~Category()
40 {
41         delete condition;
42 }
43
44 void Category::create_statements(list<FilterStatement> &st, const Theme &theme) const
45 {
46         st.clear();
47         st.push_back(FilterStatement());
48         st.back().add_line(format("SetFontSize %d", static_cast<int>(font_size*theme.get_base_font_size()+0.5)));
49         if(!border_color.empty())
50         {
51                 const Color &color = theme.get_color(border_color);
52                 st.back().add_line(format("SetBorderColor %d %d %d", color.r, color.g, color.b));
53         }
54         if(sound_type)
55                 st.back().add_line(format("PlayAlertSound %d %d", sound_type, sound_volume));
56         if(condition)
57                 condition->add_lines(st);
58 }
59
60
61 Category::Loader::Loader(Category &c, CompoundCondition *n):
62         DataFile::ObjectLoader<Category>(c),
63         compound(n)
64 {
65         add("alert_sound", &Category::sound_type, &Category::sound_volume);
66         add("and", &Loader::and_);
67         add("base_type", &Loader::condition<BaseTypeCondition>);
68         add("border_color", &Category::border_color);
69         add("class", &Loader::condition<ClassCondition>);
70         add_range<DropLevelCondition>("drop_level");
71         add("font_size", &Category::font_size);
72         add_range<HeightCondition>("height");
73         add_range<ItemLevelCondition>("item_level");
74         add("linked_colors", &Loader::linked_colors);
75         add_range<LinkedSocketsCondition>("linked_sockets");
76         add_range<SocketsCondition>("sockets");
77         add("or", &Loader::or_);
78         add("order", &Category::order);
79         add_range<QualityCondition>("quality");
80         add_range<RarityCondition>("rarity");
81         add_range<WidthCondition>("width");
82 }
83
84 template<typename T>
85 void Category::Loader::add_range(const string &keyword)
86 {
87         add(keyword, &Loader::condition<T>);
88         add(keyword, &Loader::condition_range<T>);
89         add("min_"+keyword, &Loader::condition_min<T>);
90         add("max_"+keyword, &Loader::condition_max<T>);
91 }
92
93 void Category::Loader::add_condition(Condition *cond)
94 {
95         if(compound)
96                 compound->add(cond);
97         else if(!obj.condition)
98                 obj.condition = cond;
99         else
100         {
101                 compound = new AndCondition;
102                 compound->add(obj.condition);
103                 compound->add(cond);
104                 obj.condition = compound;
105         }
106 }
107
108 void Category::Loader::and_()
109 {
110         RefPtr<AndCondition> cond = new AndCondition;
111         Loader sub_ldr(obj, cond.get());
112         load_sub_with(sub_ldr);
113         add_condition(cond.release());
114 }
115
116 template<typename T>
117 void Category::Loader::condition(typename T::Type value)
118 {
119         add_condition(new T(value));
120 }
121
122 template<typename T>
123 void Category::Loader::condition_max(typename T::Type max)
124 {
125         add_condition(new T(T::Traits::get_min(), max));
126 }
127
128 template<typename T>
129 void Category::Loader::condition_min(typename T::Type min)
130 {
131         add_condition(new T(min, T::Traits::get_max()));
132 }
133
134 template<typename T>
135 void Category::Loader::condition_range(typename T::Type min, typename T::Type max)
136 {
137         add_condition(new T(min, max));
138 }
139
140 void Category::Loader::linked_colors(const LinkedColorsCondition::Colors &colors)
141 {
142         add_condition(new LinkedColorsCondition(colors));
143 }
144
145 void Category::Loader::or_()
146 {
147         RefPtr<OrCondition> cond = new OrCondition;
148         Loader sub_ldr(obj, cond.get());
149         load_sub_with(sub_ldr);
150         add_condition(cond.release());
151 }