]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
added a "disabled" font
[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, const Font *disabledFont = 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         const Font *disabledFont;
75         std::vector<Entry> entries;
76         int charsPerEntry;
77         int rows;
78         int rowGap;
79         int cols;
80         int colGap;
81         int selected;
82         int topRow;
83
84 };
85
86
87 template<class T>
88 Menu<T>::Menu(const Font *font, const Font *disabledFont, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
89 : font(font)
90 , disabledFont(disabledFont ? disabledFont : font)
91 , charsPerEntry(charsPerEntry)
92 , rows(rows)
93 , rowGap(rowGap)
94 , cols(cols)
95 , colGap(colGap)
96 , selected(0)
97 , topRow(0) {
98
99 }
100
101
102 template<class T>
103 int Menu<T>::Width() const {
104         return cols * ColWidth() + (cols - 1) * colGap;
105 }
106
107 template<class T>
108 int Menu<T>::Height() const {
109         return rows * font->CharHeight() + (rows - 1) * rowGap;
110 }
111
112
113 template<class T>
114 void Menu<T>::NextItem() {
115         SelectIndex(selected + 1);
116 }
117
118 template<class T>
119 void Menu<T>::PreviousItem() {
120         SelectIndex(selected - 1);
121 }
122
123 template<class T>
124 void Menu<T>::NextRow() {
125         SelectIndex(selected + cols);
126 }
127
128 template<class T>
129 void Menu<T>::PreviousRow() {
130         SelectIndex(selected - cols);
131 }
132
133 template<class T>
134 void Menu<T>::SelectIndex(int index) {
135         if (index < 0 || entries.size() < index) return;
136         selected = index;
137         if (GetRow(selected) - rows > topRow) {
138                 topRow = GetRow(selected) - rows;
139         } else if (GetRow(selected) < topRow) {
140                 topRow = GetRow(selected);
141         }
142 }
143
144
145 template<class T>
146 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
147         int start(topRow * cols);
148         int slots((topRow + rows) * cols);
149         int items(entries.size() - start);
150         int end(items < slots ? items : slots);
151         for (int i(0), count(end - start); i < count; ++i) {
152                 geometry::Vector<int> offset((i % cols) * (ColWidth() + colGap), (i / cols) * RowHeight());
153                 if (entries[start + i].enabled) {
154                         font->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
155                 } else {
156                         disabledFont->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
157                 }
158         }
159 }
160
161 }
162
163 #endif /* GRAPHICS_MENU_H_ */