1 #ifndef GRAPHICS_MENU_H_
2 #define GRAPHICS_MENU_H_
7 #include "../geometry/Vector.h"
15 struct MenuProperties {
16 static const int TYPE_ID = 407;
19 const Font *disabledFont;
21 const Sprite *selectedCursor;
29 int charsPerAdditionalText;
30 int additionalTextGap;
37 : font(0), disabledFont(0), cursor(0), selectedCursor(0)
38 , charsPerEntry(0), rows(1), rowGap(0)
39 , iconSpace(0), cols(1), colGap(0)
40 , charsPerNumber(0), charsPerAdditionalText(0)
41 , additionalTextGap(0), thirdColumnHack(0), delimiter(':')
42 , wrapX(false), wrapY(false) { }
44 static void CreateTypeDescription();
45 static void Construct(void *);
51 : private MenuProperties {
55 Menu(const MenuProperties &);
58 void SetInactive() { state = STATE_INACTIVE; }
59 void SetActive() { state = STATE_ACTIVE; }
60 void SetSelected() { state = STATE_SELECTED; }
61 void SetDualSelection() { state = STATE_DUAL; secondarySelection = selected; }
62 bool IsActive() const { return state == STATE_ACTIVE; }
63 bool HasSelected() const { return state == STATE_SELECTED; }
64 bool InDualMode() const { return state == STATE_DUAL; }
69 int RowHeight() const { return font->CharHeight() + rowGap; }
70 int CharsPerEntry() const { return charsPerEntry; }
72 T &Selected() { return entries[selected].value; }
73 const T &Selected() const { return entries[selected].value; }
74 const char *SelectedTitle() const { return entries[selected].title; }
75 int SelectedNumber() const { return entries[selected].number; }
76 bool SelectedIsEnabled() const { return entries[selected].enabled; }
78 T &SecondarySelection() { return entries[secondarySelection].value; }
79 const T &SecondarySelection() const { return entries[secondarySelection].value; }
80 const char *SecondaryTitle() const { return entries[secondarySelection].title; }
81 int SecondaryNumber() const { return entries[secondarySelection].number; }
82 bool SecondaryIsEnabled() const { return entries[secondarySelection].enabled; }
84 void SwapSelected() { SwapEntriesAt(selected, secondarySelection); }
85 void SwapEntriesAt(int lhs, int rhs) { std::swap(entries[lhs], entries[rhs]); }
91 void SelectIndex(int index);
92 int SelectedIndex() const { return selected; }
93 int SecondaryIndex() const { return secondarySelection; }
94 bool IsSelected(int index) const { return index == selected; }
96 int EntryCount() const { return entries.size(); }
97 T &ValueAt(int index) { return entries[index].value; }
98 const T &ValueAt(int index) const { return entries[index].value; }
100 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)); }
101 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
102 void Disable(int index) { entries[index].enabled = false; }
103 void Enable(int index) { entries[index].enabled = true; }
104 void Reserve(int n) { entries.reserve(n); }
105 void Clear() { entries.clear(); }
106 void ClearEntry(int at) { entries[at] = Entry(0, T(), false); }
108 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
111 int GetRow(int index) const { return index / cols; }
112 int GetCol(int index) const { return index % cols; }
116 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
117 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
119 const char *additionalText;
125 std::vector<Entry> entries;
127 int secondarySelection;
144 , secondarySelection(0)
146 , state(STATE_ACTIVE) {
151 Menu<T>::Menu(const MenuProperties &p)
154 , secondarySelection(0)
156 , state(STATE_ACTIVE) {
162 int Menu<T>::ColWidth() const {
163 int width(iconSpace);
164 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
165 if (charsPerNumber) {
166 width += font->CharWidth();
168 if (charsPerAdditionalText) {
169 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
175 int Menu<T>::Width() const {
176 return cols * ColWidth() + (cols - 1) * colGap;
180 int Menu<T>::Height() const {
181 return rows * font->CharHeight() + (rows - 1) * rowGap;
186 void Menu<T>::NextItem() {
187 int index(selected + 1);
188 if (wrapX && index % cols == 0) {
195 void Menu<T>::PreviousItem() {
196 int index(selected - 1);
197 if (wrapX && selected % cols == 0) {
204 void Menu<T>::NextRow() {
205 int index(selected + cols);
206 if (wrapY && index >= int(entries.size())) {
207 index -= entries.size();
213 void Menu<T>::PreviousRow() {
214 int index(selected - cols);
215 if (wrapY && index < 0) {
216 index += entries.size();
222 void Menu<T>::SelectIndex(int index) {
223 if (index < 0 || int(entries.size()) <= index) return;
225 if (topRow <= GetRow(selected) - rows) {
226 topRow = GetRow(selected) - rows + 1;
227 } else if (GetRow(selected) < topRow) {
228 topRow = GetRow(selected);
234 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
235 int start(topRow * cols);
236 int slots(rows * cols);
237 int items(entries.size() - start);
238 int end(start + (items < slots ? items : slots));
239 for (int i(0), count(end - start); i < count; ++i) {
240 if (!entries[start + i].title) continue;
241 geometry::Vector<int> iconOffset(
242 (i % cols) * (ColWidth() + colGap),
243 (i / cols) * RowHeight());
245 // This fixes the position of the third column of the inventory and capsule menus.
246 if (thirdColumnHack && i % cols == 2) {
247 iconOffset += geometry::Vector<int>(font->CharWidth() * thirdColumnHack, 0);
250 if (entries[start + i].icon) {
251 entries[start + i].icon->Draw(dest, position + iconOffset);
253 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
254 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
255 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
257 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
259 if (charsPerAdditionalText) {
260 textOffset += geometry::Vector<int>(additionalTextGap, 0);
261 if (entries[start + i].additionalText) {
262 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
264 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
267 if (charsPerNumber) {
268 usedFont->DrawChar(delimiter, dest, position + textOffset);
269 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
270 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
273 geometry::Vector<int> cursorOffset(
274 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
275 ((selected - start) / cols) * RowHeight());
276 // This fixes the position of the third column of the inventory and capsule menus.
277 if (thirdColumnHack && selected % cols == 2) {
278 cursorOffset += geometry::Vector<int>(font->CharWidth() * thirdColumnHack, 0);
284 cursor->Draw(dest, position + cursorOffset);
287 selectedCursor->Draw(dest, position + cursorOffset);
290 cursor->Draw(dest, position + cursorOffset
291 - geometry::Vector<int>(selectedCursor->Width(), 0));
292 if (secondarySelection >= start && secondarySelection <= end) {
293 geometry::Vector<int> secondaryOffset(
294 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
295 ((secondarySelection - start) / cols) * RowHeight());
296 selectedCursor->Draw(dest, position + secondaryOffset);
304 #endif /* GRAPHICS_MENU_H_ */