BsGUIVector2Field.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUIVector2Field.h"
  4. #include "GUI/BsGUILayoutX.h"
  5. #include "GUI/BsGUILayoutY.h"
  6. #include "GUI/BsGUIFloatField.h"
  7. #include "GUI/BsGUILabel.h"
  8. using namespace std::placeholders;
  9. namespace bs
  10. {
  11. const UINT32 GUIVector2Field::ELEMENT_LABEL_WIDTH = 10;
  12. GUIVector2Field::GUIVector2Field(const PrivatelyConstruct& dummy, const GUIContent& labelContent,
  13. UINT32 labelWidth, const String& style, const GUIDimensions& dimensions, bool withLabel)
  14. :TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel),
  15. mFieldX(nullptr), mFieldY(nullptr)
  16. {
  17. mFieldX = GUIFloatField::create(HString("X"), ELEMENT_LABEL_WIDTH, getSubStyleName(getFloatFieldStyleType()));
  18. mFieldY = GUIFloatField::create(HString("Y"), ELEMENT_LABEL_WIDTH, getSubStyleName(getFloatFieldStyleType()));
  19. mFieldX->onValueChanged.connect(std::bind(&GUIVector2Field::valueChanged, this, _1));
  20. mFieldY->onValueChanged.connect(std::bind(&GUIVector2Field::valueChanged, this, _1));
  21. mFieldX->onConfirm.connect(std::bind(&GUIVector2Field::inputConfirmed, this));
  22. mFieldY->onConfirm.connect(std::bind(&GUIVector2Field::inputConfirmed, this));
  23. mLayout->removeElement(mLabel);
  24. GUILayout* layout = mLayout->addNewElement<GUILayoutY>();
  25. layout->addElement(mLabel);
  26. mLabel->resetDimensions();
  27. GUILayout* elementLayout = layout->addNewElement<GUILayoutX>();
  28. elementLayout->addElement(mFieldX);
  29. elementLayout->addElement(mFieldY);
  30. }
  31. Vector2 GUIVector2Field::getValue() const
  32. {
  33. Vector2 value;
  34. value.x = mFieldX->getValue();
  35. value.y = mFieldY->getValue();
  36. return value;
  37. }
  38. void GUIVector2Field::setValue(const Vector2& value)
  39. {
  40. mFieldX->setValue(value.x);
  41. mFieldY->setValue(value.y);
  42. }
  43. bool GUIVector2Field::hasInputFocus() const
  44. {
  45. return mFieldX->hasInputFocus() || mFieldY->hasInputFocus();
  46. }
  47. void GUIVector2Field::setTint(const Color& color)
  48. {
  49. if (mLabel != nullptr)
  50. mLabel->setTint(color);
  51. mFieldX->setTint(color);
  52. mFieldY->setTint(color);
  53. }
  54. void GUIVector2Field::valueChanged(float newValue)
  55. {
  56. Vector2 value = getValue();
  57. onValueChanged(value);
  58. }
  59. void GUIVector2Field::inputConfirmed()
  60. {
  61. onConfirm();
  62. }
  63. void GUIVector2Field::styleUpdated()
  64. {
  65. if (mLabel != nullptr)
  66. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  67. mFieldX->setStyle(getSubStyleName(getFloatFieldStyleType()));
  68. mFieldY->setStyle(getSubStyleName(getFloatFieldStyleType()));
  69. }
  70. const String& GUIVector2Field::getGUITypeName()
  71. {
  72. static String typeName = "GUIVector2Field";
  73. return typeName;
  74. }
  75. const String& GUIVector2Field::getFloatFieldStyleType()
  76. {
  77. static String LABEL_STYLE_TYPE = "EditorFloatField";
  78. return LABEL_STYLE_TYPE;
  79. }
  80. }