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"
23 // TODO: alternate font for disabled entries
24 // TODO: sprite for the cursor
25 // TODO: animation when top row changes
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);
35 int ColWidth() const { return font->CharWidth() * charsPerEntry; }
36 int RowHeight() const { return font->CharHeight() + rowGap; }
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; }
47 void SelectIndex(int index);
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; }
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); }
58 void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
61 int GetRow(int index) const { return index / cols; }
62 int GetCol(int index) const { return index % cols; }
66 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0)
67 : title(title), icon(icon), value(value), enabled(enabled) { }
74 const Font *disabledFont;
75 std::vector<Entry> entries;
88 Menu<T>::Menu(const Font *font, const Font *disabledFont, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
90 , disabledFont(disabledFont ? disabledFont : font)
91 , charsPerEntry(charsPerEntry)
103 int Menu<T>::Width() const {
104 return cols * ColWidth() + (cols - 1) * colGap;
108 int Menu<T>::Height() const {
109 return rows * font->CharHeight() + (rows - 1) * rowGap;
114 void Menu<T>::NextItem() {
115 SelectIndex(selected + 1);
119 void Menu<T>::PreviousItem() {
120 SelectIndex(selected - 1);
124 void Menu<T>::NextRow() {
125 SelectIndex(selected + cols);
129 void Menu<T>::PreviousRow() {
130 SelectIndex(selected - cols);
134 void Menu<T>::SelectIndex(int index) {
135 if (index < 0 || entries.size() < index) return;
137 if (GetRow(selected) - rows > topRow) {
138 topRow = GetRow(selected) - rows;
139 } else if (GetRow(selected) < topRow) {
140 topRow = GetRow(selected);
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);
156 disabledFont->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
163 #endif /* GRAPHICS_MENU_H_ */