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