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