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