BsEditorWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsEditorWidget.h"
  4. #include "BsGUIManager.h"
  5. #include "BsGUIWidget.h"
  6. #include "BsGUITexture.h"
  7. #include "BsGUILayout.h"
  8. #include "BsGUIPanel.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. : 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::close()
  49. {
  50. EditorWidgetManager::instance().close(this);
  51. }
  52. void EditorWidgetBase::destroy(EditorWidgetBase* widget)
  53. {
  54. widget->~EditorWidgetBase();
  55. bs_free(widget);
  56. }
  57. void EditorWidgetBase::_setPosition(INT32 x, INT32 y)
  58. {
  59. if (mX == x && mY == y)
  60. return;
  61. mX = x;
  62. mY = y;
  63. if(mContent != nullptr)
  64. mContent->setPosition(x, y);
  65. doOnMoved(x, y);
  66. }
  67. void EditorWidgetBase::_setSize(UINT32 width, UINT32 height)
  68. {
  69. if (mWidth == width && mHeight == height)
  70. return;
  71. mWidth = width;
  72. mHeight = height;
  73. if (mContent != nullptr)
  74. {
  75. mContent->setWidth(width);
  76. mContent->setHeight(height);
  77. }
  78. doOnResized(width, height);
  79. }
  80. void EditorWidgetBase::_setHasFocus(bool focus)
  81. {
  82. if (mHasFocus != focus)
  83. {
  84. mHasFocus = focus;
  85. onFocusChanged(focus);
  86. }
  87. }
  88. Vector2I EditorWidgetBase::screenToWidgetPos(const Vector2I& screenPos) const
  89. {
  90. if (mParent == nullptr)
  91. return screenPos;
  92. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  93. SPtr<RenderWindow> parentRenderWindow = parentEditorWindow->getRenderWindow();
  94. Vector2I windowPos = parentRenderWindow->screenToWindowPos(screenPos);
  95. windowPos.x -= mX;
  96. windowPos.y -= mY;
  97. return windowPos;
  98. }
  99. Vector2I EditorWidgetBase::widgetToScreenPos(const Vector2I& widgetPos) const
  100. {
  101. if (mParent == nullptr)
  102. return widgetPos;
  103. EditorWindowBase* parentEditorWindow = mParent->getParentWindow();
  104. SPtr<RenderWindow> parentRenderWindow = parentEditorWindow->getRenderWindow();
  105. Vector2I windowPos = widgetPos;
  106. windowPos.x += mX;
  107. windowPos.y += mY;
  108. return parentRenderWindow->windowToScreenPos(windowPos);
  109. }
  110. void EditorWidgetBase::doOnMoved(INT32 x, INT32 y)
  111. {
  112. onMoved(x, y);
  113. }
  114. void EditorWidgetBase::doOnResized(UINT32 width, UINT32 height)
  115. {
  116. onResized(width, height);
  117. }
  118. void EditorWidgetBase::doOnParentChanged()
  119. {
  120. }
  121. // Note: Must not be virtual as parent container uses it in constructor
  122. void EditorWidgetBase::_changeParent(EditorWidgetContainer* parent)
  123. {
  124. if(mParent != parent)
  125. {
  126. if(parent != nullptr)
  127. {
  128. if (mContent == nullptr)
  129. mContent = parent->getParentWidget().getPanel()->addNewElement<GUIPanel>();
  130. else
  131. parent->getParentWidget().getPanel()->addElement(mContent);
  132. }
  133. else
  134. {
  135. if (mContent != nullptr)
  136. mParent->getParentWidget().getPanel()->removeElement(mContent);
  137. }
  138. mParent = parent;
  139. doOnParentChanged();
  140. if(!onParentChanged.empty())
  141. onParentChanged(mParent);
  142. }
  143. }
  144. void EditorWidgetBase::_disable()
  145. {
  146. mContent->setVisible(false);
  147. mIsActive = false;
  148. }
  149. void EditorWidgetBase::_enable()
  150. {
  151. mContent->setVisible(true);
  152. mIsActive = true;
  153. }
  154. GUIWidget& EditorWidgetBase::getParentWidget() const
  155. {
  156. return mParent->getParentWidget();
  157. }
  158. }