DockBase.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/Skin.h"
  8. #include "Gwen/Controls/DockBase.h"
  9. #include "Gwen/Controls/DockedTabControl.h"
  10. #include "Gwen/Controls/Highlight.h"
  11. #include "Gwen/DragAndDrop.h"
  12. #include "Gwen/Controls/Resizer.h"
  13. using namespace Gwen;
  14. using namespace Gwen::Controls;
  15. GWEN_CONTROL_CONSTRUCTOR( DockBase )
  16. {
  17. SetPadding( Padding( 1, 1, 1, 1 ) );
  18. SetSize( 200, 200 );
  19. m_DockedTabControl = NULL;
  20. m_Left = NULL;
  21. m_Right = NULL;
  22. m_Top = NULL;
  23. m_Bottom = NULL;
  24. m_bDrawHover = false;
  25. }
  26. TabControl* DockBase::GetTabControl()
  27. {
  28. return m_DockedTabControl;
  29. }
  30. void DockBase::SetupChildDock( int iPos )
  31. {
  32. if ( !m_DockedTabControl )
  33. {
  34. m_DockedTabControl = new DockedTabControl( this );
  35. m_DockedTabControl->onLoseTab.Add( this, &DockBase::OnTabRemoved );
  36. m_DockedTabControl->SetTabStripPosition( Pos::Bottom );
  37. m_DockedTabControl->SetShowTitlebar( true );
  38. }
  39. Dock( iPos );
  40. int iSizeDirection = Pos::Left;
  41. if ( iPos == Pos::Left ) iSizeDirection = Pos::Right;
  42. if ( iPos == Pos::Top ) iSizeDirection = Pos::Bottom;
  43. if ( iPos == Pos::Bottom ) iSizeDirection = Pos::Top;
  44. ControlsInternal::Resizer* sizer = new ControlsInternal::Resizer( this );
  45. sizer->Dock( iSizeDirection );
  46. sizer->SetResizeDir( iSizeDirection );
  47. sizer->SetSize( 2, 2 );
  48. sizer->SetTarget( this );
  49. }
  50. void DockBase::Render( Skin::Base* /*skin*/ )
  51. {
  52. //Gwen::Render->SetDrawColor( Colors::Black );
  53. //Gwen::Render->DrawLinedRect( GetRenderBounds() );
  54. }
  55. DockBase** DockBase::GetChildDockPtr( int iPos )
  56. {
  57. if ( iPos == Pos::Left ) return &m_Left;
  58. if ( iPos == Pos::Right ) return &m_Right;
  59. if ( iPos == Pos::Top ) return &m_Top;
  60. if ( iPos == Pos::Bottom ) return &m_Bottom;
  61. return NULL;
  62. }
  63. DockBase* DockBase::GetChildDock( int iPos )
  64. {
  65. DockBase** pDock = GetChildDockPtr( iPos );
  66. if ( !(*pDock) )
  67. {
  68. (*pDock) = new DockBase( this );
  69. (*pDock)->SetupChildDock( iPos );
  70. }
  71. else
  72. {
  73. (*pDock)->SetHidden( false );
  74. }
  75. return *pDock;
  76. }
  77. int DockBase::GetDroppedTabDirection( int x, int y )
  78. {
  79. int w = Width();
  80. int h = Height();
  81. float top = (float)y / (float) h;
  82. float left = (float)x / (float) w;
  83. float right = (float)(w - x) /(float) w;
  84. float bottom = (float)(h - y) / (float) h;
  85. float minimum = GwenUtil_Min( GwenUtil_Min( GwenUtil_Min( top, left ), right ), bottom );
  86. m_bDropFar = ( minimum < 0.2f );
  87. if ( minimum > 0.3 ) return Pos::Fill;
  88. if ( top == minimum && (!m_Top || m_Top->Hidden()) ) return Pos::Top;
  89. if ( left == minimum && (!m_Left || m_Left->Hidden()) ) return Pos::Left;
  90. if ( right == minimum && (!m_Right || m_Right->Hidden()) ) return Pos::Right;
  91. if ( bottom == minimum && (!m_Bottom || m_Bottom->Hidden()) ) return Pos::Bottom;
  92. return Pos::Fill;
  93. }
  94. bool DockBase::DragAndDrop_CanAcceptPackage( Gwen::DragAndDrop::Package* pPackage )
  95. {
  96. // A TAB button dropped
  97. if ( pPackage->name == "TabButtonMove" )
  98. return true;
  99. // a TAB window dropped
  100. if ( pPackage->name == "TabWindowMove" )
  101. return true;
  102. return false;
  103. }
  104. void AddTabToDock( TabButton* pTabButton, DockedTabControl* pControl )
  105. {
  106. pControl->AddPage( pTabButton );
  107. }
  108. bool DockBase::DragAndDrop_HandleDrop( Gwen::DragAndDrop::Package* pPackage, int x, int y )
  109. {
  110. Gwen::Point pPos = CanvasPosToLocal( Gwen::Point( x, y ) );
  111. int dir = GetDroppedTabDirection( pPos.x, pPos.y );
  112. DockedTabControl* pAddTo = m_DockedTabControl;
  113. if ( dir == Pos::Fill && pAddTo == NULL ) return false;
  114. if ( dir != Pos::Fill )
  115. {
  116. DockBase* pDock = GetChildDock( dir );
  117. pAddTo = pDock->m_DockedTabControl;
  118. if ( !m_bDropFar ) pDock->BringToFront();
  119. else pDock->SendToBack();
  120. }
  121. if ( pPackage->name == "TabButtonMove" )
  122. {
  123. TabButton* pTabButton = DragAndDrop::SourceControl->DynamicCastTabButton();
  124. if ( !pTabButton ) return false;
  125. AddTabToDock( pTabButton, pAddTo );
  126. }
  127. if ( pPackage->name == "TabWindowMove" )
  128. {
  129. DockedTabControl* pTabControl = DragAndDrop::SourceControl->DynamicCastDockedTabControl();
  130. if ( !pTabControl ) return false;
  131. if ( pTabControl == pAddTo ) return false;
  132. pTabControl->MoveTabsTo( pAddTo );
  133. }
  134. Invalidate();
  135. return true;
  136. }
  137. bool DockBase::IsEmpty()
  138. {
  139. if ( m_DockedTabControl && m_DockedTabControl->TabCount() > 0 ) return false;
  140. if ( m_Left && !m_Left->IsEmpty() ) return false;
  141. if ( m_Right && !m_Right->IsEmpty() ) return false;
  142. if ( m_Top && !m_Top->IsEmpty() ) return false;
  143. if ( m_Bottom && !m_Bottom->IsEmpty() ) return false;
  144. return true;
  145. }
  146. void DockBase::OnTabRemoved( Gwen::Controls::Base* /*pControl*/ )
  147. {
  148. DoRedundancyCheck();
  149. DoConsolidateCheck();
  150. }
  151. void DockBase::DoRedundancyCheck()
  152. {
  153. if ( !IsEmpty() ) return;
  154. DockBase* pDockParent = GetParent()->DynamicCastDockBase();
  155. if ( !pDockParent ) return;
  156. pDockParent->OnRedundantChildDock( this );
  157. }
  158. void DockBase::DoConsolidateCheck()
  159. {
  160. if ( IsEmpty() ) return;
  161. if ( !m_DockedTabControl ) return;
  162. if ( m_DockedTabControl->TabCount() > 0 ) return;
  163. if ( m_Bottom && !m_Bottom->IsEmpty() )
  164. {
  165. m_Bottom->m_DockedTabControl->MoveTabsTo( m_DockedTabControl );
  166. return;
  167. }
  168. if ( m_Top && !m_Top->IsEmpty() )
  169. {
  170. m_Top->m_DockedTabControl->MoveTabsTo( m_DockedTabControl );
  171. return;
  172. }
  173. if ( m_Left && !m_Left->IsEmpty() )
  174. {
  175. m_Left->m_DockedTabControl->MoveTabsTo( m_DockedTabControl );
  176. return;
  177. }
  178. if ( m_Right && !m_Right->IsEmpty() )
  179. {
  180. m_Right->m_DockedTabControl->MoveTabsTo( m_DockedTabControl );
  181. return;
  182. }
  183. }
  184. void DockBase::OnRedundantChildDock( DockBase* pDockBase )
  185. {
  186. pDockBase->SetHidden( true );
  187. DoRedundancyCheck();
  188. DoConsolidateCheck();
  189. }
  190. void DockBase::DragAndDrop_HoverEnter( Gwen::DragAndDrop::Package* /*pPackage*/, int /*x*/, int /*y*/ )
  191. {
  192. m_bDrawHover = true;
  193. }
  194. void DockBase::DragAndDrop_HoverLeave( Gwen::DragAndDrop::Package* /*pPackage*/ )
  195. {
  196. m_bDrawHover = false;
  197. }
  198. void DockBase::DragAndDrop_Hover( Gwen::DragAndDrop::Package* /*pPackage*/, int x, int y )
  199. {
  200. Gwen::Point pPos = CanvasPosToLocal( Gwen::Point( x, y ) );
  201. int dir = GetDroppedTabDirection( pPos.x, pPos.y );
  202. if ( dir == Pos::Fill )
  203. {
  204. if ( !m_DockedTabControl )
  205. {
  206. m_HoverRect = Gwen::Rect( 0, 0, 0, 0 );
  207. return;
  208. }
  209. m_HoverRect = GetInnerBounds();
  210. return;
  211. }
  212. m_HoverRect = GetRenderBounds();
  213. int HelpBarWidth = 0;
  214. if ( dir == Pos::Left )
  215. {
  216. HelpBarWidth = m_HoverRect.w * 0.25f;
  217. m_HoverRect.w = HelpBarWidth;
  218. }
  219. if ( dir == Pos::Right )
  220. {
  221. HelpBarWidth = m_HoverRect.w * 0.25f;
  222. m_HoverRect.x = m_HoverRect.w - HelpBarWidth;
  223. m_HoverRect.w = HelpBarWidth;
  224. }
  225. if ( dir == Pos::Top )
  226. {
  227. HelpBarWidth = m_HoverRect.h * 0.25f;
  228. m_HoverRect.h = HelpBarWidth;
  229. }
  230. if ( dir == Pos::Bottom )
  231. {
  232. HelpBarWidth = m_HoverRect.h * 0.25f;
  233. m_HoverRect.y = m_HoverRect.h - HelpBarWidth;
  234. m_HoverRect.h = HelpBarWidth;
  235. }
  236. if ( (dir == Pos::Top || dir == Pos::Bottom ) && !m_bDropFar )
  237. {
  238. if ( m_Left && m_Left->Visible() )
  239. {
  240. m_HoverRect.x += m_Left->Width();
  241. m_HoverRect.w -= m_Left->Width();
  242. }
  243. if ( m_Right && m_Right->Visible() )
  244. {
  245. m_HoverRect.w -= m_Right->Width();
  246. }
  247. }
  248. if ( (dir == Pos::Left || dir == Pos::Right ) && !m_bDropFar )
  249. {
  250. if ( m_Top && m_Top->Visible() )
  251. {
  252. m_HoverRect.y += m_Top->Height();
  253. m_HoverRect.h -= m_Top->Height();
  254. }
  255. if ( m_Bottom && m_Bottom->Visible() )
  256. {
  257. m_HoverRect.h -= m_Bottom->Height();
  258. }
  259. }
  260. }
  261. void DockBase::RenderOver( Skin::Base* skin )
  262. {
  263. if ( !m_bDrawHover ) return;
  264. Gwen::Renderer::Base* render = skin->GetRender();
  265. render->SetDrawColor( Gwen::Color( 255, 100, 255, 20 ) );
  266. render->DrawFilledRect( GetRenderBounds() );
  267. if ( m_HoverRect.w == 0 ) return;
  268. render->SetDrawColor( Gwen::Color( 255, 100, 255, 100 ) );
  269. render->DrawFilledRect( m_HoverRect );
  270. render->SetDrawColor( Gwen::Color( 255, 100, 255, 200 ) );
  271. render->DrawLinedRect( m_HoverRect );
  272. }