FAQ  •  Login

weapon swap HUD element

<<

vecima

User avatar

Type 3
Type 3

Posts: 2409

Joined: Sun Jun 24, 2007 7:32 pm

Location: New Jersey

Post Mon Nov 02, 2009 12:03 am

weapon swap HUD element

(I made this same post over at steampowered - there's usually more programmers around there)

I've gotten the code for weapon swapping to function exactly as I want it to. It works like a charm. My problem now is I want to show the player that they are able to swap.

Since my code is in BumpWeapon i can use the MSG( "..." ); to see in the console which weapon I'm "bumping" and if I press use I swap to it. I've even gotten the server message across with a UserMessageBegin. I did it with the "HintText" message first to try it, then I made my own message.

Here's my existing code:

(hl2_player.cpp)
  Code:
bool CHL2_Player::BumpWeapon( CBaseCombatWeapon *pWeapon )
{
 
#if    HL2_SINGLE_PRIMARY_WEAPON_MODE // this is true in my mod

    //BOP weapon swap code.
    CBaseCombatWeapon *pActiveWeapon = Weapon_GetSlot( pWeapon->GetSlot() );

    if ( pActiveWeapon != NULL )
    {
        //send a message to the HUD to let the player know if they can swap weapons.
        CSingleUserRecipientFilter user( this );
        user.MakeReliable();

        UserMessageBegin( user, "SwapText" );
            WRITE_STRING( "#BOP_Swap" );  // BOP_Swap is defined in my resource/Mod_English.txt
        MessageEnd();

    //    Msg( "press use to swap " );
    //    Msg( pActiveWeapon->GetClassname() );
    //    Msg( " for " );
    //    Msg( pWeapon->GetClassname() );
    }
... (omitted)
}
here is the message I added to be received in the client: (it's mostly copied from hud_hintdisplay.cpp)

(hud_weaponswap.cpp)
  Code:
#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include "vgui_controls/AnimationController.h"
#include "vgui_controls/Label.h"
#include "vgui/ILocalize.h"
#include "vgui/ISurface.h"
#include "text_message.h"
#include "c_baseplayer.h"
#include "IGameUIFuncs.h"
#include "inputsystem/iinputsystem.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Purpose: Displays hints across the center of the screen
//-----------------------------------------------------------------------------
class CHudWeaponSwap : public CHudElement, public vgui::Panel
{
    DECLARE_CLASS_SIMPLE( CHudWeaponSwap, vgui::Panel );
public:
    CHudWeaponSwap( const char *pElementName );

    void Init();
    void Reset();
    void MsgFunc_SwapText( bf_read &msg );

    bool SetSwapText( wchar_t *text );
    void LocalizeAndDisplay( const char *pszHudTxtMsg, const char *szRawString );
protected:
    virtual void ApplySchemeSettings( vgui::IScheme *pScheme );

protected:
    vgui::HFont m_hFont;
    Color        m_bgColor;
    vgui::Label *m_pLabel;
    CUtlVector<vgui::Label *> m_Labels;
    CPanelAnimationVarAliasType( int, m_iTextX, "text_xpos", "8", "proportional_int" );
    CPanelAnimationVarAliasType( int, m_iTextY, "text_ypos", "8", "proportional_int" );
    CPanelAnimationVarAliasType( int, m_iCenterX, "center_x", "0", "proportional_int" );
    CPanelAnimationVarAliasType( int, m_iCenterY, "center_y", "0", "proportional_int" );

    bool        m_bLastLabelUpdateHack;
    CPanelAnimationVar( float, m_flLabelSizePercentage, "HintSize", "0" );
};

DECLARE_HUDELEMENT( CHudWeaponSwap );
DECLARE_HUD_MESSAGE( CHudWeaponSwap, SwapText );


//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudWeaponSwap::CHudWeaponSwap( const char *pElementName ) : BaseClass(NULL, "HudWeaponSwap"), CHudElement( pElementName )
{
    vgui::Panel *pParent = g_pClientMode->GetViewport();
    SetParent( pParent );
    SetVisible( false );
    m_pLabel = new vgui::Label( this, "HudWeaponSwapLabel", "" );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudWeaponSwap::Init()
{
    HOOK_HUD_MESSAGE( CHudWeaponSwap, SwapText );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudWeaponSwap::Reset()
{
    SetSwapText( NULL );
    g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "SwapMessageHide" );
    m_bLastLabelUpdateHack = true;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudWeaponSwap::ApplySchemeSettings( vgui::IScheme *pScheme )
{
    BaseClass::ApplySchemeSettings( pScheme );

    SetFgColor( GetSchemeColor("HintMessageFg", pScheme) );
    m_hFont = pScheme->GetFont( "HudHintText", true );
    m_pLabel->SetBgColor( GetSchemeColor("HintMessageBg", pScheme) );
    m_pLabel->SetPaintBackgroundType( 2 );
    m_pLabel->SetSize( 0, GetTall() );        // Start tiny, it'll grow.
}

//-----------------------------------------------------------------------------
// Purpose: Sets the hint text, replacing variables as necessary
//-----------------------------------------------------------------------------
bool CHudWeaponSwap::SetSwapText( wchar_t *text )
{
    // clear the existing text
    for (int i = 0; i < m_Labels.Count(); i++)
    {
        m_Labels[i]->MarkForDeletion();
    }
    m_Labels.RemoveAll();

    wchar_t *p = text;

    while ( p )
    {
        wchar_t *line = p;
        wchar_t *end = wcschr( p, L'\n' );
        int linelengthbytes = 0;
        if ( end )
        {
            //*end = 0;    //eek
            p = end+1;
            linelengthbytes = ( end - line ) * 2;
        }
        else
        {
            p = NULL;
        }       

        // replace any key references with bound keys
        wchar_t buf[512];
        UTIL_ReplaceKeyBindings( line, linelengthbytes, buf, sizeof( buf ) );

        // put it in a label
        vgui::Label *label = vgui::SETUP_PANEL(new vgui::Label(this, NULL, buf));
        label->SetFont( m_hFont );
        label->SetPaintBackgroundEnabled( false );
        label->SetPaintBorderEnabled( false );
        label->SizeToContents();
        label->SetContentAlignment( vgui::Label::a_west );
        label->SetFgColor( GetFgColor() );
        m_Labels.AddToTail( vgui::SETUP_PANEL(label) );
    }

    InvalidateLayout( true );

    return true;
}

//-----------------------------------------------------------------------------
// Purpose: Activates the hint display
//-----------------------------------------------------------------------------
void CHudWeaponSwap::MsgFunc_SwapText( bf_read &msg )
{
    // Read the string(s)
    char szString[255];
    msg.ReadString( szString, sizeof(szString) );

    char *tmpStr = hudtextmessage->LookupString( szString, NULL );
    LocalizeAndDisplay( tmpStr, szString );
}

//-----------------------------------------------------------------------------
// Purpose: Localize, display, and animate the hud element
//-----------------------------------------------------------------------------
void CHudWeaponSwap::LocalizeAndDisplay( const char *pszHudTxtMsg, const char *szRawString )
{
    static wchar_t szBuf[128];
    static wchar_t *pszBuf;

    // init buffers & pointers
    szBuf[0] = 0;
    pszBuf = szBuf;

    // try to localize
    if ( pszHudTxtMsg )
    {
        pszBuf = g_pVGuiLocalize->Find( pszHudTxtMsg );
    }
    else
    {
        pszBuf = g_pVGuiLocalize->Find( szRawString );
    }

    if ( !pszBuf )
    {
        // use plain ASCII string
        g_pVGuiLocalize->ConvertANSIToUnicode( szRawString, szBuf, sizeof(szBuf) );
        pszBuf = szBuf;
    }

    // make it visible
    if ( SetSwapText( pszBuf ) )
    {
        SetVisible( true );
        g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "SwapMessageShow" );
    }
    else
    {
        g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "SwapMessageHide" );
    }
}
HudLayout.res has this in it:
  Code:
HudWeaponSwap
    {
        "fieldName"    "HudWeaponSwap"
        "visible"    "0"
        "enabled"    "1"
        "Alpha"        "0"
        "xpos"    "c50"    [$WIN32]
        "ypos"    "r36"    [$WIN32]
        "wide"        "100"
        "tall"        "36"
        "text_xpos"    "8"
        "text_ypos"    "8"
        "text_xgap"    "8"
        "text_ygap"    "8"
        "TextColor"    "255 170 0 220"

        "PaintBackgroundType"    "2"
    }
HudAnimations.txt has this:
  Code:
event SwapMessageShow
{
    // show the weapon swap
    Animate HudWeaponSwap Alpha    255 Linear 0.0 0.5

    // flash text
    Animate HudWeaponSwap FgColor    "FgColor"    Linear 0.0 0.01
    Animate HudWeaponSwap FgColor    "255 220 0 255" Linear 0.5 0.2
    Animate HudWeaponSwap FgColor    "FgColor"     Linear 0.7 0.2
    Animate HudWeaponSwap FgColor    "255 220 0 255" Linear 1.5 0.2
    Animate HudWeaponSwap FgColor    "FgColor"     Linear 1.7 0.2
}

event SwapMessageHide
{
    // hide near immediately
    Animate HudWeaponSwap Alpha        0        Linear 0.0 0.1
}
I added the message to hl2_usermessages.cpp RegisterMessages, and I know it works, because the client shows the message when it comes across from the server.

there are just a few things I want to fix / improve:
-I can't get the "swap weapon" message to hide. It's hidden at first when the map loads, then as soon as I "bump" a weapon, the message appears, and It doesn't go away.
-how can I add the weapon icons to this element? I'd like to have a the picture of the current weapon (from the weapon selection panel) and the picture of the weapon the player can swap to.
-What's up with my panel? (see image) I tried to give it a sensible "xpos" "ypos" "tall" and "wide" values, but it's sticking down past the bottom of the screen.

here is my HUD element so far. It proves the server -> client message is working... there's just a few things I'm not grasping, possibly relating to VGUI. Using the example of Hud Hints, I can't find where the logic is to tell them to hide. I can probably figure out the weapon Icon part myself eventually, but any help on the other part/parts is appreciated

Image
<<

Kenny

User avatar

Warrant Officer 1
Warrant Officer 1

Posts: 1016

Joined: Sun Mar 16, 2008 3:43 am

Post Mon Nov 02, 2009 6:08 am

Re: weapon swap HUD element

I'd make the E (Swap Weapon) vgui a bit bigger- in that screen it's rather difficult to see.
Image
<<

vecima

User avatar

Type 3
Type 3

Posts: 2409

Joined: Sun Jun 24, 2007 7:32 pm

Location: New Jersey

Post Mon Nov 02, 2009 11:47 am

Re: weapon swap HUD element

actually my plan is to show the icon of the weapon you have, and the icon of the weapon you can pick up with a "swap" icon between them. the current thing is was just an attempt to get the server -> client message working and to draw something on the HUD.

It's no longer all the way at the bottom (it's now level with health/ammo) and it's now centered... so I got those parts working ok.

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.