4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
13 #include "../geometry/Vector.h"
27 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);
33 int RowHeight() const { return font->CharHeight() + rowGap; }
34 int CharsPerEntry() const { return charsPerEntry; }
36 T &Selected() { return entries[selected].value; }
37 const T &Selected() const { return entries[selected].value; }
38 const char *SelectedTitle() const { return entries[selected].title; }
39 int SelectedNumber() const { return entries[selected].number; }
40 bool SelectedIsEnabled() const { return entries[selected].enabled; }
46 void SelectIndex(int index);
47 int SelectedIndex() const { return selected; }
48 bool IsSelected(int index) const { return index == selected; }
50 int EntryCount() const { return entries.size(); }
51 T &ValueAt(int index) { return entries[index].value; }
52 const T &ValueAt(int index) const { return entries[index].value; }
54 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)); }
55 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
56 void Disable(int index) { entries[index].enabled = false; }
57 void Enable(int index) { entries[index].enabled = true; }
58 void Reserve(int n) { entries.reserve(n); }
59 void Clear() { entries.clear(); }
61 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
64 int GetRow(int index) const { return index / cols; }
65 int GetCol(int index) const { return index % cols; }
69 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
70 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
72 const char *additionalText;
79 const Font *disabledFont;
81 std::vector<Entry> entries;
91 int charsPerAdditionalText;
92 int additionalTextGap;
112 , charsPerAdditionalText(0)
113 , additionalTextGap(0)
119 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)
121 , disabledFont(disabledFont ? disabledFont : font)
123 , charsPerEntry(charsPerEntry)
126 , iconSpace(iconSpace)
131 , charsPerNumber(charsPerNumber)
132 , charsPerAdditionalText(charsPerAdditionalText)
133 , additionalTextGap(additionalTextGap)
134 , delimiter(delimiter) {
140 int Menu<T>::ColWidth() const {
141 int width(iconSpace);
142 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
143 if (charsPerNumber) {
144 width += font->CharWidth();
146 if (charsPerAdditionalText) {
147 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
153 int Menu<T>::Width() const {
154 return cols * ColWidth() + (cols - 1) * colGap;
158 int Menu<T>::Height() const {
159 return rows * font->CharHeight() + (rows - 1) * rowGap;
164 void Menu<T>::NextItem() {
165 SelectIndex(selected + 1);
169 void Menu<T>::PreviousItem() {
170 SelectIndex(selected - 1);
174 void Menu<T>::NextRow() {
175 SelectIndex(selected + cols);
179 void Menu<T>::PreviousRow() {
180 SelectIndex(selected - cols);
184 void Menu<T>::SelectIndex(int index) {
185 if (index < 0 || int(entries.size()) <= index) return;
187 if (topRow <= GetRow(selected) - rows) {
188 topRow = GetRow(selected) - rows + 1;
189 } else if (GetRow(selected) < topRow) {
190 topRow = GetRow(selected);
196 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
197 int start(topRow * cols);
198 int slots(rows * cols);
199 int items(entries.size() - start);
200 int end(start + (items < slots ? items : slots));
201 for (int i(0), count(end - start); i < count; ++i) {
202 if (!entries[start + i].title) continue;
203 geometry::Vector<int> iconOffset(
204 (i % cols) * (ColWidth() + colGap),
205 (i / cols) * RowHeight());
206 if (entries[start + i].icon) {
207 entries[start + i].icon->Draw(dest, position + iconOffset);
209 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
210 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
211 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
213 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
215 if (charsPerAdditionalText) {
216 textOffset += geometry::Vector<int>(additionalTextGap, 0);
217 if (entries[start + i].additionalText) {
218 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
220 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
223 if (charsPerNumber) {
224 usedFont->DrawChar(delimiter, dest, position + textOffset);
225 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
226 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
229 geometry::Vector<int> cursorOffset(
230 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
231 ((selected - start) / cols) * RowHeight());
232 cursor->Draw(dest, position + cursorOffset);
237 #endif /* GRAPHICS_MENU_H_ */