BsEditorWidget.cpp 3.8 KB

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