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