BsDropDownWindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "EditorWindow/BsDropDownWindow.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "GUI/BsCGUIWidget.h"
  6. #include "GUI/BsGUIPanel.h"
  7. #include "GUI/BsGUITexture.h"
  8. #include "GUI/BsGUIWindowFrame.h"
  9. #include "GUI/BsDropDownAreaPlacement.h"
  10. #include "Utility/BsBuiltinEditorResources.h"
  11. #include "GUI/BsGUIDropDownHitBox.h"
  12. #include "EditorWindow/BsDropDownWindowManager.h"
  13. #include "Renderer/BsCamera.h"
  14. namespace bs
  15. {
  16. DropDownWindow::DropDownWindow(const SPtr<RenderWindow>& parent, const SPtr<Camera>& camera,
  17. const Vector2I& position, UINT32 width, UINT32 height)
  18. : mRenderWindow(parent), mFrontHitBox(nullptr), mBackHitBox(nullptr), mRootPanel(nullptr), mPosition(position)
  19. , mWidth(width), mHeight(height)
  20. {
  21. mSceneObject = SceneObject::create("EditorWindow", SOF_Internal | SOF_Persistent | SOF_DontSave);
  22. mGUI = mSceneObject->addComponent<CGUIWidget>(camera);
  23. mGUI->setDepth(1); // Needs to be in front of everything (except other drop down and tooltip elements)
  24. mGUI->setSkin(BuiltinEditorResources::instance().getSkin());
  25. mRootPanel = mGUI->getPanel()->addNewElement<GUIPanel>();
  26. GUIPanel* frontHitBoxPanel = mRootPanel->addNewElement<GUIPanel>(std::numeric_limits<INT16>::min());
  27. mFrontHitBox = GUIDropDownHitBox::create(false, false);
  28. mFrontHitBox->onFocusGained.connect(std::bind(&DropDownWindow::dropDownFocusGained, this));
  29. frontHitBoxPanel->addElement(mFrontHitBox);
  30. GUIPanel* backHitBoxPanel = mRootPanel->addNewElement<GUIPanel>(std::numeric_limits<INT16>::max());
  31. mBackHitBox = GUIDropDownHitBox::create(false, true);
  32. backHitBoxPanel->addElement(mBackHitBox);
  33. SPtr<Viewport> viewport = camera->getViewport();
  34. GUIPanel* captureHitBoxPanel = mGUI->getPanel()->addNewElement<GUIPanel>(std::numeric_limits<INT16>::max());
  35. GUIDropDownHitBox* captureHitBox = GUIDropDownHitBox::create(true, false);
  36. captureHitBox->setBounds(Rect2I(0, 0, viewport->getPixelArea().width, viewport->getPixelArea().height));
  37. captureHitBoxPanel->addElement(captureHitBox);
  38. setSize(width, height);
  39. GUIPanel* backgroundPanel = mRootPanel->addNewElement<GUIPanel>(500);
  40. backgroundPanel->addElement(GUITexture::create(TextureScaleMode::RepeatToFit,
  41. GUIOptions(GUIOption::flexibleWidth(), GUIOption::flexibleHeight()), "WindowBackground"));
  42. GUIPanel* windowFramePanel = mRootPanel->addNewElement<GUIPanel>(499);
  43. GUIWindowFrame* windowFrame = GUIWindowFrame::create("WindowFrame");
  44. windowFramePanel->addElement(windowFrame);
  45. mContents = mRootPanel->addNewElement<GUIPanel>();
  46. mContents->setPosition(1, 1);
  47. mContents->setWidth(width - 2);
  48. mContents->setHeight(height - 2);
  49. }
  50. DropDownWindow::~DropDownWindow()
  51. {
  52. mSceneObject->destroy();
  53. }
  54. Vector2I DropDownWindow::screenToWindowPos(const Vector2I& screenPos) const
  55. {
  56. Vector2I renderWindowPos = mRenderWindow->screenToWindowPos(screenPos);
  57. Vector2I contentsPos = renderWindowPos;
  58. Rect2I contentArea = mContents->getGlobalBounds();
  59. contentsPos.x -= contentArea.x;
  60. contentsPos.y -= contentArea.y;
  61. return contentsPos;
  62. }
  63. Vector2I DropDownWindow::windowToScreenPos(const Vector2I& windowPos) const
  64. {
  65. Vector2I contentsPos = windowPos;
  66. Rect2I contentArea = mContents->getGlobalBounds();
  67. contentsPos.x += contentArea.x;
  68. contentsPos.y += contentArea.y;
  69. return mRenderWindow->windowToScreenPos(contentsPos);
  70. }
  71. void DropDownWindow::setSize(UINT32 width, UINT32 height)
  72. {
  73. Rect2I availableBounds(0, 0, mGUI->getTarget()->getPixelArea().width, mGUI->getTarget()->getPixelArea().height);
  74. DropDownAreaPlacement dropDownPlacement = DropDownAreaPlacement::aroundPosition(mPosition);
  75. DropDownAreaPlacement::HorzDir horzDir;
  76. DropDownAreaPlacement::VertDir vertDir;
  77. Rect2I placementBounds = dropDownPlacement.getOptimalBounds(width, height, availableBounds, horzDir, vertDir);
  78. placementBounds.width = width;
  79. placementBounds.height = height;
  80. mRootPanel->setPosition(placementBounds.x, placementBounds.y);
  81. mRootPanel->setWidth(width);
  82. mRootPanel->setHeight(height);
  83. Vector<Rect2I> nonDropDownBounds;
  84. availableBounds.cut(placementBounds, nonDropDownBounds);
  85. mFrontHitBox->setBounds(nonDropDownBounds);
  86. mBackHitBox->setBounds(placementBounds);
  87. mWidth = width;
  88. mHeight = height;
  89. }
  90. void DropDownWindow::close()
  91. {
  92. DropDownWindowManager::instance().close();
  93. }
  94. void DropDownWindow::dropDownFocusGained()
  95. {
  96. close();
  97. }
  98. }