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.

236 lines
6.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is a panel which is rendered on top of an entity
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "vgui_EntityPanel.h"
  10. #include "ienginevgui.h"
  11. #include "c_BaseTFPlayer.h"
  12. #include "clientmode_commander.h"
  13. #include "hud_commander_statuspanel.h"
  14. #include <KeyValues.h>
  15. #include "commanderoverlaypanel.h"
  16. #include <vgui/IVGui.h>
  17. #include "cdll_util.h"
  18. #include "view.h"
  19. //-----------------------------------------------------------------------------
  20. // constructor
  21. //-----------------------------------------------------------------------------
  22. CEntityPanel::CEntityPanel( vgui::Panel *pParent, const char *panelName )
  23. : BaseClass( pParent, panelName )
  24. {
  25. SetPaintBackgroundEnabled( false );
  26. m_pBaseEntity = NULL;
  27. // FIXME: ComputeParent is yucky... can we be rid of it?
  28. if (!pParent)
  29. {
  30. ComputeParent();
  31. }
  32. m_szMouseOverText[ 0 ] = 0;
  33. // Send mouse inputs (but not cursorenter/exit for now) up to parent
  34. SetReflectMouse( true );
  35. m_bShowInNormal = false;
  36. m_flScale = 0;
  37. m_OffsetX = 0;
  38. m_OffsetY = 0;
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Attach to a new entity
  42. //-----------------------------------------------------------------------------
  43. void CEntityPanel::SetEntity( C_BaseEntity* pEntity )
  44. {
  45. m_pBaseEntity = pEntity;
  46. // Recompute position
  47. OnTick();
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose:
  51. //-----------------------------------------------------------------------------
  52. void CEntityPanel::ComputeParent( void )
  53. {
  54. vgui::VPANEL parent = NULL;
  55. if ( IsLocalPlayerInTactical() || !m_bShowInNormal )
  56. {
  57. CClientModeCommander *commander = ( CClientModeCommander * )ClientModeCommander();
  58. Assert( commander );
  59. parent = commander->GetCommanderOverlayPanel()->GetVPanel();
  60. }
  61. else
  62. {
  63. parent = enginevgui->GetPanel( PANEL_CLIENTDLL );
  64. }
  65. if ( !GetParent() || ( GetParent()->GetVPanel() != parent ) )
  66. {
  67. SetParent( parent );
  68. }
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose: Compute the size of the panel based upon the commander's zoom level
  72. //-----------------------------------------------------------------------------
  73. void CEntityPanel::ComputeAndSetSize( void )
  74. {
  75. m_flScale = 1.0;
  76. // Scale the image
  77. // Use different scales in tactical / normal
  78. if ( IsLocalPlayerInTactical() )
  79. {
  80. CClientModeCommander *commander = ( CClientModeCommander * )ClientModeCommander();
  81. Assert( commander );
  82. float flZoom = commander->GetCommanderOverlayPanel()->GetZoom();
  83. // Scale our size
  84. m_flScale = 0.75 + (0.25 * (1.0 - flZoom)); // 1/2 size at max zoomed out, full size by half zoomed in
  85. }
  86. else if ( m_pBaseEntity )
  87. {
  88. // Get distance to entity
  89. float flDistance = (m_pBaseEntity->GetRenderOrigin() - MainViewOrigin()).Length();
  90. flDistance *= 2;
  91. m_flScale = 0.25 + MAX( 0, 2.0 - (flDistance / 2048) );
  92. }
  93. // Update the size
  94. int w = m_iOrgWidth * m_flScale;
  95. int h = m_iOrgHeight * m_flScale;
  96. SetSize( w,h );
  97. // Update the offsets too
  98. m_OffsetX = m_iOrgOffsetX * m_flScale;
  99. m_OffsetY = m_iOrgOffsetY * m_flScale;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // initialization
  103. //-----------------------------------------------------------------------------
  104. bool CEntityPanel::Init( ::KeyValues* pInitData, C_BaseEntity* pEntity )
  105. {
  106. Assert( pInitData && pEntity );
  107. m_pBaseEntity = pEntity;
  108. if ( pInitData->GetInt( "showinnormalmode", 0 ) )
  109. {
  110. m_bShowInNormal = true;
  111. }
  112. // get the size...
  113. int w, h;
  114. if (!ParseCoord( pInitData, "offset", m_OffsetX, m_OffsetY ))
  115. return false;
  116. if (!ParseCoord( pInitData, "size", w, h ))
  117. return false;
  118. const char *mouseover = pInitData->GetString( "mousehint", "" );
  119. if ( mouseover && mouseover[ 0 ] )
  120. {
  121. Q_strncpy( m_szMouseOverText, mouseover, sizeof( m_szMouseOverText ) );
  122. }
  123. SetSize( w, h );
  124. m_iOrgWidth = w;
  125. m_iOrgHeight = h;
  126. m_iOrgOffsetX = m_OffsetX;
  127. m_iOrgOffsetY = m_OffsetY;
  128. // we need updating
  129. vgui::ivgui()->AddTickSignal( GetVPanel() );
  130. return true;
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose: Determine where our entity is in screen space.
  134. //-----------------------------------------------------------------------------
  135. void CEntityPanel::GetEntityPosition( int& sx, int& sy )
  136. {
  137. if (!m_pBaseEntity)
  138. {
  139. sx = sy = -1.0f;
  140. return;
  141. }
  142. GetTargetInScreenSpace( m_pBaseEntity, sx, sy );
  143. }
  144. //-----------------------------------------------------------------------------
  145. // Should we draw?.
  146. //-----------------------------------------------------------------------------
  147. bool CEntityPanel::ShouldDraw()
  148. {
  149. return ( ( IsLocalPlayerInTactical() && ClientModeCommander()->ShouldDrawEntity( m_pBaseEntity ) ) ||
  150. ( !IsLocalPlayerInTactical() && m_bShowInNormal) );
  151. }
  152. //-----------------------------------------------------------------------------
  153. // called when we're ticked...
  154. //-----------------------------------------------------------------------------
  155. void CEntityPanel::OnTick()
  156. {
  157. // Determine if panel parent should switch
  158. ComputeParent();
  159. // If we should shouldn't draw, don't bother with any of this
  160. if ( !ShouldDraw() )
  161. return;
  162. // Update our current position
  163. int sx, sy;
  164. GetEntityPosition( sx, sy );
  165. // Recalculate our size
  166. ComputeAndSetSize();
  167. // Set the position
  168. SetPos( (int)(sx + m_OffsetX + 0.5f), (int)(sy + m_OffsetY + 0.5f));
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose:
  172. //-----------------------------------------------------------------------------
  173. void CEntityPanel::OnCursorEntered()
  174. {
  175. if ( m_szMouseOverText[ 0 ] )
  176. {
  177. StatusPrint( TYPE_HINT, "%s", m_szMouseOverText );
  178. }
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose:
  182. //-----------------------------------------------------------------------------
  183. void CEntityPanel::OnCursorExited()
  184. {
  185. if ( m_szMouseOverText[ 0 ] )
  186. {
  187. StatusClear();
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. // Output : char const
  193. //-----------------------------------------------------------------------------
  194. const char *CEntityPanel::GetMouseOverText( void )
  195. {
  196. return m_szMouseOverText;
  197. }