BsEditorWidgetContainer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "BsEditorWidgetContainer.h"
  2. #include "BsGUITabbedTitleBar.h"
  3. #include "BsEditorWidget.h"
  4. using namespace CamelotFramework;
  5. using namespace BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. const CM::UINT32 EditorWidgetContainer::TitleBarHeight = 13;
  9. EditorWidgetContainer::EditorWidgetContainer(BS::GUIWidget* parent)
  10. :mParent(parent), mX(0), mY(0), mWidth(0), mHeight(0), mTitleBar(nullptr), mActiveWidget(-1)
  11. {
  12. mTitleBar = cm_new<GUITabbedTitleBar>(parent);
  13. mTitleBar->onTabActivated.connect(boost::bind(&EditorWidgetContainer::tabActivated, this, _1));
  14. mTitleBar->onTabClosed.connect(boost::bind(&EditorWidgetContainer::tabClosed, this, _1));
  15. }
  16. EditorWidgetContainer::~EditorWidgetContainer()
  17. {
  18. cm_delete(mTitleBar);
  19. for(auto& widget : mWidgets)
  20. {
  21. EditorWidget::destroy(widget);
  22. }
  23. }
  24. void EditorWidgetContainer::add(EditorWidget& widget)
  25. {
  26. for(auto& curWidget : mWidgets)
  27. {
  28. if(curWidget == &widget)
  29. return;
  30. }
  31. mTitleBar->addTab(widget.getName());
  32. mWidgets.push_back(&widget);
  33. widget._changeParent(this);
  34. if(mActiveWidget == -1)
  35. setActiveWidget((UINT32)mWidgets.size() - 1);
  36. else
  37. widget._disable();
  38. }
  39. void EditorWidgetContainer::remove(EditorWidget& widget)
  40. {
  41. INT32 widgetIdx = -1;
  42. UINT32 curIdx = 0;
  43. for(auto& curWidget : mWidgets)
  44. {
  45. if(curWidget == &widget)
  46. {
  47. widgetIdx = curIdx;
  48. break;
  49. }
  50. curIdx++;
  51. }
  52. if(widgetIdx == -1)
  53. return;
  54. mWidgets.erase(mWidgets.begin() + widgetIdx);
  55. mTitleBar->removeTab((UINT32)widgetIdx);
  56. if(widgetIdx == mActiveWidget)
  57. {
  58. if(mWidgets.size() > 0)
  59. {
  60. setActiveWidget(0);
  61. }
  62. }
  63. }
  64. void EditorWidgetContainer::move(EditorWidget& widget, UINT32 newPosition)
  65. {
  66. // TODO
  67. }
  68. void EditorWidgetContainer::setSize(UINT32 width, UINT32 height)
  69. {
  70. // TODO - Title bar is always TitleBarHeight size, so what happens when the container area is smaller than that?
  71. mTitleBar->setSize(width, TitleBarHeight);
  72. if(mActiveWidget >= 0)
  73. {
  74. EditorWidget* activeWidgetPtr = mWidgets[mActiveWidget];
  75. UINT32 contentHeight = (UINT32)std::max(0, (INT32)height - (INT32)TitleBarHeight);
  76. activeWidgetPtr->_setSize(width, contentHeight);
  77. }
  78. mWidth = width;
  79. mHeight = height;
  80. }
  81. void EditorWidgetContainer::setPosition(INT32 x, INT32 y)
  82. {
  83. mTitleBar->setPosition(x, y);
  84. if(mActiveWidget >= 0)
  85. {
  86. EditorWidget* activeWidgetPtr = mWidgets[mActiveWidget];
  87. activeWidgetPtr->_setPosition(x, y + TitleBarHeight);
  88. }
  89. mX = x;
  90. mY = y;
  91. }
  92. void EditorWidgetContainer::setActiveWidget(UINT32 idx)
  93. {
  94. if(mActiveWidget == idx)
  95. return;
  96. mActiveWidget = idx;
  97. UINT32 curIdx = 0;
  98. for(auto& curWidget : mWidgets)
  99. {
  100. if(curIdx != (UINT32)mActiveWidget)
  101. curWidget->_disable();
  102. else
  103. curWidget->_enable();
  104. curIdx++;
  105. }
  106. setPosition(mX, mY);
  107. setSize(mWidth, mHeight);
  108. }
  109. void EditorWidgetContainer::tabActivated(UINT32 idx)
  110. {
  111. setActiveWidget(idx);
  112. }
  113. void EditorWidgetContainer::tabClosed(UINT32 idx)
  114. {
  115. EditorWidget* widget = mWidgets[idx];
  116. remove(*widget);
  117. EditorWidget::destroy(widget);
  118. if(!onWidgetClosed.empty())
  119. onWidgetClosed();
  120. }
  121. void EditorWidgetContainer::_notifyWidgetDestroyed(EditorWidget* widget)
  122. {
  123. for(auto& curWidget : mWidgets)
  124. {
  125. if(curWidget == widget)
  126. {
  127. remove(*widget);
  128. if(!onWidgetClosed.empty())
  129. onWidgetClosed();
  130. return;
  131. }
  132. }
  133. }
  134. }