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