Counter Strike : Global Offensive Source Code
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.

241 lines
6.4 KiB

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