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;
41 : font(0), disabledFont(0), cursor(0)
42 , charsPerEntry(0), rows(0), rowGap(0)
43 , iconSpace(0), cols(0), colGap(0)
44 , charsPerNumber(0), charsPerAdditionalText(0)
45 , additionalTextGap(0), delimiter(':')
46 , wrapX(false), wrapY(false) { }
48 static void CreateTypeDescription();
49 static void Construct(void *);
55 : private MenuProperties {
59 Menu(const MenuProperties &);
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)
135 int Menu<T>::ColWidth() const {
136 int width(iconSpace);
137 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
138 if (charsPerNumber) {
139 width += font->CharWidth();
141 if (charsPerAdditionalText) {
142 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
148 int Menu<T>::Width() const {
149 return cols * ColWidth() + (cols - 1) * colGap;
153 int Menu<T>::Height() const {
154 return rows * font->CharHeight() + (rows - 1) * rowGap;
159 void Menu<T>::NextItem() {
160 int index(selected + 1);
161 if (wrapX && index % cols == 0) {
168 void Menu<T>::PreviousItem() {
169 int index(selected - 1);
170 if (wrapX && selected % cols == 0) {
177 void Menu<T>::NextRow() {
178 int index(selected + cols);
179 if (wrapY && index >= int(entries.size())) {
180 index -= entries.size();
186 void Menu<T>::PreviousRow() {
187 int index(selected - cols);
188 if (wrapY && index < 0) {
189 index += entries.size();
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_ */