]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
ac986466c8be2408b87542d54b837c00f2294dc1
[l2e.git] / src / graphics / Menu.h
1 #ifndef GRAPHICS_MENU_H_
2 #define GRAPHICS_MENU_H_
3
4 #include "Font.h"
5 #include "Sprite.h"
6 #include "../math/Vector.h"
7
8 #include <algorithm>
9 #include <vector>
10 #include <SDL.h>
11
12 namespace graphics {
13
14 struct MenuProperties {
15         static const int TYPE_ID = 407;
16
17         const Font *font;
18         const Font *disabledFont;
19         const Sprite *cursor;
20         const Sprite *selectedCursor;
21         int charsPerEntry;
22         int rows;
23         int rowGap;
24         int iconSpace;
25         int cols;
26         int colGap;
27         int charsPerNumber;
28         int charsPerAdditionalText;
29         int additionalTextGap;
30         int thirdColumnHack;
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), thirdColumnHack(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 math::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 math::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                 math::Vector<int> iconOffset(
241                                 (i % cols) * (ColWidth() + colGap),
242                                 (i / cols) * RowHeight());
243
244                 // This fixes the position of the third column of the inventory and capsule menus.
245                 if (thirdColumnHack && i % cols == 2) {
246                         iconOffset += math::Vector<int>(font->CharWidth() * thirdColumnHack, 0);
247                 }
248
249                 if (entries[start + i].icon) {
250                         entries[start + i].icon->Draw(dest, position + iconOffset);
251                 }
252                 math::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
253                 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
254                 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
255
256                 textOffset += math::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
257
258                 if (charsPerAdditionalText) {
259                         textOffset += math::Vector<int>(additionalTextGap, 0);
260                         if (entries[start + i].additionalText) {
261                                 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
262                         }
263                         textOffset += math::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
264                 }
265
266                 if (charsPerNumber) {
267                         usedFont->DrawChar(delimiter, dest, position + textOffset);
268                         textOffset += math::Vector<int>(usedFont->CharWidth(), 0);
269                         usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
270                 }
271         }
272         math::Vector<int> cursorOffset(
273                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
274                         ((selected - start) / cols) * RowHeight());
275         // This fixes the position of the third column of the inventory and capsule menus.
276         if (thirdColumnHack && selected % cols == 2) {
277                 cursorOffset += math::Vector<int>(font->CharWidth() * thirdColumnHack, 0);
278         }
279         switch (state) {
280                 case STATE_INACTIVE:
281                         break;
282                 case STATE_ACTIVE:
283                         cursor->Draw(dest, position + cursorOffset);
284                         break;
285                 case STATE_SELECTED:
286                         selectedCursor->Draw(dest, position + cursorOffset);
287                         break;
288                 case STATE_DUAL:
289                         cursor->Draw(dest, position + cursorOffset
290                                         - math::Vector<int>(selectedCursor->Width(), 0));
291                         if (secondarySelection >= start && secondarySelection <= end) {
292                                 math::Vector<int> secondaryOffset(
293                                                 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
294                                                 ((secondarySelection - start) / cols) * RowHeight());
295                                 selectedCursor->Draw(dest, position + secondaryOffset);
296                         }
297                         break;
298         }
299 }
300
301 }
302
303 #endif /* GRAPHICS_MENU_H_ */