BsEditorWidget.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "BsGUIPanel.h"
  8. #include "BsBuiltinResources.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. GUILayout::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. {
  56. mContent->setWidth(width);
  57. mContent->setHeight(height);
  58. }
  59. doOnResized(width, height);
  60. }
  61. void EditorWidgetBase::_setHasFocus(bool focus)
  62. {
  63. if (mHasFocus != focus)
  64. {
  65. mHasFocus = focus;
  66. onFocusChanged(focus);
  67. }
  68. }
  69. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  70. {
  71. if (mParent == nullptr)
  72. return screenPos;
  73. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  74. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  75. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  76. windowPos.x -= mX;
  77. windowPos.y -= mY;
  78. return windowPos;
  79. }
  80. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  81. {
  82. if (mParent == nullptr)
  83. return widgetPos;
  84. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  85. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  86. Vector2I windowPos = widgetPos;
  87. windowPos.x += mX;
  88. windowPos.y += mY;
  89. return parentRenderWindow->windowToScreenPos(windowPos);
  90. }
  91. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  92. {
  93. onMoved(x, y);
  94. }
  95. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  96. {
  97. onResized(width, height);
  98. }
  99. void EditorWidgetBase::doOnParentChanged()
  100. {
  101. }
  102. // Note: Must not be virtual as parent container uses it in constructor
  103. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent)
  104. {
  105. if(mParent != parent)
  106. {
  107. if(parent != nullptr)
  108. {
  109. if (mContent == nullptr)
  110. mContent = parent->getParentWidget().getPanel()->addNewElement<GUIPanel>();
  111. else
  112. parent->getParentWidget().getPanel()->addElement(mContent);
  113. }
  114. else
  115. {
  116. if (mContent != nullptr)
  117. mParent->getParentWidget().getPanel()->removeElement(mContent);
  118. }
  119. mParent = parent;
  120. doOnParentChanged();
  121. if(!onParentChanged.empty())
  122. onParentChanged(mParent);
  123. }
  124. }
  125. void EditorWidgetBase::_disable()
  126. {
  127. mContent->disableRecursively();
  128. }
  129. void EditorWidgetBase::_enable()
  130. {
  131. mContent->enableRecursively();
  132. }
  133. GUIWidget& EditorWidgetBase::getParentWidget() const
  134. {
  135. return mParent->getParentWidget();
  136. }
  137. }