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"
25 // TODO: animation when top row changes
31 Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap = 0, int cols = 1, int colGap = 0);
37 int ColWidth() const { return font->CharWidth() * charsPerEntry; }
38 int RowHeight() const { return font->CharHeight() + rowGap; }
40 T &Selected() { return entries[selected].value; }
41 const T &Selected() const { return entries[selected].value; }
42 const char *SelectedTitle() const { return entries[selected].title; }
43 bool SelectedIsEnabled() const { return entries[selected].enabled; }
49 void SelectIndex(int index);
50 int SelectedIndex() const { return selected; }
51 bool IsSelected(int index) const { return index == selected; }
53 int EntryCount() const { return entries.size(); }
54 T &ValueAt(int index) { return entries[index].value; }
55 const T &ValueAt(int index) const { return entries[index].value; }
57 void Add(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0) { entries.push_back(Entry(title, value, enabled, icon)); }
58 void Disable(int index) { entries[index].enabled = false; }
59 void Enable(int index) { entries[index].enabled = true; }
60 void Reserve(int n) { entries.reserve(n); }
62 void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
65 int GetRow(int index) const { return index / cols; }
66 int GetCol(int index) const { return index % cols; }
70 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0)
71 : title(title), icon(icon), value(value), enabled(enabled) { }
78 const Font *disabledFont;
80 std::vector<Entry> entries;
108 Menu<T>::Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int cols, int colGap)
110 , disabledFont(disabledFont ? disabledFont : font)
112 , charsPerEntry(charsPerEntry)
123 Menu<T>::Menu(const Menu &other)
125 , disabledFont(other.disabledFont)
126 , cursor(other.cursor)
127 , entries(other.entries)
128 , charsPerEntry(other.charsPerEntry)
130 , rowGap(other.rowGap)
132 , colGap(other.colGap)
133 , selected(other.selected)
134 , topRow(other.topRow) {
135 std::cout << "copied Menu" << std::endl;
140 int Menu<T>::Width() const {
141 return cols * ColWidth() + (cols - 1) * colGap;
145 int Menu<T>::Height() const {
146 return rows * font->CharHeight() + (rows - 1) * rowGap;
151 void Menu<T>::NextItem() {
152 SelectIndex(selected + 1);
156 void Menu<T>::PreviousItem() {
157 SelectIndex(selected - 1);
161 void Menu<T>::NextRow() {
162 SelectIndex(selected + cols);
166 void Menu<T>::PreviousRow() {
167 SelectIndex(selected - cols);
171 void Menu<T>::SelectIndex(int index) {
172 if (index < 0 || int(entries.size()) < index) return;
173 std::cout << "selecting index " << index << std::endl;
175 if (GetRow(selected) - rows > topRow) {
176 topRow = GetRow(selected) - rows;
177 } else if (GetRow(selected) < topRow) {
178 topRow = GetRow(selected);
184 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
185 int start(topRow * cols);
186 int slots((topRow + rows) * cols);
187 int items(entries.size() - start);
188 int end(items < slots ? items : slots);
189 for (int i(0), count(end - start); i < count; ++i) {
190 geometry::Vector<int> offset(
191 (i % cols) * (ColWidth() + colGap),
192 (i / cols) * RowHeight());
193 if (entries[start + i].enabled) {
194 font->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
196 disabledFont->DrawString(entries[start + i].title, dest, position + offset, charsPerEntry);
199 geometry::Vector<int> cursorOffset(
200 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
201 (selected / cols) * RowHeight());
202 cursor->Draw(dest, position + cursorOffset);
207 #endif /* GRAPHICS_MENU_H_ */