BsEditorWidget.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. EditorWindowBase* EditorWidgetBase::getParentWindow() const
  26. {
  27. return mParent->getParentWindow();
  28. }
  29. void EditorWidgetBase::close()
  30. {
  31. EditorWidgetManager::instance().close(this);
  32. }
  33. void EditorWidgetBase::destroy(EditorWidgetBase* widget)
  34. {
  35. widget->~EditorWidgetBase();
  36. bs_free(widget);
  37. }
  38. void EditorWidgetBase::_setPosition(INT32 x, INT32 y)
  39. {
  40. if (mX == x && mY == y)
  41. return;
  42. mX = x;
  43. mY = y;
  44. if(mContent != nullptr)
  45. mContent->setPosition(x, y);
  46. doOnMoved(x, y);
  47. }
  48. void EditorWidgetBase::_setSize(UINT32 width, UINT32 height)
  49. {
  50. if (mWidth == width && mHeight == height)
  51. return;
  52. mWidth = width;
  53. mHeight = height;
  54. if(mContent != nullptr)
  55. mContent->setSize(width, height);
  56. doOnResized(width, height);
  57. }
  58. void EditorWidgetBase::_setHasFocus(bool focus)
  59. {
  60. if (mHasFocus != focus)
  61. {
  62. mHasFocus = focus;
  63. onFocusChanged(focus);
  64. }
  65. }
  66. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  67. {
  68. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  69. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  70. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  71. windowPos.x -= mX;
  72. windowPos.y -= mY;
  73. return windowPos;
  74. }
  75. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  76. {
  77. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  78. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  79. Vector2I windowPos = widgetPos;
  80. windowPos.x += mX;
  81. windowPos.y += mY;
  82. return parentRenderWindow->windowToScreenPos(windowPos);
  83. }
  84. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  85. {
  86. onMoved(x, y);
  87. }
  88. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  89. {
  90. onResized(width, height);
  91. }
  92. void EditorWidgetBase::doOnParentChanged()
  93. {
  94. }
  95. // Note: Must not be virtual as parent container uses it in constructor
  96. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent)
  97. {
  98. if(mParent != parent)
  99. {
  100. if(parent != nullptr)
  101. {
  102. if(mContent == nullptr)
  103. mContent = GUIArea::create(parent->getParentWidget(), 0, 0, 0, 0, 0);
  104. else
  105. mContent->changeParentWidget(&parent->getParentWidget());
  106. }
  107. else
  108. {
  109. if(mContent != nullptr)
  110. mContent->changeParentWidget(nullptr);
  111. }
  112. mParent = parent;
  113. doOnParentChanged();
  114. if(!onParentChanged.empty())
  115. onParentChanged(mParent);
  116. }
  117. }
  118. void EditorWidgetBase::_disable()
  119. {
  120. mContent->disable();
  121. }
  122. void EditorWidgetBase::_enable()
  123. {
  124. mContent->enable();
  125. }
  126. GUIWidget& EditorWidgetBase::getParentWidget() const
  127. {
  128. return mParent->getParentWidget();
  129. }
  130. }