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) { }
50 : private MenuProperties {
54 Menu(const MenuProperties &);
55 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);
61 int RowHeight() const { return font->CharHeight() + rowGap; }
62 int CharsPerEntry() const { return charsPerEntry; }
64 T &Selected() { return entries[selected].value; }
65 const T &Selected() const { return entries[selected].value; }
66 const char *SelectedTitle() const { return entries[selected].title; }
67 int SelectedNumber() const { return entries[selected].number; }
68 bool SelectedIsEnabled() const { return entries[selected].enabled; }
74 void SelectIndex(int index);
75 int SelectedIndex() const { return selected; }
76 bool IsSelected(int index) const { return index == selected; }
78 int EntryCount() const { return entries.size(); }
79 T &ValueAt(int index) { return entries[index].value; }
80 const T &ValueAt(int index) const { return entries[index].value; }
82 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)); }
83 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
84 void Disable(int index) { entries[index].enabled = false; }
85 void Enable(int index) { entries[index].enabled = true; }
86 void Reserve(int n) { entries.reserve(n); }
87 void Clear() { entries.clear(); }
89 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
92 int GetRow(int index) const { return index / cols; }
93 int GetCol(int index) const { return index % cols; }
97 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
98 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
100 const char *additionalText;
106 std::vector<Entry> entries;
122 Menu<T>::Menu(const MenuProperties &p)
130 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)
132 font, disabledFont ? disabledFont : font,
133 cursor, charsPerEntry,
134 rows, rowGap, iconSpace,
135 cols, colGap, charsPerNumber,
137 charsPerAdditionalText,
146 int Menu<T>::ColWidth() const {
147 int width(iconSpace);
148 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
149 if (charsPerNumber) {
150 width += font->CharWidth();
152 if (charsPerAdditionalText) {
153 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
159 int Menu<T>::Width() const {
160 return cols * ColWidth() + (cols - 1) * colGap;
164 int Menu<T>::Height() const {
165 return rows * font->CharHeight() + (rows - 1) * rowGap;
170 void Menu<T>::NextItem() {
171 SelectIndex(selected + 1);
175 void Menu<T>::PreviousItem() {
176 SelectIndex(selected - 1);
180 void Menu<T>::NextRow() {
181 SelectIndex(selected + cols);
185 void Menu<T>::PreviousRow() {
186 SelectIndex(selected - cols);
190 void Menu<T>::SelectIndex(int index) {
191 if (index < 0 || int(entries.size()) <= index) return;
193 if (topRow <= GetRow(selected) - rows) {
194 topRow = GetRow(selected) - rows + 1;
195 } else if (GetRow(selected) < topRow) {
196 topRow = GetRow(selected);
202 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
203 int start(topRow * cols);
204 int slots(rows * cols);
205 int items(entries.size() - start);
206 int end(start + (items < slots ? items : slots));
207 for (int i(0), count(end - start); i < count; ++i) {
208 if (!entries[start + i].title) continue;
209 geometry::Vector<int> iconOffset(
210 (i % cols) * (ColWidth() + colGap),
211 (i / cols) * RowHeight());
212 if (entries[start + i].icon) {
213 entries[start + i].icon->Draw(dest, position + iconOffset);
215 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
216 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
217 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
219 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
221 if (charsPerAdditionalText) {
222 textOffset += geometry::Vector<int>(additionalTextGap, 0);
223 if (entries[start + i].additionalText) {
224 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
226 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
229 if (charsPerNumber) {
230 usedFont->DrawChar(delimiter, dest, position + textOffset);
231 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
232 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
235 geometry::Vector<int> cursorOffset(
236 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
237 ((selected - start) / cols) * RowHeight());
238 cursor->Draw(dest, position + cursorOffset);
243 #endif /* GRAPHICS_MENU_H_ */