]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
decd99dac3bc423d23ac1717a8020a0ddc9d1764
[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 cols = 1, int colGap = 0);
31
32 public:
33         int Width() const;
34         int Height() const;
35         int ColWidth() const { return 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 cols;
83         int colGap;
84         int selected;
85         int topRow;
86
87 };
88
89
90 template<class T>
91 Menu<T>::Menu()
92 : font(0)
93 , disabledFont(0)
94 , cursor(0)
95 , charsPerEntry(0)
96 , rows(0)
97 , rowGap(0)
98 , cols(0)
99 , colGap(0)
100 , selected(0)
101 , topRow(0) {
102
103 }
104
105 template<class T>
106 Menu<T>::Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
107 : font(font)
108 , disabledFont(disabledFont ? disabledFont : font)
109 , cursor(cursor)
110 , charsPerEntry(charsPerEntry)
111 , rows(rows)
112 , rowGap(rowGap)
113 , cols(cols)
114 , colGap(colGap)
115 , selected(0)
116 , topRow(0) {
117
118 }
119
120
121 template<class T>
122 int Menu<T>::Width() const {
123         return cols * ColWidth() + (cols - 1) * colGap;
124 }
125
126 template<class T>
127 int Menu<T>::Height() const {
128         return rows * font->CharHeight() + (rows - 1) * rowGap;
129 }
130
131
132 template<class T>
133 void Menu<T>::NextItem() {
134         SelectIndex(selected + 1);
135 }
136
137 template<class T>
138 void Menu<T>::PreviousItem() {
139         SelectIndex(selected - 1);
140 }
141
142 template<class T>
143 void Menu<T>::NextRow() {
144         SelectIndex(selected + cols);
145 }
146
147 template<class T>
148 void Menu<T>::PreviousRow() {
149         SelectIndex(selected - cols);
150 }
151
152 template<class T>
153 void Menu<T>::SelectIndex(int index) {
154         if (index < 0 || int(entries.size()) <= index) return;
155         selected = index;
156         if (topRow <= GetRow(selected) - rows) {
157                 topRow = GetRow(selected) - rows + 1;
158         } else if (GetRow(selected) < topRow) {
159                 topRow = GetRow(selected);
160         }
161 }
162
163
164 template<class T>
165 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
166         int start(topRow * cols);
167         int slots(rows * cols);
168         int items(entries.size() - start);
169         int end(start + (items < slots ? items : slots));
170         for (int i(0), count(end - start); i < count; ++i) {
171                 geometry::Vector<int> offset(
172                                 (i % cols) * (ColWidth() + colGap),
173                                 (i / cols) * RowHeight());
174                 if (entries[start + i].enabled) {
175                         font->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
176                 } else {
177                         disabledFont->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
178                 }
179         }
180         geometry::Vector<int> cursorOffset(
181                         (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
182                         ((selected - start) / cols) * RowHeight());
183         cursor->Draw(dest, position + cursorOffset);
184 }
185
186 }
187
188 #endif /* GRAPHICS_MENU_H_ */