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