4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
13 #include "../geometry/operators.h"
14 #include "../geometry/Point.h"
15 #include "../geometry/Vector.h"
24 // TODO: animation when top row changes
30 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);
36 int RowHeight() const { return font->CharHeight() + rowGap; }
37 int CharsPerEntry() const { return charsPerEntry; }
39 T &Selected() { return entries[selected].value; }
40 const T &Selected() const { return entries[selected].value; }
41 const char *SelectedTitle() const { return entries[selected].title; }
42 int SelectedNumber() const { return entries[selected].number; }
43 bool SelectedIsEnabled() const { return entries[selected].enabled; }
49 void SelectIndex(int index);
50 int SelectedIndex() const { return selected; }
51 bool IsSelected(int index) const { return index == selected; }
53 int EntryCount() const { return entries.size(); }
54 T &ValueAt(int index) { return entries[index].value; }
55 const T &ValueAt(int index) const { return entries[index].value; }
57 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)); }
58 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
59 void Disable(int index) { entries[index].enabled = false; }
60 void Enable(int index) { entries[index].enabled = true; }
61 void Reserve(int n) { entries.reserve(n); }
62 void Clear() { entries.clear(); }
64 void Draw(SDL_Surface *dest, const geometry::Point<int> &position) const;
67 int GetRow(int index) const { return index / cols; }
68 int GetCol(int index) const { return index % cols; }
72 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
73 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
75 const char *additionalText;
82 const Font *disabledFont;
84 std::vector<Entry> entries;
94 int charsPerAdditionalText;
95 int additionalTextGap;
115 , charsPerAdditionalText(0)
116 , additionalTextGap(0)
122 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)
124 , disabledFont(disabledFont ? disabledFont : font)
126 , charsPerEntry(charsPerEntry)
129 , iconSpace(iconSpace)
134 , charsPerNumber(charsPerNumber)
135 , charsPerAdditionalText(charsPerAdditionalText)
136 , additionalTextGap(additionalTextGap)
137 , delimiter(delimiter) {
143 int Menu<T>::ColWidth() const {
144 int width(iconSpace);
145 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
146 if (charsPerNumber) {
147 width += font->CharWidth();
149 if (charsPerAdditionalText) {
150 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
156 int Menu<T>::Width() const {
157 return cols * ColWidth() + (cols - 1) * colGap;
161 int Menu<T>::Height() const {
162 return rows * font->CharHeight() + (rows - 1) * rowGap;
167 void Menu<T>::NextItem() {
168 SelectIndex(selected + 1);
172 void Menu<T>::PreviousItem() {
173 SelectIndex(selected - 1);
177 void Menu<T>::NextRow() {
178 SelectIndex(selected + cols);
182 void Menu<T>::PreviousRow() {
183 SelectIndex(selected - cols);
187 void Menu<T>::SelectIndex(int index) {
188 if (index < 0 || int(entries.size()) <= index) return;
190 if (topRow <= GetRow(selected) - rows) {
191 topRow = GetRow(selected) - rows + 1;
192 } else if (GetRow(selected) < topRow) {
193 topRow = GetRow(selected);
199 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Point<int> &position) const {
200 int start(topRow * cols);
201 int slots(rows * cols);
202 int items(entries.size() - start);
203 int end(start + (items < slots ? items : slots));
204 for (int i(0), count(end - start); i < count; ++i) {
205 if (!entries[start + i].title) continue;
206 geometry::Vector<int> iconOffset(
207 (i % cols) * (ColWidth() + colGap),
208 (i / cols) * RowHeight());
209 if (entries[start + i].icon) {
210 entries[start + i].icon->Draw(dest, position + iconOffset);
212 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
213 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
214 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
216 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
218 if (charsPerAdditionalText) {
219 textOffset += geometry::Vector<int>(additionalTextGap, 0);
220 if (entries[start + i].additionalText) {
221 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
223 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
226 if (charsPerNumber) {
227 usedFont->DrawChar(delimiter, dest, position + textOffset);
228 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
229 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
232 geometry::Vector<int> cursorOffset(
233 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
234 ((selected - start) / cols) * RowHeight());
235 cursor->Draw(dest, position + cursorOffset);
240 #endif /* GRAPHICS_MENU_H_ */