4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
13 #include "../geometry/Vector.h"
22 struct MenuProperties {
24 const Font *disabledFont;
33 int charsPerAdditionalText;
34 int additionalTextGap;
38 : font(0), disabledFont(0), cursor(0)
39 , charsPerEntry(0), rows(0), rowGap(0)
40 , iconSpace(0), cols(0), colGap(0)
41 , charsPerNumber(0), charsPerAdditionalText(0)
42 , additionalTextGap(0), delimiter(':') { }
44 MenuProperties(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, int charsPerAdditionalText, int additionalTextGap)
45 : font(font), disabledFont(disabledFont), cursor(cursor), charsPerEntry(charsPerEntry), rows(rows), rowGap(rowGap), iconSpace(iconSpace), cols(cols), colGap(colGap), charsPerNumber(charsPerNumber), charsPerAdditionalText(charsPerAdditionalText), additionalTextGap(additionalTextGap), delimiter(delimiter) { }
47 static void CreateTypeDescription();
52 : private MenuProperties {
56 Menu(const MenuProperties &);
57 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 = ':', int charsPerAdditionalText = 0, int additionalTextGap = 0);
63 int RowHeight() const { return font->CharHeight() + rowGap; }
64 int CharsPerEntry() const { return charsPerEntry; }
66 T &Selected() { return entries[selected].value; }
67 const T &Selected() const { return entries[selected].value; }
68 const char *SelectedTitle() const { return entries[selected].title; }
69 int SelectedNumber() const { return entries[selected].number; }
70 bool SelectedIsEnabled() const { return entries[selected].enabled; }
76 void SelectIndex(int index);
77 int SelectedIndex() const { return selected; }
78 bool IsSelected(int index) const { return index == selected; }
80 int EntryCount() const { return entries.size(); }
81 T &ValueAt(int index) { return entries[index].value; }
82 const T &ValueAt(int index) const { return entries[index].value; }
84 void Add(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0) { entries.push_back(Entry(title, value, enabled, icon, number, additionalText)); }
85 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
86 void Disable(int index) { entries[index].enabled = false; }
87 void Enable(int index) { entries[index].enabled = true; }
88 void Reserve(int n) { entries.reserve(n); }
89 void Clear() { entries.clear(); }
91 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
94 int GetRow(int index) const { return index / cols; }
95 int GetCol(int index) const { return index % cols; }
99 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
100 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
102 const char *additionalText;
108 std::vector<Entry> entries;
124 Menu<T>::Menu(const MenuProperties &p)
132 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, int charsPerAdditionalText, int additionalTextGap)
134 font, disabledFont ? disabledFont : font,
135 cursor, charsPerEntry,
136 rows, rowGap, iconSpace,
137 cols, colGap, charsPerNumber,
139 charsPerAdditionalText,
148 int Menu<T>::ColWidth() const {
149 int width(iconSpace);
150 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
151 if (charsPerNumber) {
152 width += font->CharWidth();
154 if (charsPerAdditionalText) {
155 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
161 int Menu<T>::Width() const {
162 return cols * ColWidth() + (cols - 1) * colGap;
166 int Menu<T>::Height() const {
167 return rows * font->CharHeight() + (rows - 1) * rowGap;
172 void Menu<T>::NextItem() {
173 SelectIndex(selected + 1);
177 void Menu<T>::PreviousItem() {
178 SelectIndex(selected - 1);
182 void Menu<T>::NextRow() {
183 SelectIndex(selected + cols);
187 void Menu<T>::PreviousRow() {
188 SelectIndex(selected - cols);
192 void Menu<T>::SelectIndex(int index) {
193 if (index < 0 || int(entries.size()) <= index) return;
195 if (topRow <= GetRow(selected) - rows) {
196 topRow = GetRow(selected) - rows + 1;
197 } else if (GetRow(selected) < topRow) {
198 topRow = GetRow(selected);
204 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
205 int start(topRow * cols);
206 int slots(rows * cols);
207 int items(entries.size() - start);
208 int end(start + (items < slots ? items : slots));
209 for (int i(0), count(end - start); i < count; ++i) {
210 if (!entries[start + i].title) continue;
211 geometry::Vector<int> iconOffset(
212 (i % cols) * (ColWidth() + colGap),
213 (i / cols) * RowHeight());
214 if (entries[start + i].icon) {
215 entries[start + i].icon->Draw(dest, position + iconOffset);
217 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
218 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
219 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
221 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
223 if (charsPerAdditionalText) {
224 textOffset += geometry::Vector<int>(additionalTextGap, 0);
225 if (entries[start + i].additionalText) {
226 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
228 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
231 if (charsPerNumber) {
232 usedFont->DrawChar(delimiter, dest, position + textOffset);
233 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
234 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
237 geometry::Vector<int> cursorOffset(
238 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
239 ((selected - start) / cols) * RowHeight());
240 cursor->Draw(dest, position + cursorOffset);
245 #endif /* GRAPHICS_MENU_H_ */