4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
13 #include "../geometry/operators.h"
14 #include "../geometry/Point.h"
15 #include "../geometry/Vector.h"
24 // TODO: animation when top row changes
30 Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, 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);
48 int SelectedIndex() const { return selected; }
49 bool IsSelected(int index) const { return index == selected; }
51 int EntryCount() const { return entries.size(); }
52 T &ValueAt(int index) { return entries[index].value; }
53 const T &ValueAt(int index) const { return entries[index].value; }
55 void Add(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0) { entries.push_back(Entry(title, value, enabled, icon)); }
56 void Disable(int index) { entries[index].enabled = false; }
57 void Enable(int index) { entries[index].enabled = true; }
58 void Reserve(int n) { entries.reserve(n); }
60 void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
63 int GetRow(int index) const { return index / cols; }
64 int GetCol(int index) const { return index % cols; }
68 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0)
69 : title(title), icon(icon), value(value), enabled(enabled) { }
76 const Font *disabledFont;
78 std::vector<Entry> entries;
106 Menu<T>::Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
108 , disabledFont(disabledFont ? disabledFont : font)
110 , charsPerEntry(charsPerEntry)
122 int Menu<T>::Width() const {
123 return cols * ColWidth() + (cols - 1) * colGap;
127 int Menu<T>::Height() const {
128 return rows * font->CharHeight() + (rows - 1) * rowGap;
133 void Menu<T>::NextItem() {
134 SelectIndex(selected + 1);
138 void Menu<T>::PreviousItem() {
139 SelectIndex(selected - 1);
143 void Menu<T>::NextRow() {
144 SelectIndex(selected + cols);
148 void Menu<T>::PreviousRow() {
149 SelectIndex(selected - cols);
153 void Menu<T>::SelectIndex(int index) {
154 if (index < 0 || int(entries.size()) <= index) return;
156 if (topRow <= GetRow(selected) - rows) {
157 topRow = GetRow(selected) - rows + 1;
158 } else if (GetRow(selected) < topRow) {
159 topRow = GetRow(selected);
165 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
166 int start(topRow * cols);
167 int slots(rows * cols);
168 int items(entries.size() - start);
169 int end(start + (items < slots ? items : slots));
170 for (int i(0), count(end - start); i < count; ++i) {
171 geometry::Vector<int> offset(
172 (i % cols) * (ColWidth() + colGap),
173 (i / cols) * RowHeight());
174 if (entries[start + i].enabled) {
175 font->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
177 disabledFont->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
180 geometry::Vector<int> cursorOffset(
181 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
182 ((selected - start) / cols) * RowHeight());
183 cursor->Draw(dest, position + cursorOffset);
188 #endif /* GRAPHICS_MENU_H_ */