BsGUIIntField.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <regex>
  11. using namespace CamelotFramework;
  12. using namespace BansheeEngine;
  13. namespace BansheeEditor
  14. {
  15. const float GUIIntField::SPLIT_POSITION = 0.5f;
  16. GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, GUIWidget& parent, const GUIContent& labelContent,
  17. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  18. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mInputBox(nullptr)
  19. {
  20. const GUIElementStyle* curLabelStyle = labelStyle;
  21. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  22. if(curLabelStyle == nullptr)
  23. curLabelStyle = parent.getSkin().getStyle("Label");
  24. if(curInputBoxStyle == nullptr)
  25. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  26. mLabel = GUILabel::create(parent, labelContent, curLabelStyle);
  27. mInputBox = GUIInputBox::create(parent, false, inputBoxStyle);
  28. mInputBox->setFilter(&GUIIntField::intFilter);
  29. _registerChildElement(mLabel);
  30. _registerChildElement(mInputBox);
  31. }
  32. GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, GUIWidget& parent,
  33. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  34. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mInputBox(nullptr)
  35. {
  36. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  37. if(curInputBoxStyle == nullptr)
  38. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  39. mInputBox = GUIInputBox::create(parent, false, inputBoxStyle);
  40. mInputBox->setFilter(&GUIIntField::intFilter);
  41. _registerChildElement(mInputBox);
  42. }
  43. GUIIntField::~GUIIntField()
  44. {
  45. }
  46. GUIIntField* GUIIntField::create(GUIWidget& parent, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  47. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  48. {
  49. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  50. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  51. }
  52. GUIIntField* GUIIntField::create(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  53. GUIElementStyle* inputBoxStyle)
  54. {
  55. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  56. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  57. }
  58. GUIIntField* GUIIntField::create(GUIWidget& parent, const HString& labelContent, const GUIOptions& layoutOptions,
  59. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  60. {
  61. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle,
  62. inputBoxStyle, GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  63. }
  64. GUIIntField* GUIIntField::create(GUIWidget& parent, const HString& labelContent, GUIElementStyle* labelStyle,
  65. GUIElementStyle* inputBoxStyle)
  66. {
  67. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle, inputBoxStyle,
  68. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  69. }
  70. GUIIntField* GUIIntField::create(GUIWidget& parent, const GUIOptions& layoutOptions, GUIElementStyle* labelStyle,
  71. GUIElementStyle* inputBoxStyle)
  72. {
  73. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle,
  74. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  75. }
  76. GUIIntField* GUIIntField::create(GUIWidget& parent,
  77. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  78. {
  79. return cm_new<GUIIntField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  80. }
  81. bool GUIIntField::mouseEvent(const GUIMouseEvent& event)
  82. {
  83. GUIElementContainer::mouseEvent(event);
  84. if(event.getType() == GUIMouseEventType::MouseDragStart)
  85. {
  86. // TODO -If over draggable area start drag
  87. return true;
  88. }
  89. else if(event.getType() == GUIMouseEventType::MouseDrag)
  90. {
  91. // TODO - If drag is started increase/lower the value
  92. return true;
  93. }
  94. else if(event.getType() == GUIMouseEventType::MouseOut)
  95. {
  96. // TODO - Ensure cursor is set back to arrow
  97. return true;
  98. }
  99. else if(event.getType() == GUIMouseEventType::MouseMove)
  100. {
  101. // TODO - If mouse is over drag area change cursor to Arrow Left/Right
  102. return true;
  103. }
  104. return false;
  105. }
  106. CM::INT32 GUIIntField::getValue() const
  107. {
  108. return parseInt(mInputBox->getText());
  109. }
  110. void GUIIntField::setValue(CM::INT32 value)
  111. {
  112. mInputBox->setText(toWString(value));
  113. }
  114. void GUIIntField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  115. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  116. {
  117. UINT32 inputBoxOffset = 0;
  118. UINT32 inputBoxWidth = width;
  119. if(mLabel != nullptr)
  120. {
  121. UINT32 labelWidth = Math::roundToInt(width * SPLIT_POSITION);
  122. Vector2I optimalSize = mLabel->_getOptimalSize();
  123. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  124. Vector2I offset(x, y + yOffset);
  125. mLabel->_setOffset(offset);
  126. mLabel->_setWidth(labelWidth);
  127. mLabel->_setHeight(optimalSize.y);
  128. mLabel->_setAreaDepth(areaDepth);
  129. mLabel->_setWidgetDepth(widgetDepth);
  130. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  131. mLabel->_setClipRect(elemClipRect);
  132. inputBoxOffset = labelWidth;
  133. inputBoxWidth = width - labelWidth;
  134. }
  135. Vector2I inputBoxSize = mInputBox->_getOptimalSize();
  136. {
  137. Vector2I optimalSize = mInputBox->_getOptimalSize();
  138. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  139. Vector2I offset(x + inputBoxOffset, y + yOffset);
  140. mInputBox->_setOffset(offset);
  141. mInputBox->_setWidth(inputBoxWidth);
  142. mInputBox->_setHeight(optimalSize.y);
  143. mInputBox->_setAreaDepth(areaDepth);
  144. mInputBox->_setWidgetDepth(widgetDepth);
  145. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  146. mInputBox->_setClipRect(elemClipRect);
  147. }
  148. }
  149. const String& GUIIntField::getGUITypeName()
  150. {
  151. static String typeName = "GUIIntField";
  152. return typeName;
  153. }
  154. bool GUIIntField::intFilter(const CM::WString& str)
  155. {
  156. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  157. }
  158. }