ScummVM API documentation
PageMenu.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This code is based on the CRAB engine
24  *
25  * Copyright (c) Arvind Raja Yadav
26  *
27  * Licensed under MIT
28  *
29  */
30 
31 #ifndef CRAB_PAGEMENU_H
32 #define CRAB_PAGEMENU_H
33 
34 #include "crab/ui/menu.h"
35 
36 namespace Crab {
37 
38 namespace pyrodactyl {
39 namespace ui {
40 // Sometimes we need multiple pages in a menu, this object does that
41 // Used in save, load, mod and quest menu
42 template<typename T>
43 class PageMenu {
44  // The buttons for cycling between pages of the menu
45  Button _prev, _next;
46 
47  // Each page is stored separately in a menu object
48  Common::Array<Menu<T>> _menu;
49 
50  // Keep track of which page we are at, and how many elements we keep in a page
51  uint _currentPage, _elementsPerPage, _rows, _cols;
52 
53  // The image used for the elements
54  Button _ref;
55 
56  // This vector stores the increments in x,y for each new button
57  Vector2i _inc;
58 
59  // Display "Page 1 of 3" style information for the menu
60  TextData _status;
61  Common::String _info;
62 
63 public:
64  PageMenu() {
65  _currentPage = 0;
66  _elementsPerPage = 1;
67  _rows = 1;
68  _cols = 1;
69  clear();
70  }
71  ~PageMenu() {}
72 
73  void reset() {
74  for (auto &m : _menu)
75  m.reset();
76  }
77 
78  void clear() {
79  _menu.resize(1);
80  _menu[0].clear();
81  _menu[0].useKeyboard(true);
82  }
83 
84  // Get the elements per page
85  uint elementsPerPage() {
86  return _elementsPerPage;
87  }
88 
89  // This is added to the result from handleEvents to calculate the exact position
90  uint index() {
91  return _currentPage * _elementsPerPage;
92  }
93 
94  // The end position of the elements
95  uint indexPlusOne() {
96  return (_currentPage + 1) * _elementsPerPage;
97  }
98 
99  // Get the current page of the menu
100  uint currentPage() {
101  return _currentPage;
102  }
103 
104  void currentPage(int &val) {
105  _currentPage = val;
106  }
107 
108  // Get the index of the hovered element in the menu
109  int hoverIndex() {
110  if (_menu[_currentPage].hoverIndex() >= 0)
111  return (_currentPage * _elementsPerPage) + _menu[_currentPage].hoverIndex();
112 
113  return -1;
114  }
115 
116  // Get the base position of the elements
117  int baseX(const int &count) {
118  return _ref.x + _inc.x * (count % _cols);
119  }
120 
121  int baseY(const int &count) {
122  return _ref.y + _inc.y * (count / _cols);
123  }
124 
125  // This is used to get the coordinates of a button
126  const int &curX(const int &count) {
127  return _menu[_currentPage]._element[count].x;
128  }
129 
130  const int &curY(const int &count) {
131  return _menu[_currentPage]._element[count].y;
132  }
133 
134  void image(const int &slot, const int &page, ButtonImage &bi) {
135  _menu[page]._element[slot].img(bi);
136  }
137 
138  void assignPaths() {
139  for (auto &m : _menu)
140  m.assignPaths();
141  }
142 
143  void useKeyboard(const bool &val) {
144  for (auto &m : _menu)
145  m.useKeyboard(val);
146  }
147 
148  void setUI() {
149  _prev.setUI();
150  _next.setUI();
151  _ref.setUI();
152  _status.setUI();
153 
154  for (auto &m : _menu)
155  m.setUI();
156  }
157 
158  void updateInfo() {
159  _info = numberToString(_currentPage + 1);
160  _info += " of ";
161  _info += numberToString(_menu.size());
162  }
163 
164  void load(rapidxml::xml_node<char> *node) {
165  using namespace pyrodactyl::input;
166  if (nodeValid(node)) {
167  if (nodeValid("prev", node)) {
168  _prev.load(node->first_node("prev"));
169  _prev._hotkey.set(IU_PREV);
170  }
171 
172  if (nodeValid("next", node)) {
173  _next.load(node->first_node("next"));
174  _next._hotkey.set(IU_NEXT);
175  }
176 
177  if (nodeValid("reference", node))
178  _ref.load(node->first_node("reference"));
179 
180  if (nodeValid("inc", node))
181  _inc.load(node->first_node("inc"));
182 
183  if (nodeValid("status", node))
184  _status.load(node->first_node("status"));
185 
186  if (nodeValid("dim", node)) {
187  rapidxml::xml_node<char> *dimnode = node->first_node("dim");
188  loadNum(_rows, "rows", dimnode);
189  loadNum(_cols, "cols", dimnode);
190  _elementsPerPage = _rows * _cols;
191  }
192  }
193  }
194 
195  void add(uint &slot, uint &page) {
196  if (slot >= _elementsPerPage) {
197  ++page;
198  slot = 0;
199  _menu.resize(page + 1);
200  _menu[page].useKeyboard(true);
201  }
202 
203  T b;
204  b.init(_ref, _inc.x * (slot % _cols), _inc.y * (slot / _cols));
205  _menu[page]._element.push_back(b);
206  ++slot;
207 
208  assignPaths();
209  updateInfo();
210  }
211 
212  void add() {
213  uint page = _menu.size() - 1;
214  uint slot = _menu[page]._element.size();
215  add(slot, page);
216  }
217 
218  void erase() {
219  uint page = _menu.size() - 1;
220  _menu[page]._element.pop_back();
221  assignPaths();
222  updateInfo();
223  }
224 
225  int handleEvents(const Common::Event &event) {
226  using namespace pyrodactyl::input;
227 
228  if (_currentPage > 0 && _prev.handleEvents(event) == BUAC_LCLICK) {
229  _currentPage--;
230  updateInfo();
231 
232  if ((int)_currentPage < 0)
233  _currentPage = 0;
234  }
235 
236  if (_currentPage < _menu.size() - 1 && _next.handleEvents(event) == BUAC_LCLICK) {
237  _currentPage++;
238  updateInfo();
239 
240  if (_currentPage >= _menu.size())
241  _currentPage = _menu.size() - 1;
242  }
243 
244  return _menu[_currentPage].handleEvents(event);
245  }
246 
247  void draw() {
248  _status.draw(_info);
249  _menu[_currentPage].draw();
250 
251  if (_currentPage > 0)
252  _prev.draw();
253 
254  if (_currentPage < _menu.size() - 1)
255  _next.draw();
256  }
257 };
258 
260 } // End of namespace ui
261 } // End of namespace pyrodactyl
262 
263 } // End of namespace Crab
264 
265 #endif // CRAB_PAGEMENU_H
Definition: str.h:59
Definition: array.h:52
void clear()
Definition: array.h:320
Definition: button.h:58
void push_back(const T &element)
Definition: array.h:180
Definition: TextData.h:41
Definition: events.h:199
void pop_back()
Definition: array.h:199
size_type size() const
Definition: array.h:315
Definition: moveeffect.h:37
Definition: button.h:86
void resize(size_type newSize)
Definition: array.h:411
Definition: PageMenu.h:43