source-engine/game/client/vgui_avatarimage.cpp

212 lines
6.0 KiB
C++
Raw Normal View History

2023-10-03 17:23:56 +03:00
//====== Copyright <20> 1996-2005, Valve Corporation, All rights reserved. =======
2020-04-22 12:56:21 -04:00
//
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//
//=============================================================================
#include "cbase.h"
#include <vgui_controls/Controls.h>
#include <vgui_controls/Panel.h>
#include <vgui/ISurface.h>
#include "vgui_avatarimage.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif
#include "steam/steam_api.h"
2023-10-03 17:23:56 +03:00
#include "hud.h"
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
DECLARE_BUILD_FACTORY( CAvatarImagePanel );
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
CAvatarImage::CAvatarImage( void )
{
2023-10-03 17:23:56 +03:00
m_iTextureID = -1;
2020-04-22 12:56:21 -04:00
ClearAvatarSteamID();
2023-10-03 17:23:56 +03:00
m_SourceArtSize = k_EAvatarSize32x32;
2020-04-22 12:56:21 -04:00
m_pFriendIcon = NULL;
m_nX = 0;
m_nY = 0;
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
void CAvatarImage::ClearAvatarSteamID( void )
{
m_bValid = false;
2020-04-22 12:56:21 -04:00
m_bFriend = false;
m_SteamID.Set( 0, k_EUniverseInvalid, k_EAccountTypeInvalid );
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
bool CAvatarImage::SetAvatarSteamID( CSteamID steamIDUser )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
if ( m_steamIDUser == steamIDUser && m_bValid )
return true;
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
ClearAvatarSteamID();
m_steamIDUser = steamIDUser;
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
if ( steamapicontext->SteamFriends() && steamapicontext->SteamUtils() )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
m_SteamID = steamIDUser;
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
int iAvatar = steamapicontext->SteamFriends()->GetFriendAvatar( steamIDUser, m_SourceArtSize );
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
/*
// See if it's in our list already
*/
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
uint32 wide, tall;
if ( steamapicontext->SteamUtils()->GetImageSize( iAvatar, &wide, &tall ) )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
int cubImage = wide * tall * 4;
byte *rgubDest = (byte*)_alloca( cubImage );
steamapicontext->SteamUtils()->GetImageRGBA( iAvatar, rgubDest, cubImage );
InitFromRGBA( rgubDest, wide, tall );
/*
// put it in the list
RGBAImage *pRGBAImage = new RGBAImage( rgubDest, wide, tall );
int iImageList = m_pImageList->AddImage( pRGBAImage );
m_mapAvatarToIImageList.Insert( iAvatar, iImageList );
*/
2020-04-22 12:56:21 -04:00
}
2023-10-03 17:23:56 +03:00
UpdateFriendStatus();
2020-04-22 12:56:21 -04:00
}
2023-10-03 17:23:56 +03:00
return m_bValid;
}
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
void CAvatarImage::UpdateFriendStatus( void )
{
if ( !m_SteamID.IsValid() )
return;
if ( steamapicontext->SteamFriends() && steamapicontext->SteamUtils() )
2023-10-03 17:23:56 +03:00
{
2020-04-22 12:56:21 -04:00
m_bFriend = steamapicontext->SteamFriends()->HasFriend( m_SteamID, k_EFriendFlagImmediate );
2023-10-03 17:23:56 +03:00
if ( m_bFriend && !m_pFriendIcon )
{
m_pFriendIcon = HudIcons().GetIcon( "ico_friend_indicator_avatar" );
}
}
2020-04-22 12:56:21 -04:00
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
void CAvatarImage::InitFromRGBA( const byte *rgba, int width, int height )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
if ( m_iTextureID == -1 )
2020-04-22 12:56:21 -04:00
{
m_iTextureID = vgui::surface()->CreateNewTextureID( true );
}
2023-10-03 17:23:56 +03:00
vgui::surface()->DrawSetTextureRGBA( m_iTextureID, rgba, width, height );
m_nWide = XRES(width);
m_nTall = YRES(height);
m_Color = Color( 255, 255, 255, 255 );
2020-04-22 12:56:21 -04:00
m_bValid = true;
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
void CAvatarImage::Paint( void )
{
if ( m_bValid )
{
2023-10-03 17:23:56 +03:00
if ( m_bFriend && m_pFriendIcon )
{
m_pFriendIcon->DrawSelf( m_nX, m_nY, m_nWide, m_nTall, m_Color );
}
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
vgui::surface()->DrawSetColor( m_Color );
vgui::surface()->DrawSetTexture( m_iTextureID );
vgui::surface()->DrawTexturedRect( m_nX + AVATAR_INDENT_X, m_nY + AVATAR_INDENT_Y, m_nX + AVATAR_INDENT_X + m_iAvatarWidth, m_nY + AVATAR_INDENT_Y + m_iAvatarHeight );
2020-04-22 12:56:21 -04:00
}
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
CAvatarImagePanel::CAvatarImagePanel( vgui::Panel *parent, const char *name ) : vgui::ImagePanel( parent, name )
2020-04-22 12:56:21 -04:00
{
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
void CAvatarImagePanel::SetPlayer( C_BasePlayer *pPlayer )
2020-04-22 12:56:21 -04:00
{
if ( pPlayer )
{
int iIndex = pPlayer->entindex();
2023-10-03 17:23:56 +03:00
SetPlayerByIndex( iIndex );
2020-04-22 12:56:21 -04:00
}
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
void CAvatarImagePanel::SetPlayerByIndex( int iIndex )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
if ( iIndex && steamapicontext->SteamUtils() )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
player_info_t pi;
if ( engine->GetPlayerInfo(iIndex, &pi) )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
if ( pi.friendsID )
{
CSteamID steamIDForPlayer( pi.friendsID, 1, steamapicontext->SteamUtils()->GetConnectedUniverse(), k_EAccountTypeIndividual );
SetAvatarBySteamID( &steamIDForPlayer );
}
2020-04-22 12:56:21 -04:00
}
}
}
//-----------------------------------------------------------------------------
2023-10-03 17:23:56 +03:00
// Purpose:
2020-04-22 12:56:21 -04:00
//-----------------------------------------------------------------------------
void CAvatarImagePanel::PaintBackground( void )
{
2023-10-03 17:23:56 +03:00
vgui::IImage *pImage = GetImage();
if ( pImage )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
pImage->SetColor( GetDrawColor() );
pImage->Paint();
2020-04-22 12:56:21 -04:00
}
}
2023-10-03 17:23:56 +03:00
void CAvatarImagePanel::SetAvatarBySteamID( CSteamID *friendsID )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
if ( !GetImage() )
2020-04-22 12:56:21 -04:00
{
2023-10-03 17:23:56 +03:00
CAvatarImage *pImage = new CAvatarImage();
SetImage( pImage );
2020-04-22 12:56:21 -04:00
}
2023-10-03 17:23:56 +03:00
// Indent the image. These are deliberately non-resolution-scaling.
int iIndent = 2;
GetImage()->SetPos( iIndent, iIndent );
int wide = GetWide() - (iIndent*2);
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
((CAvatarImage*)GetImage())->SetAvatarSize( ( wide > 32 ) ? k_EAvatarSize64x64 : k_EAvatarSize32x32 );
((CAvatarImage*)GetImage())->SetAvatarSteamID( *friendsID );
2020-04-22 12:56:21 -04:00
2023-10-03 17:23:56 +03:00
GetImage()->SetSize( wide, GetTall()-(iIndent*2) );
}