BsModalWindow.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "EditorWindow/BsModalWindow.h"
  4. #include "EditorWindow/BsEditorWindowManager.h"
  5. #include "RenderAPI/BsRenderWindow.h"
  6. #include "Platform/BsPlatform.h"
  7. #include "GUI/BsGUILayoutX.h"
  8. #include "GUI/BsGUILayoutY.h"
  9. #include "GUI/BsGUISpace.h"
  10. #include "GUI/BsGUIButton.h"
  11. #include "GUI/BsGUITexture.h"
  12. #include "GUI/BsGUILabel.h"
  13. #include "GUI/BsGUIPanel.h"
  14. #include "GUI/BsCGUIWidget.h"
  15. namespace bs
  16. {
  17. ModalWindow::ModalWindow(const HString& title, bool hasCloseButton)
  18. : EditorWindowBase(true), mTitleBarPanel(nullptr), mTitleBarBgPanel(nullptr), mTitle(nullptr), mCloseButton(nullptr)
  19. , mTitleBarBg(nullptr),mContents(nullptr)
  20. {
  21. EditorWindowManager::instance().registerWindow(this);
  22. mTitleBarBgPanel = mGUI->getPanel()->addNewElement<GUIPanel>();
  23. mTitleBarBgPanel->setDepthRange(std::numeric_limits<INT16>::max() - 1);
  24. mTitleBarBgPanel->setPosition(1, 1);
  25. mTitleBarPanel = mGUI->getPanel()->addNewElement<GUIPanel>();
  26. mTitleBarPanel->setDepthRange(0);
  27. mTitleBarPanel->setPosition(1, 1);
  28. mTitleBarBg = GUITexture::create(GUIOptions(GUIOption::flexibleWidth()), "TitleBarBackground");
  29. mTitle = GUILabel::create(title);
  30. GUILayout* bgLayout = mTitleBarBgPanel->addNewElement<GUILayoutY>();
  31. GUILayout* bgLayoutX = bgLayout->addNewElement<GUILayoutX>();
  32. bgLayoutX->addElement(mTitleBarBg);
  33. bgLayoutX->addNewElement<GUIFixedSpace>(1);
  34. bgLayout->addNewElement<GUIFlexibleSpace>();
  35. bgLayout->addNewElement<GUIFixedSpace>(1);
  36. GUILayout* contentLayoutY = mTitleBarPanel->addNewElement<GUILayoutY>();
  37. GUILayout* contentLayoutX = contentLayoutY->addNewElement<GUILayoutX>();
  38. contentLayoutX->addNewElement<GUIFlexibleSpace>();
  39. GUILayout* titleLayout = contentLayoutX->addNewElement<GUILayoutY>();
  40. titleLayout->addNewElement<GUIFixedSpace>(2);
  41. titleLayout->addElement(mTitle);
  42. titleLayout->addNewElement<GUIFlexibleSpace>();
  43. contentLayoutX->addNewElement<GUIFlexibleSpace>();
  44. if (hasCloseButton)
  45. {
  46. mCloseButton = GUIButton::create(HString(""), "WinCloseBtn");
  47. contentLayoutX->addElement(mCloseButton);
  48. mCloseButton->onClick.connect(std::bind(&ModalWindow::close, this));
  49. }
  50. contentLayoutX->addNewElement<GUIFixedSpace>(1);
  51. contentLayoutY->addNewElement<GUIFlexibleSpace>();
  52. contentLayoutY->addNewElement<GUIFixedSpace>(1);
  53. mContents = mGUI->getPanel()->addNewElement<GUIPanel>();
  54. mContents->setDepthRange(0);
  55. mContents->setPosition(1, 1 + getTitleBarHeight());
  56. updateSize();
  57. }
  58. ModalWindow::~ModalWindow()
  59. {
  60. }
  61. Vector2I ModalWindow::screenToWindowPos(const Vector2I& screenPos) const
  62. {
  63. Vector2I renderWindowPos = getRenderWindow()->screenToWindowPos(screenPos);
  64. Vector2I contentsPos = renderWindowPos;
  65. Rect2I contentArea = getContentArea();
  66. contentsPos.x -= contentArea.x;
  67. contentsPos.y -= contentArea.y;
  68. return contentsPos;
  69. }
  70. Vector2I ModalWindow::windowToScreenPos(const Vector2I& windowPos) const
  71. {
  72. Vector2I contentsPos = windowPos;
  73. Rect2I contentArea = getContentArea();
  74. contentsPos.x += contentArea.x;
  75. contentsPos.y += contentArea.y;
  76. return getRenderWindow()->windowToScreenPos(contentsPos);
  77. }
  78. void ModalWindow::update()
  79. {
  80. }
  81. void ModalWindow::close()
  82. {
  83. EditorWindowManager::instance().destroy(this);
  84. }
  85. void ModalWindow::setTitle(const HString& title)
  86. {
  87. mTitle->setContent(GUIContent(title));
  88. }
  89. void ModalWindow::resized()
  90. {
  91. EditorWindowBase::resized();
  92. updateSize();
  93. }
  94. void ModalWindow::updateSize()
  95. {
  96. mContents->setWidth(getWidth() - 2);
  97. mContents->setHeight(getHeight() - 2 - getTitleBarHeight());
  98. UINT32 captionWidth = getWidth() - 2;
  99. if (mCloseButton != nullptr)
  100. captionWidth = mCloseButton->getGlobalBounds().x;
  101. Vector<Rect2I> captionAreas;
  102. captionAreas.push_back(Rect2I(1, 1, captionWidth, getTitleBarHeight()));
  103. Platform::setCaptionNonClientAreas(*mRenderWindow->getCore().get(), captionAreas);
  104. }
  105. Rect2I ModalWindow::getContentArea() const
  106. {
  107. return Rect2I(1, 1 + getTitleBarHeight(), getWidth() - 2, getHeight() - getTitleBarHeight() - 2);
  108. }
  109. UINT32 ModalWindow::getTitleBarHeight() const
  110. {
  111. return mTitleBarBg->getBounds().height;
  112. }
  113. }