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.

391 lines
11 KiB

  1. //====== Copyright © 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include <vgui_controls/Controls.h>
  8. #include <vgui_controls/Panel.h>
  9. #include <vgui/ISurface.h>
  10. #include "vgui_avatarimage.h"
  11. #include "engineinterface.h"
  12. #if defined( _X360 )
  13. #include "xbox/xbox_win32stubs.h"
  14. #endif
  15. #if defined( _PS3 )
  16. #include "ps3/ps3_core.h"
  17. #include "ps3/ps3_win32stubs.h"
  18. #endif
  19. #ifndef NO_STEAM
  20. #include "steam/steam_api.h"
  21. #endif
  22. #include "hud.h"
  23. // NOTE: This has to be the last file included!
  24. #include "tier0/memdbgon.h"
  25. DECLARE_BUILD_FACTORY( CAvatarImagePanel );
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. CAvatarImage::CAvatarImage( void )
  30. {
  31. ClearAvatarSteamID();
  32. m_nX = 0;
  33. m_nY = 0;
  34. m_wide = m_tall = 0;
  35. m_avatarWide = m_avatarTall = 0;
  36. m_Color = Color( 255, 255, 255, 255 );
  37. m_bLoadPending = false;
  38. m_fNextLoadTime = 0.0f;
  39. m_AvatarSize = eAvatarSmall;
  40. // [tj] Default to drawing the friend icon for avatars
  41. m_bDrawFriend = true;
  42. // [menglish] Default icon for avatar icons if there is no avatar icon for the player
  43. m_iTextureID = -1;
  44. // set up friend icon
  45. m_pFriendIcon = HudIcons().GetIcon( "ico_friend_indicator_avatar" );
  46. m_pDefaultImage = NULL;
  47. SetAvatarSize(DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE);
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: reset the image to a default state (will render with the default image)
  51. //-----------------------------------------------------------------------------
  52. void CAvatarImage::ClearAvatarSteamID( void )
  53. {
  54. m_bValid = false;
  55. m_bFriend = false;
  56. m_bLoadPending = false;
  57. m_SteamID.Set( 0, k_EUniverseInvalid, k_EAccountTypeInvalid );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose: Set the CSteamID for this image; this will cause a deferred load
  61. //-----------------------------------------------------------------------------
  62. bool CAvatarImage::SetAvatarSteamID( CSteamID steamIDUser, EAvatarSize avatarSize /*= eAvatarSmall */ )
  63. {
  64. ClearAvatarSteamID();
  65. m_SteamID = steamIDUser;
  66. m_AvatarSize = avatarSize;
  67. m_bLoadPending = true;
  68. LoadAvatarImage();
  69. UpdateFriendStatus();
  70. return m_bValid;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose: load the avatar image if we have a load pending
  74. //-----------------------------------------------------------------------------
  75. void CAvatarImage::LoadAvatarImage()
  76. {
  77. // attempt to retrieve the avatar image from Steam
  78. if ( m_bLoadPending && steamapicontext->SteamFriends() && steamapicontext->SteamUtils() && gpGlobals->curtime >= m_fNextLoadTime )
  79. {
  80. int iAvatar = 0;
  81. switch ( m_AvatarSize )
  82. {
  83. case eAvatarSmall:
  84. iAvatar = steamapicontext->SteamFriends()->GetSmallFriendAvatar( m_SteamID );
  85. break;
  86. case eAvatarMedium:
  87. iAvatar = steamapicontext->SteamFriends()->GetMediumFriendAvatar( m_SteamID );
  88. break;
  89. case eAvatarLarge:
  90. iAvatar = steamapicontext->SteamFriends()->GetLargeFriendAvatar( m_SteamID );
  91. break;
  92. }
  93. if ( iAvatar != 0 ) // if its zero, user doesn't have an avatar
  94. {
  95. uint32 wide = 0, tall = 0;
  96. if ( steamapicontext->SteamUtils()->GetImageSize( iAvatar, &wide, &tall ) && wide > 0 && tall > 0 )
  97. {
  98. int destBufferSize = wide * tall * 4;
  99. byte *rgbDest = (byte*)stackalloc( destBufferSize );
  100. if ( steamapicontext->SteamUtils()->GetImageRGBA( iAvatar, rgbDest, destBufferSize ) )
  101. InitFromRGBA( rgbDest, wide, tall );
  102. stackfree( rgbDest );
  103. }
  104. }
  105. if ( m_bValid )
  106. {
  107. // if we have a valid image, don't attempt to load it again
  108. m_bLoadPending = false;
  109. }
  110. else
  111. {
  112. // otherwise schedule another attempt to retrieve the image
  113. m_fNextLoadTime = gpGlobals->curtime + 1.0f;
  114. }
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose: Query Steam to set the m_bFriend status flag
  119. //-----------------------------------------------------------------------------
  120. void CAvatarImage::UpdateFriendStatus( void )
  121. {
  122. if ( !m_SteamID.IsValid() )
  123. return;
  124. if ( steamapicontext->SteamFriends() && steamapicontext->SteamUtils() )
  125. m_bFriend = steamapicontext->SteamFriends()->HasFriend( m_SteamID, k_EFriendFlagImmediate );
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Purpose:
  129. //-----------------------------------------------------------------------------
  130. void CAvatarImage::InitFromRGBA( const byte *rgba, int width, int height )
  131. {
  132. // Texture size may be changing, so recreate
  133. if ( m_iTextureID < 0 )
  134. {
  135. vgui::surface()->DestroyTextureID( m_iTextureID );
  136. m_iTextureID = -1;
  137. }
  138. m_iTextureID = vgui::surface()->CreateNewTextureID( true );
  139. vgui::surface()->DrawSetTextureRGBAEx( m_iTextureID, rgba, width, height, IMAGE_FORMAT_RGBA8888 );
  140. m_bValid = true;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose:
  144. //-----------------------------------------------------------------------------
  145. void CAvatarImage::Paint( void )
  146. {
  147. if ( m_bFriend && m_pFriendIcon && m_bDrawFriend)
  148. {
  149. m_pFriendIcon->DrawSelf( m_nX, m_nY, m_wide, m_tall, m_Color );
  150. }
  151. int posX = m_nX;
  152. int posY = m_nY;
  153. if (m_bDrawFriend)
  154. {
  155. posX += FRIEND_ICON_AVATAR_INDENT_X * m_avatarWide / DEFAULT_AVATAR_SIZE;
  156. posY += FRIEND_ICON_AVATAR_INDENT_Y * m_avatarTall / DEFAULT_AVATAR_SIZE;
  157. }
  158. if ( m_bLoadPending )
  159. {
  160. LoadAvatarImage();
  161. }
  162. if ( m_bValid )
  163. {
  164. vgui::surface()->DrawSetTexture( m_iTextureID );
  165. vgui::surface()->DrawSetColor( m_Color );
  166. vgui::surface()->DrawTexturedRect(posX, posY, posX + m_avatarWide, posY + m_avatarTall);
  167. }
  168. else if (m_pDefaultImage)
  169. {
  170. // draw default
  171. m_pDefaultImage->SetSize(m_avatarWide, m_avatarTall);
  172. m_pDefaultImage->SetPos(posX, posY);
  173. m_pDefaultImage->SetColor(m_Color);
  174. m_pDefaultImage->Paint();
  175. }
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Purpose: Set the avatar size; scale the total image and friend icon to fit
  179. //-----------------------------------------------------------------------------
  180. void CAvatarImage::SetAvatarSize(int wide, int tall)
  181. {
  182. m_avatarWide = wide;
  183. m_avatarTall = tall;
  184. if (m_bDrawFriend)
  185. {
  186. // scale the size of the friend background frame icon
  187. m_wide = FRIEND_ICON_SIZE_X * m_avatarWide / DEFAULT_AVATAR_SIZE;
  188. m_tall = FRIEND_ICON_SIZE_Y * m_avatarTall / DEFAULT_AVATAR_SIZE;
  189. }
  190. else
  191. {
  192. m_wide = m_avatarWide;
  193. m_tall = m_avatarTall;
  194. }
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Purpose: Set the total image size; scale the avatar portion to fit
  198. //-----------------------------------------------------------------------------
  199. void CAvatarImage::SetSize( int wide, int tall )
  200. {
  201. m_wide = wide;
  202. m_tall = tall;
  203. if (m_bDrawFriend)
  204. {
  205. // scale the size of the avatar portion based on the total image size
  206. m_avatarWide = DEFAULT_AVATAR_SIZE * m_wide / FRIEND_ICON_SIZE_X;
  207. m_avatarTall = DEFAULT_AVATAR_SIZE * m_tall / FRIEND_ICON_SIZE_Y ;
  208. }
  209. else
  210. {
  211. m_avatarWide = m_wide;
  212. m_avatarTall = m_tall;
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. // Purpose:
  217. //-----------------------------------------------------------------------------
  218. CAvatarImagePanel::CAvatarImagePanel( vgui::Panel *parent, const char *name ) : BaseClass( parent, name )
  219. {
  220. m_bScaleImage = false;
  221. m_pImage = new CAvatarImage();
  222. m_bSizeDirty = true;
  223. }
  224. //-----------------------------------------------------------------------------
  225. //=============================================================================
  226. // HPE_BEGIN:
  227. // [menglish] Added parameter to specify a default avatar
  228. //=============================================================================
  229. void CAvatarImagePanel::SetPlayer( C_BasePlayer *pPlayer, EAvatarSize avatarSize )
  230. {
  231. if ( pPlayer )
  232. {
  233. int iIndex = pPlayer->entindex();
  234. SetPlayerByIndex(iIndex, avatarSize);
  235. }
  236. else
  237. m_pImage->ClearAvatarSteamID();
  238. }
  239. //-----------------------------------------------------------------------------
  240. // Purpose:
  241. //-----------------------------------------------------------------------------
  242. // HPE_BEGIN:
  243. // [tj] Adding a second function so we don't need a player pointer to get an avatar
  244. void CAvatarImagePanel::SetPlayerByIndex( int entindex, EAvatarSize avatarSize )
  245. {
  246. m_pImage->ClearAvatarSteamID();
  247. player_info_t pi;
  248. if ( engine->GetPlayerInfo(entindex, &pi) )
  249. {
  250. if ( pi.friendsID != 0 && steamapicontext->SteamUtils() )
  251. {
  252. CSteamID steamIDForPlayer( pi.friendsID, 1, steamapicontext->SteamUtils()->GetConnectedUniverse(), k_EAccountTypeIndividual );
  253. SetPlayerBySteamID( steamIDForPlayer, avatarSize );
  254. }
  255. else
  256. {
  257. m_pImage->ClearAvatarSteamID();
  258. }
  259. }
  260. }
  261. //-----------------------------------------------------------------------------
  262. // HPE_BEGIN:
  263. //-----------------------------------------------------------------------------
  264. void CAvatarImagePanel::SetPlayerBySteamID(CSteamID steamIDForPlayer, EAvatarSize avatarSize )
  265. {
  266. m_pImage->ClearAvatarSteamID();
  267. if (steamIDForPlayer.GetAccountID() != 0 )
  268. m_pImage->SetAvatarSteamID( steamIDForPlayer, avatarSize );
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Purpose:
  272. //-----------------------------------------------------------------------------
  273. void CAvatarImagePanel::PaintBackground( void )
  274. {
  275. if ( m_bSizeDirty )
  276. UpdateSize();
  277. m_pImage->Paint();
  278. }
  279. void CAvatarImagePanel::ClearAvatar()
  280. {
  281. m_pImage->ClearAvatarSteamID();
  282. }
  283. void CAvatarImagePanel::SetDefaultAvatar( vgui::IImage* pDefaultAvatar )
  284. {
  285. m_pImage->SetDefaultImage(pDefaultAvatar);
  286. }
  287. void CAvatarImagePanel::SetAvatarSize( int width, int height )
  288. {
  289. if ( m_bScaleImage )
  290. {
  291. // panel is charge of image size - setting avatar size this way not allowed
  292. Assert(false);
  293. return;
  294. }
  295. else
  296. {
  297. m_pImage->SetAvatarSize( width, height );
  298. m_bSizeDirty = true;
  299. }
  300. }
  301. void CAvatarImagePanel::OnSizeChanged( int newWide, int newTall )
  302. {
  303. BaseClass::OnSizeChanged(newWide, newTall);
  304. m_bSizeDirty = true;
  305. }
  306. void CAvatarImagePanel::SetShouldScaleImage( bool bScaleImage )
  307. {
  308. m_bScaleImage = bScaleImage;
  309. m_bSizeDirty = true;
  310. }
  311. void CAvatarImagePanel::SetShouldDrawFriendIcon( bool bDrawFriend )
  312. {
  313. m_pImage->SetDrawFriend(bDrawFriend);
  314. m_bSizeDirty = true;
  315. }
  316. void CAvatarImagePanel::UpdateSize()
  317. {
  318. if ( m_bScaleImage )
  319. {
  320. // the panel is in charge of the image size
  321. m_pImage->SetAvatarSize(GetWide(), GetTall());
  322. }
  323. else
  324. {
  325. // the image is in charge of the panel size
  326. SetSize(m_pImage->GetAvatarWide(), m_pImage->GetAvatarTall() );
  327. }
  328. m_bSizeDirty = false;
  329. }
  330. void CAvatarImagePanel::ApplySettings( KeyValues *inResourceData )
  331. {
  332. m_bScaleImage = ( inResourceData->GetInt( "scaleImage", 0 ) != 0 );
  333. BaseClass::ApplySettings(inResourceData);
  334. }