]> git.tdb.fi Git - poefilter.git/blob - source/category.cpp
fb12fec0ddfd25fa0dc14996b80e780af62507ae
[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                 condition->add_lines(st);
51 }
52
53
54 Category::Loader::Loader(Category &c, const PoeFilter &p, CompoundCondition *n):
55         DataFile::ObjectLoader<Category>(c),
56         poe(p),
57         compound(n),
58         app_loader(c.appearance)
59 {
60         add("and", &Loader::and_);
61         add("base_type", &Loader::condition<BaseTypeCondition>);
62         add("category", &Loader::category);
63         add("class", &Loader::condition<ClassCondition>);
64         add_range<DropLevelCondition>("drop_level");
65         add_range<HeightCondition>("height");
66         add_range<ItemLevelCondition>("item_level");
67         add("linked_colors", &Loader::linked_colors);
68         add_range<LinkedSocketsCondition>("linked_sockets");
69         add_range<SocketsCondition>("sockets");
70         add("or", &Loader::or_);
71         add_range<QualityCondition>("quality");
72         add_range<RarityCondition>("rarity");
73         add_range<WidthCondition>("width");
74
75         add_auxiliary_loader(app_loader);
76 }
77
78 template<typename T>
79 void Category::Loader::add_range(const string &keyword)
80 {
81         add(keyword, &Loader::condition<T>);
82         add(keyword, &Loader::condition_range<T>);
83         add("min_"+keyword, &Loader::condition_min<T>);
84         add("max_"+keyword, &Loader::condition_max<T>);
85 }
86
87 void Category::Loader::add_condition(Condition *cond)
88 {
89         if(compound)
90                 compound->add(cond);
91         else if(!obj.condition)
92                 obj.condition = cond;
93         else
94         {
95                 compound = new AndCondition;
96                 compound->add(obj.condition);
97                 compound->add(cond);
98                 obj.condition = compound;
99         }
100 }
101
102 void Category::Loader::and_()
103 {
104         RefPtr<AndCondition> cond = new AndCondition;
105         Loader sub_ldr(obj, poe, cond.get());
106         load_sub_with(sub_ldr);
107         add_condition(cond.release());
108 }
109
110 void Category::Loader::category(const string &name)
111 {
112         const Category &categ = poe.get_category(name);
113         add_condition(categ.condition->clone());
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, poe, cond.get());
149         load_sub_with(sub_ldr);
150         add_condition(cond.release());
151 }