]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
added 'dual' menu state
[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
113         void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
114
115 private:
116         int GetRow(int index) const { return index / cols; }
117         int GetCol(int index) const { return index % cols; }
118
119 private:
120         struct Entry {
121                 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
122                 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
123                 const char *title;
124                 const char *additionalText;
125                 const Sprite *icon;
126                 int number;
127                 T value;
128                 bool enabled;
129         };
130         std::vector<Entry> entries;
131         int selected;
132         int secondarySelection;
133         int topRow;
134         enum State {
135                 STATE_INACTIVE,
136                 STATE_ACTIVE,
137                 STATE_SELECTED,
138                 STATE_DUAL,
139         };
140         State state;
141
142 };
143
144
145 template<class T>
146 Menu<T>::Menu()
147 : MenuProperties()
148 , selected(0)
149 , secondarySelection(0)
150 , topRow(0)
151 , state(STATE_ACTIVE) {
152
153 }
154
155 template<class T>
156 Menu<T>::Menu(const MenuProperties &p)
157 : MenuProperties(p)
158 , selected(0)
159 , secondarySelection(0)
160 , topRow(0)
161 , state(STATE_ACTIVE) {
162
163 }
164
165
166 template<class T>
167 int Menu<T>::ColWidth() const {
168         int width(iconSpace);
169         width += font->CharWidth() * (charsPerEntry + charsPerNumber);
170         if (charsPerNumber) {
171                 width += font->CharWidth();
172         }
173         if (charsPerAdditionalText) {
174                 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
175         }
176         return width;
177 }
178
179 template<class T>
180 int Menu<T>::Width() const {
181         return cols * ColWidth() + (cols - 1) * colGap;
182 }
183
184 template<class T>
185 int Menu<T>::Height() const {
186         return rows * font->CharHeight() + (rows - 1) * rowGap;
187 }
188
189
190 template<class T>
191 void Menu<T>::NextItem() {
192         int index(selected + 1);
193         if (wrapX && index % cols == 0) {
194                 index -= cols;
195         }
196         SelectIndex(index);
197 }
198
199 template<class T>
200 void Menu<T>::PreviousItem() {
201         int index(selected - 1);
202         if (wrapX && selected % cols == 0) {
203                 index += cols;
204         }
205         SelectIndex(index);
206 }
207
208 template<class T>
209 void Menu<T>::NextRow() {
210         int index(selected + cols);
211         if (wrapY && index >= int(entries.size())) {
212                 index -= entries.size();
213         }
214         SelectIndex(index);
215 }
216
217 template<class T>
218 void Menu<T>::PreviousRow() {
219         int index(selected - cols);
220         if (wrapY && index < 0) {
221                 index += entries.size();
222         }
223         SelectIndex(index);
224 }
225
226 template<class T>
227 void Menu<T>::SelectIndex(int index) {
228         if (index < 0 || int(entries.size()) <= index) return;
229         selected = index;
230         if (topRow <= GetRow(selected) - rows) {
231                 topRow = GetRow(selected) - rows + 1;
232         } else if (GetRow(selected) < topRow) {
233                 topRow = GetRow(selected);
234         }
235 }
236
237
238 template<class T>
239 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
240         int start(topRow * cols);
241         int slots(rows * cols);
242         int items(entries.size() - start);
243         int end(start + (items < slots ? items : slots));
244         for (int i(0), count(end - start); i < count; ++i) {
245                 if (!entries[start + i].title) continue;
246                 geometry::Vector<int> iconOffset(
247                                 (i % cols) * (ColWidth() + colGap),
248                                 (i / cols) * RowHeight());
249
250                 // Third column hack!
251                 // This fixes the position of the "DROP" item in the inventory menu.
252                 if (i % cols == 2) {
253                         iconOffset += geometry::Vector<int>(font->CharWidth(), 0);
254                 }
255
256                 if (entries[start + i].icon) {
257                         entries[start + i].icon->Draw(dest, position + iconOffset);
258                 }
259                 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
260                 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
261                 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
262
263                 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
264
265                 if (charsPerAdditionalText) {
266                         textOffset += geometry::Vector<int>(additionalTextGap, 0);
267                         if (entries[start + i].additionalText) {
268                                 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
269                         }
270                         textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
271                 }
272
273                 if (charsPerNumber) {
274                         usedFont->DrawChar(delimiter, dest, position + textOffset);
275                         textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
276                         usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
277                 }
278         }
279         geometry::Vector<int> cursorOffset(
280                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
281                         ((selected - start) / cols) * RowHeight());
282         switch (state) {
283                 case STATE_INACTIVE:
284                         break;
285                 case STATE_ACTIVE:
286                         cursor->Draw(dest, position + cursorOffset);
287                         break;
288                 case STATE_SELECTED:
289                         selectedCursor->Draw(dest, position + cursorOffset);
290                         break;
291                 case STATE_DUAL:
292                         cursor->Draw(dest, position + cursorOffset
293                                         - geometry::Vector<int>(selectedCursor->Width(), 0));
294                         if (secondarySelection >= start && secondarySelection <= end) {
295                                 geometry::Vector<int> secondaryOffset(
296                                                 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
297                                                 ((secondarySelection - start) / cols) * RowHeight());
298                                 selectedCursor->Draw(dest, position + secondaryOffset);
299                         }
300                         break;
301         }
302 }
303
304 }
305
306 #endif /* GRAPHICS_MENU_H_ */