1 #ifndef GRAPHICS_MENU_H_
2 #define GRAPHICS_MENU_H_
6 #include "../math/Vector.h"
14 struct MenuProperties {
15 static const int TYPE_ID = 407;
18 const Font *disabledFont;
20 const Sprite *selectedCursor;
28 int charsPerAdditionalText;
29 int additionalTextGap;
36 : font(0), disabledFont(0), cursor(0), selectedCursor(0)
37 , charsPerEntry(0), rows(1), rowGap(0)
38 , iconSpace(0), cols(1), colGap(0)
39 , charsPerNumber(0), charsPerAdditionalText(0)
40 , additionalTextGap(0), thirdColumnHack(0), delimiter(':')
41 , wrapX(false), wrapY(false) { }
43 static void CreateTypeDescription();
44 static void Construct(void *);
50 : private MenuProperties {
54 Menu(const MenuProperties &);
57 void SetInactive() { state = STATE_INACTIVE; }
58 void SetActive() { state = STATE_ACTIVE; }
59 void SetSelected() { state = STATE_SELECTED; }
60 void SetDualSelection() { state = STATE_DUAL; secondarySelection = selected; }
61 bool IsActive() const { return state == STATE_ACTIVE; }
62 bool HasSelected() const { return state == STATE_SELECTED; }
63 bool InDualMode() const { return state == STATE_DUAL; }
68 int RowHeight() const { return font->CharHeight() + rowGap; }
69 int CharsPerEntry() const { return charsPerEntry; }
71 T &Selected() { return entries[selected].value; }
72 const T &Selected() const { return entries[selected].value; }
73 const char *SelectedTitle() const { return entries[selected].title; }
74 int SelectedNumber() const { return entries[selected].number; }
75 bool SelectedIsEnabled() const { return entries[selected].enabled; }
77 T &SecondarySelection() { return entries[secondarySelection].value; }
78 const T &SecondarySelection() const { return entries[secondarySelection].value; }
79 const char *SecondaryTitle() const { return entries[secondarySelection].title; }
80 int SecondaryNumber() const { return entries[secondarySelection].number; }
81 bool SecondaryIsEnabled() const { return entries[secondarySelection].enabled; }
83 void SwapSelected() { SwapEntriesAt(selected, secondarySelection); }
84 void SwapEntriesAt(int lhs, int rhs) { std::swap(entries[lhs], entries[rhs]); }
90 void SelectIndex(int index);
91 int SelectedIndex() const { return selected; }
92 int SecondaryIndex() const { return secondarySelection; }
93 bool IsSelected(int index) const { return index == selected; }
95 int EntryCount() const { return entries.size(); }
96 T &ValueAt(int index) { return entries[index].value; }
97 const T &ValueAt(int index) const { return entries[index].value; }
99 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)); }
100 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
101 void Disable(int index) { entries[index].enabled = false; }
102 void Enable(int index) { entries[index].enabled = true; }
103 void Reserve(int n) { entries.reserve(n); }
104 void Clear() { entries.clear(); }
105 void ClearEntry(int at) { entries[at] = Entry(0, T(), false); }
107 void Draw(SDL_Surface *dest, const math::Vector<int> &position) const;
110 int GetRow(int index) const { return index / cols; }
111 int GetCol(int index) const { return index % cols; }
115 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
116 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
118 const char *additionalText;
124 std::vector<Entry> entries;
126 int secondarySelection;
143 , secondarySelection(0)
145 , state(STATE_ACTIVE) {
150 Menu<T>::Menu(const MenuProperties &p)
153 , secondarySelection(0)
155 , state(STATE_ACTIVE) {
161 int Menu<T>::ColWidth() const {
162 int width(iconSpace);
163 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
164 if (charsPerNumber) {
165 width += font->CharWidth();
167 if (charsPerAdditionalText) {
168 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
174 int Menu<T>::Width() const {
175 return cols * ColWidth() + (cols - 1) * colGap;
179 int Menu<T>::Height() const {
180 return rows * font->CharHeight() + (rows - 1) * rowGap;
185 void Menu<T>::NextItem() {
186 int index(selected + 1);
187 if (wrapX && index % cols == 0) {
194 void Menu<T>::PreviousItem() {
195 int index(selected - 1);
196 if (wrapX && selected % cols == 0) {
203 void Menu<T>::NextRow() {
204 int index(selected + cols);
205 if (wrapY && index >= int(entries.size())) {
206 index -= entries.size();
212 void Menu<T>::PreviousRow() {
213 int index(selected - cols);
214 if (wrapY && index < 0) {
215 index += entries.size();
221 void Menu<T>::SelectIndex(int index) {
222 if (index < 0 || int(entries.size()) <= index) return;
224 if (topRow <= GetRow(selected) - rows) {
225 topRow = GetRow(selected) - rows + 1;
226 } else if (GetRow(selected) < topRow) {
227 topRow = GetRow(selected);
233 void Menu<T>::Draw(SDL_Surface *dest, const math::Vector<int> &position) const {
234 int start(topRow * cols);
235 int slots(rows * cols);
236 int items(entries.size() - start);
237 int end(start + (items < slots ? items : slots));
238 for (int i(0), count(end - start); i < count; ++i) {
239 if (!entries[start + i].title) continue;
240 math::Vector<int> iconOffset(
241 (i % cols) * (ColWidth() + colGap),
242 (i / cols) * RowHeight());
244 // This fixes the position of the third column of the inventory and capsule menus.
245 if (thirdColumnHack && i % cols == 2) {
246 iconOffset += math::Vector<int>(font->CharWidth() * thirdColumnHack, 0);
249 if (entries[start + i].icon) {
250 entries[start + i].icon->Draw(dest, position + iconOffset);
252 math::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
253 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
254 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
256 textOffset += math::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
258 if (charsPerAdditionalText) {
259 textOffset += math::Vector<int>(additionalTextGap, 0);
260 if (entries[start + i].additionalText) {
261 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
263 textOffset += math::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
266 if (charsPerNumber) {
267 usedFont->DrawChar(delimiter, dest, position + textOffset);
268 textOffset += math::Vector<int>(usedFont->CharWidth(), 0);
269 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
272 math::Vector<int> cursorOffset(
273 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
274 ((selected - start) / cols) * RowHeight());
275 // This fixes the position of the third column of the inventory and capsule menus.
276 if (thirdColumnHack && selected % cols == 2) {
277 cursorOffset += math::Vector<int>(font->CharWidth() * thirdColumnHack, 0);
283 cursor->Draw(dest, position + cursorOffset);
286 selectedCursor->Draw(dest, position + cursorOffset);
289 cursor->Draw(dest, position + cursorOffset
290 - math::Vector<int>(selectedCursor->Width(), 0));
291 if (secondarySelection >= start && secondarySelection <= end) {
292 math::Vector<int> secondaryOffset(
293 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
294 ((secondarySelection - start) / cols) * RowHeight());
295 selectedCursor->Draw(dest, position + secondaryOffset);
303 #endif /* GRAPHICS_MENU_H_ */