BsModalWindow.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, UINT32 width, UINT32 height)
  18. : EditorWindowBase(true, width, height)
  19. {
  20. EditorWindowManager::instance().registerWindow(this);
  21. mTitleBarBgPanel = mGUI->getPanel()->addNewElement<GUIPanel>();
  22. mTitleBarBgPanel->setDepthRange(std::numeric_limits<INT16>::max() - 1);
  23. mTitleBarBgPanel->setPosition(1, 1);
  24. mTitleBarPanel = mGUI->getPanel()->addNewElement<GUIPanel>();
  25. mTitleBarPanel->setDepthRange(0);
  26. mTitleBarPanel->setPosition(1, 1);
  27. mTitleBarBg = GUITexture::create(GUIOptions(GUIOption::flexibleWidth()), "TitleBarBackground");
  28. mTitle = GUILabel::create(title);
  29. GUILayout* bgLayout = mTitleBarBgPanel->addNewElement<GUILayoutY>();
  30. GUILayout* bgLayoutX = bgLayout->addNewElement<GUILayoutX>();
  31. bgLayoutX->addElement(mTitleBarBg);
  32. bgLayoutX->addNewElement<GUIFixedSpace>(1);
  33. bgLayout->addNewElement<GUIFlexibleSpace>();
  34. bgLayout->addNewElement<GUIFixedSpace>(1);
  35. GUILayout* contentLayoutY = mTitleBarPanel->addNewElement<GUILayoutY>();
  36. GUILayout* contentLayoutX = contentLayoutY->addNewElement<GUILayoutX>();
  37. contentLayoutX->addNewElement<GUIFlexibleSpace>();
  38. GUILayout* titleLayout = contentLayoutX->addNewElement<GUILayoutY>();
  39. titleLayout->addNewElement<GUIFixedSpace>(2);
  40. titleLayout->addElement(mTitle);
  41. titleLayout->addNewElement<GUIFlexibleSpace>();
  42. contentLayoutX->addNewElement<GUIFlexibleSpace>();
  43. if (hasCloseButton)
  44. {
  45. mCloseButton = GUIButton::create(HString(""), "WinCloseBtn");
  46. contentLayoutX->addElement(mCloseButton);
  47. mCloseButton->onClick.connect(std::bind(&ModalWindow::close, this));
  48. GUIElementOptions options = mCloseButton->getOptionFlags();
  49. options.unset(GUIElementOption::AcceptsKeyFocus);
  50. mCloseButton->setOptionFlags(options);
  51. }
  52. contentLayoutX->addNewElement<GUIFixedSpace>(1);
  53. contentLayoutY->addNewElement<GUIFlexibleSpace>();
  54. contentLayoutY->addNewElement<GUIFixedSpace>(1);
  55. mContents = mGUI->getPanel()->addNewElement<GUIPanel>();
  56. mContents->setDepthRange(0);
  57. mContents->setPosition(1, 1 + getTitleBarHeight());
  58. updateSize();
  59. }
  60. Vector2I ModalWindow::screenToWindowPos(const Vector2I& screenPos) const
  61. {
  62. Vector2I renderWindowPos = getRenderWindow()->screenToWindowPos(screenPos);
  63. Vector2I contentsPos = renderWindowPos;
  64. Rect2I contentArea = getContentArea();
  65. contentsPos.x -= contentArea.x;
  66. contentsPos.y -= contentArea.y;
  67. return contentsPos;
  68. }
  69. Vector2I ModalWindow::windowToScreenPos(const Vector2I& windowPos) const
  70. {
  71. Vector2I contentsPos = windowPos;
  72. Rect2I contentArea = getContentArea();
  73. contentsPos.x += contentArea.x;
  74. contentsPos.y += contentArea.y;
  75. return getRenderWindow()->windowToScreenPos(contentsPos);
  76. }
  77. void ModalWindow::update()
  78. {
  79. }
  80. void ModalWindow::close()
  81. {
  82. EditorWindowManager::instance().destroy(this);
  83. }
  84. void ModalWindow::setTitle(const HString& title)
  85. {
  86. mTitle->setContent(GUIContent(title));
  87. }
  88. void ModalWindow::setSize(UINT32 width, UINT32 height)
  89. {
  90. EditorWindowBase::setSize(width, height);
  91. updateSize();
  92. }
  93. UINT32 ModalWindow::getContentWidth() const
  94. {
  95. return getWidth() - 2;
  96. }
  97. UINT32 ModalWindow::getContentHeight() const
  98. {
  99. return getHeight() - getTitleBarHeight() - 2;
  100. }
  101. void ModalWindow::setContentSize(UINT32 width, UINT32 height)
  102. {
  103. UINT32 actualWidth = width + 2;
  104. UINT32 actualHeight = height + getTitleBarHeight() + 2;
  105. setSize(actualWidth, actualHeight);
  106. }
  107. void ModalWindow::resized()
  108. {
  109. EditorWindowBase::resized();
  110. updateSize();
  111. }
  112. void ModalWindow::updateSize()
  113. {
  114. mContents->setWidth(getWidth() - 2);
  115. mContents->setHeight(getHeight() - 2 - getTitleBarHeight());
  116. UINT32 captionWidth = getWidth() - 2;
  117. if (mCloseButton != nullptr)
  118. captionWidth = mCloseButton->getGlobalBounds().x;
  119. Vector<Rect2I> captionAreas;
  120. captionAreas.push_back(Rect2I(1, 1, captionWidth, getTitleBarHeight()));
  121. Platform::setCaptionNonClientAreas(*mRenderWindow->getCore().get(), captionAreas);
  122. }
  123. Rect2I ModalWindow::getContentArea() const
  124. {
  125. return Rect2I(1, 1 + getTitleBarHeight(), getContentWidth(), getContentHeight());
  126. }
  127. UINT32 ModalWindow::getTitleBarHeight() const
  128. {
  129. return mTitleBarBg->getBounds().height;
  130. }
  131. }