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