]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
added ability to disable menu entries
[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 "../geometry/operators.h"
13 #include "../geometry/Point.h"
14 #include "../geometry/Vector.h"
15
16 #include <vector>
17 #include <SDL.h>
18
19 namespace graphics {
20
21 class Sprite;
22
23 // TODO: alternate font for disabled entries
24 // TODO: sprite for the cursor
25 // TODO: animation when top row changes
26 template<class T>
27 class Menu {
28
29 public:
30         explicit Menu(const Font *font = NULL, int charsPerEntry = 16, int rows = 1, 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
49         int EntryCount() const { return entries.size(); }
50         T &ValueAt(int index) { return entries[index].value; }
51         const T &ValueAt(int index) const { return entries[index].value; }
52
53         void Add(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0) { entries.push_back(Entry(title, value, enabled, icon)); }
54         void Disable(int index) { entries[index].enabled = false; }
55         void Enable(int index) { entries[index].enabled = true; }
56         void Reserve(int n) { entries.reserve(n); }
57
58         void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
59
60 private:
61         int GetRow(int index) const { return index / cols; }
62         int GetCol(int index) const { return index % cols; }
63
64 private:
65         struct Entry {
66                 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0)
67                 : title(title), icon(icon), value(value), enabled(enabled) { }
68                 const char *title;
69                 const Sprite *icon;
70                 T value;
71                 bool enabled;
72         };
73         const Font *font;
74         std::vector<Entry> entries;
75         int charsPerEntry;
76         int rows;
77         int rowGap;
78         int cols;
79         int colGap;
80         int selected;
81         int topRow;
82
83 };
84
85
86 template<class T>
87 Menu<T>::Menu(const Font *font, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
88 : font(font)
89 , charsPerEntry(charsPerEntry)
90 , rows(rows)
91 , rowGap(rowGap)
92 , cols(cols)
93 , colGap(colGap)
94 , selected(0)
95 , topRow(0) {
96
97 }
98
99
100 template<class T>
101 int Menu<T>::Width() const {
102         return cols * ColWidth() + (cols - 1) * colGap;
103 }
104
105 template<class T>
106 int Menu<T>::Height() const {
107         return rows * font->CharHeight() + (rows - 1) * rowGap;
108 }
109
110
111 template<class T>
112 void Menu<T>::NextItem() {
113         SelectIndex(selected + 1);
114 }
115
116 template<class T>
117 void Menu<T>::PreviousItem() {
118         SelectIndex(selected - 1);
119 }
120
121 template<class T>
122 void Menu<T>::NextRow() {
123         SelectIndex(selected + cols);
124 }
125
126 template<class T>
127 void Menu<T>::PreviousRow() {
128         SelectIndex(selected - cols);
129 }
130
131 template<class T>
132 void Menu<T>::SelectIndex(int index) {
133         if (index < 0 || entries.size() < index) return;
134         selected = index;
135         if (GetRow(selected) - rows > topRow) {
136                 topRow = GetRow(selected) - rows;
137         } else if (GetRow(selected) < topRow) {
138                 topRow = GetRow(selected);
139         }
140 }
141
142
143 template<class T>
144 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
145         int start(topRow * cols);
146         int slots((topRow + rows) * cols);
147         int items(entries.size() - start);
148         int end(items < slots ? items : slots);
149         for (int i(0), count(end - start); i < count; ++i) {
150                 geometry::Vector<int> offset((i % cols) * (ColWidth() + colGap), (i / cols) * RowHeight());
151                 font->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
152         }
153 }
154
155 }
156
157 #endif /* GRAPHICS_MENU_H_ */