]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
added icons for item select
[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);
31
32 public:
33         int Width() const;
34         int Height() const;
35         int ColWidth() const { return iconSpace + font->CharWidth() * charsPerEntry; }
36         int RowHeight() const { return font->CharHeight() + rowGap; }
37
38         T &Selected() { return entries[selected].value; }
39         const T &Selected() const { return entries[selected].value; }
40         const char *SelectedTitle() const { return entries[selected].title; }
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) { entries.push_back(Entry(title, value, enabled, icon)); }
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
60         void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
61
62 private:
63         int GetRow(int index) const { return index / cols; }
64         int GetCol(int index) const { return index % cols; }
65
66 private:
67         struct Entry {
68                 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0)
69                 : title(title), icon(icon), value(value), enabled(enabled) { }
70                 const char *title;
71                 const Sprite *icon;
72                 T value;
73                 bool enabled;
74         };
75         const Font *font;
76         const Font *disabledFont;
77         const Sprite *cursor;
78         std::vector<Entry> entries;
79         int charsPerEntry;
80         int rows;
81         int rowGap;
82         int iconSpace;
83         int cols;
84         int colGap;
85         int selected;
86         int topRow;
87
88 };
89
90
91 template<class T>
92 Menu<T>::Menu()
93 : font(0)
94 , disabledFont(0)
95 , cursor(0)
96 , charsPerEntry(0)
97 , rows(0)
98 , rowGap(0)
99 , iconSpace(0)
100 , cols(0)
101 , colGap(0)
102 , selected(0)
103 , topRow(0) {
104
105 }
106
107 template<class T>
108 Menu<T>::Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int iconSpace, int cols, int colGap)
109 : font(font)
110 , disabledFont(disabledFont ? disabledFont : font)
111 , cursor(cursor)
112 , charsPerEntry(charsPerEntry)
113 , rows(rows)
114 , rowGap(rowGap)
115 , iconSpace(iconSpace)
116 , cols(cols)
117 , colGap(colGap)
118 , selected(0)
119 , topRow(0) {
120
121 }
122
123
124 template<class T>
125 int Menu<T>::Width() const {
126         return cols * ColWidth() + (cols - 1) * colGap;
127 }
128
129 template<class T>
130 int Menu<T>::Height() const {
131         return rows * font->CharHeight() + (rows - 1) * rowGap;
132 }
133
134
135 template<class T>
136 void Menu<T>::NextItem() {
137         SelectIndex(selected + 1);
138 }
139
140 template<class T>
141 void Menu<T>::PreviousItem() {
142         SelectIndex(selected - 1);
143 }
144
145 template<class T>
146 void Menu<T>::NextRow() {
147         SelectIndex(selected + cols);
148 }
149
150 template<class T>
151 void Menu<T>::PreviousRow() {
152         SelectIndex(selected - cols);
153 }
154
155 template<class T>
156 void Menu<T>::SelectIndex(int index) {
157         if (index < 0 || int(entries.size()) <= index) return;
158         selected = index;
159         if (topRow <= GetRow(selected) - rows) {
160                 topRow = GetRow(selected) - rows + 1;
161         } else if (GetRow(selected) < topRow) {
162                 topRow = GetRow(selected);
163         }
164 }
165
166
167 template<class T>
168 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
169         int start(topRow * cols);
170         int slots(rows * cols);
171         int items(entries.size() - start);
172         int end(start + (items < slots ? items : slots));
173         for (int i(0), count(end - start); i < count; ++i) {
174                 geometry::Vector<int> iconOffset(
175                                 (i % cols) * (ColWidth() + colGap),
176                                 (i / cols) * RowHeight());
177                 if (entries[start + i].icon) {
178                         entries[start + i].icon->Draw(dest, position + iconOffset);
179                 }
180                 geometry::Vector<int> labelOffset(iconOffset.X() +  + iconSpace, iconOffset.Y());
181                 if (entries[start + i].enabled) {
182                         font->DrawString(entries[start + i].title, dest, position + labelOffset, charsPerEntry);
183                 } else {
184                         disabledFont->DrawString(entries[start + i].title, dest, position + labelOffset, charsPerEntry);
185                 }
186         }
187         geometry::Vector<int> cursorOffset(
188                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
189                         ((selected - start) / cols) * RowHeight());
190         cursor->Draw(dest, position + cursorOffset);
191 }
192
193 }
194
195 #endif /* GRAPHICS_MENU_H_ */