BsDropDownWindow.cpp 4.4 KB

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