BsEditorWidget.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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), mParent(nullptr), mX(0), mY(0), mWidth(0), mHeight(0)
  18. , mDefaultWidth(defaultWidth), mDefaultHeight(defaultHeight), mContent(nullptr), mHasFocus(false), mIsActive(true)
  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::setHasFocus(bool focus)
  35. {
  36. EditorWidgetContainer* parentContainer = _getParent();
  37. if (parentContainer == nullptr)
  38. return;
  39. EditorWindowBase* parentWindow = parentContainer->getParentWindow();
  40. SPtr<RenderWindow> parentRenderWindow = parentWindow->getRenderWindow();
  41. const RenderWindowProperties& props = parentRenderWindow->getProperties();
  42. if (!props.hasFocus)
  43. return;
  44. if (parentContainer->getActiveWidget() != this)
  45. return;
  46. _setHasFocus(focus);
  47. }
  48. void EditorWidgetBase::setActive()
  49. {
  50. EditorWidgetContainer* parentContainer = _getParent();
  51. if (parentContainer == nullptr)
  52. return;
  53. parentContainer->setActiveWidget(mIndex);
  54. }
  55. void EditorWidgetBase::close()
  56. {
  57. EditorWidgetManager::instance().close(this);
  58. }
  59. void EditorWidgetBase::destroy(EditorWidgetBase* widget)
  60. {
  61. widget->~EditorWidgetBase();
  62. bs_free(widget);
  63. }
  64. void EditorWidgetBase::_setPosition(INT32 x, INT32 y)
  65. {
  66. if (mX == x && mY == y)
  67. return;
  68. mX = x;
  69. mY = y;
  70. if(mContent != nullptr)
  71. mContent->setPosition(x, y);
  72. doOnMoved(x, y);
  73. }
  74. void EditorWidgetBase::_setSize(UINT32 width, UINT32 height)
  75. {
  76. if (mWidth == width && mHeight == height)
  77. return;
  78. mWidth = width;
  79. mHeight = height;
  80. if (mContent != nullptr)
  81. {
  82. mContent->setWidth(width);
  83. mContent->setHeight(height);
  84. }
  85. doOnResized(width, height);
  86. }
  87. void EditorWidgetBase::_setHasFocus(bool focus)
  88. {
  89. if (mHasFocus != focus)
  90. {
  91. mHasFocus = focus;
  92. onFocusChanged(focus);
  93. }
  94. }
  95. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  96. {
  97. if (mParent == nullptr)
  98. return screenPos;
  99. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  100. SPtr<RenderWindow> parentRenderWindow = parentEditorWindow->getRenderWindow();
  101. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  102. windowPos.x -= mX;
  103. windowPos.y -= mY;
  104. return windowPos;
  105. }
  106. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  107. {
  108. if (mParent == nullptr)
  109. return widgetPos;
  110. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  111. SPtr<RenderWindow> parentRenderWindow = parentEditorWindow->getRenderWindow();
  112. Vector2I windowPos = widgetPos;
  113. windowPos.x += mX;
  114. windowPos.y += mY;
  115. return parentRenderWindow->windowToScreenPos(windowPos);
  116. }
  117. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  118. {
  119. onMoved(x, y);
  120. }
  121. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  122. {
  123. onResized(width, height);
  124. }
  125. void EditorWidgetBase::doOnParentChanged()
  126. {
  127. }
  128. // Note: Must not be virtual as parent container uses it in constructor
  129. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent, UINT32 indx)
  130. {
  131. if(mParent != parent)
  132. {
  133. if(parent != nullptr)
  134. {
  135. if (mContent == nullptr)
  136. mContent = parent->getParentWidget().getPanel()->addNewElement<GUIPanel>();
  137. else
  138. parent->getParentWidget().getPanel()->addElement(mContent);
  139. }
  140. else
  141. {
  142. if (mContent != nullptr)
  143. mParent->getParentWidget().getPanel()->removeElement(mContent);
  144. }
  145. mParent = parent;
  146. mIndex = indx;
  147. doOnParentChanged();
  148. if(!onParentChanged.empty())
  149. onParentChanged(mParent);
  150. }
  151. }
  152. void EditorWidgetBase::_disable()
  153. {
  154. mContent->setVisible(false);
  155. mIsActive = false;
  156. }
  157. void EditorWidgetBase::_enable()
  158. {
  159. mContent->setVisible(true);
  160. mIsActive = true;
  161. }
  162. GUIWidget& EditorWidgetBase::getParentWidget() const
  163. {
  164. return mParent->getParentWidget();
  165. }
  166. }