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 iconSpace = 0, int cols = 1, int colGap = 0, int charsPerNumber = 0, char delimiter = ':');
35 int ColWidth() const { return iconSpace + font->CharWidth() * (charsPerEntry + charsPerNumber) + (charsPerNumber ? font->CharWidth() : 0); }
36 int RowHeight() const { return font->CharHeight() + rowGap; }
37 int CharsPerEntry() const { return charsPerEntry; }
39 T &Selected() { return entries[selected].value; }
40 const T &Selected() const { return entries[selected].value; }
41 const char *SelectedTitle() const { return entries[selected].title; }
42 int SelectedNumber() const { return entries[selected].number; }
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, int number = 0) { entries.push_back(Entry(title, value, enabled, icon, number)); }
58 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
59 void Disable(int index) { entries[index].enabled = false; }
60 void Enable(int index) { entries[index].enabled = true; }
61 void Reserve(int n) { entries.reserve(n); }
62 void Clear() { entries.clear(); }
64 void Draw(SDL_Surface *dest, geometry::Point<int> position) const;
67 int GetRow(int index) const { return index / cols; }
68 int GetCol(int index) const { return index % cols; }
72 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0)
73 : title(title), icon(icon), number(number), value(value), enabled(enabled) { }
81 const Font *disabledFont;
83 std::vector<Entry> entries;
117 Menu<T>::Menu(const Font *font, const Font *disabledFont, const Sprite *cursor, int charsPerEntry, int rows, int rowGap, int iconSpace, int cols, int colGap, int charsPerNumber, char delimiter)
119 , disabledFont(disabledFont ? disabledFont : font)
121 , charsPerEntry(charsPerEntry)
124 , iconSpace(iconSpace)
129 , charsPerNumber(charsPerNumber)
130 , delimiter(delimiter) {
136 int Menu<T>::Width() const {
137 return cols * ColWidth() + (cols - 1) * colGap;
141 int Menu<T>::Height() const {
142 return rows * font->CharHeight() + (rows - 1) * rowGap;
147 void Menu<T>::NextItem() {
148 SelectIndex(selected + 1);
152 void Menu<T>::PreviousItem() {
153 SelectIndex(selected - 1);
157 void Menu<T>::NextRow() {
158 SelectIndex(selected + cols);
162 void Menu<T>::PreviousRow() {
163 SelectIndex(selected - cols);
167 void Menu<T>::SelectIndex(int index) {
168 if (index < 0 || int(entries.size()) <= index) return;
170 if (topRow <= GetRow(selected) - rows) {
171 topRow = GetRow(selected) - rows + 1;
172 } else if (GetRow(selected) < topRow) {
173 topRow = GetRow(selected);
179 void Menu<T>::Draw(SDL_Surface *dest, geometry::Point<int> position) const {
180 int start(topRow * cols);
181 int slots(rows * cols);
182 int items(entries.size() - start);
183 int end(start + (items < slots ? items : slots));
184 for (int i(0), count(end - start); i < count; ++i) {
185 if (!entries[start + i].title) continue;
186 geometry::Vector<int> iconOffset(
187 (i % cols) * (ColWidth() + colGap),
188 (i / cols) * RowHeight());
189 if (entries[start + i].icon) {
190 entries[start + i].icon->Draw(dest, position + iconOffset);
192 geometry::Vector<int> labelOffset(iconOffset.X() + iconSpace, iconOffset.Y());
193 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
194 usedFont->DrawString(entries[start + i].title, dest, position + labelOffset, charsPerEntry);
196 if (charsPerNumber) {
197 geometry::Vector<int> delimiterOffset(labelOffset.X() + charsPerEntry * usedFont->CharWidth(), labelOffset.Y());
198 usedFont->DrawChar(delimiter, dest, position + delimiterOffset);
199 geometry::Vector<int> numberOffset(delimiterOffset.X() + usedFont->CharWidth(), delimiterOffset.Y());
200 usedFont->DrawNumber(entries[start + i].number, dest, position + numberOffset);
203 geometry::Vector<int> cursorOffset(
204 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
205 ((selected - start) / cols) * RowHeight());
206 cursor->Draw(dest, position + cursorOffset);
211 #endif /* GRAPHICS_MENU_H_ */