guiGameListMenuCtrl.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. /// Gets the text for the currently selected option of the given row.
  143. ///
  144. /// \param rowIndex Index of the row to get the option from.
  145. /// \return A string representing the text currently displayed as the selected
  146. /// option on the given row. If there is no such displayed text then the empty
  147. /// string is returned.
  148. StringTableEntry getCurrentOption(S32 rowIndex) const;
  149. /// Attempts to set the given row to the specified selected option. The option
  150. /// will only be set if the option exists in the control.
  151. ///
  152. /// \param rowIndex Index of the row to set an option on.
  153. /// \param option The option to be made active.
  154. /// \return True if the row contained the option and was set, false otherwise.
  155. bool selectOption(S32 rowIndex, StringTableEntry option);
  156. /// Sets the list of options on the given row.
  157. ///
  158. /// \param rowIndex Index of the row to set options on.
  159. /// \param optionsList A tab separated list of options for the control.
  160. void setOptions(S32 rowIndex, const char* optionsList);
  161. /// Activates the current row. The script callback of the current row will
  162. /// be called (if it has one).
  163. virtual void activateRow();
  164. /// Gets the number of rows in the control.
  165. ///
  166. /// \return The number of rows in this control.
  167. virtual S32 getRowCount() const { return mRows.size(); }
  168. /// Gets the value of a row
  169. ///
  170. /// \param rowIndex Index of the row to get the value of.
  171. F32 getValue(S32 rowIndex);
  172. /// Sets the value of a row
  173. ///
  174. /// \param rowIndex Index of the row to set the value of.
  175. /// \param value The new value to be set.
  176. void setValue(S32 rowIndex, F32 value);
  177. /// Gets the tooltip of a row
  178. ///
  179. /// \param rowIndex Index of the row to get the tooltip of.
  180. const char* getTooltip(S32 rowIndex);
  181. GuiGameListMenuCtrl();
  182. ~GuiGameListMenuCtrl();
  183. void onRender(Point2I offset, const RectI &updateRect);
  184. void onRenderListOption(Row* row, Point2I currentOffset);
  185. void onRenderSliderOption(Row* row, Point2I currentOffset);
  186. void onRenderKeybindOption(Row* row, Point2I currentOffset);
  187. /// Callback when the object is registered with the sim.
  188. ///
  189. /// \return True if the profile was successfully added, false otherwise.
  190. bool onAdd();
  191. /// Callback when the control wakes up.
  192. bool onWake();
  193. /// Callback when a key is pressed.
  194. ///
  195. /// \param event The event that triggered this callback.
  196. bool onKeyDown(const GuiEvent &event);
  197. /// Callback when a key is repeating.
  198. ///
  199. /// \param event The event that triggered this callback.
  200. bool onKeyRepeat(const GuiEvent &event){ return onKeyDown(event); }
  201. /// Callback when the mouse button is clicked on the control.
  202. ///
  203. /// \param event A reference to the event that triggered the callback.
  204. void onMouseDown(const GuiEvent &event);
  205. /// Callback when the mouse is dragged on the control.
  206. ///
  207. /// \param event A reference to the event that triggered the callback.
  208. void onMouseDragged(const GuiEvent &event){ onMouseDown(event); }
  209. /// Callback when the mouse leaves the control.
  210. ///
  211. /// \param event A reference to the event that triggered the callback.
  212. void onMouseLeave(const GuiEvent &event);
  213. /// Callback when the mouse is moving over this control
  214. ///
  215. /// \param event A reference to the event that triggered the callback.
  216. void onMouseMove(const GuiEvent &event);
  217. /// Callback when the mouse button is released.
  218. ///
  219. /// \param event A reference to the event that triggered the callback.
  220. void onMouseUp(const GuiEvent &event);
  221. virtual bool onInputEvent(const InputEventInfo& event);
  222. /// Callback when the gamepad axis is activated.
  223. ///
  224. /// \param event A reference to the event that triggered the callback.
  225. virtual bool onGamepadAxisUp(const GuiEvent & event);
  226. /// Callback when the gamepad axis is activated.
  227. ///
  228. /// \param event A reference to the event that triggered the callback.
  229. virtual bool onGamepadAxisDown(const GuiEvent & event);
  230. /// Callback when the gamepad axis is activated.
  231. ///
  232. /// \param event A reference to the event that triggered the callback.
  233. virtual bool onGamepadAxisLeft(const GuiEvent& event);
  234. /// Callback when the gamepad axis is activated.
  235. ///
  236. /// \param event A reference to the event that triggered the callback.
  237. virtual bool onGamepadAxisRight(const GuiEvent& event);
  238. void clearRows();
  239. void refresh();
  240. RectI getRowBounds(S32 rowIndex);
  241. DECLARE_CONOBJECT(GuiGameListMenuCtrl);
  242. DECLARE_CATEGORY( "Gui Game" );
  243. DECLARE_DESCRIPTION( "Base class for cross platform menu controls that are gamepad friendly." );
  244. /// Initializes fields accessible through the console.
  245. static void initPersistFields();
  246. static const S32 NO_ROW = -1; ///< Indicates a query result of no row found.
  247. static const S32 NO_ICON = -1; ///< Indicates a row has no extra icon available
  248. static const S32 NO_OPTION = -1; ///< Indicates there is no option
  249. protected:
  250. /// Adds a row to the control.
  251. ///
  252. /// \param row A reference to the row object to fill.
  253. /// \param label The text to display on the row as a label.
  254. /// \param callback Name of a script function to use as a callback when this
  255. /// row is activated.
  256. /// \param icon [optional] Index of the icon to use as a marker. Default -1
  257. /// means no icon will be shown on this row.
  258. /// \param yPad [optional] An extra amount of height padding before the row.
  259. /// \param enabled [optional] If this row is initially enabled. Default true.
  260. 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 = "");
  261. /// Determines if the given index is a valid row index. Any index pointing at
  262. /// an existing row is valid.
  263. ///
  264. /// \param index The index to check for validity.
  265. /// \return True if the index points at a valid row, false otherwise.
  266. virtual bool isValidRowIndex(S32 index) const;
  267. /// Sets the script variable $ThisControl to reflect this control.
  268. virtual void setThisControl();
  269. /// Called to implement debug rendering which displays colored lines to
  270. /// provide visual feedback on extents and hit zones.
  271. virtual void onDebugRender(Point2I offset);
  272. /// Looks up the row having a hit area at the given global point.
  273. ///
  274. /// \param globalPoint The point we want to check for hitting a row.
  275. /// \return The index of the hit row or NO_ROW if no row was hit.
  276. virtual S32 getRow(Point2I globalPoint);
  277. /// Checks to make sure our control has a profile of the correct type.
  278. ///
  279. /// \return True if the profile is of type GuiGameListMenuProfile or false if
  280. /// the profile is of any other type.
  281. virtual bool hasValidProfile() const;
  282. /// Enforces the validity of the fields on this control and its profile (if
  283. /// the profile is valid, see: hasValidProfile).
  284. virtual void enforceConstraints();
  285. /// @name Callbacks
  286. /// @{
  287. DECLARE_CALLBACK( void, onChange, () );
  288. DECLARE_CALLBACK(void, onInputEvent, (const char* device, const char* action, bool state));
  289. DECLARE_CALLBACK(void, onAxisEvent, (const char* device, const char* action, F32 axisValue));
  290. /// @}
  291. /// Evaluates some script. If the command is empty then nothing is evaluated.
  292. ///
  293. /// \param command The script to evaluate.
  294. void doScriptCommand(StringTableEntry command);
  295. StringTableEntry mCallbackOnA; ///< Script callback when the 'A' button is pressed
  296. StringTableEntry mCallbackOnB; ///< Script callback when the 'B' button is pressed
  297. StringTableEntry mCallbackOnX; ///< Script callback when the 'X' button is pressed
  298. StringTableEntry mCallbackOnY; ///< Script callback when the 'Y' button is pressed
  299. bool mDebugRender; ///< Determines when to show debug render lines
  300. Vector<Row *> mRows; ///< Holds data wrappers on all the rows we have
  301. private:
  302. /// Performs a click on the current option row. The x position is used to
  303. /// determine if the left or right arrow were clicked. If one was clicked, the
  304. /// option will be changed. If neither was clicked, the option is unaffected.
  305. /// This method should only be called when there is an actively selected row.
  306. ///
  307. /// \param row The row to perform the click on.
  308. /// \param xPos The x position of the the click, relative to the control.
  309. void clickOption(Row* row, S32 xPos);
  310. /// Changes the option on the currently selected row. If there is no row
  311. /// selected, this method does nothing.
  312. ///
  313. /// \param delta The amount to change the option selection by. Typically this
  314. /// will be 1 or -1.
  315. void changeOption(S32 delta);
  316. /// Changes the option on the given row.
  317. ///
  318. /// \param row The row to change the option on.
  319. /// \param delta The amount to change the option selection by. Typically this
  320. /// will be 1 or -1.
  321. void changeOption(Row* row, S32 delta);
  322. /// Performs a click on the current slider row. The x position is used to
  323. /// determine if the left or right arrow were clicked, or if it landed somewhere on the sliderbar.
  324. /// If one was clicked, the option will be changed. If neither was clicked, the option is unaffected.
  325. /// This method should only be called when there is an actively selected row.
  326. ///
  327. /// \param row The row to perform the click on.
  328. /// \param xPos The x position of the the click, relative to the control.
  329. void clickSlider(Row* row, S32 xPos);
  330. void clickKeybind(Row* row, S32 xPos);
  331. private:
  332. /// Recalculates the height of this control based on the stored row height and
  333. /// and padding on the rows.
  334. virtual Point2I getMinExtent() const;
  335. /// Makes sure the height will allow all rows to be displayed without being
  336. /// truncated.
  337. void updateHeight();
  338. /// Sets the first enabled row as selected. If there are no enabled rows then
  339. /// selected will be set to NO_ROW.
  340. void selectFirstEnabledRow();
  341. /// Changes the currently selected row.
  342. ///
  343. /// \param delta The amount to change the row selection by. Typically this will
  344. /// be 1 or -1.
  345. void changeRow(S32 delta);
  346. S32 mSelected; ///< index of the currently selected row
  347. S32 mHighlighted; ///< index of the currently highlighted row
  348. bool mCallbackOnInputs;
  349. };
  350. /// \class GuiGameListMenuProfile
  351. /// A gui profile with additional fields specific to GuiGameListMenuCtrl.
  352. class GuiGameListMenuProfile : public GuiControlProfile
  353. {
  354. typedef GuiControlProfile Parent;
  355. public:
  356. /// Enforces range constraints on all required fields.
  357. virtual void enforceConstraints();
  358. /// Get the height of rows in this profile. All rows are considered to be the
  359. /// same base height. Rows can have an extra amount of y padding defined when
  360. /// they are added to the control.
  361. ///
  362. /// \return The height of rows in this profile.
  363. S32 getRowHeight() { return (mRowSize.y) ? mRowSize.y : getBitmapArrayRect(TEX_NORMAL).extent.y; }
  364. /// Get the width of rows in this profile. All rows are considered to be the
  365. /// same width.
  366. ///
  367. /// \return The width of rows in this profile.
  368. S32 getRowWidth() { return (mRowSize.x) ? mRowSize.x : getBitmapArrayRect(TEX_NORMAL).extent.x; }
  369. /// Row scale is the ratio between the defined row size and the raw size of
  370. /// the bitmap.
  371. ///
  372. /// \return The row scale.
  373. const Point2F & getRowScale() const { return mRowScale; }
  374. /// Gets the extent of icons for this profile. If there are no icons you will
  375. /// get a point of (0, 0);
  376. ///
  377. /// \return The extent of icons or (0, 0) if there aren't any.
  378. Point2I getIconExtent();
  379. /// Gets the extent of arrows for this profile. If there are no arrows you
  380. /// will get a point of (0, 0).
  381. ///
  382. /// \return The extent of icons or (0, 0) if there aren't any.
  383. Point2I getArrowExtent();
  384. /// Gets the extent of the defined hit area for this profile. If the hit area
  385. /// is not defined then it defaults to the full size of a row.
  386. ///
  387. /// \return The extents of the defined hit area or the full size of the row.
  388. Point2I getHitAreaExtent();
  389. /// Determines if this profile has textures for the left and right arrows.
  390. ///
  391. /// \return True if the profile's bitmap has textures for the arrows, false
  392. /// otherwise.
  393. bool hasArrows(){ return (! getBitmapArrayRect(TEX_FIRST_ARROW).extent.isZero()); }
  394. /// Callback when the object is registered with the sim.
  395. ///
  396. /// \return True if the profile was successfully added, false otherwise.
  397. bool onAdd();
  398. Point2I mHitAreaUpperLeft; ///< Offset for the upper left corner of the hit area
  399. Point2I mHitAreaLowerRight; ///< Offset for the lower right corner of the hit area
  400. Point2I mIconOffset; ///< Offset for a row's extra icon
  401. Point2I mRowSize; ///< The base size of a row
  402. S32 mColumnSplit; ///< Absolute position of the split between columns
  403. S32 mRightPad; ///< Extra padding between the right arrow and the hit area
  404. GuiGameListMenuProfile();
  405. DECLARE_CONOBJECT(GuiGameListMenuProfile);
  406. /// Initializes fields accessible through the console.
  407. static void initPersistFields();
  408. enum
  409. {
  410. TEX_NORMAL = 0, ///< texture index for a normal, unselected row
  411. TEX_SELECTED = 1, ///< texture index for a selected row
  412. TEX_HIGHLIGHT = 2, ///< texture index for a highlighted row (moused over, not selected)
  413. TEX_DISABLED = 3, ///< texture index for a disabled row
  414. TEX_L_ARROW_OFF = 4, ///< texture index for the left arrow of an unselected row
  415. TEX_L_ARROW_ON = 5, ///< texture index for the left arrow of a selected row
  416. TEX_R_ARROW_OFF = 6, ///< texture index for the right arrow of an unselected row
  417. TEX_R_ARROW_ON = 7, ///< texture index for the right arrow of a selected row
  418. TEX_FIRST_ARROW = 4, ///< texture index for the first arrow
  419. TEX_FIRST_ICON = 8, ///< texture index for the first row marker icon
  420. };
  421. private:
  422. Point2F mRowScale; ///< Ratio of row size to actual bitmap size
  423. };
  424. #endif