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