BsEditorWidget.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "BsEditorWidget.h"
  2. #include "BsGUIManager.h"
  3. #include "BsGUIWidget.h"
  4. #include "BsGUITexture.h"
  5. #include "BsGUISkin.h"
  6. #include "BsGUILayout.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsGUIArea.h"
  9. #include "BsEditorWidgetContainer.h"
  10. #include "BsEditorWidgetManager.h"
  11. #include "BsEditorWindow.h"
  12. #include "BsRenderWindow.h"
  13. namespace BansheeEngine
  14. {
  15. EditorWidgetBase::EditorWidgetBase(const HString& displayName, const String& name, EditorWidgetContainer& parentContainer)
  16. :mDisplayName(displayName), mName(name), mParent(nullptr), mContent(nullptr), mX(0), mY(0), mWidth(0), mHeight(0), mHasFocus(false)
  17. {
  18. parentContainer.add(*this);
  19. }
  20. EditorWidgetBase::~EditorWidgetBase()
  21. {
  22. if (mContent != nullptr)
  23. GUIArea::destroy(mContent);
  24. }
  25. void EditorWidgetBase::close()
  26. {
  27. EditorWidgetManager::instance().close(this);
  28. }
  29. void EditorWidgetBase::destroy(EditorWidgetBase* widget)
  30. {
  31. widget->~EditorWidgetBase();
  32. bs_free(widget);
  33. }
  34. void EditorWidgetBase::_setPosition(INT32 x, INT32 y)
  35. {
  36. if (mX == x && mY == y)
  37. return;
  38. mX = x;
  39. mY = y;
  40. if(mContent != nullptr)
  41. mContent->setPosition(x, y);
  42. doOnMoved(x, y);
  43. }
  44. void EditorWidgetBase::_setSize(UINT32 width, UINT32 height)
  45. {
  46. if (mWidth == width && mHeight == height)
  47. return;
  48. mWidth = width;
  49. mHeight = height;
  50. if(mContent != nullptr)
  51. mContent->setSize(width, height);
  52. doOnResized(width, height);
  53. }
  54. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  55. {
  56. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  57. RenderWindowPtr parentRenderWindow = parentEditorWindow->_getRenderWindow();
  58. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  59. windowPos.x -= mX;
  60. windowPos.y -= mY;
  61. return windowPos;
  62. }
  63. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  64. {
  65. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  66. RenderWindowPtr parentRenderWindow = parentEditorWindow->_getRenderWindow();
  67. Vector2I windowPos = widgetPos;
  68. windowPos.x += mX;
  69. windowPos.y += mY;
  70. return parentRenderWindow->windowToScreenPos(windowPos);
  71. }
  72. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  73. {
  74. if (!onMoved.empty())
  75. onMoved(x, y);
  76. }
  77. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  78. {
  79. if (!onResized.empty())
  80. onResized(width, height);
  81. }
  82. void EditorWidgetBase::doOnParentChanged()
  83. {
  84. }
  85. // Note: Must not be virtual as parent container uses it in constructor
  86. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent)
  87. {
  88. if(mParent != parent)
  89. {
  90. if(parent != nullptr)
  91. {
  92. if(mContent == nullptr)
  93. mContent = GUIArea::create(parent->getParentWidget(), 0, 0, 0, 0, 10000);
  94. else
  95. mContent->changeParentWidget(&parent->getParentWidget());
  96. }
  97. else
  98. {
  99. if(mContent != nullptr)
  100. mContent->changeParentWidget(nullptr);
  101. }
  102. mParent = parent;
  103. doOnParentChanged();
  104. if(!onParentChanged.empty())
  105. onParentChanged(mParent);
  106. }
  107. }
  108. void EditorWidgetBase::_disable()
  109. {
  110. mContent->disable();
  111. }
  112. void EditorWidgetBase::_enable()
  113. {
  114. mContent->enable();
  115. }
  116. GUIWidget& EditorWidgetBase::getParentWidget() const
  117. {
  118. return mParent->getParentWidget();
  119. }
  120. }