Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
5.9 KiB

  1. //--------------------------------------------------------------------------------------------------------
  2. //========= Copyright Valve Corporation, All rights reserved. ============//
  3. #include "cbase.h"
  4. #include "SelectionTool.h"
  5. #include "nav_mesh.h"
  6. #include "nav_pathfind.h"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include "tier0/memdbgon.h"
  9. #ifdef SERVER_USES_VGUI
  10. using namespace vgui;
  11. //--------------------------------------------------------------------------------------------------------
  12. SelectionToolPanel::SelectionToolPanel( vgui::Panel *parent, const char *toolName ) : CNavUIToolPanel( parent, toolName )
  13. {
  14. LoadControlSettings( "Resource/UI/NavTools/SelectionTool.res" );
  15. }
  16. //--------------------------------------------------------------------------------------------------------
  17. void SelectionToolPanel::Init( void )
  18. {
  19. m_dragType = DRAG_NONE;
  20. }
  21. //--------------------------------------------------------------------------------------------------------
  22. void SelectionToolPanel::Shutdown( void )
  23. {
  24. }
  25. //--------------------------------------------------------------------------------------------------------
  26. void SelectionToolPanel::PerformLayout( void )
  27. {
  28. Panel *parent = GetParent();
  29. if ( parent )
  30. {
  31. int w, h;
  32. parent->GetSize( w, h );
  33. SetBounds( 0, 0, w, h );
  34. }
  35. BaseClass::PerformLayout();
  36. }
  37. //--------------------------------------------------------------------------------------------------------
  38. void SelectionToolPanel::OnCommand( const char *command )
  39. {
  40. if ( FStrEq( "FloodSelect", command ) )
  41. {
  42. TheNavUI()->SetLeftClickAction( "Selection::Flood", "Flood Select" );
  43. }
  44. BaseClass::OnCommand( command );
  45. }
  46. //--------------------------------------------------------------------------------------------------------
  47. void SelectionToolPanel::OnCursorMoved( int x, int y )
  48. {
  49. CNavArea *area = TheNavMesh->GetSelectedArea();
  50. if ( area )
  51. {
  52. bool selected = TheNavMesh->IsInSelectedSet( area );
  53. if ( selected && m_dragType == DRAG_UNSELECT )
  54. {
  55. TheNavMesh->RemoveFromSelectedSet( area );
  56. TheNavUI()->PlaySound( "EDIT_END_AREA.Creating" );
  57. }
  58. else if ( !selected && m_dragType == DRAG_SELECT )
  59. {
  60. TheNavMesh->AddToSelectedSet( area );
  61. TheNavUI()->PlaySound( "EDIT_END_AREA.Creating" );
  62. }
  63. }
  64. BaseClass::OnCursorMoved( x, y );
  65. }
  66. //--------------------------------------------------------------------------------------------------------
  67. class FloodSelectionCollector
  68. {
  69. public:
  70. FloodSelectionCollector( SelectionToolPanel *panel )
  71. {
  72. m_count = 0;
  73. m_panel = panel;
  74. }
  75. bool operator() ( CNavArea *area )
  76. {
  77. // already selected areas terminate flood select
  78. if ( TheNavMesh->IsInSelectedSet( area ) )
  79. return false;
  80. if ( !m_panel->IsFloodSelectable( area ) )
  81. return false;
  82. TheNavMesh->AddToSelectedSet( area );
  83. ++m_count;
  84. return true;
  85. }
  86. int m_count;
  87. private:
  88. SelectionToolPanel *m_panel;
  89. };
  90. //--------------------------------------------------------------------------------------------------------
  91. bool SelectionToolPanel::IsFloodSelectable( CNavArea *area )
  92. {
  93. if ( IsCheckButtonChecked( "Place" ) )
  94. {
  95. if ( m_floodStartArea->GetPlace() != area->GetPlace() )
  96. {
  97. return false;
  98. }
  99. }
  100. if ( IsCheckButtonChecked( "Jump" ) )
  101. {
  102. if ( (m_floodStartArea->GetAttributes() & NAV_MESH_JUMP) != (area->GetAttributes() & NAV_MESH_JUMP) )
  103. {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. //--------------------------------------------------------------------------------------------------------
  110. void SelectionToolPanel::FloodSelect( void )
  111. {
  112. m_floodStartArea = TheNavMesh->GetSelectedArea();
  113. if ( m_floodStartArea )
  114. {
  115. TheNavUI()->PlaySound( "EDIT_DELETE" );
  116. int connections = INCLUDE_BLOCKED_AREAS;
  117. if ( IsCheckButtonChecked( "Incoming" ) )
  118. {
  119. connections = connections | INCLUDE_INCOMING_CONNECTIONS;
  120. }
  121. if ( !IsCheckButtonChecked( "Outgoing" ) )
  122. {
  123. connections = connections | EXCLUDE_OUTGOING_CONNECTIONS;
  124. }
  125. // collect all areas connected to this area
  126. FloodSelectionCollector collector( this );
  127. SearchSurroundingAreas( m_floodStartArea, m_floodStartArea->GetCenter(), collector, -1, connections );
  128. Msg( "Selected %d areas.\n", collector.m_count );
  129. }
  130. m_floodStartArea = NULL;
  131. TheNavMesh->SetMarkedArea( NULL ); // unmark the mark area
  132. }
  133. //--------------------------------------------------------------------------------------------------------
  134. void SelectionToolPanel::StartLeftClickAction( const char *actionName )
  135. {
  136. if ( FStrEq( actionName, "Selection::Flood" ) )
  137. {
  138. TheNavUI()->SetLeftClickAction( "", "" );
  139. FloodSelect();
  140. }
  141. else if ( FStrEq( actionName, "Selection::Select" ) )
  142. {
  143. CNavArea *area = TheNavMesh->GetSelectedArea();
  144. if ( area )
  145. {
  146. if ( TheNavMesh->IsInSelectedSet( area ) )
  147. {
  148. TheNavMesh->RemoveFromSelectedSet( area );
  149. m_dragType = DRAG_UNSELECT;
  150. }
  151. else
  152. {
  153. TheNavMesh->AddToSelectedSet( area );
  154. m_dragType = DRAG_SELECT;
  155. }
  156. TheNavUI()->PlaySound( "EDIT_END_AREA.Creating" );
  157. }
  158. }
  159. }
  160. //--------------------------------------------------------------------------------------------------------
  161. void SelectionToolPanel::FinishLeftClickAction( const char *actionName )
  162. {
  163. m_dragType = DRAG_NONE;
  164. }
  165. //--------------------------------------------------------------------------------------------------------
  166. void SelectionToolPanel::StartRightClickAction( const char *actionName )
  167. {
  168. if ( m_dragType != DRAG_NONE )
  169. {
  170. TheNavUI()->PlaySound( "EDIT_END_AREA.Creating" );
  171. m_dragType = DRAG_NONE;
  172. return;
  173. }
  174. if ( FStrEq( actionName, "Selection::ClearSelection" ) )
  175. {
  176. if ( FStrEq( TheNavUI()->GetLeftClickAction(), "Selection::Select" ) )
  177. {
  178. if ( TheNavMesh->GetSelecteSetSize() > 0 )
  179. {
  180. TheNavUI()->PlaySound( "EDIT_END_AREA.Creating" );
  181. }
  182. TheNavMesh->ClearSelectedSet();
  183. }
  184. else
  185. {
  186. TheNavUI()->SetLeftClickAction( "", "" );
  187. }
  188. }
  189. }
  190. #endif // SERVER_USES_VGUI
  191. //--------------------------------------------------------------------------------------------------------