4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
13 #include "../geometry/Vector.h"
22 // TODO: animation when top row changes
28 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);
34 int RowHeight() const { return font->CharHeight() + rowGap; }
35 int CharsPerEntry() const { return charsPerEntry; }
37 T &Selected() { return entries[selected].value; }
38 const T &Selected() const { return entries[selected].value; }
39 const char *SelectedTitle() const { return entries[selected].title; }
40 int SelectedNumber() const { return entries[selected].number; }
41 bool SelectedIsEnabled() const { return entries[selected].enabled; }
47 void SelectIndex(int index);
48 int SelectedIndex() const { return selected; }
49 bool IsSelected(int index) const { return index == selected; }
51 int EntryCount() const { return entries.size(); }
52 T &ValueAt(int index) { return entries[index].value; }
53 const T &ValueAt(int index) const { return entries[index].value; }
55 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)); }
56 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
57 void Disable(int index) { entries[index].enabled = false; }
58 void Enable(int index) { entries[index].enabled = true; }
59 void Reserve(int n) { entries.reserve(n); }
60 void Clear() { entries.clear(); }
62 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
65 int GetRow(int index) const { return index / cols; }
66 int GetCol(int index) const { return index % cols; }
70 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
71 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
73 const char *additionalText;
80 const Font *disabledFont;
82 std::vector<Entry> entries;
92 int charsPerAdditionalText;
93 int additionalTextGap;
113 , charsPerAdditionalText(0)
114 , additionalTextGap(0)
120 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)
122 , disabledFont(disabledFont ? disabledFont : font)
124 , charsPerEntry(charsPerEntry)
127 , iconSpace(iconSpace)
132 , charsPerNumber(charsPerNumber)
133 , charsPerAdditionalText(charsPerAdditionalText)
134 , additionalTextGap(additionalTextGap)
135 , delimiter(delimiter) {
141 int Menu<T>::ColWidth() const {
142 int width(iconSpace);
143 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
144 if (charsPerNumber) {
145 width += font->CharWidth();
147 if (charsPerAdditionalText) {
148 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
154 int Menu<T>::Width() const {
155 return cols * ColWidth() + (cols - 1) * colGap;
159 int Menu<T>::Height() const {
160 return rows * font->CharHeight() + (rows - 1) * rowGap;
165 void Menu<T>::NextItem() {
166 SelectIndex(selected + 1);
170 void Menu<T>::PreviousItem() {
171 SelectIndex(selected - 1);
175 void Menu<T>::NextRow() {
176 SelectIndex(selected + cols);
180 void Menu<T>::PreviousRow() {
181 SelectIndex(selected - cols);
185 void Menu<T>::SelectIndex(int index) {
186 if (index < 0 || int(entries.size()) <= index) return;
188 if (topRow <= GetRow(selected) - rows) {
189 topRow = GetRow(selected) - rows + 1;
190 } else if (GetRow(selected) < topRow) {
191 topRow = GetRow(selected);
197 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
198 int start(topRow * cols);
199 int slots(rows * cols);
200 int items(entries.size() - start);
201 int end(start + (items < slots ? items : slots));
202 for (int i(0), count(end - start); i < count; ++i) {
203 if (!entries[start + i].title) continue;
204 geometry::Vector<int> iconOffset(
205 (i % cols) * (ColWidth() + colGap),
206 (i / cols) * RowHeight());
207 if (entries[start + i].icon) {
208 entries[start + i].icon->Draw(dest, position + iconOffset);
210 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
211 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
212 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
214 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
216 if (charsPerAdditionalText) {
217 textOffset += geometry::Vector<int>(additionalTextGap, 0);
218 if (entries[start + i].additionalText) {
219 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
221 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
224 if (charsPerNumber) {
225 usedFont->DrawChar(delimiter, dest, position + textOffset);
226 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
227 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset);
230 geometry::Vector<int> cursorOffset(
231 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
232 ((selected - start) / cols) * RowHeight());
233 cursor->Draw(dest, position + cursorOffset);
238 #endif /* GRAPHICS_MENU_H_ */