BsEditorWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "EditorWindow/BsEditorWidget.h"
  4. #include "GUI/BsGUIManager.h"
  5. #include "GUI/BsGUIWidget.h"
  6. #include "GUI/BsGUITexture.h"
  7. #include "GUI/BsGUILayout.h"
  8. #include "GUI/BsGUIPanel.h"
  9. #include "EditorWindow/BsEditorWidgetContainer.h"
  10. #include "EditorWindow/BsEditorWidgetManager.h"
  11. #include "EditorWindow/BsEditorWindow.h"
  12. #include "RenderAPI/BsRenderWindow.h"
  13. namespace bs
  14. {
  15. EditorWidgetBase::EditorWidgetBase(const HString& displayName, const String& name, UINT32 defaultWidth,
  16. UINT32 defaultHeight, EditorWidgetContainer& parentContainer)
  17. : mName(name), mDisplayName(displayName), mDefaultWidth(defaultWidth), mDefaultHeight(defaultHeight)
  18. {
  19. parentContainer.add(*this);
  20. }
  21. EditorWidgetBase::~EditorWidgetBase()
  22. {
  23. if (mContent != nullptr)
  24. GUILayout::destroy(mContent);
  25. }
  26. EditorWindowBase* EditorWidgetBase::getParentWindow() const
  27. {
  28. if (mParent != nullptr)
  29. return mParent->getParentWindow();
  30. else
  31. return nullptr;
  32. }
  33. void EditorWidgetBase::setHasFocus(bool focus)
  34. {
  35. EditorWidgetContainer* parentContainer = _getParent();
  36. if (parentContainer == nullptr)
  37. return;
  38. EditorWindowBase* parentWindow = parentContainer->getParentWindow();
  39. SPtr<RenderWindow> parentRenderWindow = parentWindow->getRenderWindow();
  40. const RenderWindowProperties& props = parentRenderWindow->getProperties();
  41. if (!props.hasFocus)
  42. return;
  43. if (parentContainer->getActiveWidget() != this)
  44. return;
  45. _setHasFocus(focus);
  46. }
  47. void EditorWidgetBase::setActive()
  48. {
  49. EditorWidgetContainer* parentContainer = _getParent();
  50. if (parentContainer == nullptr)
  51. return;
  52. parentContainer->setActiveWidget(mIndex);
  53. }
  54. void EditorWidgetBase::close()
  55. {
  56. EditorWidgetManager::instance().close(this);
  57. }
  58. void EditorWidgetBase::destroy(EditorWidgetBase* widget)
  59. {
  60. widget->~EditorWidgetBase();
  61. bs_free(widget);
  62. }
  63. void EditorWidgetBase::_setPosition(INT32 x, INT32 y)
  64. {
  65. if (mX == x && mY == y)
  66. return;
  67. mX = x;
  68. mY = y;
  69. if(mContent != nullptr)
  70. mContent->setPosition(x, y);
  71. doOnMoved(x, y);
  72. }
  73. void EditorWidgetBase::_setSize(UINT32 width, UINT32 height)
  74. {
  75. if (mWidth == width && mHeight == height)
  76. return;
  77. mWidth = width;
  78. mHeight = height;
  79. if (mContent != nullptr)
  80. {
  81. mContent->setWidth(width);
  82. mContent->setHeight(height);
  83. }
  84. doOnResized(width, height);
  85. }
  86. void EditorWidgetBase::_setHasFocus(bool focus)
  87. {
  88. if (mHasFocus != focus)
  89. {
  90. mHasFocus = focus;
  91. onFocusChanged(focus);
  92. }
  93. }
  94. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  95. {
  96. if (mParent == nullptr)
  97. return screenPos;
  98. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  99. SPtr<RenderWindow> parentRenderWindow = parentEditorWindow->getRenderWindow();
  100. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  101. windowPos.x -= mX;
  102. windowPos.y -= mY;
  103. return windowPos;
  104. }
  105. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  106. {
  107. if (mParent == nullptr)
  108. return widgetPos;
  109. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  110. SPtr<RenderWindow> parentRenderWindow = parentEditorWindow->getRenderWindow();
  111. Vector2I windowPos = widgetPos;
  112. windowPos.x += mX;
  113. windowPos.y += mY;
  114. return parentRenderWindow->windowToScreenPos(windowPos);
  115. }
  116. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  117. {
  118. onMoved(x, y);
  119. }
  120. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  121. {
  122. onResized(width, height);
  123. }
  124. void EditorWidgetBase::doOnParentChanged()
  125. {
  126. }
  127. // Note: Must not be virtual as parent container uses it in constructor
  128. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent, UINT32 indx)
  129. {
  130. if(mParent != parent)
  131. {
  132. if(parent != nullptr)
  133. {
  134. if (mContent == nullptr)
  135. mContent = parent->getParentWidget().getPanel()->addNewElement<GUIPanel>();
  136. else
  137. parent->getParentWidget().getPanel()->addElement(mContent);
  138. }
  139. else
  140. {
  141. if (mContent != nullptr)
  142. mParent->getParentWidget().getPanel()->removeElement(mContent);
  143. }
  144. mParent = parent;
  145. mIndex = indx;
  146. doOnParentChanged();
  147. if(!onParentChanged.empty())
  148. onParentChanged(mParent);
  149. }
  150. }
  151. void EditorWidgetBase::_disable()
  152. {
  153. mContent->setVisible(false);
  154. mIsActive = false;
  155. }
  156. void EditorWidgetBase::_enable()
  157. {
  158. mContent->setVisible(true);
  159. mIsActive = true;
  160. }
  161. GUIWidget& EditorWidgetBase::getParentWidget() const
  162. {
  163. return mParent->getParentWidget();
  164. }
  165. }