BsModalWindow.cpp 3.9 KB

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