]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
removed stupid file headers that eclipse put in
[l2e.git] / src / graphics / Menu.h
1 #ifndef GRAPHICS_MENU_H_
2 #define GRAPHICS_MENU_H_
3
4 #include "Font.h"
5 #include "fwd.h"
6 #include "Sprite.h"
7 #include "../geometry/Vector.h"
8
9 #include <algorithm>
10 #include <vector>
11 #include <SDL.h>
12
13 namespace graphics {
14
15 struct MenuProperties {
16         static const int TYPE_ID = 407;
17
18         const Font *font;
19         const Font *disabledFont;
20         const Sprite *cursor;
21         const Sprite *selectedCursor;
22         int charsPerEntry;
23         int rows;
24         int rowGap;
25         int iconSpace;
26         int cols;
27         int colGap;
28         int charsPerNumber;
29         int charsPerAdditionalText;
30         int additionalTextGap;
31         char delimiter;
32         bool wrapX;
33         bool wrapY;
34
35         MenuProperties()
36         : font(0), disabledFont(0), cursor(0), selectedCursor(0)
37         , charsPerEntry(0), rows(1), rowGap(0)
38         , iconSpace(0), cols(1), colGap(0)
39         , charsPerNumber(0), charsPerAdditionalText(0)
40         , additionalTextGap(0), delimiter(':')
41         , wrapX(false), wrapY(false) { }
42
43         static void CreateTypeDescription();
44         static void Construct(void *);
45
46 };
47
48 template<class T>
49 class Menu
50 : private MenuProperties {
51
52 public:
53         Menu();
54         Menu(const MenuProperties &);
55
56 public:
57         void SetInactive() { state = STATE_INACTIVE; }
58         void SetActive() { state = STATE_ACTIVE; }
59         void SetSelected() { state = STATE_SELECTED; }
60         void SetDualSelection() { state = STATE_DUAL; secondarySelection = selected; }
61         bool IsActive() const { return state == STATE_ACTIVE; }
62         bool HasSelected() const { return state == STATE_SELECTED; }
63         bool InDualMode() const { return state == STATE_DUAL; }
64
65         int Width() const;
66         int Height() const;
67         int ColWidth() const;
68         int RowHeight() const { return font->CharHeight() + rowGap; }
69         int CharsPerEntry() const { return charsPerEntry; }
70
71         T &Selected() { return entries[selected].value; }
72         const T &Selected() const { return entries[selected].value; }
73         const char *SelectedTitle() const { return entries[selected].title; }
74         int SelectedNumber() const { return entries[selected].number; }
75         bool SelectedIsEnabled() const { return entries[selected].enabled; }
76
77         T &SecondarySelection() { return entries[secondarySelection].value; }
78         const T &SecondarySelection() const { return entries[secondarySelection].value; }
79         const char *SecondaryTitle() const { return entries[secondarySelection].title; }
80         int SecondaryNumber() const { return entries[secondarySelection].number; }
81         bool SecondaryIsEnabled() const { return entries[secondarySelection].enabled; }
82
83         void SwapSelected() { SwapEntriesAt(selected, secondarySelection); }
84         void SwapEntriesAt(int lhs, int rhs) { std::swap(entries[lhs], entries[rhs]); }
85
86         void NextItem();
87         void PreviousItem();
88         void NextRow();
89         void PreviousRow();
90         void SelectIndex(int index);
91         int SelectedIndex() const { return selected; }
92         int SecondaryIndex() const { return secondarySelection; }
93         bool IsSelected(int index) const { return index == selected; }
94
95         int EntryCount() const { return entries.size(); }
96         T &ValueAt(int index) { return entries[index].value; }
97         const T &ValueAt(int index) const { return entries[index].value; }
98
99         void Add(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0) { entries.push_back(Entry(title, value, enabled, icon, number, additionalText)); }
100         void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
101         void Disable(int index) { entries[index].enabled = false; }
102         void Enable(int index) { entries[index].enabled = true; }
103         void Reserve(int n) { entries.reserve(n); }
104         void Clear() { entries.clear(); }
105         void ClearEntry(int at) { entries[at] = Entry(0, T(), false); }
106
107         void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
108
109 private:
110         int GetRow(int index) const { return index / cols; }
111         int GetCol(int index) const { return index % cols; }
112
113 private:
114         struct Entry {
115                 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
116                 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
117                 const char *title;
118                 const char *additionalText;
119                 const Sprite *icon;
120                 int number;
121                 T value;
122                 bool enabled;
123         };
124         std::vector<Entry> entries;
125         int selected;
126         int secondarySelection;
127         int topRow;
128         enum State {
129                 STATE_INACTIVE,
130                 STATE_ACTIVE,
131                 STATE_SELECTED,
132                 STATE_DUAL,
133         };
134         State state;
135
136 };
137
138
139 template<class T>
140 Menu<T>::Menu()
141 : MenuProperties()
142 , selected(0)
143 , secondarySelection(0)
144 , topRow(0)
145 , state(STATE_ACTIVE) {
146
147 }
148
149 template<class T>
150 Menu<T>::Menu(const MenuProperties &p)
151 : MenuProperties(p)
152 , selected(0)
153 , secondarySelection(0)
154 , topRow(0)
155 , state(STATE_ACTIVE) {
156
157 }
158
159
160 template<class T>
161 int Menu<T>::ColWidth() const {
162         int width(iconSpace);
163         width += font->CharWidth() * (charsPerEntry + charsPerNumber);
164         if (charsPerNumber) {
165                 width += font->CharWidth();
166         }
167         if (charsPerAdditionalText) {
168                 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
169         }
170         return width;
171 }
172
173 template<class T>
174 int Menu<T>::Width() const {
175         return cols * ColWidth() + (cols - 1) * colGap;
176 }
177
178 template<class T>
179 int Menu<T>::Height() const {
180         return rows * font->CharHeight() + (rows - 1) * rowGap;
181 }
182
183
184 template<class T>
185 void Menu<T>::NextItem() {
186         int index(selected + 1);
187         if (wrapX && index % cols == 0) {
188                 index -= cols;
189         }
190         SelectIndex(index);
191 }
192
193 template<class T>
194 void Menu<T>::PreviousItem() {
195         int index(selected - 1);
196         if (wrapX && selected % cols == 0) {
197                 index += cols;
198         }
199         SelectIndex(index);
200 }
201
202 template<class T>
203 void Menu<T>::NextRow() {
204         int index(selected + cols);
205         if (wrapY && index >= int(entries.size())) {
206                 index -= entries.size();
207         }
208         SelectIndex(index);
209 }
210
211 template<class T>
212 void Menu<T>::PreviousRow() {
213         int index(selected - cols);
214         if (wrapY && index < 0) {
215                 index += entries.size();
216         }
217         SelectIndex(index);
218 }
219
220 template<class T>
221 void Menu<T>::SelectIndex(int index) {
222         if (index < 0 || int(entries.size()) <= index) return;
223         selected = index;
224         if (topRow <= GetRow(selected) - rows) {
225                 topRow = GetRow(selected) - rows + 1;
226         } else if (GetRow(selected) < topRow) {
227                 topRow = GetRow(selected);
228         }
229 }
230
231
232 template<class T>
233 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
234         int start(topRow * cols);
235         int slots(rows * cols);
236         int items(entries.size() - start);
237         int end(start + (items < slots ? items : slots));
238         for (int i(0), count(end - start); i < count; ++i) {
239                 if (!entries[start + i].title) continue;
240                 geometry::Vector<int> iconOffset(
241                                 (i % cols) * (ColWidth() + colGap),
242                                 (i / cols) * RowHeight());
243
244                 // Third column hack!
245                 // This fixes the position of the "DROP" item in the inventory menu.
246                 if (i % cols == 2) {
247                         iconOffset += geometry::Vector<int>(font->CharWidth(), 0);
248                 }
249
250                 if (entries[start + i].icon) {
251                         entries[start + i].icon->Draw(dest, position + iconOffset);
252                 }
253                 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
254                 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
255                 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
256
257                 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
258
259                 if (charsPerAdditionalText) {
260                         textOffset += geometry::Vector<int>(additionalTextGap, 0);
261                         if (entries[start + i].additionalText) {
262                                 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
263                         }
264                         textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
265                 }
266
267                 if (charsPerNumber) {
268                         usedFont->DrawChar(delimiter, dest, position + textOffset);
269                         textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
270                         usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
271                 }
272         }
273         geometry::Vector<int> cursorOffset(
274                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
275                         ((selected - start) / cols) * RowHeight());
276         // Third column hack!
277         // This fixes the position of the "DROP" item in the inventory menu.
278         if (selected % cols == 2) {
279                 cursorOffset += geometry::Vector<int>(font->CharWidth(), 0);
280         }
281         switch (state) {
282                 case STATE_INACTIVE:
283                         break;
284                 case STATE_ACTIVE:
285                         cursor->Draw(dest, position + cursorOffset);
286                         break;
287                 case STATE_SELECTED:
288                         selectedCursor->Draw(dest, position + cursorOffset);
289                         break;
290                 case STATE_DUAL:
291                         cursor->Draw(dest, position + cursorOffset
292                                         - geometry::Vector<int>(selectedCursor->Width(), 0));
293                         if (secondarySelection >= start && secondarySelection <= end) {
294                                 geometry::Vector<int> secondaryOffset(
295                                                 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
296                                                 ((secondarySelection - start) / cols) * RowHeight());
297                                 selectedCursor->Draw(dest, position + secondaryOffset);
298                         }
299                         break;
300         }
301 }
302
303 }
304
305 #endif /* GRAPHICS_MENU_H_ */