BsEditorWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "BsEditorWidget.h"
  2. #include "BsGUIManager.h"
  3. #include "BsCGUIWidget.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, UINT32 defaultWidth,
  16. UINT32 defaultHeight, EditorWidgetContainer& parentContainer)
  17. :mDisplayName(displayName), mName(name), mParent(nullptr), mContent(nullptr), mX(0), mY(0), mWidth(0),
  18. mHeight(0), mHasFocus(false), mDefaultWidth(defaultWidth), mDefaultHeight(defaultHeight)
  19. {
  20. parentContainer.add(*this);
  21. }
  22. EditorWidgetBase::~EditorWidgetBase()
  23. {
  24. if (mContent != nullptr)
  25. GUILayout::destroy(mContent);
  26. }
  27. EditorWindowBase* EditorWidgetBase::getParentWindow() const
  28. {
  29. if (mParent != nullptr)
  30. return mParent->getParentWindow();
  31. else
  32. return nullptr;
  33. }
  34. void EditorWidgetBase::close()
  35. {
  36. EditorWidgetManager::instance().close(this);
  37. }
  38. void EditorWidgetBase::destroy(EditorWidgetBase* widget)
  39. {
  40. widget->~EditorWidgetBase();
  41. bs_free(widget);
  42. }
  43. void EditorWidgetBase::_setPosition(INT32 x, INT32 y)
  44. {
  45. if (mX == x && mY == y)
  46. return;
  47. mX = x;
  48. mY = y;
  49. if(mContent != nullptr)
  50. mContent->setPosition(x, y);
  51. doOnMoved(x, y);
  52. }
  53. void EditorWidgetBase::_setSize(UINT32 width, UINT32 height)
  54. {
  55. if (mWidth == width && mHeight == height)
  56. return;
  57. mWidth = width;
  58. mHeight = height;
  59. if (mContent != nullptr)
  60. {
  61. mContent->setWidth(width);
  62. mContent->setHeight(height);
  63. }
  64. doOnResized(width, height);
  65. }
  66. void EditorWidgetBase::_setHasFocus(bool focus)
  67. {
  68. if (mHasFocus != focus)
  69. {
  70. mHasFocus = focus;
  71. onFocusChanged(focus);
  72. }
  73. }
  74. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  75. {
  76. if (mParent == nullptr)
  77. return screenPos;
  78. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  79. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  80. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  81. windowPos.x -= mX;
  82. windowPos.y -= mY;
  83. return windowPos;
  84. }
  85. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  86. {
  87. if (mParent == nullptr)
  88. return widgetPos;
  89. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  90. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  91. Vector2I windowPos = widgetPos;
  92. windowPos.x += mX;
  93. windowPos.y += mY;
  94. return parentRenderWindow->windowToScreenPos(windowPos);
  95. }
  96. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  97. {
  98. onMoved(x, y);
  99. }
  100. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  101. {
  102. onResized(width, height);
  103. }
  104. void EditorWidgetBase::doOnParentChanged()
  105. {
  106. }
  107. // Note: Must not be virtual as parent container uses it in constructor
  108. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent)
  109. {
  110. if(mParent != parent)
  111. {
  112. if(parent != nullptr)
  113. {
  114. if (mContent == nullptr)
  115. mContent = parent->getParentWidget().getPanel()->addNewElement<GUIPanel>();
  116. else
  117. parent->getParentWidget().getPanel()->addElement(mContent);
  118. }
  119. else
  120. {
  121. if (mContent != nullptr)
  122. mParent->getParentWidget().getPanel()->removeElement(mContent);
  123. }
  124. mParent = parent;
  125. doOnParentChanged();
  126. if(!onParentChanged.empty())
  127. onParentChanged(mParent);
  128. }
  129. }
  130. void EditorWidgetBase::_disable()
  131. {
  132. mContent->setVisible(false);
  133. }
  134. void EditorWidgetBase::_enable()
  135. {
  136. mContent->setVisible(true);
  137. }
  138. CGUIWidget& EditorWidgetBase::getParentWidget() const
  139. {
  140. return mParent->getParentWidget();
  141. }
  142. }