BsGUIFieldBase.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "GUI/BsGUIElementContainer.h"
  6. #include "GUI/BsGUIContent.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Implementation
  10. * @{
  11. */
  12. /** Base class for all editor GUI fields. All fields are a combination of an optional label and an input field. */
  13. class BS_ED_EXPORT GUIFieldBase : public GUIElementContainer
  14. {
  15. protected:
  16. struct PrivatelyConstruct {};
  17. public:
  18. /** Returns the style type name for the internal label element. */
  19. static const String& getLabelStyleType()
  20. {
  21. static String LABEL_STYLE_TYPE = "EditorFieldLabel";
  22. return LABEL_STYLE_TYPE;
  23. }
  24. GUIFieldBase(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  25. const String& labelStyle, const GUIDimensions& dimensions, bool withLabel);
  26. /** @name Internal
  27. * @{
  28. */
  29. /** @copydoc GUIElementContainer::_updateLayoutInternal */
  30. void _updateLayoutInternal(const GUILayoutData& data) override;
  31. /** @copydoc GUIElementContainer::_getOptimalSize */
  32. Vector2I _getOptimalSize() const override;
  33. /** @} */
  34. protected:
  35. virtual ~GUIFieldBase() = default;
  36. /** @copydoc GUIElementContainer::styleUpdated */
  37. void styleUpdated() override;
  38. static const UINT32 DEFAULT_LABEL_WIDTH;
  39. GUILayout* mLayout;
  40. GUILabel* mLabel;
  41. };
  42. /** Templated GUI field class that provides common methods needed for constructing an editor field. */
  43. template <class T>
  44. class TGUIField : public GUIFieldBase
  45. {
  46. public:
  47. /**
  48. * Creates a new GUI editor field with a label.
  49. *
  50. * @param[in] labelContent Content to display in the editor field label.
  51. * @param[in] labelWidth Width of the label in pixels.
  52. * @param[in] options Options that allow you to control how is the element positioned and sized.
  53. * This will override any similar options set by style.
  54. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  55. * GUIWidget the element is used on. If not specified default style is used.
  56. */
  57. static T* create(const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
  58. const String& style = StringUtil::BLANK)
  59. {
  60. const String* curStyle = &style;
  61. if (*curStyle == StringUtil::BLANK)
  62. curStyle = &T::getGUITypeName();
  63. return bs_new<T>(PrivatelyConstruct(), labelContent, labelWidth, *curStyle,
  64. GUIDimensions::create(options), true);
  65. }
  66. /**
  67. * Creates a new GUI editor field with a label.
  68. *
  69. * @param[in] labelContent Content to display in the editor field label.
  70. * @param[in] options Options that allow you to control how is the element positioned and sized. This will
  71. * override any similar options set by style.
  72. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  73. * GUIWidget the element is used on. If not specified default style is used.
  74. */
  75. static T* create(const GUIContent& labelContent, const GUIOptions& options,
  76. const String& style = StringUtil::BLANK)
  77. {
  78. const String* curStyle = &style;
  79. if (*curStyle == StringUtil::BLANK)
  80. curStyle = &T::getGUITypeName();
  81. return bs_new<T>(PrivatelyConstruct(), labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  82. GUIDimensions::create(options), true);
  83. }
  84. /**
  85. * Creates a new GUI editor field with a label.
  86. *
  87. * @param[in] labelText String to display in the editor field label.
  88. * @param[in] labelWidth Width of the label in pixels.
  89. * @param[in] options Options that allow you to control how is the element positioned and sized.
  90. * This will override any similar options set by style.
  91. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  92. * GUIWidget the element is used on. If not specified default style is used.
  93. */
  94. static T* create(const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  95. const String& style = StringUtil::BLANK)
  96. {
  97. const String* curStyle = &style;
  98. if (*curStyle == StringUtil::BLANK)
  99. curStyle = &T::getGUITypeName();
  100. return bs_new<T>(PrivatelyConstruct(), GUIContent(labelText), labelWidth, *curStyle,
  101. GUIDimensions::create(options), true);
  102. }
  103. /**
  104. * Creates a new GUI editor field with a label.
  105. *
  106. * @param[in] labelText String to display in the editor field label.
  107. * @param[in] options Options that allow you to control how is the element positioned and sized.
  108. * This will override any similar options set by style.
  109. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  110. * GUIWidget the element is used on. If not specified default style is used.
  111. */
  112. static T* create(const HString& labelText, const GUIOptions& options,
  113. const String& style = StringUtil::BLANK)
  114. {
  115. const String* curStyle = &style;
  116. if (*curStyle == StringUtil::BLANK)
  117. curStyle = &T::getGUITypeName();
  118. return bs_new<T>(PrivatelyConstruct(), GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  119. GUIDimensions::create(options), true);
  120. }
  121. /**
  122. * Creates a new GUI editor field without a label.
  123. *
  124. * @param[in] options Options that allow you to control how is the element positioned and sized.
  125. * This will override any similar options set by style.
  126. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  127. * GUIWidget the element is used on. If not specified default style is used.
  128. */
  129. static T* create(const GUIOptions& options, const String& style = StringUtil::BLANK)
  130. {
  131. const String* curStyle = &style;
  132. if (*curStyle == StringUtil::BLANK)
  133. curStyle = &T::getGUITypeName();
  134. return bs_new<T>(PrivatelyConstruct(), GUIContent(), 0, *curStyle,
  135. GUIDimensions::create(options), false);
  136. }
  137. /**
  138. * Creates a new GUI editor field with a label.
  139. *
  140. * @param[in] labelContent Content to display in the editor field label.
  141. * @param[in] labelWidth Width of the label in pixels.
  142. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  143. * GUIWidget the element is used on. If not specified default style is used.
  144. */
  145. BS_SCRIPT_EXPORT(ec:T)
  146. static T* create(const GUIContent& labelContent, UINT32 labelWidth,
  147. const String& style = StringUtil::BLANK)
  148. {
  149. const String* curStyle = &style;
  150. if (*curStyle == StringUtil::BLANK)
  151. curStyle = &T::getGUITypeName();
  152. return bs_new<T>(PrivatelyConstruct(), labelContent, labelWidth, *curStyle, GUIDimensions::create(), true);
  153. }
  154. /**
  155. * Creates a new GUI editor field with a label.
  156. *
  157. * @param[in] labelContent Content to display in the editor field label.
  158. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  159. * GUIWidget the element is used on. If not specified default style is used.
  160. */
  161. BS_SCRIPT_EXPORT(ec:T)
  162. static T* create(const GUIContent& labelContent,
  163. const String& style = StringUtil::BLANK)
  164. {
  165. const String* curStyle = &style;
  166. if (*curStyle == StringUtil::BLANK)
  167. curStyle = &T::getGUITypeName();
  168. return bs_new<T>(PrivatelyConstruct(), labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  169. GUIDimensions::create(), true);
  170. }
  171. /**
  172. * Creates a new GUI editor field with a label.
  173. *
  174. * @param[in] labelText String to display in the editor field label.
  175. * @param[in] labelWidth Width of the label in pixels.
  176. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  177. * GUIWidget the element is used on. If not specified default style is used.
  178. */
  179. BS_SCRIPT_EXPORT(ec:T)
  180. static T* create(const HString& labelText, UINT32 labelWidth,
  181. const String& style = StringUtil::BLANK)
  182. {
  183. const String* curStyle = &style;
  184. if (*curStyle == StringUtil::BLANK)
  185. curStyle = &T::getGUITypeName();
  186. return bs_new<T>(PrivatelyConstruct(), GUIContent(labelText), labelWidth, *curStyle,
  187. GUIDimensions::create(), true);
  188. }
  189. /**
  190. * Creates a new GUI editor field with a label.
  191. *
  192. * @param[in] labelText String to display in the editor field label.
  193. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  194. * GUIWidget the element is used on. If not specified default style is used.
  195. */
  196. BS_SCRIPT_EXPORT(ec:T)
  197. static T* create(const HString& labelText, const String& style = StringUtil::BLANK)
  198. {
  199. const String* curStyle = &style;
  200. if (*curStyle == StringUtil::BLANK)
  201. curStyle = &T::getGUITypeName();
  202. return bs_new<T>(PrivatelyConstruct(), GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  203. GUIDimensions::create(), true);
  204. }
  205. /**
  206. * Creates a new GUI editor field without a label.
  207. *
  208. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  209. * GUIWidget the element is used on. If not specified default style is used.
  210. */
  211. BS_SCRIPT_EXPORT(ec:T)
  212. static T* create(const String& style = StringUtil::BLANK)
  213. {
  214. const String* curStyle = &style;
  215. if (*curStyle == StringUtil::BLANK)
  216. curStyle = &T::getGUITypeName();
  217. return bs_new<T>(PrivatelyConstruct(), GUIContent(), 0, *curStyle,
  218. GUIDimensions::create(), false);
  219. }
  220. TGUIField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  221. const String& style, const GUIDimensions& dimensions, bool withLabel)
  222. :GUIFieldBase(dummy, labelContent, labelWidth, style, dimensions, withLabel)
  223. { }
  224. };
  225. /** @} */
  226. }