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