Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created December 22, 2019 16:10
Show Gist options
  • Save untodesu/a300ea5b04a229a76f271510d6ec52d5 to your computer and use it in GitHub Desktop.
Save untodesu/a300ea5b04a229a76f271510d6ec52d5 to your computer and use it in GitHub Desktop.
#include "cbase.h"
#include "vgui/ISurface.h"
#define GAMEUI_ALLOW_INIT //GameUI: Foolproof
#define GAMEUI_ALLOW_OVERRIDE //GameUI: Foolproof
#include <gameui.h>
#include <gameui_override.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
// See interface.h/.cpp for specifics: basically this ensures that we actually Sys_UnloadModule the dll and that we don't call Sys_LoadModule
// over and over again.
static CDllDemandLoader GameUIDLL("GameUI");
namespace gameui
{
//-----------------------------------------------------------------------------
// Purpose: Panel used INSTEAD of main menu
//-----------------------------------------------------------------------------
class OverrideUI::RootPanel : public vgui::Panel {
DECLARE_CLASS_SIMPLE(RootPanel, vgui::Panel);
private:
IGameUI* m_pGameui;
//-----------------------------------------------------------------------------
// Purpose: creates new IGameUI instance.
// RETURNS: True on success.
//-----------------------------------------------------------------------------
bool LoadGameUI(void)
{
//We dont need to do anything if it already here
if(m_pGameui) {
return true;
}
CreateInterfaceFn gameUIFactory = GameUIDLL.GetFactory();
if(!gameUIFactory) {
return false;
}
m_pGameui = (IGameUI *)gameUIFactory(GAMEUI_INTERFACE_VERSION, NULL);
return (m_pGameui != NULL);
}
public:
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
RootPanel(VPANEL parent) : Panel(NULL, "GameUI_RootPanel")
{
SetParent(parent);
m_pGameui = NULL;
LoadGameUI();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
virtual ~RootPanel(void)
{
m_pGameui = NULL;
GameUIDLL.Unload();
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
IGameUI * GetGameUI(void)
{
if(!m_pGameui && !LoadGameUI()) {
return NULL;
}
return m_pGameui;
}
protected:
//-----------------------------------------------------------------------------
// Purpose: Applies scheme settings and resizes panel to full screen space
//-----------------------------------------------------------------------------
virtual void ApplySchemeSettings(vgui::IScheme *scheme)
{
BaseClass::ApplySchemeSettings(scheme);
// Resize the panel to the screen size
// Otherwise, it'll just be in a little corner
int wide, tall;
surface()->GetScreenSize(wide, tall);
SetSize(wide, tall);
}
};
//The root panel.
OverrideUI::RootPanel * OverrideUI::m_pRootPanel = NULL;
#ifdef GAMEUI_ALLOW_OVERRIDE //GameUI: Foolproof
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void OverrideUI::OverrideGameUI(void)
{
if(!OverrideUI::GetVPanel()) {
OverrideUI::Initialize(NULL);
}
if(m_pRootPanel->GetGameUI()) {
Msg("RefUI: IGameUI::SetMainMenuOverride -> GameUI\n");
m_pRootPanel->GetGameUI()->SetMainMenuOverride(m_pRootPanel->GetVPanel());
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void OverrideUI::ShutdownGameUI(void)
{
if(OverrideUI::GetVPanel()) {
IGameUI *ui = OverrideUI::GetGameUI();
if(ui) {
//Actually shutdown gameUI
Msg("RefUI: IGameUI::SetMainMenuOverride -> 0\n");
ui->SetMainMenuOverride((VPANEL)0);
}
}
}
#endif
//-----------------------------------------------------------------------------
// Purpose: Creates new root panel and initializes UI elements.
//-----------------------------------------------------------------------------
void OverrideUI::Initialize(VPANEL parent)
{
m_pRootPanel = new RootPanel(parent);
GameUI::Initialize();
}
//-----------------------------------------------------------------------------
// Purpose: Deletes root panel, un-overrides gameUI
//-----------------------------------------------------------------------------
void OverrideUI::Shutdown(void)
{
if(m_pRootPanel) {
GameUI::Shutdown();
m_pRootPanel->SetParent((Panel *)NULL);
delete m_pRootPanel;
}
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
VPANEL OverrideUI::GetVPanel(void)
{
if(!m_pRootPanel) {
return NULL;
}
return m_pRootPanel->GetVPanel();
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
IGameUI * OverrideUI::GetGameUI(void)
{
if(!m_pRootPanel) {
return NULL;
}
return m_pRootPanel->GetGameUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment