4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
12 #include "../geometry/operators.h"
13 #include "../geometry/Point.h"
14 #include "../geometry/Vector.h"
27 Menu(const Font *font, int charsPerEntry, int rows, int rowGap = 0, int cols = 1, int colGap = 0);
32 int ColWidth() const { return font->CharWidth() * charsPerEntry; }
33 int RowHeight() const { return font->CharHeight() + rowGap; }
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; }
43 void SelectIndex(int index);
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); }
48 void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
51 int GetRow(int index) const { return index / cols; }
52 int GetCol(int index) const { return index % cols; }
56 Entry(const char *title, const T &value, const Sprite *icon = 0)
57 : title(title), icon(icon), value(value) { }
63 std::vector<Entry> entries;
76 Menu<T>::Menu(const Font *font, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
78 , charsPerEntry(charsPerEntry)
90 int Menu<T>::Width() const {
91 return cols * ColWidth() + (cols - 1) * colGap;
95 int Menu<T>::Height() const {
96 return rows * font->CharHeight() + (rows - 1) * rowGap;
101 void Menu<T>::NextItem() {
102 SelectIndex(selected + 1);
106 void Menu<T>::PreviousItem() {
107 SelectIndex(selected - 1);
111 void Menu<T>::NextRow() {
112 SelectIndex(selected + cols);
116 void Menu<T>::PreviousRow() {
117 SelectIndex(selected - cols);
121 void Menu<T>::SelectIndex(int index) {
122 if (index < 0 || entries.size() < index) return;
124 if (GetRow(selected) - rows > topRow) {
125 topRow = GetRow(selected) - rows;
126 } else if (GetRow(selected) < topRow) {
127 topRow = GetRow(selected);
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);
146 #endif /* GRAPHICS_MENU_H_ */