]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
reworked Menu to include an optional number
[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 = ':');
31
32 public:
33         int Width() const;
34         int Height() const;
35         int ColWidth() const { return iconSpace + font->CharWidth() * (charsPerEntry + charsPerNumber) + (charsPerNumber ? font->CharWidth() : 0); }
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) { entries.push_back(Entry(title, value, enabled, icon, number)); }
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, 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)
73                 : title(title), icon(icon), number(number), value(value), enabled(enabled) { }
74                 const char *title;
75                 const Sprite *icon;
76                 int number;
77                 T value;
78                 bool enabled;
79         };
80         const Font *font;
81         const Font *disabledFont;
82         const Sprite *cursor;
83         std::vector<Entry> entries;
84         int charsPerEntry;
85         int rows;
86         int rowGap;
87         int iconSpace;
88         int cols;
89         int colGap;
90         int selected;
91         int topRow;
92         int charsPerNumber;
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 , delimiter(':') {
113
114 }
115
116 template<class T>
117 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)
118 : font(font)
119 , disabledFont(disabledFont ? disabledFont : font)
120 , cursor(cursor)
121 , charsPerEntry(charsPerEntry)
122 , rows(rows)
123 , rowGap(rowGap)
124 , iconSpace(iconSpace)
125 , cols(cols)
126 , colGap(colGap)
127 , selected(0)
128 , topRow(0)
129 , charsPerNumber(charsPerNumber)
130 , delimiter(delimiter) {
131
132 }
133
134
135 template<class T>
136 int Menu<T>::Width() const {
137         return cols * ColWidth() + (cols - 1) * colGap;
138 }
139
140 template<class T>
141 int Menu<T>::Height() const {
142         return rows * font->CharHeight() + (rows - 1) * rowGap;
143 }
144
145
146 template<class T>
147 void Menu<T>::NextItem() {
148         SelectIndex(selected + 1);
149 }
150
151 template<class T>
152 void Menu<T>::PreviousItem() {
153         SelectIndex(selected - 1);
154 }
155
156 template<class T>
157 void Menu<T>::NextRow() {
158         SelectIndex(selected + cols);
159 }
160
161 template<class T>
162 void Menu<T>::PreviousRow() {
163         SelectIndex(selected - cols);
164 }
165
166 template<class T>
167 void Menu<T>::SelectIndex(int index) {
168         if (index < 0 || int(entries.size()) <= index) return;
169         selected = index;
170         if (topRow <= GetRow(selected) - rows) {
171                 topRow = GetRow(selected) - rows + 1;
172         } else if (GetRow(selected) < topRow) {
173                 topRow = GetRow(selected);
174         }
175 }
176
177
178 template<class T>
179 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
180         int start(topRow * cols);
181         int slots(rows * cols);
182         int items(entries.size() - start);
183         int end(start + (items < slots ? items : slots));
184         for (int i(0), count(end - start); i < count; ++i) {
185                 if (!entries[start + i].title) continue;
186                 geometry::Vector<int> iconOffset(
187                                 (i % cols) * (ColWidth() + colGap),
188                                 (i / cols) * RowHeight());
189                 if (entries[start + i].icon) {
190                         entries[start + i].icon->Draw(dest, position + iconOffset);
191                 }
192                 geometry::Vector<int> labelOffset(iconOffset.X() + iconSpace, iconOffset.Y());
193                 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
194                 usedFont->DrawString(entries[start + i].title, dest, position + labelOffset, charsPerEntry);
195
196                 if (charsPerNumber) {
197                         geometry::Vector<int> delimiterOffset(labelOffset.X() + charsPerEntry * usedFont->CharWidth(), labelOffset.Y());
198                         usedFont->DrawChar(delimiter, dest, position + delimiterOffset);
199                         geometry::Vector<int> numberOffset(delimiterOffset.X() + usedFont->CharWidth(), delimiterOffset.Y());
200                         usedFont->DrawNumber(entries[start + i].number, dest, position + numberOffset);
201                 }
202         }
203         geometry::Vector<int> cursorOffset(
204                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
205                         ((selected - start) / cols) * RowHeight());
206         cursor->Draw(dest, position + cursorOffset);
207 }
208
209 }
210
211 #endif /* GRAPHICS_MENU_H_ */