BsEditorWidget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  72. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  73. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  74. windowPos.x -= mX;
  75. windowPos.y -= mY;
  76. return windowPos;
  77. }
  78. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  79. {
  80. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  81. RenderWindowPtr parentRenderWindow = parentEditorWindow->getRenderWindow();
  82. Vector2I windowPos = widgetPos;
  83. windowPos.x += mX;
  84. windowPos.y += mY;
  85. return parentRenderWindow->windowToScreenPos(windowPos);
  86. }
  87. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  88. {
  89. onMoved(x, y);
  90. }
  91. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  92. {
  93. onResized(width, height);
  94. }
  95. void EditorWidgetBase::doOnParentChanged()
  96. {
  97. }
  98. // Note: Must not be virtual as parent container uses it in constructor
  99. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent)
  100. {
  101. if(mParent != parent)
  102. {
  103. if(parent != nullptr)
  104. {
  105. if (mContent == nullptr)
  106. mContent = parent->getParentWidget().getPanel()->addNewElement<GUIPanel>();
  107. else
  108. parent->getParentWidget().getPanel()->addElement(mContent);
  109. }
  110. else
  111. {
  112. if (mContent != nullptr)
  113. mParent->getParentWidget().getPanel()->removeElement(mContent);
  114. }
  115. mParent = parent;
  116. doOnParentChanged();
  117. if(!onParentChanged.empty())
  118. onParentChanged(mParent);
  119. }
  120. }
  121. void EditorWidgetBase::_disable()
  122. {
  123. mContent->disableRecursively();
  124. }
  125. void EditorWidgetBase::_enable()
  126. {
  127. mContent->enableRecursively();
  128. }
  129. GUIWidget& EditorWidgetBase::getParentWidget() const
  130. {
  131. return mParent->getParentWidget();
  132. }
  133. }