4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
14 #include "../geometry/Vector.h"
21 struct MenuProperties {
22 static const int TYPE_ID = 407;
25 const Font *disabledFont;
34 int charsPerAdditionalText;
35 int additionalTextGap;
39 : font(0), disabledFont(0), cursor(0)
40 , charsPerEntry(0), rows(0), rowGap(0)
41 , iconSpace(0), cols(0), colGap(0)
42 , charsPerNumber(0), charsPerAdditionalText(0)
43 , additionalTextGap(0), delimiter(':') { }
45 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)
46 : 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) { }
48 static void CreateTypeDescription();
49 static void Construct(void *);
55 : private MenuProperties {
59 Menu(const MenuProperties &);
60 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);
66 int RowHeight() const { return font->CharHeight() + rowGap; }
67 int CharsPerEntry() const { return charsPerEntry; }
69 T &Selected() { return entries[selected].value; }
70 const T &Selected() const { return entries[selected].value; }
71 const char *SelectedTitle() const { return entries[selected].title; }
72 int SelectedNumber() const { return entries[selected].number; }
73 bool SelectedIsEnabled() const { return entries[selected].enabled; }
79 void SelectIndex(int index);
80 int SelectedIndex() const { return selected; }
81 bool IsSelected(int index) const { return index == selected; }
83 int EntryCount() const { return entries.size(); }
84 T &ValueAt(int index) { return entries[index].value; }
85 const T &ValueAt(int index) const { return entries[index].value; }
87 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)); }
88 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
89 void Disable(int index) { entries[index].enabled = false; }
90 void Enable(int index) { entries[index].enabled = true; }
91 void Reserve(int n) { entries.reserve(n); }
92 void Clear() { entries.clear(); }
94 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
97 int GetRow(int index) const { return index / cols; }
98 int GetCol(int index) const { return index % cols; }
102 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
103 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
105 const char *additionalText;
111 std::vector<Entry> entries;
127 Menu<T>::Menu(const MenuProperties &p)
135 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)
137 font, disabledFont ? disabledFont : font,
138 cursor, charsPerEntry,
139 rows, rowGap, iconSpace,
140 cols, colGap, charsPerNumber,
142 charsPerAdditionalText,
151 int Menu<T>::ColWidth() const {
152 int width(iconSpace);
153 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
154 if (charsPerNumber) {
155 width += font->CharWidth();
157 if (charsPerAdditionalText) {
158 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
164 int Menu<T>::Width() const {
165 return cols * ColWidth() + (cols - 1) * colGap;
169 int Menu<T>::Height() const {
170 return rows * font->CharHeight() + (rows - 1) * rowGap;
175 void Menu<T>::NextItem() {
176 SelectIndex(selected + 1);
180 void Menu<T>::PreviousItem() {
181 SelectIndex(selected - 1);
185 void Menu<T>::NextRow() {
186 SelectIndex(selected + cols);
190 void Menu<T>::PreviousRow() {
191 SelectIndex(selected - cols);
195 void Menu<T>::SelectIndex(int index) {
196 if (index < 0 || int(entries.size()) <= index) return;
198 if (topRow <= GetRow(selected) - rows) {
199 topRow = GetRow(selected) - rows + 1;
200 } else if (GetRow(selected) < topRow) {
201 topRow = GetRow(selected);
207 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
208 int start(topRow * cols);
209 int slots(rows * cols);
210 int items(entries.size() - start);
211 int end(start + (items < slots ? items : slots));
212 for (int i(0), count(end - start); i < count; ++i) {
213 if (!entries[start + i].title) continue;
214 geometry::Vector<int> iconOffset(
215 (i % cols) * (ColWidth() + colGap),
216 (i / cols) * RowHeight());
217 if (entries[start + i].icon) {
218 entries[start + i].icon->Draw(dest, position + iconOffset);
220 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
221 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
222 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
224 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
226 if (charsPerAdditionalText) {
227 textOffset += geometry::Vector<int>(additionalTextGap, 0);
228 if (entries[start + i].additionalText) {
229 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
231 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
234 if (charsPerNumber) {
235 usedFont->DrawChar(delimiter, dest, position + textOffset);
236 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
237 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
240 geometry::Vector<int> cursorOffset(
241 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
242 ((selected - start) / cols) * RowHeight());
243 cursor->Draw(dest, position + cursorOffset);
248 #endif /* GRAPHICS_MENU_H_ */