BsDropDownWindow.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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), mHitBox(nullptr)
  17. {
  18. mSceneObject = SceneObject::create("EditorWindow");
  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* hitBoxPanel = mRootPanel->addNewElement<GUIPanel>(std::numeric_limits<INT16>::min());
  24. mHitBox = GUIDropDownHitBox::create(false);
  25. mHitBox->onFocusLost.connect(std::bind(&DropDownWindow::dropDownFocusLost, this));
  26. mHitBox->setFocus(true);
  27. hitBoxPanel->addElement(mHitBox);
  28. GUIPanel* captureHitBoxPanel = mGUI->getPanel()->addNewElement<GUIPanel>(std::numeric_limits<INT16>::max());
  29. GUIDropDownHitBox* captureHitBox = GUIDropDownHitBox::create(true);
  30. captureHitBox->setBounds(Rect2I(0, 0, target->getWidth(), target->getHeight()));
  31. captureHitBoxPanel->addElement(captureHitBox);
  32. setSize(width, height);
  33. GUIPanel* backgroundPanel = mRootPanel->addNewElement<GUIPanel>(500);
  34. backgroundPanel->addElement(GUITexture::create(GUIImageScaleMode::RepeatToFit,
  35. GUIOptions(GUIOption::flexibleWidth(), GUIOption::flexibleHeight()), "WindowBackground"));
  36. GUIPanel* windowFramePanel = mRootPanel->addNewElement<GUIPanel>(499);
  37. GUIWindowFrame* windowFrame = GUIWindowFrame::create("WindowFrame");
  38. windowFramePanel->addElement(windowFrame);
  39. mContents = mRootPanel->addNewElement<GUIPanel>();
  40. mContents->setPosition(1, 1);
  41. mContents->setWidth(width - 2);
  42. mContents->setHeight(height - 2);
  43. }
  44. DropDownWindow::~DropDownWindow()
  45. {
  46. mSceneObject->destroy();
  47. }
  48. Vector2I DropDownWindow::screenToWindowPos(const Vector2I& screenPos) const
  49. {
  50. Vector2I renderWindowPos = mRenderWindow->screenToWindowPos(screenPos);
  51. Vector2I contentsPos = renderWindowPos;
  52. Rect2I contentArea = mContents->getGlobalBounds();
  53. contentsPos.x -= contentArea.x;
  54. contentsPos.y -= contentArea.y;
  55. return contentsPos;
  56. }
  57. Vector2I DropDownWindow::windowToScreenPos(const Vector2I& windowPos) const
  58. {
  59. Vector2I contentsPos = windowPos;
  60. Rect2I contentArea = mContents->getGlobalBounds();
  61. contentsPos.x += contentArea.x;
  62. contentsPos.y += contentArea.y;
  63. return mRenderWindow->windowToScreenPos(contentsPos);
  64. }
  65. void DropDownWindow::setSize(UINT32 width, UINT32 height)
  66. {
  67. Rect2I availableBounds(0, 0, mGUI->getTarget()->getWidth(), mGUI->getTarget()->getHeight());
  68. DropDownAreaPlacement dropDownPlacement = DropDownAreaPlacement::aroundPosition(mPosition);
  69. DropDownAreaPlacement::HorzDir horzDir;
  70. DropDownAreaPlacement::VertDir vertDir;
  71. Rect2I placementBounds = dropDownPlacement.getOptimalBounds(width, height, availableBounds, horzDir, vertDir);
  72. mRootPanel->setPosition(placementBounds.x, placementBounds.y);
  73. mRootPanel->setWidth(width);
  74. mRootPanel->setHeight(height);
  75. mHitBox->setBounds(Rect2I(placementBounds.x, placementBounds.y, width, height));
  76. mWidth = width;
  77. mHeight = height;
  78. }
  79. void DropDownWindow::close()
  80. {
  81. DropDownWindowManager::instance().close();
  82. }
  83. void DropDownWindow::dropDownFocusLost()
  84. {
  85. close();
  86. }
  87. }