]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
fixed flags in makefiles
[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
38         MenuProperties()
39         : font(0), disabledFont(0), cursor(0)
40         , charsPerEntry(0), rows(0), rowGap(0)
41         , iconSpace(0), cols(0), colGap(0)
42         , charsPerNumber(0), charsPerAdditionalText(0)
43         , additionalTextGap(0), delimiter(':') { }
44
45         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)
46         : 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) { }
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         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);
61
62 public:
63         int Width() const;
64         int Height() const;
65         int ColWidth() const;
66         int RowHeight() const { return font->CharHeight() + rowGap; }
67         int CharsPerEntry() const { return charsPerEntry; }
68
69         T &Selected() { return entries[selected].value; }
70         const T &Selected() const { return entries[selected].value; }
71         const char *SelectedTitle() const { return entries[selected].title; }
72         int SelectedNumber() const { return entries[selected].number; }
73         bool SelectedIsEnabled() const { return entries[selected].enabled; }
74
75         void NextItem();
76         void PreviousItem();
77         void NextRow();
78         void PreviousRow();
79         void SelectIndex(int index);
80         int SelectedIndex() const { return selected; }
81         bool IsSelected(int index) const { return index == selected; }
82
83         int EntryCount() const { return entries.size(); }
84         T &ValueAt(int index) { return entries[index].value; }
85         const T &ValueAt(int index) const { return entries[index].value; }
86
87         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)); }
88         void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
89         void Disable(int index) { entries[index].enabled = false; }
90         void Enable(int index) { entries[index].enabled = true; }
91         void Reserve(int n) { entries.reserve(n); }
92         void Clear() { entries.clear(); }
93
94         void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
95
96 private:
97         int GetRow(int index) const { return index / cols; }
98         int GetCol(int index) const { return index % cols; }
99
100 private:
101         struct Entry {
102                 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
103                 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
104                 const char *title;
105                 const char *additionalText;
106                 const Sprite *icon;
107                 int number;
108                 T value;
109                 bool enabled;
110         };
111         std::vector<Entry> entries;
112         int selected;
113         int topRow;
114
115 };
116
117
118 template<class T>
119 Menu<T>::Menu()
120 : MenuProperties()
121 , selected(0)
122 , topRow(0) {
123
124 }
125
126 template<class T>
127 Menu<T>::Menu(const MenuProperties &p)
128 : MenuProperties(p)
129 , selected(0)
130 , topRow(0) {
131
132 }
133
134 template<class T>
135 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)
136 : MenuProperties(
137                 font, disabledFont ? disabledFont : font,
138                 cursor, charsPerEntry,
139                 rows, rowGap, iconSpace,
140                 cols, colGap, charsPerNumber,
141                 delimiter,
142                 charsPerAdditionalText,
143                 additionalTextGap)
144 , selected(0)
145 , topRow(0) {
146
147 }
148
149
150 template<class T>
151 int Menu<T>::ColWidth() const {
152         int width(iconSpace);
153         width += font->CharWidth() * (charsPerEntry + charsPerNumber);
154         if (charsPerNumber) {
155                 width += font->CharWidth();
156         }
157         if (charsPerAdditionalText) {
158                 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
159         }
160         return width;
161 }
162
163 template<class T>
164 int Menu<T>::Width() const {
165         return cols * ColWidth() + (cols - 1) * colGap;
166 }
167
168 template<class T>
169 int Menu<T>::Height() const {
170         return rows * font->CharHeight() + (rows - 1) * rowGap;
171 }
172
173
174 template<class T>
175 void Menu<T>::NextItem() {
176         SelectIndex(selected + 1);
177 }
178
179 template<class T>
180 void Menu<T>::PreviousItem() {
181         SelectIndex(selected - 1);
182 }
183
184 template<class T>
185 void Menu<T>::NextRow() {
186         SelectIndex(selected + cols);
187 }
188
189 template<class T>
190 void Menu<T>::PreviousRow() {
191         SelectIndex(selected - cols);
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_ */