BsDropDownWindow.cpp 3.4 KB

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