guiGameListMenuCtrl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GuiGameListMenuCtrl_H_
  23. #define _GuiGameListMenuCtrl_H_
  24. #include "gui/core/guiControl.h"
  25. class GuiGameListMenuProfile;
  26. /// \class GuiGameListMenuCtrl
  27. /// A base class for cross platform menu controls that are gamepad friendly.
  28. class GuiGameListMenuCtrl : public GuiControl
  29. {
  30. public:
  31. typedef GuiControl Parent;
  32. typedef GuiGameListMenuProfile Profile;
  33. protected:
  34. /// \struct Row
  35. /// Internal data representation of a single row in the control.
  36. struct Row
  37. {
  38. StringTableEntry mLabel; ///< Text to display in the row as a label
  39. StringTableEntry mScriptCallback; ///< Script callback when row is activated
  40. S32 mIconIndex; ///< Index of the icon to display on the row (-1 = no icon)
  41. S32 mHeightPad; ///< Extra amount to pad above this row
  42. bool mUseHighlightIcon; ///< Toggle the use of the highlight icon
  43. bool mEnabled; ///< If this row is enabled or not (grayed out)
  44. enum Mode
  45. {
  46. Default = 0,
  47. OptionList,
  48. Keybind
  49. };
  50. Mode mMode;
  51. //List options
  52. Vector<StringTableEntry> mOptions; ///< Collection of options available to display
  53. S32 mSelectedOption; ///< Index into mOptions pointing at the selected option
  54. bool mWrapOptions; ///< Determines if options should "wrap around" at the ends
  55. Row() : mLabel(StringTable->EmptyString()), mScriptCallback(StringTable->EmptyString()), mIconIndex(-1), mHeightPad(0), mUseHighlightIcon(false), mEnabled(true),
  56. mSelectedOption(0), mWrapOptions(false), mMode(Mode::Default)
  57. {
  58. VECTOR_SET_ASSOCIATION(mOptions);
  59. }
  60. virtual ~Row() {}
  61. };
  62. public:
  63. /// \return The index of the highlighted row or NO_ROW if none of the rows
  64. /// are currently highlighted.
  65. virtual S32 getHighlighted() const { return mHighlighted; }
  66. /// \return The index of the selected row or NO_ROW if none of the rows are
  67. /// currently selected.
  68. virtual S32 getSelected() const { return mSelected; }
  69. /// Sets the selected row. Only rows that are enabled can be selected. Input is
  70. /// clamped to [0, mRows.size())
  71. ///
  72. /// \param index The index to set as selected.
  73. virtual void setSelected(S32 index);
  74. /// Determines if the specified row is enabled or disabled.
  75. ///
  76. /// \param index Index of the row to check.
  77. /// \return True if the specified row is enabled. False if the row is not
  78. /// enabled or the given index was not valid.
  79. virtual bool isRowEnabled(S32 index) const;
  80. /// Sets a row's enabled status according to the given parameters.
  81. ///
  82. /// \param index The row to set the enabled status of.
  83. /// \param enabled Indicate true to enable the row or false to disable it.
  84. virtual void setRowEnabled(S32 index, bool enabled);
  85. /// Gets the label displayed on the specified row.
  86. ///
  87. /// \param rowIndex Index of the row to get the label of.
  88. /// \return The label for the row.
  89. virtual StringTableEntry getRowLabel(S32 rowIndex) const;
  90. /// Sets the label on the given row.
  91. ///
  92. /// \param rowIndex Index of the row to set the label on.
  93. /// \param label Text to set as the label of the row.
  94. virtual void setRowLabel(S32 rowIndex, const char * label);
  95. /// Adds a row to the control.
  96. ///
  97. /// \param label The text to display on the row as a label.
  98. /// \param callback Name of a script function to use as a callback when this
  99. /// row is activated.
  100. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  101. /// means no icon will be shown on this row.
  102. /// \param yPad [optional] An extra amount of height padding before the row.
  103. /// \param enabled [optional] If this row is initially enabled. Default true.
  104. virtual void addRow(const char* label, const char* callback, S32 icon = -1, S32 yPad = 0, bool useHighlightIcon = true, bool enabled = true, S32 mode = 0);
  105. /// Adds a row to the control.
  106. ///
  107. /// \param label The text to display on the row as a label.
  108. /// \param optionsList A tab separated list of options for the control.
  109. /// \param wrapOptions Specify true to allow options to wrap at the ends or
  110. /// false to prevent wrapping.
  111. /// \param callback [optional] Name of a script function to use as a callback
  112. /// when this row is activated. Default NULL means no callback.
  113. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  114. /// means no icon will be shown on this row.
  115. /// \param yPad [optional] An extra amount of height padding before the row.
  116. /// \param enabled [optional] If this row is initially enabled. Default true.
  117. void addRow(const char* label, const char* optionsList, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled);
  118. /// Gets the text for the currently selected option of the given row.
  119. ///
  120. /// \param rowIndex Index of the row to get the option from.
  121. /// \return A string representing the text currently displayed as the selected
  122. /// option on the given row. If there is no such displayed text then the empty
  123. /// string is returned.
  124. StringTableEntry getCurrentOption(S32 rowIndex) const;
  125. /// Attempts to set the given row to the specified selected option. The option
  126. /// will only be set if the option exists in the control.
  127. ///
  128. /// \param rowIndex Index of the row to set an option on.
  129. /// \param option The option to be made active.
  130. /// \return True if the row contained the option and was set, false otherwise.
  131. bool selectOption(S32 rowIndex, StringTableEntry option);
  132. /// Sets the list of options on the given row.
  133. ///
  134. /// \param rowIndex Index of the row to set options on.
  135. /// \param optionsList A tab separated list of options for the control.
  136. void setOptions(S32 rowIndex, const char* optionsList);
  137. /// Activates the current row. The script callback of the current row will
  138. /// be called (if it has one).
  139. virtual void activateRow();
  140. /// Gets the number of rows in the control.
  141. ///
  142. /// \return The number of rows in this control.
  143. virtual S32 getRowCount() const { return mRows.size(); }
  144. GuiGameListMenuCtrl();
  145. ~GuiGameListMenuCtrl();
  146. void onRender(Point2I offset, const RectI &updateRect);
  147. void onRenderOptionList(Row* row, Point2I currentOffset);
  148. /// Callback when the object is registered with the sim.
  149. ///
  150. /// \return True if the profile was successfully added, false otherwise.
  151. bool onAdd();
  152. /// Callback when the control wakes up.
  153. bool onWake();
  154. /// Callback when a key is pressed.
  155. ///
  156. /// \param event The event that triggered this callback.
  157. bool onKeyDown(const GuiEvent &event);
  158. /// Callback when a key is repeating.
  159. ///
  160. /// \param event The event that triggered this callback.
  161. bool onKeyRepeat(const GuiEvent &event){ return onKeyDown(event); }
  162. /// Callback when the mouse button is clicked on the control.
  163. ///
  164. /// \param event A reference to the event that triggered the callback.
  165. void onMouseDown(const GuiEvent &event);
  166. /// Callback when the mouse is dragged on the control.
  167. ///
  168. /// \param event A reference to the event that triggered the callback.
  169. void onMouseDragged(const GuiEvent &event){ onMouseDown(event); }
  170. /// Callback when the mouse leaves the control.
  171. ///
  172. /// \param event A reference to the event that triggered the callback.
  173. void onMouseLeave(const GuiEvent &event);
  174. /// Callback when the mouse is moving over this control
  175. ///
  176. /// \param event A reference to the event that triggered the callback.
  177. void onMouseMove(const GuiEvent &event);
  178. /// Callback when the mouse button is released.
  179. ///
  180. /// \param event A reference to the event that triggered the callback.
  181. void onMouseUp(const GuiEvent &event);
  182. virtual bool onInputEvent(const InputEventInfo& event);
  183. /// Callback when the gamepad axis is activated.
  184. ///
  185. /// \param event A reference to the event that triggered the callback.
  186. virtual bool onGamepadAxisUp(const GuiEvent & event);
  187. /// Callback when the gamepad axis is activated.
  188. ///
  189. /// \param event A reference to the event that triggered the callback.
  190. virtual bool onGamepadAxisDown(const GuiEvent & event);
  191. /// Callback when the gamepad axis is activated.
  192. ///
  193. /// \param event A reference to the event that triggered the callback.
  194. virtual bool onGamepadAxisLeft(const GuiEvent& event);
  195. /// Callback when the gamepad axis is activated.
  196. ///
  197. /// \param event A reference to the event that triggered the callback.
  198. virtual bool onGamepadAxisRight(const GuiEvent& event);
  199. void clearRows();
  200. RectI getRowBounds(S32 rowIndex);
  201. DECLARE_CONOBJECT(GuiGameListMenuCtrl);
  202. DECLARE_CATEGORY( "Gui Game" );
  203. DECLARE_DESCRIPTION( "Base class for cross platform menu controls that are gamepad friendly." );
  204. /// Initializes fields accessible through the console.
  205. static void initPersistFields();
  206. static const S32 NO_ROW = -1; ///< Indicates a query result of no row found.
  207. static const S32 NO_ICON = -1; ///< Indicates a row has no extra icon available
  208. static const S32 NO_OPTION = -1; ///< Indicates there is no option
  209. protected:
  210. /// Adds a row to the control.
  211. ///
  212. /// \param row A reference to the row object to fill.
  213. /// \param label The text to display on the row as a label.
  214. /// \param callback Name of a script function to use as a callback when this
  215. /// row is activated.
  216. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  217. /// means no icon will be shown on this row.
  218. /// \param yPad [optional] An extra amount of height padding before the row.
  219. /// \param enabled [optional] If this row is initially enabled. Default true.
  220. virtual void addRow(Row * row, const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, S32 mode = 0);
  221. /// Determines if the given index is a valid row index. Any index pointing at
  222. /// an existing row is valid.
  223. ///
  224. /// \param index The index to check for validity.
  225. /// \return True if the index points at a valid row, false otherwise.
  226. virtual bool isValidRowIndex(S32 index) const;
  227. /// Sets the script variable $ThisControl to reflect this control.
  228. virtual void setThisControl();
  229. /// Called to implement debug rendering which displays colored lines to
  230. /// provide visual feedback on extents and hit zones.
  231. virtual void onDebugRender(Point2I offset);
  232. /// Looks up the row having a hit area at the given global point.
  233. ///
  234. /// \param globalPoint The point we want to check for hitting a row.
  235. /// \return The index of the hit row or NO_ROW if no row was hit.
  236. virtual S32 getRow(Point2I globalPoint);
  237. /// Checks to make sure our control has a profile of the correct type.
  238. ///
  239. /// \return True if the profile is of type GuiGameListMenuProfile or false if
  240. /// the profile is of any other type.
  241. virtual bool hasValidProfile() const;
  242. /// Enforces the validity of the fields on this control and its profile (if
  243. /// the profile is valid, see: hasValidProfile).
  244. virtual void enforceConstraints();
  245. /// @name Callbacks
  246. /// @{
  247. DECLARE_CALLBACK( void, onChange, () );
  248. DECLARE_CALLBACK(void, onInputEvent, (const char* device, const char* action, bool state));
  249. DECLARE_CALLBACK(void, onAxisEvent, (const char* device, const char* action, F32 axisValue));
  250. /// @}
  251. /// Evaluates some script. If the command is empty then nothing is evaluated.
  252. ///
  253. /// \param command The script to evaluate.
  254. void doScriptCommand(StringTableEntry command);
  255. StringTableEntry mCallbackOnA; ///< Script callback when the 'A' button is pressed
  256. StringTableEntry mCallbackOnB; ///< Script callback when the 'B' button is pressed
  257. StringTableEntry mCallbackOnX; ///< Script callback when the 'X' button is pressed
  258. StringTableEntry mCallbackOnY; ///< Script callback when the 'Y' button is pressed
  259. bool mDebugRender; ///< Determines when to show debug render lines
  260. Vector<Row *> mRows; ///< Holds data wrappers on all the rows we have
  261. private:
  262. /// Performs a click on the current option row. The x position is used to
  263. /// determine if the left or right arrow were clicked. If one was clicked, the
  264. /// option will be changed. If neither was clicked, the option is unaffected.
  265. /// This method should only be called when there is an actively selected row.
  266. ///
  267. /// \param row The row to perform the click on.
  268. /// \param xPos The x position of the the click, relative to the control.
  269. void clickOption(Row* row, S32 xPos);
  270. /// Changes the option on the currently selected row. If there is no row
  271. /// selected, this method does nothing.
  272. ///
  273. /// \param delta The amount to change the option selection by. Typically this
  274. /// will be 1 or -1.
  275. void changeOption(S32 delta);
  276. /// Changes the option on the given row.
  277. ///
  278. /// \param row The row to change the option on.
  279. /// \param delta The amount to change the option selection by. Typically this
  280. /// will be 1 or -1.
  281. void changeOption(Row* row, S32 delta);
  282. private:
  283. /// Recalculates the height of this control based on the stored row height and
  284. /// and padding on the rows.
  285. virtual Point2I getMinExtent() const;
  286. /// Makes sure the height will allow all rows to be displayed without being
  287. /// truncated.
  288. void updateHeight();
  289. /// Sets the first enabled row as selected. If there are no enabled rows then
  290. /// selected will be set to NO_ROW.
  291. void selectFirstEnabledRow();
  292. /// Changes the currently selected row.
  293. ///
  294. /// \param delta The amount to change the row selection by. Typically this will
  295. /// be 1 or -1.
  296. void changeRow(S32 delta);
  297. S32 mSelected; ///< index of the currently selected row
  298. S32 mHighlighted; ///< index of the currently highlighted row
  299. bool mCallbackOnInputs;
  300. };
  301. /// \class GuiGameListMenuProfile
  302. /// A gui profile with additional fields specific to GuiGameListMenuCtrl.
  303. class GuiGameListMenuProfile : public GuiControlProfile
  304. {
  305. typedef GuiControlProfile Parent;
  306. public:
  307. /// Enforces range constraints on all required fields.
  308. virtual void enforceConstraints();
  309. /// Get the height of rows in this profile. All rows are considered to be the
  310. /// same base height. Rows can have an extra amount of y padding defined when
  311. /// they are added to the control.
  312. ///
  313. /// \return The height of rows in this profile.
  314. S32 getRowHeight() { return (mRowSize.y) ? mRowSize.y : getBitmapArrayRect(TEX_NORMAL).extent.y; }
  315. /// Get the width of rows in this profile. All rows are considered to be the
  316. /// same width.
  317. ///
  318. /// \return The width of rows in this profile.
  319. S32 getRowWidth() { return (mRowSize.x) ? mRowSize.x : getBitmapArrayRect(TEX_NORMAL).extent.x; }
  320. /// Row scale is the ratio between the defined row size and the raw size of
  321. /// the bitmap.
  322. ///
  323. /// \return The row scale.
  324. const Point2F & getRowScale() const { return mRowScale; }
  325. /// Gets the extent of icons for this profile. If there are no icons you will
  326. /// get a point of (0, 0);
  327. ///
  328. /// \return The extent of icons or (0, 0) if there aren't any.
  329. Point2I getIconExtent();
  330. /// Gets the extent of arrows for this profile. If there are no arrows you
  331. /// will get a point of (0, 0).
  332. ///
  333. /// \return The extent of icons or (0, 0) if there aren't any.
  334. Point2I getArrowExtent();
  335. /// Gets the extent of the defined hit area for this profile. If the hit area
  336. /// is not defined then it defaults to the full size of a row.
  337. ///
  338. /// \return The extents of the defined hit area or the full size of the row.
  339. Point2I getHitAreaExtent();
  340. /// Determines if this profile has textures for the left and right arrows.
  341. ///
  342. /// \return True if the profile's bitmap has textures for the arrows, false
  343. /// otherwise.
  344. bool hasArrows(){ return (! getBitmapArrayRect(TEX_FIRST_ARROW).extent.isZero()); }
  345. /// Callback when the object is registered with the sim.
  346. ///
  347. /// \return True if the profile was successfully added, false otherwise.
  348. bool onAdd();
  349. Point2I mHitAreaUpperLeft; ///< Offset for the upper left corner of the hit area
  350. Point2I mHitAreaLowerRight; ///< Offset for the lower right corner of the hit area
  351. Point2I mIconOffset; ///< Offset for a row's extra icon
  352. Point2I mRowSize; ///< The base size of a row
  353. S32 mColumnSplit; ///< Absolute position of the split between columns
  354. S32 mRightPad; ///< Extra padding between the right arrow and the hit area
  355. GuiGameListMenuProfile();
  356. DECLARE_CONOBJECT(GuiGameListMenuProfile);
  357. /// Initializes fields accessible through the console.
  358. static void initPersistFields();
  359. enum
  360. {
  361. TEX_NORMAL = 0, ///< texture index for a normal, unselected row
  362. TEX_SELECTED = 1, ///< texture index for a selected row
  363. TEX_HIGHLIGHT = 2, ///< texture index for a highlighted row (moused over, not selected)
  364. TEX_DISABLED = 3, ///< texture index for a disabled row
  365. TEX_L_ARROW_OFF = 4, ///< texture index for the left arrow of an unselected row
  366. TEX_L_ARROW_ON = 5, ///< texture index for the left arrow of a selected row
  367. TEX_R_ARROW_OFF = 6, ///< texture index for the right arrow of an unselected row
  368. TEX_R_ARROW_ON = 7, ///< texture index for the right arrow of a selected row
  369. TEX_FIRST_ARROW = 4, ///< texture index for the first arrow
  370. TEX_FIRST_ICON = 8, ///< texture index for the first row marker icon
  371. };
  372. private:
  373. Point2F mRowScale; ///< Ratio of row size to actual bitmap size
  374. };
  375. #endif