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();
48 static void Construct(void *);
54 : private MenuProperties {
58 Menu(const MenuProperties &);
59 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);
65 int RowHeight() const { return font->CharHeight() + rowGap; }
66 int CharsPerEntry() const { return charsPerEntry; }
68 T &Selected() { return entries[selected].value; }
69 const T &Selected() const { return entries[selected].value; }
70 const char *SelectedTitle() const { return entries[selected].title; }
71 int SelectedNumber() const { return entries[selected].number; }
72 bool SelectedIsEnabled() const { return entries[selected].enabled; }
78 void SelectIndex(int index);
79 int SelectedIndex() const { return selected; }
80 bool IsSelected(int index) const { return index == selected; }
82 int EntryCount() const { return entries.size(); }
83 T &ValueAt(int index) { return entries[index].value; }
84 const T &ValueAt(int index) const { return entries[index].value; }
86 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)); }
87 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
88 void Disable(int index) { entries[index].enabled = false; }
89 void Enable(int index) { entries[index].enabled = true; }
90 void Reserve(int n) { entries.reserve(n); }
91 void Clear() { entries.clear(); }
93 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
96 int GetRow(int index) const { return index / cols; }
97 int GetCol(int index) const { return index % cols; }
101 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
102 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
104 const char *additionalText;
110 std::vector<Entry> entries;
126 Menu<T>::Menu(const MenuProperties &p)
134 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)
136 font, disabledFont ? disabledFont : font,
137 cursor, charsPerEntry,
138 rows, rowGap, iconSpace,
139 cols, colGap, charsPerNumber,
141 charsPerAdditionalText,
150 int Menu<T>::ColWidth() const {
151 int width(iconSpace);
152 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
153 if (charsPerNumber) {
154 width += font->CharWidth();
156 if (charsPerAdditionalText) {
157 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
163 int Menu<T>::Width() const {
164 return cols * ColWidth() + (cols - 1) * colGap;
168 int Menu<T>::Height() const {
169 return rows * font->CharHeight() + (rows - 1) * rowGap;
174 void Menu<T>::NextItem() {
175 SelectIndex(selected + 1);
179 void Menu<T>::PreviousItem() {
180 SelectIndex(selected - 1);
184 void Menu<T>::NextRow() {
185 SelectIndex(selected + cols);
189 void Menu<T>::PreviousRow() {
190 SelectIndex(selected - cols);
194 void Menu<T>::SelectIndex(int index) {
195 if (index < 0 || int(entries.size()) <= index) return;
197 if (topRow <= GetRow(selected) - rows) {
198 topRow = GetRow(selected) - rows + 1;
199 } else if (GetRow(selected) < topRow) {
200 topRow = GetRow(selected);
206 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
207 int start(topRow * cols);
208 int slots(rows * cols);
209 int items(entries.size() - start);
210 int end(start + (items < slots ? items : slots));
211 for (int i(0), count(end - start); i < count; ++i) {
212 if (!entries[start + i].title) continue;
213 geometry::Vector<int> iconOffset(
214 (i % cols) * (ColWidth() + colGap),
215 (i / cols) * RowHeight());
216 if (entries[start + i].icon) {
217 entries[start + i].icon->Draw(dest, position + iconOffset);
219 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
220 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
221 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
223 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
225 if (charsPerAdditionalText) {
226 textOffset += geometry::Vector<int>(additionalTextGap, 0);
227 if (entries[start + i].additionalText) {
228 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
230 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
233 if (charsPerNumber) {
234 usedFont->DrawChar(delimiter, dest, position + textOffset);
235 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
236 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
239 geometry::Vector<int> cursorOffset(
240 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
241 ((selected - start) / cols) * RowHeight());
242 cursor->Draw(dest, position + cursorOffset);
247 #endif /* GRAPHICS_MENU_H_ */