]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Menu.h
added rough implementation of a Menu
[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 template<class T>
24 class Menu {
25
26 public:
27         Menu(const Font *font, int charsPerEntry, int rows, int rowGap = 0, int cols = 1, int colGap = 0);
28
29 public:
30         int Width() const;
31         int Height() const;
32         int ColWidth() const { return font->CharWidth() * charsPerEntry; }
33         int RowHeight() const { return font->CharHeight() + rowGap; }
34
35         T &Selected() { return entries[selected].value; }
36         const T &Selected() const { return entries[selected].value; }
37         const char *SelectedTitle() const { return entries[selected].title; }
38
39         void NextItem();
40         void PreviousItem();
41         void NextRow();
42         void PreviousRow();
43         void SelectIndex(int index);
44
45         void Add(const char *title, const T &value, const Sprite *icon = 0) { entries.push_back(Entry(title, value, icon)); }
46         void Reserve(int n) { entries.reserve(n); }
47
48         void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
49
50 private:
51         int GetRow(int index) const { return index / cols; }
52         int GetCol(int index) const { return index % cols; }
53
54 private:
55         struct Entry {
56                 Entry(const char *title, const T &value, const Sprite *icon = 0)
57                 : title(title), icon(icon), value(value) { }
58                 const char *title;
59                 const Sprite *icon;
60                 T value;
61         };
62         const Font *font;
63         std::vector<Entry> entries;
64         int charsPerEntry;
65         int rows;
66         int rowGap;
67         int cols;
68         int colGap;
69         int selected;
70         int topRow;
71
72 };
73
74
75 template<class T>
76 Menu<T>::Menu(const Font *font, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
77 : font(font)
78 , charsPerEntry(charsPerEntry)
79 , rows(rows)
80 , rowGap(rowGap)
81 , cols(cols)
82 , colGap(colGap)
83 , selected(0)
84 , topRow(0) {
85
86 }
87
88
89 template<class T>
90 int Menu<T>::Width() const {
91         return cols * ColWidth() + (cols - 1) * colGap;
92 }
93
94 template<class T>
95 int Menu<T>::Height() const {
96         return rows * font->CharHeight() + (rows - 1) * rowGap;
97 }
98
99
100 template<class T>
101 void Menu<T>::NextItem() {
102         SelectIndex(selected + 1);
103 }
104
105 template<class T>
106 void Menu<T>::PreviousItem() {
107         SelectIndex(selected - 1);
108 }
109
110 template<class T>
111 void Menu<T>::NextRow() {
112         SelectIndex(selected + cols);
113 }
114
115 template<class T>
116 void Menu<T>::PreviousRow() {
117         SelectIndex(selected - cols);
118 }
119
120 template<class T>
121 void Menu<T>::SelectIndex(int index) {
122         if (index < 0 || entries.size() < index) return;
123         selected = index;
124         if (GetRow(selected) - rows > topRow) {
125                 topRow = GetRow(selected) - rows;
126         } else if (GetRow(selected) < topRow) {
127                 topRow = GetRow(selected);
128         }
129 }
130
131
132 template<class T>
133 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
134         int start(topRow * cols);
135         int slots((topRow + rows) * cols);
136         int items(entries.size() - start);
137         int end(items < slots ? items : slots);
138         for (int i(0), count(end - start); i < count; ++i) {
139                 geometry::Vector<int> offset((i % cols) * (ColWidth() + colGap), (i / cols) * RowHeight());
140                 font->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
141         }
142 }
143
144 }
145
146 #endif /* GRAPHICS_MENU_H_ */