4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
14 #include "../geometry/Vector.h"
22 struct MenuProperties {
23 static const int TYPE_ID = 407;
26 const Font *disabledFont;
28 const Sprite *selectedCursor;
36 int charsPerAdditionalText;
37 int additionalTextGap;
43 : font(0), disabledFont(0), cursor(0), selectedCursor(0)
44 , charsPerEntry(0), rows(1), rowGap(0)
45 , iconSpace(0), cols(1), colGap(0)
46 , charsPerNumber(0), charsPerAdditionalText(0)
47 , additionalTextGap(0), delimiter(':')
48 , wrapX(false), wrapY(false) { }
50 static void CreateTypeDescription();
51 static void Construct(void *);
57 : private MenuProperties {
61 Menu(const MenuProperties &);
64 void SetInactive() { state = STATE_INACTIVE; }
65 void SetActive() { state = STATE_ACTIVE; }
66 void SetSelected() { state = STATE_SELECTED; }
67 void SetDualSelection() { state = STATE_DUAL; secondarySelection = selected; }
68 bool IsActive() const { return state == STATE_ACTIVE; }
69 bool HasSelected() const { return state == STATE_SELECTED; }
70 bool InDualMode() const { return state == STATE_DUAL; }
75 int RowHeight() const { return font->CharHeight() + rowGap; }
76 int CharsPerEntry() const { return charsPerEntry; }
78 T &Selected() { return entries[selected].value; }
79 const T &Selected() const { return entries[selected].value; }
80 const char *SelectedTitle() const { return entries[selected].title; }
81 int SelectedNumber() const { return entries[selected].number; }
82 bool SelectedIsEnabled() const { return entries[selected].enabled; }
84 T &SecondarySelection() { return entries[secondarySelection].value; }
85 const T &SecondarySelection() const { return entries[secondarySelection].value; }
86 const char *SecondaryTitle() const { return entries[secondarySelection].title; }
87 int SecondaryNumber() const { return entries[secondarySelection].number; }
88 bool SecondaryIsEnabled() const { return entries[secondarySelection].enabled; }
90 void SwapSelected() { SwapEntriesAt(selected, secondarySelection); }
91 void SwapEntriesAt(int lhs, int rhs) { std::swap(entries[lhs], entries[rhs]); }
97 void SelectIndex(int index);
98 int SelectedIndex() const { return selected; }
99 int SecondaryIndex() const { return secondarySelection; }
100 bool IsSelected(int index) const { return index == selected; }
102 int EntryCount() const { return entries.size(); }
103 T &ValueAt(int index) { return entries[index].value; }
104 const T &ValueAt(int index) const { return entries[index].value; }
106 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)); }
107 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
108 void Disable(int index) { entries[index].enabled = false; }
109 void Enable(int index) { entries[index].enabled = true; }
110 void Reserve(int n) { entries.reserve(n); }
111 void Clear() { entries.clear(); }
113 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
116 int GetRow(int index) const { return index / cols; }
117 int GetCol(int index) const { return index % cols; }
121 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
122 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
124 const char *additionalText;
130 std::vector<Entry> entries;
132 int secondarySelection;
149 , secondarySelection(0)
151 , state(STATE_ACTIVE) {
156 Menu<T>::Menu(const MenuProperties &p)
159 , secondarySelection(0)
161 , state(STATE_ACTIVE) {
167 int Menu<T>::ColWidth() const {
168 int width(iconSpace);
169 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
170 if (charsPerNumber) {
171 width += font->CharWidth();
173 if (charsPerAdditionalText) {
174 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
180 int Menu<T>::Width() const {
181 return cols * ColWidth() + (cols - 1) * colGap;
185 int Menu<T>::Height() const {
186 return rows * font->CharHeight() + (rows - 1) * rowGap;
191 void Menu<T>::NextItem() {
192 int index(selected + 1);
193 if (wrapX && index % cols == 0) {
200 void Menu<T>::PreviousItem() {
201 int index(selected - 1);
202 if (wrapX && selected % cols == 0) {
209 void Menu<T>::NextRow() {
210 int index(selected + cols);
211 if (wrapY && index >= int(entries.size())) {
212 index -= entries.size();
218 void Menu<T>::PreviousRow() {
219 int index(selected - cols);
220 if (wrapY && index < 0) {
221 index += entries.size();
227 void Menu<T>::SelectIndex(int index) {
228 if (index < 0 || int(entries.size()) <= index) return;
230 if (topRow <= GetRow(selected) - rows) {
231 topRow = GetRow(selected) - rows + 1;
232 } else if (GetRow(selected) < topRow) {
233 topRow = GetRow(selected);
239 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
240 int start(topRow * cols);
241 int slots(rows * cols);
242 int items(entries.size() - start);
243 int end(start + (items < slots ? items : slots));
244 for (int i(0), count(end - start); i < count; ++i) {
245 if (!entries[start + i].title) continue;
246 geometry::Vector<int> iconOffset(
247 (i % cols) * (ColWidth() + colGap),
248 (i / cols) * RowHeight());
250 // Third column hack!
251 // This fixes the position of the "DROP" item in the inventory menu.
253 iconOffset += geometry::Vector<int>(font->CharWidth(), 0);
256 if (entries[start + i].icon) {
257 entries[start + i].icon->Draw(dest, position + iconOffset);
259 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
260 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
261 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
263 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
265 if (charsPerAdditionalText) {
266 textOffset += geometry::Vector<int>(additionalTextGap, 0);
267 if (entries[start + i].additionalText) {
268 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
270 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
273 if (charsPerNumber) {
274 usedFont->DrawChar(delimiter, dest, position + textOffset);
275 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
276 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
279 geometry::Vector<int> cursorOffset(
280 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
281 ((selected - start) / cols) * RowHeight());
286 cursor->Draw(dest, position + cursorOffset);
289 selectedCursor->Draw(dest, position + cursorOffset);
292 cursor->Draw(dest, position + cursorOffset
293 - geometry::Vector<int>(selectedCursor->Width(), 0));
294 if (secondarySelection >= start && secondarySelection <= end) {
295 geometry::Vector<int> secondaryOffset(
296 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
297 ((secondarySelection - start) / cols) * RowHeight());
298 selectedCursor->Draw(dest, position + secondaryOffset);
306 #endif /* GRAPHICS_MENU_H_ */