BsDropDownWindow.cpp 4.0 KB

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