guiGameSettingsCtrl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 _GuiGameSettingsCtrl_H_
  23. #define _GuiGameSettingsCtrl_H_
  24. #include "gui/buttons/guiButtonCtrl.h"
  25. #include "T3D/assets/ImageAsset.h"
  26. /// \class GuiGameSettingsCtrl
  27. /// A base class for cross platform menu controls that are gamepad friendly.
  28. class GuiGameSettingsCtrl : public GuiButtonCtrl
  29. {
  30. public:
  31. typedef GuiButtonCtrl Parent;
  32. enum Mode
  33. {
  34. Default = 0,
  35. OptionList,
  36. Slider,
  37. Keybind,
  38. Text
  39. };
  40. protected:
  41. /// \struct OptionEntry
  42. /// Display text and ID key for each entry in an option.
  43. struct OptionEntry
  44. {
  45. StringTableEntry mDisplayText; ///< The text that is displayed for the option
  46. StringTableEntry mKeyString; ///< Key value that is associated with this option
  47. OptionEntry() : mDisplayText(StringTable->EmptyString()), mKeyString(StringTable->EmptyString()) {}
  48. virtual ~OptionEntry() {}
  49. };
  50. StringTableEntry mLabel; ///< Text to display in the control as a label
  51. StringTableEntry mScriptCallback; ///< Script callback when control is activated
  52. StringTableEntry mTooltip; ///< A descriptive tooltip message for what the control is
  53. Mode mMode;
  54. //List options
  55. Vector<OptionEntry> 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. DECLARE_IMAGEASSET(GuiGameSettingsCtrl, KeybindBitmap, GFXDefaultGUIProfile)
  64. DECLARE_IMAGEASSET(GuiGameSettingsCtrl, PreviousBitmap, GFXDefaultGUIProfile)
  65. DECLARE_IMAGEASSET(GuiGameSettingsCtrl, NextBitmap, GFXDefaultGUIProfile)
  66. S32 mArrowSize;
  67. S32 mColumnSplit; //Padding between the leftmost edge of the control, and the left side of the 'option'.
  68. S32 mRightPad;
  69. bool mEnabled;
  70. bool mSelected;
  71. public:
  72. /// Sets the control as selected . Only controls that are enabled can be selected.
  73. virtual void setSelected();
  74. /// Determines if the specified control is enabled or disabled.
  75. ///
  76. /// \return True if the specified control is enabled. False if the control is not
  77. /// enabled
  78. virtual bool isEnabled() const;
  79. /// Sets a control's enabled status according to the given parameters.
  80. ///
  81. /// \param enabled Indicate true to enable the control or false to disable it.
  82. virtual void setEnabled(bool enabled);
  83. /// Gets the label displayed on the control.
  84. ///
  85. /// \return The label for the control.
  86. virtual StringTableEntry getLabel() const;
  87. /// Sets the label on the control.
  88. ///
  89. /// \param label Text to set as the label.
  90. virtual void setLabel(const char * label);
  91. /// Sets the control to a List setting.
  92. ///
  93. /// \param label The text to display on the control as a label.
  94. /// \param optionsList A tab separated list of options for the control.
  95. /// \param wrapOptions Specify true to allow options to wrap at the ends or
  96. /// false to prevent wrapping.
  97. /// \param callback [optional] Name of a script function to use as a callback
  98. /// when this control is activated. Default NULL means no callback.
  99. /// \param enabled [optional] If this control is initially enabled. Default true.
  100. void setListSetting(const char* label, const char* optionsList, bool wrapOptions, const char* callback,bool enabled, const char* tooltip = "", const char* defaultValue = "");
  101. /// Sets the control to a Slider setting
  102. ///
  103. /// \param label The text to display on the control as a label.
  104. /// \param defaultValue A float indicating the slider's default value
  105. /// \param increments A float indicating the incremental values the slider snaps along between it's range
  106. /// \param range A Point2F that indicates the minimum and maximum value range
  107. /// \param callback [optional] Name of a script function to use as a callback
  108. /// when this control is activated. Default NULL means no callback.
  109. /// \param enabled [optional] If this control is initially enabled. Default true.
  110. void setSliderSetting(const char* label, F32 defaultValue, F32 increments, Point2F range, const char* callback, bool enabled, const char* tooltip = "");
  111. /// Sets the control to a Keybind setting
  112. ///
  113. /// \param label The text to display on the control as a label.
  114. /// \param bitmapAssetId The assetId for the button display image
  115. /// \param range A Point2F that indicates the minimum and maximum value range
  116. /// \param callback [optional] Name of a script function to use as a callback
  117. /// when this control is activated. Default NULL means no callback.
  118. /// \param enabled [optional] If this control is initially enabled. Default true.
  119. void setKeybindSetting(const char* label, const char* bitmapAssetId, const char* callback, bool enabled, const char* tooltip);
  120. /// Gets the text for the currently selected option of the control.
  121. ///
  122. /// \return A string representing the text currently displayed as the selected
  123. /// option on the control. If there is no such displayed text then the empty
  124. /// string is returned.
  125. StringTableEntry getCurrentOption() const;
  126. /// Gets the key string for the currently selected option of the control
  127. ///
  128. /// \return The key (or id) that was assigned to the selected option on the
  129. /// control. If there is no selected option then the empty string is returned.
  130. StringTableEntry getCurrentOptionKey() const;
  131. /// Gets the index into the option list for the currently selected option of the control.
  132. ///
  133. /// \return The index of the selected option on the control. If there is no
  134. /// selected option then -1 is returned.
  135. S32 getCurrentOptionIndex() const;
  136. /// Attempts to set the control to the specified selected option. The option
  137. /// will only be set if the option exists in the control.
  138. ///
  139. /// \param option The option to be made active.
  140. /// \return True if the control contained the option and was set, false otherwise.
  141. bool selectOption(const char* option);
  142. /// Attempts to set the control to the option with the specified key. The
  143. /// option will only be set if the key exists in the control.
  144. ///
  145. /// \param optionKey The key string that was assigned to the option to be made active.
  146. /// \return True if the control contained the key and the option and was set, false otherwise.
  147. bool selectOptionByKey(const char* optionKey);
  148. /// Attempts to set the control to the option at the specified index. The option
  149. /// will only be set if the index is valid.
  150. ///
  151. /// \param optionIndex The index of the option to be made active.
  152. /// \return True if the index was valid and the option and was set, false otherwise.
  153. bool selectOptionByIndex(S32 optionIndex);
  154. /// Sets the list of options on the control.
  155. ///
  156. /// \param optionsList A tab separated list of options for the control.
  157. void setOptions(const char* optionsList);
  158. /// Adds an option to the list of options on the control.
  159. ///
  160. /// \param displayText The text to display for this option.
  161. /// \param keyText The id string to associate with this value. If NULL the
  162. /// id will be the same as the display text.
  163. void addOption(const char* displayText, const char* keyText);
  164. /// Activates the control. The script callback of the control will
  165. /// be called (if it has one).
  166. virtual void activate();
  167. /// Gets the value
  168. ///
  169. F32 getValue();
  170. /// Sets the value
  171. ///
  172. /// \param value The new value to be set.
  173. void setValue(F32 value);
  174. Mode getMode() { return mMode; }
  175. /// <summary>
  176. /// Gets the incremenet amount
  177. /// </summary>
  178. F32 getIncrement();
  179. /// <summary>
  180. /// Gets range of values allowed
  181. /// </summary>
  182. Point2F getRange();
  183. /// Gets the tooltip
  184. const char* getTooltip();
  185. GuiGameSettingsCtrl();
  186. ~GuiGameSettingsCtrl();
  187. void onRender(Point2I offset, const RectI &updateRect) override;
  188. void onRenderListOption(Point2I currentOffset);
  189. void onRenderSliderOption(Point2I currentOffset);
  190. void onRenderKeybindOption(Point2I currentOffset);
  191. /// Callback when the object is registered with the sim.
  192. ///
  193. /// \return True if the profile was successfully added, false otherwise.
  194. bool onAdd() override;
  195. /// Callback when the control wakes up.
  196. bool onWake() override;
  197. void onSleep() override;
  198. void clear() override;
  199. void onMouseMove(const GuiEvent& event) override;
  200. void onMouseUp(const GuiEvent& event) override;
  201. DECLARE_CONOBJECT(GuiGameSettingsCtrl);
  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_OPTION = -1; ///< Indicates there is no option
  207. protected:
  208. /// Sets up the option
  209. ///
  210. /// \param label The text to display on the control as a label.
  211. /// \param callback Name of a script function to use as a callback when this
  212. /// control is activated.
  213. /// \param enabled [optional] If this control is initially enabled. Default true.
  214. virtual void set(const char* label, const char* callback, bool useHighlightIcon = true, bool enabled = true, S32 mode = 0, const char* tooltip = "");
  215. /// Sets the script variable $ThisControl to reflect this control.
  216. virtual void setThisControl();
  217. /// @name Callbacks
  218. /// @{
  219. DECLARE_CALLBACK( void, onChange, () );
  220. DECLARE_CALLBACK(void, onInputEvent, (const char* device, const char* action, bool state));
  221. DECLARE_CALLBACK(void, onAxisEvent, (const char* device, const char* action, F32 axisValue));
  222. /// @}
  223. /// Evaluates some script. If the command is empty then nothing is evaluated.
  224. ///
  225. /// \param command The script to evaluate.
  226. void doScriptCommand(StringTableEntry command);
  227. StringTableEntry mCallbackOnA; ///< Script callback when the 'A' button is pressed
  228. StringTableEntry mCallbackOnB; ///< Script callback when the 'B' button is pressed
  229. StringTableEntry mCallbackOnX; ///< Script callback when the 'X' button is pressed
  230. StringTableEntry mCallbackOnY; ///< Script callback when the 'Y' button is pressed
  231. private:
  232. /// Performs a click on the current option. The x position is used to
  233. /// determine if the left or right arrow were clicked. If one was clicked, the
  234. /// option will be changed. If neither was clicked, the option is unaffected.
  235. /// This method should only be called when there is an actively selected control.
  236. ///
  237. /// \param xPos The x position of the the click, relative to the control.
  238. void clickOption(S32 xPos);
  239. /// Changes the option on the currently selected control.
  240. ///
  241. /// \param delta The amount to change the option selection by. Typically this
  242. /// will be 1 or -1.
  243. void changeOption(S32 delta);
  244. /// Performs a click on the current slider control. The x position is used to
  245. /// determine if the left or right arrow were clicked, or if it landed somewhere on the sliderbar.
  246. /// If one was clicked, the option will be changed. If neither was clicked, the option is unaffected.
  247. /// This method should only be called when there is an actively selected control.
  248. ///
  249. /// \param xPos The x position of the the click, relative to the control.
  250. void clickSlider(S32 xPos);
  251. void clickKeybind(S32 xPos);
  252. private:
  253. bool mCallbackOnInputs;
  254. bool mConsumeKeyInputEvents;
  255. };
  256. #endif