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(); }
112 void ClearEntry(int at) { entries[at] = Entry(0, T(), false); }
114 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
117 int GetRow(int index) const { return index / cols; }
118 int GetCol(int index) const { return index % cols; }
122 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
123 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
125 const char *additionalText;
131 std::vector<Entry> entries;
133 int secondarySelection;
150 , secondarySelection(0)
152 , state(STATE_ACTIVE) {
157 Menu<T>::Menu(const MenuProperties &p)
160 , secondarySelection(0)
162 , state(STATE_ACTIVE) {
168 int Menu<T>::ColWidth() const {
169 int width(iconSpace);
170 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
171 if (charsPerNumber) {
172 width += font->CharWidth();
174 if (charsPerAdditionalText) {
175 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
181 int Menu<T>::Width() const {
182 return cols * ColWidth() + (cols - 1) * colGap;
186 int Menu<T>::Height() const {
187 return rows * font->CharHeight() + (rows - 1) * rowGap;
192 void Menu<T>::NextItem() {
193 int index(selected + 1);
194 if (wrapX && index % cols == 0) {
201 void Menu<T>::PreviousItem() {
202 int index(selected - 1);
203 if (wrapX && selected % cols == 0) {
210 void Menu<T>::NextRow() {
211 int index(selected + cols);
212 if (wrapY && index >= int(entries.size())) {
213 index -= entries.size();
219 void Menu<T>::PreviousRow() {
220 int index(selected - cols);
221 if (wrapY && index < 0) {
222 index += entries.size();
228 void Menu<T>::SelectIndex(int index) {
229 if (index < 0 || int(entries.size()) <= index) return;
231 if (topRow <= GetRow(selected) - rows) {
232 topRow = GetRow(selected) - rows + 1;
233 } else if (GetRow(selected) < topRow) {
234 topRow = GetRow(selected);
240 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
241 int start(topRow * cols);
242 int slots(rows * cols);
243 int items(entries.size() - start);
244 int end(start + (items < slots ? items : slots));
245 for (int i(0), count(end - start); i < count; ++i) {
246 if (!entries[start + i].title) continue;
247 geometry::Vector<int> iconOffset(
248 (i % cols) * (ColWidth() + colGap),
249 (i / cols) * RowHeight());
251 // Third column hack!
252 // This fixes the position of the "DROP" item in the inventory menu.
254 iconOffset += geometry::Vector<int>(font->CharWidth(), 0);
257 if (entries[start + i].icon) {
258 entries[start + i].icon->Draw(dest, position + iconOffset);
260 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
261 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
262 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
264 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
266 if (charsPerAdditionalText) {
267 textOffset += geometry::Vector<int>(additionalTextGap, 0);
268 if (entries[start + i].additionalText) {
269 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
271 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
274 if (charsPerNumber) {
275 usedFont->DrawChar(delimiter, dest, position + textOffset);
276 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
277 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
280 geometry::Vector<int> cursorOffset(
281 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
282 ((selected - start) / cols) * RowHeight());
283 // Third column hack!
284 // This fixes the position of the "DROP" item in the inventory menu.
285 if (selected % cols == 2) {
286 cursorOffset += geometry::Vector<int>(font->CharWidth(), 0);
292 cursor->Draw(dest, position + cursorOffset);
295 selectedCursor->Draw(dest, position + cursorOffset);
298 cursor->Draw(dest, position + cursorOffset
299 - geometry::Vector<int>(selectedCursor->Width(), 0));
300 if (secondarySelection >= start && secondarySelection <= end) {
301 geometry::Vector<int> secondaryOffset(
302 (secondarySelection % cols) * (ColWidth() + colGap) - cursor->Width(),
303 ((secondarySelection - start) / cols) * RowHeight());
304 selectedCursor->Draw(dest, position + secondaryOffset);
312 #endif /* GRAPHICS_MENU_H_ */