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