guiGameSettingsCtrl.h 13 KB

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