BsGUIFloatField.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "BsGUIFloatField.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIInputBox.h"
  6. #include "BsGUISpace.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. #include "BsCursor.h"
  11. #include "BsGUIWidget.h"
  12. #include "CmViewport.h"
  13. #include <regex>
  14. using namespace CamelotFramework;
  15. using namespace BansheeEngine;
  16. namespace BansheeEditor
  17. {
  18. const float GUIFloatField::DRAG_SPEED = 0.05f;
  19. GUIFloatField::GUIFloatField(const PrivatelyConstruct& dummy, GUIWidget& parent, const GUIContent& labelContent,
  20. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  21. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mInputBox(nullptr), mIsDragging(false),
  22. mLastDragPos(0), mIsDragCursorSet(false), mLabelWidth(100)
  23. {
  24. const GUIElementStyle* curLabelStyle = labelStyle;
  25. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  26. if(curLabelStyle == nullptr)
  27. curLabelStyle = parent.getSkin().getStyle("Label");
  28. if(curInputBoxStyle == nullptr)
  29. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  30. mLabel = GUILabel::create(parent, labelContent, curLabelStyle);
  31. mInputBox = GUIInputBox::create(parent, false, inputBoxStyle);
  32. mInputBox->setFilter(&GUIFloatField::floatFilter);
  33. _registerChildElement(mLabel);
  34. _registerChildElement(mInputBox);
  35. setValue(0);
  36. }
  37. GUIFloatField::GUIFloatField(const PrivatelyConstruct& dummy, GUIWidget& parent,
  38. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  39. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mInputBox(nullptr), mIsDragging(false),
  40. mLastDragPos(0), mLabelWidth(100)
  41. {
  42. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  43. if(curInputBoxStyle == nullptr)
  44. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  45. mInputBox = GUIInputBox::create(parent, false, inputBoxStyle);
  46. mInputBox->setFilter(&GUIFloatField::floatFilter);
  47. _registerChildElement(mInputBox);
  48. setValue(0);
  49. }
  50. GUIFloatField::~GUIFloatField()
  51. {
  52. }
  53. GUIFloatField* GUIFloatField::create(GUIWidget& parent, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  54. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  55. {
  56. return cm_new<GUIFloatField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  57. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  58. }
  59. GUIFloatField* GUIFloatField::create(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  60. GUIElementStyle* inputBoxStyle)
  61. {
  62. return cm_new<GUIFloatField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  63. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  64. }
  65. GUIFloatField* GUIFloatField::create(GUIWidget& parent, const HString& labelContent, const GUIOptions& layoutOptions,
  66. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  67. {
  68. return cm_new<GUIFloatField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle,
  69. inputBoxStyle, GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  70. }
  71. GUIFloatField* GUIFloatField::create(GUIWidget& parent, const HString& labelContent, GUIElementStyle* labelStyle,
  72. GUIElementStyle* inputBoxStyle)
  73. {
  74. return cm_new<GUIFloatField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle, inputBoxStyle,
  75. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  76. }
  77. GUIFloatField* GUIFloatField::create(GUIWidget& parent, const GUIOptions& layoutOptions, GUIElementStyle* labelStyle,
  78. GUIElementStyle* inputBoxStyle)
  79. {
  80. return cm_new<GUIFloatField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle,
  81. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  82. }
  83. GUIFloatField* GUIFloatField::create(GUIWidget& parent,
  84. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  85. {
  86. return cm_new<GUIFloatField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  87. }
  88. bool GUIFloatField::mouseEvent(const GUIMouseEvent& event)
  89. {
  90. GUIElementContainer::mouseEvent(event);
  91. RectI draggableArea;
  92. if(mLabel != nullptr)
  93. draggableArea = mLabel->getBounds();
  94. if(event.getType() == GUIMouseEventType::MouseDragStart)
  95. {
  96. if(draggableArea.contains(event.getPosition()))
  97. {
  98. mLastDragPos = event.getPosition().x;
  99. mIsDragging = true;
  100. Cursor::instance().setCursor(CursorType::ArrowLeftRight);
  101. mIsDragCursorSet = true;
  102. }
  103. return true;
  104. }
  105. else if(event.getType() == GUIMouseEventType::MouseDrag)
  106. {
  107. if(mIsDragging)
  108. {
  109. INT32 xDiff = event.getPosition().x - mLastDragPos;
  110. INT32 jumpAmount = 0;
  111. if(event.getPosition().x < 0)
  112. {
  113. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  114. cursorScreenPos.x += _getParentWidget().getTarget()->getWidth();
  115. jumpAmount = _getParentWidget().getTarget()->getWidth();
  116. Cursor::instance().setScreenPosition(cursorScreenPos);
  117. }
  118. else if(event.getPosition().x >= _getParentWidget().getTarget()->getWidth())
  119. {
  120. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  121. cursorScreenPos.x -= _getParentWidget().getTarget()->getWidth();
  122. jumpAmount = -_getParentWidget().getTarget()->getWidth();
  123. Cursor::instance().setScreenPosition(cursorScreenPos);
  124. }
  125. float oldValue = getValue();
  126. float newValue = oldValue + xDiff * DRAG_SPEED;
  127. mLastDragPos = event.getPosition().x + jumpAmount;
  128. if(oldValue != newValue)
  129. setValue(newValue);
  130. }
  131. return true;
  132. }
  133. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  134. {
  135. mIsDragging = false;
  136. if(mIsDragCursorSet)
  137. {
  138. Cursor::instance().setCursor(CursorType::Arrow);
  139. mIsDragCursorSet = false;
  140. }
  141. return true;
  142. }
  143. else if(event.getType() == GUIMouseEventType::MouseOut)
  144. {
  145. if(!mIsDragging)
  146. {
  147. if(mIsDragCursorSet)
  148. {
  149. Cursor::instance().setCursor(CursorType::Arrow);
  150. mIsDragCursorSet = false;
  151. }
  152. }
  153. }
  154. else if(event.getType() == GUIMouseEventType::MouseMove)
  155. {
  156. if(draggableArea.contains(event.getPosition()))
  157. {
  158. Cursor::instance().setCursor(CursorType::ArrowLeftRight);
  159. mIsDragCursorSet = true;
  160. }
  161. else
  162. {
  163. if(!mIsDragging)
  164. {
  165. if(mIsDragCursorSet)
  166. {
  167. Cursor::instance().setCursor(CursorType::Arrow);
  168. mIsDragCursorSet = false;
  169. }
  170. }
  171. }
  172. }
  173. return false;
  174. }
  175. float GUIFloatField::getValue() const
  176. {
  177. return parseFloat(mInputBox->getText());
  178. }
  179. void GUIFloatField::setValue(float value)
  180. {
  181. mInputBox->setText(toWString(value));
  182. }
  183. void GUIFloatField::setLabelWidth(UINT32 width)
  184. {
  185. mLabelWidth = width;
  186. markContentAsDirty();
  187. }
  188. void GUIFloatField::updateClippedBounds()
  189. {
  190. Vector2I offset = _getOffset();
  191. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  192. }
  193. void GUIFloatField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  194. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  195. {
  196. UINT32 inputBoxOffset = 0;
  197. UINT32 inputBoxWidth = width;
  198. if(mLabel != nullptr)
  199. {
  200. UINT32 labelWidth = mLabelWidth;
  201. Vector2I optimalSize = mLabel->_getOptimalSize();
  202. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  203. Vector2I offset(x, y + yOffset);
  204. mLabel->_setOffset(offset);
  205. mLabel->_setWidth(labelWidth);
  206. mLabel->_setHeight(optimalSize.y);
  207. mLabel->_setAreaDepth(areaDepth);
  208. mLabel->_setWidgetDepth(widgetDepth);
  209. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  210. mLabel->_setClipRect(elemClipRect);
  211. inputBoxOffset = labelWidth;
  212. inputBoxWidth = width - labelWidth;
  213. }
  214. Vector2I inputBoxSize = mInputBox->_getOptimalSize();
  215. {
  216. Vector2I optimalSize = mInputBox->_getOptimalSize();
  217. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  218. Vector2I offset(x + inputBoxOffset, y + yOffset);
  219. mInputBox->_setOffset(offset);
  220. mInputBox->_setWidth(inputBoxWidth);
  221. mInputBox->_setHeight(optimalSize.y);
  222. mInputBox->_setAreaDepth(areaDepth);
  223. mInputBox->_setWidgetDepth(widgetDepth);
  224. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  225. mInputBox->_setClipRect(elemClipRect);
  226. }
  227. }
  228. Vector2I GUIFloatField::_getOptimalSize() const
  229. {
  230. Vector2I optimalsize = mInputBox->_getOptimalSize();
  231. if(mLabel != nullptr)
  232. {
  233. optimalsize.x += mLabel->_getOptimalSize().x;
  234. optimalsize.y = std::max(optimalsize.y, mLabel->_getOptimalSize().y);
  235. }
  236. return optimalsize;
  237. }
  238. const String& GUIFloatField::getGUITypeName()
  239. {
  240. static String typeName = "GUIFloatField";
  241. return typeName;
  242. }
  243. bool GUIFloatField::floatFilter(const CM::WString& str)
  244. {
  245. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  246. }
  247. }