guiGameListMenuCtrl.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. #include "gui/controls/guiBitmapCtrl.h"
  26. class GuiGameListMenuProfile;
  27. /// \class GuiGameListMenuCtrl
  28. /// A base class for cross platform menu controls that are gamepad friendly.
  29. class GuiGameListMenuCtrl : public GuiControl
  30. {
  31. public:
  32. typedef GuiControl Parent;
  33. typedef GuiGameListMenuProfile Profile;
  34. protected:
  35. /// \struct Row
  36. /// Internal data representation of a single row in the control.
  37. struct Row
  38. {
  39. StringTableEntry mLabel; ///< Text to display in the row as a label
  40. StringTableEntry mScriptCallback; ///< Script callback when row is activated
  41. StringTableEntry mTooltip; ///< A descriptive tooltip message for what the row is
  42. S32 mIconIndex; ///< Index of the icon to display on the row (-1 = no icon)
  43. S32 mHeightPad; ///< Extra amount to pad above this row
  44. bool mUseHighlightIcon; ///< Toggle the use of the highlight icon
  45. bool mEnabled; ///< If this row is enabled or not (grayed out)
  46. enum Mode
  47. {
  48. Default = 0,
  49. OptionList,
  50. Slider,
  51. Keybind
  52. };
  53. Mode mMode;
  54. //List options
  55. Vector<StringTableEntry> mOptions; ///< Collection of options available to display
  56. S32 mSelectedOption; ///< Index into mOptions pointing at the selected option
  57. bool mWrapOptions; ///< Determines if options should "wrap around" at the ends
  58. //Slider option
  59. F32 mValue; ///< When working as a slider, this contains the value
  60. F32 mStepSize; ///< When working as a slider, this is the increment levels in the range
  61. Point2F mRange; ///< When working as a slider, this sets our min/max range
  62. //Keybind option
  63. StringTableEntry mBitmap;
  64. GFXTexHandle mBitmapTex;
  65. Row() : mLabel(StringTable->EmptyString()), mScriptCallback(StringTable->EmptyString()), mTooltip(StringTable->EmptyString()), mIconIndex(-1), mHeightPad(0), mUseHighlightIcon(false), mEnabled(true),
  66. mSelectedOption(0), mWrapOptions(false), mMode(Mode::Default), mValue(0), mStepSize(1), mRange(Point2F(0, 1)), mBitmap(StringTable->EmptyString()), mBitmapTex(nullptr)
  67. {
  68. VECTOR_SET_ASSOCIATION(mOptions);
  69. }
  70. virtual ~Row() {}
  71. };
  72. public:
  73. /// \return The index of the highlighted row or NO_ROW if none of the rows
  74. /// are currently highlighted.
  75. virtual S32 getHighlighted() const { return mHighlighted; }
  76. /// \return The index of the selected row or NO_ROW if none of the rows are
  77. /// currently selected.
  78. virtual S32 getSelected() const { return mSelected; }
  79. /// Sets the selected row. Only rows that are enabled can be selected. Input is
  80. /// clamped to [0, mRows.size())
  81. ///
  82. /// \param index The index to set as selected.
  83. virtual void setSelected(S32 index);
  84. /// Determines if the specified row is enabled or disabled.
  85. ///
  86. /// \param index Index of the row to check.
  87. /// \return True if the specified row is enabled. False if the row is not
  88. /// enabled or the given index was not valid.
  89. virtual bool isRowEnabled(S32 index) const;
  90. /// Sets a row's enabled status according to the given parameters.
  91. ///
  92. /// \param index The row to set the enabled status of.
  93. /// \param enabled Indicate true to enable the row or false to disable it.
  94. virtual void setRowEnabled(S32 index, bool enabled);
  95. /// Gets the label displayed on the specified row.
  96. ///
  97. /// \param rowIndex Index of the row to get the label of.
  98. /// \return The label for the row.
  99. virtual StringTableEntry getRowLabel(S32 rowIndex) const;
  100. /// Sets the label on the given row.
  101. ///
  102. /// \param rowIndex Index of the row to set the label on.
  103. /// \param label Text to set as the label of the row.
  104. virtual void setRowLabel(S32 rowIndex, const char * label);
  105. /// Adds a row to the control.
  106. ///
  107. /// \param label The text to display on the row as a label.
  108. /// \param callback Name of a script function to use as a callback when this
  109. /// row is activated.
  110. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  111. /// means no icon will be shown on this row.
  112. /// \param yPad [optional] An extra amount of height padding before the row.
  113. /// \param enabled [optional] If this row is initially enabled. Default true.
  114. virtual void addRow(const char* label, const char* callback, S32 icon = -1, S32 yPad = 0, bool useHighlightIcon = true, bool enabled = true, S32 mode = 0, const char* tooltip = "");
  115. /// Adds a row to the control.
  116. ///
  117. /// \param label The text to display on the row as a label.
  118. /// \param optionsList A tab separated list of options for the control.
  119. /// \param wrapOptions Specify true to allow options to wrap at the ends or
  120. /// false to prevent wrapping.
  121. /// \param callback [optional] Name of a script function to use as a callback
  122. /// when this row is activated. Default NULL means no callback.
  123. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  124. /// means no icon will be shown on this row.
  125. /// \param yPad [optional] An extra amount of height padding before the row.
  126. /// \param enabled [optional] If this row is initially enabled. Default true.
  127. void addRow(const char* label, const char* optionsList, bool wrapOptions, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip = "", const char* defaultValue = "");
  128. /// Adds a row to the control.
  129. ///
  130. /// \param label The text to display on the row as a label.
  131. /// \param defaultValue A float indicating the slider's default value
  132. /// \param increments A float indicating the incremental values the slider snaps along between it's range
  133. /// \param range A Point2F that indicates the minimum and maximum value range
  134. /// \param callback [optional] Name of a script function to use as a callback
  135. /// when this row is activated. Default NULL means no callback.
  136. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  137. /// means no icon will be shown on this row.
  138. /// \param yPad [optional] An extra amount of height padding before the row.
  139. /// \param enabled [optional] If this row is initially enabled. Default true.
  140. void addRow(const char* label, F32 defaultValue, F32 increments, Point2F range, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip = "");
  141. void addRow(const char* label, const char* bitmapName, const char* callback, S32 icon, S32 yPad, bool enabled, const char* tooltip);
  142. //Removes row at the provided index
  143. void GuiGameListMenuCtrl::removeRow(const S32& row);
  144. /// Gets the text for the currently selected option of the given row.
  145. ///
  146. /// \param rowIndex Index of the row to get the option from.
  147. /// \return A string representing the text currently displayed as the selected
  148. /// option on the given row. If there is no such displayed text then the empty
  149. /// string is returned.
  150. StringTableEntry getCurrentOption(S32 rowIndex) const;
  151. /// Attempts to set the given row to the specified selected option. The option
  152. /// will only be set if the option exists in the control.
  153. ///
  154. /// \param rowIndex Index of the row to set an option on.
  155. /// \param option The option to be made active.
  156. /// \return True if the row contained the option and was set, false otherwise.
  157. bool selectOption(S32 rowIndex, StringTableEntry option);
  158. /// Sets the list of options on the given row.
  159. ///
  160. /// \param rowIndex Index of the row to set options on.
  161. /// \param optionsList A tab separated list of options for the control.
  162. void setOptions(S32 rowIndex, const char* optionsList);
  163. /// Activates the current row. The script callback of the current row will
  164. /// be called (if it has one).
  165. virtual void activateRow();
  166. /// Gets the number of rows in the control.
  167. ///
  168. /// \return The number of rows in this control.
  169. virtual S32 getRowCount() const { return mRows.size(); }
  170. /// Gets the value of a row
  171. ///
  172. /// \param rowIndex Index of the row to get the value of.
  173. F32 getValue(S32 rowIndex);
  174. /// Sets the value of a row
  175. ///
  176. /// \param rowIndex Index of the row to set the value of.
  177. /// \param value The new value to be set.
  178. void setValue(S32 rowIndex, F32 value);
  179. /// Gets the tooltip of a row
  180. ///
  181. /// \param rowIndex Index of the row to get the tooltip of.
  182. const char* getTooltip(S32 rowIndex);
  183. GuiGameListMenuCtrl();
  184. ~GuiGameListMenuCtrl();
  185. void onRender(Point2I offset, const RectI &updateRect);
  186. void onRenderListOption(Row* row, Point2I currentOffset);
  187. void onRenderSliderOption(Row* row, Point2I currentOffset);
  188. void onRenderKeybindOption(Row* row, Point2I currentOffset);
  189. /// Callback when the object is registered with the sim.
  190. ///
  191. /// \return True if the profile was successfully added, false otherwise.
  192. bool onAdd();
  193. /// Callback when the control wakes up.
  194. bool onWake();
  195. /// Callback when a key is pressed.
  196. ///
  197. /// \param event The event that triggered this callback.
  198. bool onKeyDown(const GuiEvent &event);
  199. /// Callback when a key is repeating.
  200. ///
  201. /// \param event The event that triggered this callback.
  202. bool onKeyRepeat(const GuiEvent &event){ return onKeyDown(event); }
  203. /// Callback when the mouse button is clicked on the control.
  204. ///
  205. /// \param event A reference to the event that triggered the callback.
  206. void onMouseDown(const GuiEvent &event);
  207. /// Callback when the mouse is dragged on the control.
  208. ///
  209. /// \param event A reference to the event that triggered the callback.
  210. void onMouseDragged(const GuiEvent &event){ onMouseDown(event); }
  211. /// Callback when the mouse leaves the control.
  212. ///
  213. /// \param event A reference to the event that triggered the callback.
  214. void onMouseLeave(const GuiEvent &event);
  215. /// Callback when the mouse is moving over this control
  216. ///
  217. /// \param event A reference to the event that triggered the callback.
  218. void onMouseMove(const GuiEvent &event);
  219. /// Callback when the mouse button is released.
  220. ///
  221. /// \param event A reference to the event that triggered the callback.
  222. void onMouseUp(const GuiEvent &event);
  223. virtual bool onInputEvent(const InputEventInfo& event);
  224. /// Callback when the gamepad axis is activated.
  225. ///
  226. /// \param event A reference to the event that triggered the callback.
  227. virtual bool onGamepadAxisUp(const GuiEvent & event);
  228. /// Callback when the gamepad axis is activated.
  229. ///
  230. /// \param event A reference to the event that triggered the callback.
  231. virtual bool onGamepadAxisDown(const GuiEvent & event);
  232. /// Callback when the gamepad axis is activated.
  233. ///
  234. /// \param event A reference to the event that triggered the callback.
  235. virtual bool onGamepadAxisLeft(const GuiEvent& event);
  236. /// Callback when the gamepad axis is activated.
  237. ///
  238. /// \param event A reference to the event that triggered the callback.
  239. virtual bool onGamepadAxisRight(const GuiEvent& event);
  240. void clearRows();
  241. void refresh();
  242. RectI getRowBounds(S32 rowIndex);
  243. DECLARE_CONOBJECT(GuiGameListMenuCtrl);
  244. DECLARE_CATEGORY( "Gui Game" );
  245. DECLARE_DESCRIPTION( "Base class for cross platform menu controls that are gamepad friendly." );
  246. /// Initializes fields accessible through the console.
  247. static void initPersistFields();
  248. static const S32 NO_ROW = -1; ///< Indicates a query result of no row found.
  249. static const S32 NO_ICON = -1; ///< Indicates a row has no extra icon available
  250. static const S32 NO_OPTION = -1; ///< Indicates there is no option
  251. protected:
  252. /// Adds a row to the control.
  253. ///
  254. /// \param row A reference to the row object to fill.
  255. /// \param label The text to display on the row as a label.
  256. /// \param callback Name of a script function to use as a callback when this
  257. /// row is activated.
  258. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  259. /// means no icon will be shown on this row.
  260. /// \param yPad [optional] An extra amount of height padding before the row.
  261. /// \param enabled [optional] If this row is initially enabled. Default true.
  262. virtual void addRow(Row * row, const char* label, const char* callback, S32 icon, S32 yPad, bool useHighlightIcon, bool enabled, S32 mode = 0, const char* tooltip = "");
  263. /// Determines if the given index is a valid row index. Any index pointing at
  264. /// an existing row is valid.
  265. ///
  266. /// \param index The index to check for validity.
  267. /// \return True if the index points at a valid row, false otherwise.
  268. virtual bool isValidRowIndex(S32 index) const;
  269. /// Sets the script variable $ThisControl to reflect this control.
  270. virtual void setThisControl();
  271. /// Called to implement debug rendering which displays colored lines to
  272. /// provide visual feedback on extents and hit zones.
  273. virtual void onDebugRender(Point2I offset);
  274. /// Looks up the row having a hit area at the given global point.
  275. ///
  276. /// \param globalPoint The point we want to check for hitting a row.
  277. /// \return The index of the hit row or NO_ROW if no row was hit.
  278. virtual S32 getRow(Point2I globalPoint);
  279. /// Checks to make sure our control has a profile of the correct type.
  280. ///
  281. /// \return True if the profile is of type GuiGameListMenuProfile or false if
  282. /// the profile is of any other type.
  283. virtual bool hasValidProfile() const;
  284. /// Enforces the validity of the fields on this control and its profile (if
  285. /// the profile is valid, see: hasValidProfile).
  286. virtual void enforceConstraints();
  287. /// @name Callbacks
  288. /// @{
  289. DECLARE_CALLBACK( void, onChange, () );
  290. DECLARE_CALLBACK(void, onInputEvent, (const char* device, const char* action, bool state));
  291. DECLARE_CALLBACK(void, onAxisEvent, (const char* device, const char* action, F32 axisValue));
  292. /// @}
  293. /// Evaluates some script. If the command is empty then nothing is evaluated.
  294. ///
  295. /// \param command The script to evaluate.
  296. void doScriptCommand(StringTableEntry command);
  297. StringTableEntry mCallbackOnA; ///< Script callback when the 'A' button is pressed
  298. StringTableEntry mCallbackOnB; ///< Script callback when the 'B' button is pressed
  299. StringTableEntry mCallbackOnX; ///< Script callback when the 'X' button is pressed
  300. StringTableEntry mCallbackOnY; ///< Script callback when the 'Y' button is pressed
  301. bool mDebugRender; ///< Determines when to show debug render lines
  302. Vector<Row *> mRows; ///< Holds data wrappers on all the rows we have
  303. private:
  304. /// Performs a click on the current option row. The x position is used to
  305. /// determine if the left or right arrow were clicked. If one was clicked, the
  306. /// option will be changed. If neither was clicked, the option is unaffected.
  307. /// This method should only be called when there is an actively selected row.
  308. ///
  309. /// \param row The row to perform the click on.
  310. /// \param xPos The x position of the the click, relative to the control.
  311. void clickOption(Row* row, S32 xPos);
  312. /// Changes the option on the currently selected row. If there is no row
  313. /// selected, this method does nothing.
  314. ///
  315. /// \param delta The amount to change the option selection by. Typically this
  316. /// will be 1 or -1.
  317. void changeOption(S32 delta);
  318. /// Changes the option on the given row.
  319. ///
  320. /// \param row The row to change the option on.
  321. /// \param delta The amount to change the option selection by. Typically this
  322. /// will be 1 or -1.
  323. void changeOption(Row* row, S32 delta);
  324. /// Performs a click on the current slider row. The x position is used to
  325. /// determine if the left or right arrow were clicked, or if it landed somewhere on the sliderbar.
  326. /// If one was clicked, the option will be changed. If neither was clicked, the option is unaffected.
  327. /// This method should only be called when there is an actively selected row.
  328. ///
  329. /// \param row The row to perform the click on.
  330. /// \param xPos The x position of the the click, relative to the control.
  331. void clickSlider(Row* row, S32 xPos);
  332. void clickKeybind(Row* row, S32 xPos);
  333. private:
  334. /// Recalculates the height of this control based on the stored row height and
  335. /// and padding on the rows.
  336. virtual Point2I getMinExtent() const;
  337. /// Makes sure the height will allow all rows to be displayed without being
  338. /// truncated.
  339. void updateHeight();
  340. /// Sets the first enabled row as selected. If there are no enabled rows then
  341. /// selected will be set to NO_ROW.
  342. void selectFirstEnabledRow();
  343. /// Changes the currently selected row.
  344. ///
  345. /// \param delta The amount to change the row selection by. Typically this will
  346. /// be 1 or -1.
  347. void changeRow(S32 delta);
  348. S32 mSelected; ///< index of the currently selected row
  349. S32 mHighlighted; ///< index of the currently highlighted row
  350. bool mCallbackOnInputs;
  351. };
  352. /// \class GuiGameListMenuProfile
  353. /// A gui profile with additional fields specific to GuiGameListMenuCtrl.
  354. class GuiGameListMenuProfile : public GuiControlProfile
  355. {
  356. typedef GuiControlProfile Parent;
  357. public:
  358. /// Enforces range constraints on all required fields.
  359. virtual void enforceConstraints();
  360. /// Get the height of rows in this profile. All rows are considered to be the
  361. /// same base height. Rows can have an extra amount of y padding defined when
  362. /// they are added to the control.
  363. ///
  364. /// \return The height of rows in this profile.
  365. S32 getRowHeight() { return (mRowSize.y) ? mRowSize.y : getBitmapArrayRect(TEX_NORMAL).extent.y; }
  366. /// Get the width of rows in this profile. All rows are considered to be the
  367. /// same width.
  368. ///
  369. /// \return The width of rows in this profile.
  370. S32 getRowWidth() { return (mRowSize.x) ? mRowSize.x : getBitmapArrayRect(TEX_NORMAL).extent.x; }
  371. /// Row scale is the ratio between the defined row size and the raw size of
  372. /// the bitmap.
  373. ///
  374. /// \return The row scale.
  375. const Point2F & getRowScale() const { return mRowScale; }
  376. /// Gets the extent of icons for this profile. If there are no icons you will
  377. /// get a point of (0, 0);
  378. ///
  379. /// \return The extent of icons or (0, 0) if there aren't any.
  380. Point2I getIconExtent();
  381. /// Gets the extent of arrows for this profile. If there are no arrows you
  382. /// will get a point of (0, 0).
  383. ///
  384. /// \return The extent of icons or (0, 0) if there aren't any.
  385. Point2I getArrowExtent();
  386. /// Gets the extent of the defined hit area for this profile. If the hit area
  387. /// is not defined then it defaults to the full size of a row.
  388. ///
  389. /// \return The extents of the defined hit area or the full size of the row.
  390. Point2I getHitAreaExtent();
  391. /// Determines if this profile has textures for the left and right arrows.
  392. ///
  393. /// \return True if the profile's bitmap has textures for the arrows, false
  394. /// otherwise.
  395. bool hasArrows(){ return (! getBitmapArrayRect(TEX_FIRST_ARROW).extent.isZero()); }
  396. /// Callback when the object is registered with the sim.
  397. ///
  398. /// \return True if the profile was successfully added, false otherwise.
  399. bool onAdd();
  400. Point2I mHitAreaUpperLeft; ///< Offset for the upper left corner of the hit area
  401. Point2I mHitAreaLowerRight; ///< Offset for the lower right corner of the hit area
  402. Point2I mIconOffset; ///< Offset for a row's extra icon
  403. Point2I mRowSize; ///< The base size of a row
  404. S32 mColumnSplit; ///< Absolute position of the split between columns
  405. S32 mRightPad; ///< Extra padding between the right arrow and the hit area
  406. GuiGameListMenuProfile();
  407. DECLARE_CONOBJECT(GuiGameListMenuProfile);
  408. /// Initializes fields accessible through the console.
  409. static void initPersistFields();
  410. enum
  411. {
  412. TEX_NORMAL = 0, ///< texture index for a normal, unselected row
  413. TEX_SELECTED = 1, ///< texture index for a selected row
  414. TEX_HIGHLIGHT = 2, ///< texture index for a highlighted row (moused over, not selected)
  415. TEX_DISABLED = 3, ///< texture index for a disabled row
  416. TEX_L_ARROW_OFF = 4, ///< texture index for the left arrow of an unselected row
  417. TEX_L_ARROW_ON = 5, ///< texture index for the left arrow of a selected row
  418. TEX_R_ARROW_OFF = 6, ///< texture index for the right arrow of an unselected row
  419. TEX_R_ARROW_ON = 7, ///< texture index for the right arrow of a selected row
  420. TEX_FIRST_ARROW = 4, ///< texture index for the first arrow
  421. TEX_FIRST_ICON = 8, ///< texture index for the first row marker icon
  422. };
  423. private:
  424. Point2F mRowScale; ///< Ratio of row size to actual bitmap size
  425. };
  426. #endif