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