BsGUIIntField.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "BsGUIIntField.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 "BsGUIWidget.h"
  11. #include "BsCursor.h"
  12. #include "CmViewport.h"
  13. #include <regex>
  14. using namespace CamelotFramework;
  15. using namespace BansheeEngine;
  16. namespace BansheeEditor
  17. {
  18. const INT32 GUIIntField::DRAG_SPEED = 5;
  19. GUIIntField::GUIIntField(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(&GUIIntField::intFilter);
  33. _registerChildElement(mLabel);
  34. _registerChildElement(mInputBox);
  35. setValue(0);
  36. }
  37. GUIIntField::GUIIntField(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(&GUIIntField::intFilter);
  47. _registerChildElement(mInputBox);
  48. setValue(0);
  49. }
  50. GUIIntField::~GUIIntField()
  51. {
  52. }
  53. GUIIntField* GUIIntField::create(GUIWidget& parent, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  54. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  55. {
  56. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  57. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  58. }
  59. GUIIntField* GUIIntField::create(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  60. GUIElementStyle* inputBoxStyle)
  61. {
  62. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  63. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  64. }
  65. GUIIntField* GUIIntField::create(GUIWidget& parent, const HString& labelContent, const GUIOptions& layoutOptions,
  66. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  67. {
  68. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle,
  69. inputBoxStyle, GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  70. }
  71. GUIIntField* GUIIntField::create(GUIWidget& parent, const HString& labelContent, GUIElementStyle* labelStyle,
  72. GUIElementStyle* inputBoxStyle)
  73. {
  74. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle, inputBoxStyle,
  75. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  76. }
  77. GUIIntField* GUIIntField::create(GUIWidget& parent, const GUIOptions& layoutOptions, GUIElementStyle* labelStyle,
  78. GUIElementStyle* inputBoxStyle)
  79. {
  80. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle,
  81. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  82. }
  83. GUIIntField* GUIIntField::create(GUIWidget& parent,
  84. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  85. {
  86. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  87. }
  88. bool GUIIntField::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. INT32 oldValue = getValue();
  126. INT32 newValue = oldValue;
  127. if(xDiff >= DRAG_SPEED)
  128. {
  129. while(xDiff >= DRAG_SPEED)
  130. {
  131. newValue++;
  132. xDiff -= DRAG_SPEED;
  133. }
  134. }
  135. else if(xDiff <= -DRAG_SPEED)
  136. {
  137. while(xDiff <= -DRAG_SPEED)
  138. {
  139. newValue--;
  140. xDiff += DRAG_SPEED;
  141. }
  142. }
  143. mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
  144. if(oldValue != newValue)
  145. setValue(newValue);
  146. }
  147. return true;
  148. }
  149. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  150. {
  151. mIsDragging = false;
  152. if(mIsDragCursorSet)
  153. {
  154. Cursor::instance().setCursor(CursorType::Arrow);
  155. mIsDragCursorSet = false;
  156. }
  157. return true;
  158. }
  159. else if(event.getType() == GUIMouseEventType::MouseOut)
  160. {
  161. if(!mIsDragging)
  162. {
  163. if(mIsDragCursorSet)
  164. {
  165. Cursor::instance().setCursor(CursorType::Arrow);
  166. mIsDragCursorSet = false;
  167. }
  168. }
  169. }
  170. else if(event.getType() == GUIMouseEventType::MouseMove)
  171. {
  172. if(draggableArea.contains(event.getPosition()))
  173. {
  174. Cursor::instance().setCursor(CursorType::ArrowLeftRight);
  175. mIsDragCursorSet = true;
  176. }
  177. else
  178. {
  179. if(!mIsDragging)
  180. {
  181. if(mIsDragCursorSet)
  182. {
  183. Cursor::instance().setCursor(CursorType::Arrow);
  184. mIsDragCursorSet = false;
  185. }
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. CM::INT32 GUIIntField::getValue() const
  192. {
  193. return parseInt(mInputBox->getText());
  194. }
  195. void GUIIntField::setValue(CM::INT32 value)
  196. {
  197. mInputBox->setText(toWString(value));
  198. }
  199. void GUIIntField::setLabelWidth(UINT32 width)
  200. {
  201. mLabelWidth = width;
  202. markContentAsDirty();
  203. }
  204. void GUIIntField::updateClippedBounds()
  205. {
  206. Vector2I offset = _getOffset();
  207. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  208. }
  209. void GUIIntField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  210. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  211. {
  212. UINT32 inputBoxOffset = 0;
  213. UINT32 inputBoxWidth = width;
  214. if(mLabel != nullptr)
  215. {
  216. UINT32 labelWidth = mLabelWidth;
  217. Vector2I optimalSize = mLabel->_getOptimalSize();
  218. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  219. Vector2I offset(x, y + yOffset);
  220. mLabel->_setOffset(offset);
  221. mLabel->_setWidth(labelWidth);
  222. mLabel->_setHeight(optimalSize.y);
  223. mLabel->_setAreaDepth(areaDepth);
  224. mLabel->_setWidgetDepth(widgetDepth);
  225. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  226. mLabel->_setClipRect(elemClipRect);
  227. inputBoxOffset = labelWidth;
  228. inputBoxWidth = width - labelWidth;
  229. }
  230. Vector2I inputBoxSize = mInputBox->_getOptimalSize();
  231. {
  232. Vector2I optimalSize = mInputBox->_getOptimalSize();
  233. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  234. Vector2I offset(x + inputBoxOffset, y + yOffset);
  235. mInputBox->_setOffset(offset);
  236. mInputBox->_setWidth(inputBoxWidth);
  237. mInputBox->_setHeight(optimalSize.y);
  238. mInputBox->_setAreaDepth(areaDepth);
  239. mInputBox->_setWidgetDepth(widgetDepth);
  240. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  241. mInputBox->_setClipRect(elemClipRect);
  242. }
  243. }
  244. Vector2I GUIIntField::_getOptimalSize() const
  245. {
  246. Vector2I optimalsize = mInputBox->_getOptimalSize();
  247. if(mLabel != nullptr)
  248. {
  249. optimalsize.x += mLabel->_getOptimalSize().x;
  250. optimalsize.y = std::max(optimalsize.y, mLabel->_getOptimalSize().y);
  251. }
  252. return optimalsize;
  253. }
  254. const String& GUIIntField::getGUITypeName()
  255. {
  256. static String typeName = "GUIIntField";
  257. return typeName;
  258. }
  259. bool GUIIntField::intFilter(const CM::WString& str)
  260. {
  261. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  262. }
  263. }