]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
e9627389e958323b7012f83e5e639df67640c23a
[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 "Sprite.h"
13 #include "../geometry/Vector.h"
14
15 #include <vector>
16 #include <SDL.h>
17
18 namespace graphics {
19
20 class Sprite;
21
22 struct MenuProperties {
23         const Font *font;
24         const Font *disabledFont;
25         const Sprite *cursor;
26         int charsPerEntry;
27         int rows;
28         int rowGap;
29         int iconSpace;
30         int cols;
31         int colGap;
32         int charsPerNumber;
33         int charsPerAdditionalText;
34         int additionalTextGap;
35         char delimiter;
36
37         MenuProperties()
38         : font(0), disabledFont(0), cursor(0)
39         , charsPerEntry(0), rows(0), rowGap(0)
40         , iconSpace(0), cols(0), colGap(0)
41         , charsPerNumber(0), charsPerAdditionalText(0)
42         , additionalTextGap(0), delimiter(':') { }
43
44         MenuProperties(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int iconSpace, int cols, int colGap, int charsPerNumber, char delimiter, int charsPerAdditionalText, int additionalTextGap)
45         : font(font), disabledFont(disabledFont), cursor(cursor), charsPerEntry(charsPerEntry), rows(rows), rowGap(rowGap), iconSpace(iconSpace), cols(cols), colGap(colGap), charsPerNumber(charsPerNumber), charsPerAdditionalText(charsPerAdditionalText), additionalTextGap(additionalTextGap), delimiter(delimiter) { }
46
47         static void CreateTypeDescription();
48         static void Construct(void *);
49
50 };
51
52 template<class T>
53 class Menu
54 : private MenuProperties {
55
56 public:
57         Menu();
58         Menu(const MenuProperties &);
59         Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap = 0, int iconSpace = 0, int cols = 1, int colGap = 0, int charsPerNumber = 0, char delimiter = ':', int charsPerAdditionalText = 0, int additionalTextGap = 0);
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 template<class T>
134 Menu<T>::Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int iconSpace, int cols, int colGap, int charsPerNumber, char delimiter, int charsPerAdditionalText, int additionalTextGap)
135 : MenuProperties(
136                 font, disabledFont ? disabledFont : font,
137                 cursor, charsPerEntry,
138                 rows, rowGap, iconSpace,
139                 cols, colGap, charsPerNumber,
140                 delimiter,
141                 charsPerAdditionalText,
142                 additionalTextGap)
143 , selected(0)
144 , topRow(0) {
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         SelectIndex(selected + 1);
176 }
177
178 template<class T>
179 void Menu<T>::PreviousItem() {
180         SelectIndex(selected - 1);
181 }
182
183 template<class T>
184 void Menu<T>::NextRow() {
185         SelectIndex(selected + cols);
186 }
187
188 template<class T>
189 void Menu<T>::PreviousRow() {
190         SelectIndex(selected - cols);
191 }
192
193 template<class T>
194 void Menu<T>::SelectIndex(int index) {
195         if (index < 0 || int(entries.size()) <= index) return;
196         selected = index;
197         if (topRow <= GetRow(selected) - rows) {
198                 topRow = GetRow(selected) - rows + 1;
199         } else if (GetRow(selected) < topRow) {
200                 topRow = GetRow(selected);
201         }
202 }
203
204
205 template<class T>
206 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
207         int start(topRow * cols);
208         int slots(rows * cols);
209         int items(entries.size() - start);
210         int end(start + (items < slots ? items : slots));
211         for (int i(0), count(end - start); i < count; ++i) {
212                 if (!entries[start + i].title) continue;
213                 geometry::Vector<int> iconOffset(
214                                 (i % cols) * (ColWidth() + colGap),
215                                 (i / cols) * RowHeight());
216                 if (entries[start + i].icon) {
217                         entries[start + i].icon->Draw(dest, position + iconOffset);
218                 }
219                 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
220                 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
221                 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
222
223                 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
224
225                 if (charsPerAdditionalText) {
226                         textOffset += geometry::Vector<int>(additionalTextGap, 0);
227                         if (entries[start + i].additionalText) {
228                                 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
229                         }
230                         textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
231                 }
232
233                 if (charsPerNumber) {
234                         usedFont->DrawChar(delimiter, dest, position + textOffset);
235                         textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
236                         usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
237                 }
238         }
239         geometry::Vector<int> cursorOffset(
240                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
241                         ((selected - start) / cols) * RowHeight());
242         cursor->Draw(dest, position + cursorOffset);
243 }
244
245 }
246
247 #endif /* GRAPHICS_MENU_H_ */