4 * Created on: Aug 8, 2012
8 #ifndef GRAPHICS_MENU_H_
9 #define GRAPHICS_MENU_H_
14 #include "../geometry/Vector.h"
21 struct MenuProperties {
22 static const int TYPE_ID = 407;
25 const Font *disabledFont;
27 const Sprite *selectedCursor;
35 int charsPerAdditionalText;
36 int additionalTextGap;
42 : font(0), disabledFont(0), cursor(0), selectedCursor(0)
43 , charsPerEntry(0), rows(1), rowGap(0)
44 , iconSpace(0), cols(1), colGap(0)
45 , charsPerNumber(0), charsPerAdditionalText(0)
46 , additionalTextGap(0), delimiter(':')
47 , wrapX(false), wrapY(false) { }
49 static void CreateTypeDescription();
50 static void Construct(void *);
56 : private MenuProperties {
60 Menu(const MenuProperties &);
63 void SetInactive() { state = STATE_INACTIVE; }
64 void SetActive() { state = STATE_ACTIVE; }
65 void SetSelected() { state = STATE_SELECTED; }
66 bool IsActive() const { return state == STATE_ACTIVE; }
67 bool HasSelected() const { return state == STATE_SELECTED; }
72 int RowHeight() const { return font->CharHeight() + rowGap; }
73 int CharsPerEntry() const { return charsPerEntry; }
75 T &Selected() { return entries[selected].value; }
76 const T &Selected() const { return entries[selected].value; }
77 const char *SelectedTitle() const { return entries[selected].title; }
78 int SelectedNumber() const { return entries[selected].number; }
79 bool SelectedIsEnabled() const { return entries[selected].enabled; }
85 void SelectIndex(int index);
86 int SelectedIndex() const { return selected; }
87 bool IsSelected(int index) const { return index == selected; }
89 int EntryCount() const { return entries.size(); }
90 T &ValueAt(int index) { return entries[index].value; }
91 const T &ValueAt(int index) const { return entries[index].value; }
93 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)); }
94 void AddEmptyEntry() { entries.push_back(Entry(0, T(), false)); }
95 void Disable(int index) { entries[index].enabled = false; }
96 void Enable(int index) { entries[index].enabled = true; }
97 void Reserve(int n) { entries.reserve(n); }
98 void Clear() { entries.clear(); }
100 void Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const;
103 int GetRow(int index) const { return index / cols; }
104 int GetCol(int index) const { return index % cols; }
108 Entry(const char *title, const T &value, bool enabled = true, const Sprite *icon = 0, int number = 0, const char *additionalText = 0)
109 : title(title), additionalText(additionalText), icon(icon), number(number), value(value), enabled(enabled) { }
111 const char *additionalText;
117 std::vector<Entry> entries;
135 , state(STATE_ACTIVE) {
140 Menu<T>::Menu(const MenuProperties &p)
144 , state(STATE_ACTIVE) {
150 int Menu<T>::ColWidth() const {
151 int width(iconSpace);
152 width += font->CharWidth() * (charsPerEntry + charsPerNumber);
153 if (charsPerNumber) {
154 width += font->CharWidth();
156 if (charsPerAdditionalText) {
157 width += additionalTextGap + charsPerAdditionalText * font->CharWidth();
163 int Menu<T>::Width() const {
164 return cols * ColWidth() + (cols - 1) * colGap;
168 int Menu<T>::Height() const {
169 return rows * font->CharHeight() + (rows - 1) * rowGap;
174 void Menu<T>::NextItem() {
175 int index(selected + 1);
176 if (wrapX && index % cols == 0) {
183 void Menu<T>::PreviousItem() {
184 int index(selected - 1);
185 if (wrapX && selected % cols == 0) {
192 void Menu<T>::NextRow() {
193 int index(selected + cols);
194 if (wrapY && index >= int(entries.size())) {
195 index -= entries.size();
201 void Menu<T>::PreviousRow() {
202 int index(selected - cols);
203 if (wrapY && index < 0) {
204 index += entries.size();
210 void Menu<T>::SelectIndex(int index) {
211 if (index < 0 || int(entries.size()) <= index) return;
213 if (topRow <= GetRow(selected) - rows) {
214 topRow = GetRow(selected) - rows + 1;
215 } else if (GetRow(selected) < topRow) {
216 topRow = GetRow(selected);
222 void Menu<T>::Draw(SDL_Surface *dest, const geometry::Vector<int> &position) const {
223 int start(topRow * cols);
224 int slots(rows * cols);
225 int items(entries.size() - start);
226 int end(start + (items < slots ? items : slots));
227 for (int i(0), count(end - start); i < count; ++i) {
228 if (!entries[start + i].title) continue;
229 geometry::Vector<int> iconOffset(
230 (i % cols) * (ColWidth() + colGap),
231 (i / cols) * RowHeight());
233 // Third column hack!
234 // This fixes the position of the "DROP" item in the inventory menu.
236 iconOffset += geometry::Vector<int>(font->CharWidth(), 0);
239 if (entries[start + i].icon) {
240 entries[start + i].icon->Draw(dest, position + iconOffset);
242 geometry::Vector<int> textOffset(iconOffset.X() + iconSpace, iconOffset.Y());
243 const Font *usedFont(entries[start + i].enabled ? font : disabledFont);
244 usedFont->DrawString(entries[start + i].title, dest, position + textOffset, charsPerEntry);
246 textOffset += geometry::Vector<int>(charsPerEntry * usedFont->CharWidth(), 0);
248 if (charsPerAdditionalText) {
249 textOffset += geometry::Vector<int>(additionalTextGap, 0);
250 if (entries[start + i].additionalText) {
251 usedFont->DrawString(entries[start + i].additionalText, dest, position + textOffset, charsPerAdditionalText);
253 textOffset += geometry::Vector<int>(charsPerAdditionalText * usedFont->CharWidth(), 0);
256 if (charsPerNumber) {
257 usedFont->DrawChar(delimiter, dest, position + textOffset);
258 textOffset += geometry::Vector<int>(usedFont->CharWidth(), 0);
259 usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber);
262 geometry::Vector<int> cursorOffset(
263 (selected % cols) * (ColWidth() + colGap) - cursor->Width(),
264 ((selected - start) / cols) * RowHeight());
269 cursor->Draw(dest, position + cursorOffset);
272 selectedCursor->Draw(dest, position + cursorOffset);
279 #endif /* GRAPHICS_MENU_H_ */