2
0

BsDropDownWindow.cpp 4.1 KB

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