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