Welcome, Guest! Login | Register

GameUI [Print this Article]
Posted by: ts2do
Date posted: Jun 23 2005
User Rating: 4.5 out of 5.0
Number of views: 11356
Number of comments: 11
Description: Explanation on handling VGUI without having a connection to a game.
Here's an example of a panel you want to show when a command is issed at any time:
 CODE  
"Resource/UI/TestPanel.res"
{
    "testpanel"
    {
        "ControlName"   "CTestPanel"
        "fieldName"     "testpanel"
        "title"         "Test Panel"
        "xpos"          "0"
        "ypos"          "0"
        "wide"          "200"
        "tall"          "200"
        "autoResize"    "0"
        "pinCorner"     "0"
        "visible"       "1"
        "enabled"       "1"
        "tabPosition"   "0"
    }
    "Label1"
    {
        "ControlName"   "Label"
        "fieldName"     "Label1"
        "xpos"          "8"
        "ypos"          "32"
        "wide"          "200"
        "tall"          "175"
        "wrap"          "1"
        "autoResize"    "0"
        "pinCorner"     "0"
        "visible"       "1"
        "enabled"       "1"
        "tabPosition"   "0"
        "labelText"     "An example label inside of an example frame"
        "textAlignment" "north"
        "dulltext"      "0"
        "brighttext"    "0"
    }
    "close"
    {
        "ControlName"   "Button"
        "fieldName"     "close"
        "xpos"          "68"
        "ypos"          "168"
        "wide"          "64"
        "tall"          "24"
        "labelText"     "#GameUI_Close"
        "Command"       "Close"
        "autoResize"    "0"
        "pinCorner"     "0"
        "visible"       "1"
        "enabled"       "1"
        "tabPosition"   "0"
    }
}



To start off, add these defines into vgui_helpers.h:
 CODE (C++) 
#define DeclarePanel(className,panelClassName,globalPanel)\
    class I##className\
    {\
    public:\
        virtual void        Create( vgui::VPANEL parent ) = 0;\
        virtual void        Destroy( void ) = 0;\
    };\
    class className : public I##className\
    {\
    private:\
        panelClassName *myPanel;\
    public:\
        className(void)\
        {\
            myPanel = NULL;\
        }\
        void Create( vgui::VPANEL parent )\
        {\
            myPanel = new panelClassName( parent );\
        }\
        void Destroy( void )\
        {\
            if(myPanel)\
            {\
                myPanel->SetParent( (vgui::Panel *)NULL );\
                delete myPanel;\
            }\
        }\
    };\
    extern I##className *globalPanel
//Allows access to the panel through globalPanel
#define DeclareAccessiblePanel(className,panelClassName,globalPanel)\
    class I##className\
    {\
    public:\
        virtual void        Create( vgui::VPANEL parent ) = 0;\
        virtual void        Destroy( void ) = 0;\
        virtual vgui::Panel *GetPanel(void) = 0;\
    };\
    class className : public I##className\
    {\
    private:\
        panelClassName *myPanel;\
    public:\
        className(void)\
        {\
            myPanel = NULL;\
        }\
        void Create( vgui::VPANEL parent )\
        {\
            myPanel = new panelClassName( parent );\
        }\
        vgui::Panel *GetPanel(void)\
        {\
            return myPanel;\
        }\
        void Destroy( void )\
        {\
            if(myPanel)\
            {\
                myPanel->SetParent( (vgui::Panel *)NULL );\
                delete myPanel;\
            }\
        }\
    };\
    extern I##className *globalPanel
//Doesn't use panelClassName but has it to base code on copy and paste work
#define PanelGlobals(className,panelClassName,globalPanel)\
    static className g_##className##Panel;\
    I##className *globalPanel = (I##className *)&g_##className##Panel

#define ToggleVisibility(panel)\
    panel->SetVisible(!panel->IsVisible())

//only the prototype for AlignPanel exists
#define CenterThisPanelOnScreen()\
    int x,w,h;\
    GetBounds(x,x,w,h);\
    SetPos((ScreenWidth()-w)/2,(ScreenHeight()-h)/2)
#define CenterPanelOnScreen(panel)\
    int x,w,h;\
    panel->GetBounds(x,x,w,h);\
    panel->SetPos((panel->ScreenWidth()-w)/2,(panel->ScreenHeight()-h)/2)


testpanel.h
 CODE (C++) 
#ifndef TESTPANEL_H
#define TESTPANEL_H
#ifdef _WIN32
#pragma once
#endif
#include "vgui_helpers2.h"
#include <vgui_controls/Frame.h>
using namespace vgui;
class CTestPanel : public Frame
{
    DECLARE_CLASS_SIMPLE(CTestPanel,Frame);
public:
    CTestPanel( vgui::VPANEL parent );
};
DeclareAccessiblePanel(CTest,CTestPanel,test);
#endif // TESTPANEL_H


testpanel.cpp
 CODE (C++) 
#include "cbase.h"
#include "testpanel.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
PanelGlobals(CTest,CTestPanel,test);
CON_COMMAND(ToggleTestPanel,NULL)
{
    ToggleVisibility(test->GetPanel());
}
CTestPanel::CTestPanel( vgui::VPANEL parent ) : BaseClass( NULL, "testpanel" )
{
    SetParent(parent);
    vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFile("resource/SourceScheme.res", "SourceScheme");
    SetScheme( scheme );
    LoadControlSettings("Resource/UI/TestPanel.res");
    CenterThisPanelOnScreen();//keep in mind, hl2 supports widescreen
    SetVisible(false);//made visible on command later
}


See how much the defines reduced the coding for testpanel.cpp and testpanel.h?

Now that we have the framework for the panel...
Go to the includes in vgui_int.cpp and add testpanel.h
Scroll down to VGui_CreateGlobalPanels and add this after the second line:
 CODE (C++) 
    VPANEL uiParent = enginevgui->GetPanel( PANEL_GAMEUIDLL );

The GameUI panel is what we're going to be making our panels in

In the same function, add this to the list of Creates:
 CODE (C++) 
    test->Create( uiParent );


Scroll down a little until you get to VGui_Shutdown and add this to the list of Destroys:
 CODE (C++) 
    test->Destroy();


Now we have a panel that's created during startup, destroyed on shutdown, and accessible through a command.
Keep these in mind though:
You don't have to use a frame for the panel
You can make it so the command to show it can't close it also
You can make it so the panel is created once the command is issued, and destroyed once it's closed, so it doesn't take up RAM

Here's an example of what I did based on this code
user posted image


The hotkeys for opening the build menu for the selected EditablePanel, which can also be a Frame or Frame subclasses, are Ctrl+Shift+Alt+B
The build menu makes creation easier by allowing you to dynamically edit a resource file by creating controls and moving them.

Rate This Article
This article is currently rated: 4.5 out of 5.0 (2 Votes)

You have to register to rate this article.
Related Files
Zip FileFilename: gameui.jpg
File Size: 41.3 KB

User Comments Showing comments 1-11

Posted By: PelzigesWaldtier on Apr 17 2006 at 11:18:08
I get the following error:

vgui_int.obj : error LNK2001: unresolved external symbol "class ICTest * test" (?test@@3PAVICTest@@A)
.\cl_dll___Win32_HL2_Release/client.dll : fatal error LNK1120: 1 unresolved externals

What am I doing wrong?

Posted By: ts2do on Apr 18 2006 at 04:57:10
Do you have your panel declared?

Posted By: PelzigesWaldtier on Apr 18 2006 at 07:29:47
Yes, I think so.. I followed every step of the tutorial.

Posted By: ts2do on Apr 18 2006 at 20:21:17
After reviewing the error, it seems that you didn't do PanelGlobals

Posted By: PelzigesWaldtier on Apr 19 2006 at 19:21:11
I didn't remove anything from the code from above ("PanelGlobals(CTest,CTestPanel,test);" remains in testpanel.cpp). The only thing I changed is "#include "vgui_helpers2.h"" in testpanel.h to "#include "vgui_helpers.h"".

Posted By: ts2do on Apr 19 2006 at 23:26:10
HERE'S YOUR PROBLEM:
Go to the includes in vgui_int.cpp and add testpanel.h

Posted By: PelzigesWaldtier on Apr 20 2006 at 07:27:52
I already did that.. That doesn't solve the problem.

Posted By: nuno12345 on Nov 20 2006 at 21:57:27
well...i got the same problem...i just included my "testpanel.cpp" to vgui_int.cpp and it compiled fine :D but there is no vgui panel showing :(

Posted By: G.I. Jimbo on May 20 2007 at 04:06:20
where is the .res file supposed to go? In ui?

Posted By: Evil_j on Oct 29 2007 at 14:01:47
RES file goes into the resource directory under your mod

Posted By: Unknown on Jul 13 2009 at 20:14:51
Please can anyone upload his vgui_helpers.h and vgui_int.h files? thx


You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 05 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
knock knock
General | News
By: omega | Dec 22 2016
 
knock knock
General | News
By: MIFUNE | Oct 10 2015
 
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
 
knock knock
General | News
By: Whistler | Nov 05 2011
 
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
 
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Persuter | Aug 02 2009
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (7 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!