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.

343 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "tf_layeredmappanel.h"
  9. using namespace vgui;
  10. //=========================================================
  11. // CLayeredMapToolTip
  12. //=========================================================
  13. CLayeredMapToolTip::CLayeredMapToolTip(vgui::Panel *parent, const char *text ) : vgui::BaseTooltip( parent, text )
  14. {
  15. m_hCurrentPanel = NULL;
  16. }
  17. //=========================================================
  18. void CLayeredMapToolTip::PerformLayout()
  19. {
  20. BaseClass::PerformLayout();
  21. if ( !ShouldLayout() )
  22. return;
  23. CTFLayeredMapItemPanel *pMapItemPanel = m_hCurrentPanel.Get();
  24. if ( !pMapItemPanel )
  25. return;
  26. m_pControlledPanel->SetVisible( false );
  27. int x,y;
  28. pMapItemPanel->GetPos( x, y );
  29. int iXPos = 0;
  30. int iYPos = 0;
  31. // Loop through the positions in our strategy, and hope we find a valid spot
  32. for ( int i = 0; i < NUM_POSITIONS_PER_STRATEGY; i++ )
  33. {
  34. itempanel_tooltippos_t iPos = g_iTooltipStrategies[IPTTP_TOP_SIDE][i];
  35. GetPosition( iPos, pMapItemPanel, x, y, &iXPos, &iYPos );
  36. if ( ValidatePosition( pMapItemPanel, x, y, &iXPos, &iYPos ) )
  37. break;
  38. }
  39. m_pControlledPanel->SetPos( iXPos, iYPos );
  40. m_pControlledPanel->SetVisible( true );
  41. }
  42. //=========================================================
  43. void CLayeredMapToolTip::ShowTooltip( vgui::Panel *currentPanel )
  44. {
  45. // Set Data on visible panel
  46. if ( !m_pControlledPanel )
  47. return;
  48. if ( currentPanel != m_hCurrentPanel.Get() )
  49. {
  50. CTFLayeredMapItemPanel *pMapItemPanel = dynamic_cast<CTFLayeredMapItemPanel*>( currentPanel );
  51. m_hCurrentPanel.Set( pMapItemPanel );
  52. m_pControlledPanel->SetVisible( false );
  53. if ( pMapItemPanel )
  54. {
  55. KeyValues *pData = pMapItemPanel->GetItemKvData();
  56. if ( pData )
  57. {
  58. m_pControlledPanel->SetDialogVariable( "tooltipdescription", pData->GetString( "name", "No Name" ) );
  59. }
  60. }
  61. }
  62. BaseClass::ShowTooltip( currentPanel );
  63. }
  64. //=========================================================
  65. void CLayeredMapToolTip::HideTooltip()
  66. {
  67. if ( !m_pControlledPanel )
  68. return;
  69. m_pControlledPanel->SetVisible( false );
  70. m_hCurrentPanel = NULL;
  71. }
  72. //=========================================================
  73. void CLayeredMapToolTip::SetupPanels( CTFLayeredMapPanel *pParentPanel, vgui::EditablePanel *pControlledPanel )
  74. {
  75. m_pParentPanel = pParentPanel;
  76. m_pControlledPanel = pControlledPanel;
  77. }
  78. //=========================================================
  79. void CLayeredMapToolTip::GetPosition( itempanel_tooltippos_t iTooltipPosition, CTFLayeredMapItemPanel *pItemPanel, int iItemX, int iItemY, int *iXPos, int *iYPos )
  80. {
  81. switch ( iTooltipPosition )
  82. {
  83. case IPTTP_LEFT:
  84. *iXPos = (iItemX - m_pControlledPanel->GetWide() + XRES(18));
  85. *iYPos = iItemY - YRES(7);
  86. break;
  87. case IPTTP_RIGHT:
  88. *iXPos = (iItemX + pItemPanel->GetWide() - XRES(20));
  89. *iYPos = iItemY - YRES(7);
  90. break;
  91. case IPTTP_LEFT_CENTERED:
  92. *iXPos = (iItemX - m_pControlledPanel->GetWide()) - XRES(4);
  93. *iYPos = (iItemY - (m_pControlledPanel->GetTall() * 0.5));
  94. break;
  95. case IPTTP_RIGHT_CENTERED:
  96. *iXPos = (iItemX + pItemPanel->GetWide()) + XRES(4);
  97. *iYPos = (iItemY - (m_pControlledPanel->GetTall() * 0.5));
  98. break;
  99. case IPTTP_ABOVE:
  100. *iXPos = (iItemX + (pItemPanel->GetWide() * 0.5)) - (m_pControlledPanel->GetWide() * 0.5);
  101. *iYPos = (iItemY - m_pControlledPanel->GetTall() - YRES(4));
  102. break;
  103. case IPTTP_BELOW:
  104. *iXPos = (iItemX + (pItemPanel->GetWide() * 0.5)) - (m_pControlledPanel->GetWide() * 0.5);
  105. *iYPos = (iItemY + pItemPanel->GetTall() + YRES(4));
  106. break;
  107. }
  108. }
  109. //=========================================================
  110. bool CLayeredMapToolTip::ValidatePosition( CTFLayeredMapItemPanel *pItemPanel, int iItemX, int iItemY, int *iXPos, int *iYPos )
  111. {
  112. bool bSucceeded = true;
  113. // Make sure the popup stays onscreen.
  114. if ( *iXPos < 0 )
  115. {
  116. *iXPos = 0;
  117. }
  118. else if ( (*iXPos + m_pControlledPanel->GetWide()) > m_pParentPanel->GetWide() )
  119. {
  120. int iXPosNew = m_pParentPanel->GetWide() - m_pControlledPanel->GetWide();
  121. // make sure it is still on the screen
  122. if ( iXPosNew >= 0 )
  123. {
  124. *iXPos = iXPosNew;
  125. }
  126. else
  127. {
  128. bSucceeded = false;
  129. }
  130. }
  131. if ( *iYPos < 0 )
  132. {
  133. *iYPos = 0;
  134. }
  135. else if ( (*iYPos + m_pControlledPanel->GetTall() + YRES(32)) > m_pParentPanel->GetTall() )
  136. {
  137. // Move it up above our item
  138. int iYPosNew = iItemY - m_pControlledPanel->GetTall() - YRES(4);
  139. // make sure it is still on the screen
  140. if ( iYPosNew >= 0 )
  141. {
  142. *iYPos = iYPosNew;
  143. }
  144. else
  145. {
  146. bSucceeded = false;
  147. }
  148. }
  149. if ( bSucceeded )
  150. {
  151. // We also fail if moving it to keep it on screen moved it over the item panel itself
  152. Vector2D vecToolTipMin, vecToolTipMax, vecItemMin, vecItemMax;
  153. vecToolTipMin.x = *iXPos;
  154. vecToolTipMin.y = *iYPos;
  155. vecToolTipMax.x = vecToolTipMin.x + m_pControlledPanel->GetWide();
  156. vecToolTipMax.y = vecToolTipMin.y + m_pControlledPanel->GetTall();
  157. vecItemMin.x = iItemX;
  158. vecItemMin.y = iItemY;
  159. vecItemMax.x = vecItemMin.x + m_hCurrentPanel->GetWide();
  160. vecItemMax.y = vecItemMin.y + m_hCurrentPanel->GetTall();
  161. bSucceeded = !( vecToolTipMin.x < vecItemMax.x && vecToolTipMax.x > vecItemMin.x && vecToolTipMin.y < vecItemMax.y && vecToolTipMax.y > vecItemMin.y );
  162. }
  163. return bSucceeded;
  164. }
  165. //-----------------------------------------------------------------------------
  166. // CTFLayeredImagePanelItem
  167. //-----------------------------------------------------------------------------
  168. DECLARE_BUILD_FACTORY( CTFLayeredMapItemPanel );
  169. CTFLayeredMapItemPanel::CTFLayeredMapItemPanel( Panel *parent, const char *pName ) : vgui::EditablePanel( parent, pName )
  170. {
  171. m_bIsCompleted = false;
  172. m_bIsMouseOvered = false;
  173. m_kvData = NULL;
  174. }
  175. //-----------------------------------------------------------------------------
  176. void CTFLayeredMapItemPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  177. {
  178. BaseClass::ApplySchemeSettings( pScheme );
  179. // load control settings...
  180. LoadControlSettings( "resource/UI/LayeredMapPanelItem.res" );
  181. // get References to the 4 images
  182. m_pIsCompleted = dynamic_cast<vgui::ScalableImagePanel*>( FindChildByName("IsCompletedImage") );
  183. m_pIsCompletedHighlight = dynamic_cast<vgui::ScalableImagePanel*>( FindChildByName("IsCompletedHighlight") );
  184. m_pNotCompleted = dynamic_cast<vgui::ScalableImagePanel*>( FindChildByName("NotCompletedImage") );
  185. m_pNotCompletedHighlight = dynamic_cast<vgui::ScalableImagePanel*>( FindChildByName("NotCompletedHighlight") );
  186. m_pIsCompleted->SetVisible( true );
  187. m_pIsCompleted->SetMouseInputEnabled( false );
  188. m_pIsCompletedHighlight->SetVisible( false );
  189. m_pIsCompletedHighlight->SetMouseInputEnabled( false );
  190. m_pNotCompleted->SetVisible( false );
  191. m_pNotCompleted->SetMouseInputEnabled( false );
  192. m_pNotCompletedHighlight->SetVisible( false );
  193. m_pNotCompletedHighlight->SetMouseInputEnabled( false );
  194. SetMouseInputEnabled( true );
  195. }
  196. //-----------------------------------------------------------------------------
  197. void CTFLayeredMapItemPanel::ApplySettings( KeyValues *inResourceData )
  198. {
  199. BaseClass::ApplySettings( inResourceData );
  200. // Store Information needed for a mouse over
  201. KeyValues *kvData = inResourceData->FindKey( "mapitem_kv" );
  202. if ( kvData )
  203. {
  204. if ( m_kvData )
  205. {
  206. m_kvData->deleteThis();
  207. }
  208. m_kvData = new KeyValues( "mapitem_kv" );
  209. kvData->CopySubkeys( m_kvData );
  210. }
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose:
  214. //-----------------------------------------------------------------------------
  215. void CTFLayeredMapItemPanel::OnCursorEntered( void )
  216. {
  217. BaseClass::OnCursorEntered();
  218. m_pIsCompletedHighlight->SetVisible( m_bIsCompleted );
  219. m_pNotCompletedHighlight->SetVisible( !m_bIsCompleted );
  220. }
  221. //-----------------------------------------------------------------------------
  222. // Purpose:
  223. //-----------------------------------------------------------------------------
  224. void CTFLayeredMapItemPanel::OnCursorExited( void )
  225. {
  226. BaseClass::OnCursorExited();
  227. m_pIsCompletedHighlight->SetVisible( false );
  228. m_pNotCompletedHighlight->SetVisible( false );
  229. }
  230. //-----------------------------------------------------------------------------
  231. // Purpose:
  232. //-----------------------------------------------------------------------------
  233. void CTFLayeredMapItemPanel::OnMousePressed(vgui::MouseCode code)
  234. {
  235. SetCompletionState( !m_bIsCompleted );
  236. }
  237. //-----------------------------------------------------------------------------
  238. void CTFLayeredMapItemPanel::SetCompletionState( bool bIsCompleted )
  239. {
  240. m_bIsCompleted = bIsCompleted;
  241. m_pIsCompleted->SetVisible( m_bIsCompleted );
  242. m_pNotCompleted->SetVisible( !m_bIsCompleted );
  243. }
  244. //-----------------------------------------------------------------------------
  245. // CTFLayeredMapPanel
  246. // - MultiImage Panel that places all images on top of each other.
  247. // An external manager may then individually toggle each image on or off
  248. // Each image is sized to fit the panel
  249. //-----------------------------------------------------------------------------
  250. DECLARE_BUILD_FACTORY( CTFLayeredMapPanel );
  251. CTFLayeredMapPanel::CTFLayeredMapPanel( Panel *parent, const char *pName ) : vgui::EditablePanel( parent, pName )
  252. {
  253. m_pLayeredMapKv = NULL;
  254. }
  255. //-----------------------------------------------------------------------------
  256. void CTFLayeredMapPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  257. {
  258. BaseClass::ApplySchemeSettings( pScheme );
  259. // load control settings...
  260. LoadControlSettings( "resource/UI/LayeredMapPanel.res" );
  261. m_MapItems.PurgeAndDeleteElements();
  262. // Get ToolTip
  263. m_pToolTipPanel = dynamic_cast<EditablePanel*>( FindChildByName( "ToolTipPanel" ) );
  264. m_pToolTip = new CLayeredMapToolTip( this );
  265. m_pToolTip->SetupPanels( this, m_pToolTipPanel );
  266. if ( m_pLayeredMapKv )
  267. {
  268. int iItemCount = m_pLayeredMapKv->GetInt( "item_count", 0 );
  269. for ( int i = 0; i < iItemCount; ++i )
  270. {
  271. CTFLayeredMapItemPanel *pItem = dynamic_cast<CTFLayeredMapItemPanel*>( FindChildByName( VarArgs( "MapItem%d", i ) ) );
  272. if ( pItem )
  273. {
  274. pItem->SetTooltip( m_pToolTip, "" );
  275. m_MapItems.AddToTail( pItem );
  276. }
  277. }
  278. }
  279. }
  280. //-----------------------------------------------------------------------------
  281. void CTFLayeredMapPanel::ApplySettings( KeyValues *inResourceData )
  282. {
  283. BaseClass::ApplySettings( inResourceData );
  284. if ( inResourceData )
  285. {
  286. if ( m_pLayeredMapKv )
  287. {
  288. m_pLayeredMapKv->deleteThis();
  289. }
  290. m_pLayeredMapKv = new KeyValues( "layeredMapPanel_kv" );
  291. inResourceData->CopySubkeys( m_pLayeredMapKv );
  292. }
  293. }