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