Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created December 27, 2019 15:11
Show Gist options
  • Save untodesu/bfae7059c4977ae961f72f2395184e22 to your computer and use it in GitHub Desktop.
Save untodesu/bfae7059c4977ae961f72f2395184e22 to your computer and use it in GitHub Desktop.
Source SDK GameUI wrapper
#include "cbase.h"
#include "iclientmode.h"
#include <vector>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui_controls/Frame.h>
#define GAMEUI_ALLOW_INIT //GameUI: Idiot-proof
#include <gameui.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: GameUI static members
//-----------------------------------------------------------------------------
CUtlVector<UIElementInstanceFn> GameUI::m_vInstances;
CUtlVector<UIElement *> GameUI::m_vpElements;
GameUI::RootPanel * GameUI::m_pRootPanel = NULL;
IGameUI * GameUI::m_pGameUI = NULL;
//-----------------------------------------------------------------------------
// Purpose: Print lambda :3
//-----------------------------------------------------------------------------
CON_COMMAND_F(refui_lambda, "E", FCVAR_HIDDEN)
{
Msg("RefUI: Well, take this.\n");
Msg(" ..',;;;;;;,'... \n"
" .,:lodddoooooodddol:,. \n"
" .,codol:,'........',:lodoc,. \n"
" .,ldol;.. ..;lddl,. \n"
" .codl,. ,::::;'. .,ldoc. \n"
" .ldo:. ,:clddo;. .:odl. \n"
" .cdo:. .cddo, .:odc. \n"
" ;odc. .cdddl' .cdd;.\n"
".cdo, .cdddddc. ,odl.\n"
".ldo' 'ldl::cdd:. 'odl.\n"
".ldo, .;odc' .ldo; 'odl.\n"
".:dd:. .:oo:. 'odo, .;ddc.\n"
" 'odl' .cdo;. ;odl;,;. 'ldo, \n"
" ;odl' .col, .:ddool;. 'ldo; \n"
" .;odo;. .... .,'... .;odo;. \n"
" 'codl;. .,lddc' \n"
" .,codoc,.. ..,:odoc,. \n"
" .;codoolc:;;;;:cloodol;.. \n"
" .';:clloooollc:;'.. \n"
" .......... \n");
}
//-----------------------------------------------------------------------------
// Purpose: The root panel. This panel is actually being drawn instead of main menu
//-----------------------------------------------------------------------------
class GameUI::RootPanel : public Panel {
DECLARE_CLASS_SIMPLE(RootPanel, Panel);
public:
RootPanel(VPANEL parent);
protected:
virtual void ApplySchemeSettings(IScheme *scheme);
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
GameUI::RootPanel::RootPanel(VPANEL parent) : BaseClass(NULL, "GameUI_RootPanel")
{
SetParent(parent);
}
//-----------------------------------------------------------------------------
// Purpose: Apply scheme and resize itself to screen's size
//-----------------------------------------------------------------------------
void GameUI::RootPanel::ApplySchemeSettings(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);
}
//-----------------------------------------------------------------------------
// Purpose: Create new root panel and override IGameUI to it
//-----------------------------------------------------------------------------
void GameUI::InitializeGameUI(VPANEL parent)
{
if(!m_pRootPanel) {
m_pRootPanel = new GameUI::RootPanel(parent);
LoadGameUI();
}
if(m_pGameUI) {
Msg("RefUI: IGameUI::SetMainMenuOverride -> GameUI\n");
m_pGameUI->SetMainMenuOverride(m_pRootPanel->GetVPanel());
CreatePanels();
}
}
//-----------------------------------------------------------------------------
// Purpose: De-Override IGameUI menu panel, delete root panel
//-----------------------------------------------------------------------------
void GameUI::ShutdownGameUI(void)
{
if(m_pGameUI) {
Msg("RefUI: IGameUI::SetMainMenuOverride -> 0\n");
m_pGameUI->SetMainMenuOverride((VPANEL)0);
if(m_pRootPanel) {
DeletePanels();
m_pRootPanel->SetParent((Panel *)NULL);
delete m_pRootPanel;
m_pRootPanel = NULL;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Get IGameUI from gameui.dll
//-----------------------------------------------------------------------------
bool GameUI::LoadGameUI(void)
{
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);
}
//-----------------------------------------------------------------------------
// Purpose: Add new panel instance function to list
//-----------------------------------------------------------------------------
void GameUI::RegisterPanel(UIElementInstanceFn pfnInstance)
{
//If already have same instance function - do absolutely NOTHING
for(int i = 0; i < m_vInstances.Count(); i++) {
if(m_vInstances[i] == pfnInstance) {
return;
}
}
m_vInstances.AddToTail(pfnInstance);
}
//-----------------------------------------------------------------------------
// Purpose: Create and initialize all registered panels
//-----------------------------------------------------------------------------
void GameUI::CreatePanels(void)
{
Msg("RefUI: Initializing panels...\n");
for(int i = 0; i < m_vInstances.Count(); i++) {
UIElementInstanceFn pfnInstance = m_vInstances[i];
if(pfnInstance) {
UIElement *element = pfnInstance();
element->SetParent(m_pRootPanel);
element->Init();
//Add new element to list
m_vpElements.AddToTail(element);
}
}
Msg("RefUI: Panels count: %u\n", m_vpElements.Count());
}
//-----------------------------------------------------------------------------
// Purpose: Shutdown and delete all created panels
//-----------------------------------------------------------------------------
void GameUI::DeletePanels(void)
{
for(int i = 0; i < m_vpElements.Count(); i++) {
UIElement *element = m_vpElements[i];
element->SetParent((Panel *)NULL);
element->Shutdown();
delete element;
}
//And now just clear elements list
m_vpElements.RemoveAll();
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
IGameUI * GameUI::GetGameUI(void)
{
if(!LoadGameUI()) {
return NULL;
}
return m_pGameUI;
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor.
// NOTE: This also can create panels on viewport!
//-----------------------------------------------------------------------------
VPANEL GameUI::GetVPanel(bool viewport)
{
// This function can parent windows only for UI part, or for viewport
return (viewport)
? g_pClientMode->GetViewport()->GetVPanel()
: m_pRootPanel->GetVPanel();
}
//-----------------------------------------------------------------------------
// Purpose: Get named UI element
// Returns: Found element or NULL
//-----------------------------------------------------------------------------
UIElement * GameUI::GetElement(const char *elementName)
{
for(int i = 0; i < m_vpElements.Count(); i++) {
UIElement *element = m_vpElements[i];
if(FStrEq(element->GetName(), elementName)) {
return element;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Get VGUI Localized string
//-----------------------------------------------------------------------------
void GameUI::GetLocalizedString(const char *src, wchar_t *dst, size_t dst_len)
{
size_t src_len = Q_strlen(src);
const wchar_t *value = g_pVGuiLocalize->Find(src);
if(value) {
Q_wcsncpy(dst, value, dst_len * sizeof(wchar_t));
}
else {
for(size_t i = 0; i < dst_len; i++) {
dst[i] = L'\0';
}
for(size_t i = 0; i < src_len && i < dst_len; i++) {
//Just copy shit
dst[i] = src[i];
}
}
}
}
#ifndef _GAMEUI_H
#define _GAMEUI_H
#ifdef _WIN32
#pragma once
#endif
#include <vgui/VGUI.h>
#include <vgui_controls/Panel.h>
#include <vgui_controls/Frame.h>
#include <tier1/utlvector.h>
#include <GameUI/IGameUI.h>
#include <gameui_element.h>
//define this if you want to allow initialization and shutdown
//#define GAMEUI_ALLOW_INIT
namespace gameui
{
//-----------------------------------------------------------------------------
// Purpose: Global static UI manager class
//-----------------------------------------------------------------------------
class GameUI {
class RootPanel; //See gameui.cpp
private:
static CUtlVector<UIElementInstanceFn> m_vInstances;
static CUtlVector<UIElement *> m_vpElements; //Shutdown will use this.
static GameUI::RootPanel * m_pRootPanel;
static IGameUI * m_pGameUI;
public:
#ifdef GAMEUI_ALLOW_INIT //GameUI: Idiot-proof
static void InitializeGameUI(vgui::VPANEL parent);
static void ShutdownGameUI(void);
static bool LoadGameUI(void);
static void RegisterPanel(UIElementInstanceFn pfnInstance);
static void CreatePanels(void);
static void DeletePanels(void);
#endif
static IGameUI * GetGameUI(void);
static vgui::VPANEL GetVPanel(bool viewport = false);
static UIElement * GetElement(const char *elementName);
static void GetLocalizedString(const char *src, wchar_t *dst, size_t dst_len);
};
}
#endif
#include "cbase.h"
#include <vector>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#define GAMEUI_ALLOW_INIT //GameUI: Idiot-proof
#include <gameui.h>
#include <gameui_element.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
namespace gameui
{
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
UIElement::UIElement(Panel *parent, const char *name) : BaseClass(parent, name)
{ }
//-----------------------------------------------------------------------------
// Purpose: Load stuff from UIscheme
//-----------------------------------------------------------------------------
void UIElement::Init(void)
{
//Default scheme!
HScheme scheme = vgui::scheme()->LoadSchemeFromFile("resource/uischeme.res", "UIScheme");
SetScheme(scheme);
}
//-----------------------------------------------------------------------------
// Purpose: Clear data and prepare to deletion.
//-----------------------------------------------------------------------------
void UIElement::Shutdown(void)
{ }
//-----------------------------------------------------------------------------
// Purpose: Set panel's size, but relative from screen size (because proportional do not work for some reason)
//-----------------------------------------------------------------------------
void UIElement::SetSizeNormal(float w, float h)
{
int sw, sh;
surface()->GetScreenSize(sw, sh);
SetSize((int)(sw * w), (int)(sh * h));
}
//-----------------------------------------------------------------------------
// Purpose: Set panel's pos, but relative from screen size (because proportional do not work for some reason)
//-----------------------------------------------------------------------------
void UIElement::SetPosNormal(float x, float y)
{
int sw, sh;
surface()->GetScreenSize(sw, sh);
SetPos((int)(sw * x), (int)(sh * y));
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
UIElementHelper::UIElementHelper(UIElementInstanceFn pfnInstance)
{
GameUI::RegisterPanel(pfnInstance);
}
}
#ifndef GAMEUI_ELEMENT_H
#define GAMEUI_ELEMENT_H
#ifdef _WIN32
#pragma once
#endif
#include <vgui/VGUI.h>
#include "vgui_controls/Panel.h"
namespace gameui
{
//-----------------------------------------------------------------------------
// Purpose: The UI element
//-----------------------------------------------------------------------------
class UIElement : public vgui::Panel {
DECLARE_CLASS_SIMPLE(UIElement, vgui::Panel);
public:
UIElement(vgui::Panel *parent, const char *name);
virtual void Init(void);
virtual void Shutdown(void);
virtual void SetSizeNormal(float w, float h);
virtual void SetPosNormal(float x, float y);
};
using UIElementInstanceFn = UIElement *(*)(void);
//-----------------------------------------------------------------------------
// Purpose: Helper to allow programmer make classes UI elements
//-----------------------------------------------------------------------------
class UIElementHelper {
public:
UIElementHelper(UIElementInstanceFn pfnInstance);
};
//-----------------------------------------------------------------------------
// Purpose: Makes class visible to GameUI API
//-----------------------------------------------------------------------------
#define DECLARE_UIELEMENT(className) \
static UIElement * className##_Instance(void) \
{ \
return (UIElement *)(new className(NULL)); \
} \
static UIElementHelper s_##className##_Helper(className##_Instance)
//-----------------------------------------------------------------------------
// Purpose: Makes class visible to GameUI API, but with different constructor & default parent
//-----------------------------------------------------------------------------
#define DECLARE_UIELEMENT_VA_PARENT(className, ...) \
static UIElement * className##_Instance(void) \
{ \
return (UIElement *)(new className(NULL, __VA_ARGS__)); \
} \
static UIElementHelper s_##className##_Helper(className##_Instance)
//-----------------------------------------------------------------------------
// Purpose: Makes class visible to GameUI API, but with different constructor
//-----------------------------------------------------------------------------
#define DECLARE_UIELEMENT_VA(className, ...) \
static UIElement * className##_Instance(void) \
{ \
return (UIElement *)(new className(__VA_ARGS__)); \
} \
static UIElementHelper s_##className##_Helper(className##_Instance)
}
#endif
#include "cbase.h"
#include "filesystem.h"
#include <tier1/utlvector.h>
#include <vgui_controls/Frame.h>
#include <vgui_controls/AnimationController.h>
#include <vgui/ISurface.h>
#include <vgui/ILocalize.h>
#include <gameui.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace std;
using namespace vgui;
using namespace gameui;
//-----------------------------------------------------------------------------
// Purpose: Main menu panel, this actually draws menu you see in the game
//-----------------------------------------------------------------------------
class MenuPanel : public UIElement {
DECLARE_CLASS_SIMPLE(MenuPanel, UIElement);
class Button; //See below
public:
MenuPanel(vgui::Panel *parent);
~MenuPanel(void);
virtual void Init(void);
void Clear(void);
void OnThink(void);
void OnScreenSizeChanged(int oldWide, int oldTall);
private:
bool m_bWasInGame;
CUtlVector<MenuPanel::Button *> m_vpButtons;
};
DECLARE_UIELEMENT(MenuPanel);
//-----------------------------------------------------------------------------
// Purpose: Refresh menu panel.
//-----------------------------------------------------------------------------
CON_COMMAND(refui_refreshmenu, "GameUI: Refreshes game menu")
{
MenuPanel *panel = (MenuPanel *)GameUI::GetElement("GameUI_MenuPanel");
if(panel) {
panel->Init();
}
}
//-----------------------------------------------------------------------------
// Purpose: Standard menu button
//-----------------------------------------------------------------------------
class MenuPanel::Button : public UIElement {
DECLARE_CLASS_SIMPLE(Button, UIElement);
public:
Button(vgui::Panel *parent);
void SetText(const char *txt);
void SetCommand(const char *cmd);
void SetFont(const char *name);
void SetTextPosNormalized(float x, float y);
void SetTextPos(int x, int y);
void SetTextColor(Color color);
void SetAltTextColor(Color color);
void SetBackgroundColor(Color color);
void SetRolloverSound(const char *snd);
void SetClickSound(const char *snd);
void OnCursorEntered(void);
void OnMousePressed(vgui::MouseCode code);
void Paint(void);
void Init(void);
private:
char m_szText[256];
char m_szCommand[256];
char m_szRolloverSound[256];
char m_szClickSound[256];
vgui::HFont m_hFont;
Color m_TextColor;
Color m_AltTextColor;
Color m_BackgroundColor;
int m_iTextPosX, m_iTextPosY;
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
MenuPanel::Button::Button(Panel *parent) : BaseClass(parent, "GameUI_Button")
{
//We will draw it manually
SetPaintBackgroundEnabled(false);
SetPaintBorderEnabled(false);
SetTextPos(0, 0);
SetText("default");
SetFont("Default");
SetCommand("");
SetTextColor(Color(255, 255, 255, 255));
SetBackgroundColor(Color(0, 0, 0, 255));
SetBackgroundColor(Color(255, 127, 80, 255));
Init();
PerformLayout();
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetText(const char *txt)
{
Q_strncpy(m_szText, txt, 256);
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetCommand(const char *cmd)
{
Q_strncpy(m_szCommand, cmd, 256);
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetFont(const char *name)
{
vgui::IScheme *scheme = vgui::scheme()->GetIScheme(GetScheme());
m_hFont = scheme->GetFont(name);
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
// Sets position RELATIVE to panels' wide and tall
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetTextPosNormalized(float x, float y)
{
int w, h;
GetSize(w, h);
m_iTextPosX = x * w;
m_iTextPosY = y * h;
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetTextPos(int x, int y)
{
m_iTextPosX = x;
m_iTextPosY = y;
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetTextColor(Color color)
{
m_TextColor = color;
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetAltTextColor(Color color)
{
m_AltTextColor = color;
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetBackgroundColor(Color color)
{
m_BackgroundColor = color;
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetRolloverSound(const char *snd)
{
Q_strncpy(m_szRolloverSound, snd, 256);
}
//-----------------------------------------------------------------------------
// Purpose: Data accessor
//-----------------------------------------------------------------------------
void MenuPanel::Button::SetClickSound(const char *snd)
{
Q_strncpy(m_szClickSound, snd, 256);
}
//-----------------------------------------------------------------------------
// Purpose: Emit some sounds if cursor is over
//-----------------------------------------------------------------------------
void MenuPanel::Button::OnCursorEntered(void)
{
BaseClass::OnCursorEntered();
vgui::surface()->PlaySound(m_szRolloverSound); //"ui/button_rollover.wav"
}
//-----------------------------------------------------------------------------
// Purpose: Mouse click event
//-----------------------------------------------------------------------------
void MenuPanel::Button::OnMousePressed(vgui::MouseCode code)
{
BaseClass::OnMousePressed(code);
//If there is single click
if(code == vgui::MouseCode::MOUSE_FIRST) {
//Do sound
vgui::surface()->PlaySound(m_szClickSound); //"ui/button_click.wav"
IGameUI *ui = GameUI::GetGameUI();
if(ui) {
ui->SendMainMenuCommand(m_szCommand);
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Draw stuff
//-----------------------------------------------------------------------------
void MenuPanel::Button::Paint(void)
{
//Get size and pos
int w, h;
GetSize(w, h);
//draw background if needed
if(IsCursorOver()) {
vgui::surface()->DrawSetColor(m_BackgroundColor);
// vgui::surface()->DrawFilledRectFade(0, 0, w, h, backgroundColor.a(), 0, true);
vgui::surface()->DrawFilledRectFastFade(0, 0, w, h, 0, w, m_BackgroundColor.a(), 0, true);
}
//Get localized label
wchar_t value[1024];
GameUI::GetLocalizedString(m_szText, value, 1024);
//draw text
vgui::surface()->DrawSetTextFont(m_hFont);
vgui::surface()->DrawSetTextColor(IsCursorOver() ? m_AltTextColor : m_TextColor); //
vgui::surface()->DrawSetTextPos(m_iTextPosX, m_iTextPosY);
vgui::surface()->DrawPrintText(value, Q_wcslen(value));
}
//-----------------------------------------------------------------------------
// Purpose: Initialize some data
//-----------------------------------------------------------------------------
void MenuPanel::Button::Init(void)
{
BaseClass::Init();
vgui::IScheme *scheme = vgui::scheme()->GetIScheme(GetScheme());
const char *res_rollover = scheme->GetResourceString("Menu.ButtonRollover");
SetRolloverSound(FStrEq(res_rollover, "") ? "ui/button_rollover.wav" : res_rollover);
const char *res_click = scheme->GetResourceString("Menu.ButtonClick");
SetClickSound(FStrEq(res_click, "") ? "ui/button_click.wav" : res_click);
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
MenuPanel::MenuPanel(Panel *parent) : BaseClass(parent, "GameUI_MenuPanel")
{
SetProportional(true);
//This is invisible panel which controls stuff like menu
SetPaintBackgroundEnabled(false);
SetPaintBorderEnabled(false);
SetPaintEnabled(false);
m_bWasInGame = engine->IsInGame();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
MenuPanel::~MenuPanel(void)
{
Clear();
}
//-----------------------------------------------------------------------------
// Purpose: (Re-)Create menu buttons
//-----------------------------------------------------------------------------
void MenuPanel::Init(void)
{
BaseClass::Init();
Clear(); //Here this deletes all the panels
int newTall = 0;
vgui::IScheme *scheme = vgui::scheme()->GetIScheme(GetScheme());
//Panel sizes
int xpos = 0, ypos = 0;
int buttonWide = 0, buttonTall = 0;
//Get X position
const char *res_xpos = scheme->GetResourceString("Menu.XPos");
if(res_xpos && *res_xpos) {
xpos = atoi(res_xpos);
}
//Get Y position
const char *res_ypos = scheme->GetResourceString("Menu.YPos");
if(res_ypos && *res_ypos) {
ypos = atoi(res_ypos);
}
//Get panel width
const char *res_bwide = scheme->GetResourceString("Menu.ButtonWide");
if(res_bwide && *res_bwide) {
buttonWide = atoi(res_bwide);
}
//Get panel height
const char *res_btall = scheme->GetResourceString("Menu.ButtonTall");
if(res_btall && *res_btall) {
buttonTall = atoi(res_btall);
}
//Set sizes
SetPos(xpos, ypos);
SetWide(buttonWide);
//Load standard key values with menu layout
KeyValues *kv = new KeyValues("");
kv->LoadFromFile(filesystem, "resource/menulayout.res");
for(KeyValues *sub = kv->GetFirstTrueSubKey(); sub; sub = sub->GetNextTrueSubKey()) {
//Get label
const char *name = sub->GetName(); //Name = label!
//Will it be blank?
bool isBlank = sub->GetBool("blank", false);
//Will it show in the game or menu?
//NOTE: in_game 0 and in_menu 0 will result NOTHING
bool showInGame = sub->GetBool("in_game", true);
bool showInMenu = sub->GetBool("in_menu", true);
//Get color palette
Color textColor = scheme->GetColor(sub->GetString("text_color", "MenuText"), Color());
Color altTextColor = scheme->GetColor(sub->GetString("alt_text_color", "MenuAltText"), Color());
Color backgroundColor = scheme->GetColor(sub->GetString("background_color", "MenuBackground"), Color());
//Get string values
const char *command = sub->GetString("command", "");
const char *textFont = sub->GetString("text_font", "MenuLarge");
//Get color and pos
float textXnorm = sub->GetFloat("text_xnorm", 0.05);
float textYnorm = sub->GetFloat("text_ynorm", 0.50);
//Dont draw in the game (in_game is 0)
if(m_bWasInGame && !showInGame) {
continue;
}
//Dont draw in the menu (in_menu is 0)
if(!m_bWasInGame && !showInMenu) {
continue;
}
//If button is blank - just skip space between prev and next buttons
if(isBlank) {
newTall += buttonTall;
SetTall(newTall);
continue;
}
//Create new button
MenuPanel::Button *btn = new MenuPanel::Button(this);
//Set text data
btn->SetText(name);
btn->SetCommand(command);
btn->SetFont(textFont);
//Set colors
btn->SetTextColor(textColor);
btn->SetAltTextColor(altTextColor);
btn->SetBackgroundColor(backgroundColor);
//Set coords and size
btn->SetTextPosNormalized(textXnorm, textYnorm);
btn->SetSize(GetWide(), buttonTall);
btn->SetPos(0, newTall);
//Append height
newTall += buttonTall;
SetTall(newTall);
m_vpButtons.AddToTail(btn);
}
kv->deleteThis();
}
//-----------------------------------------------------------------------------
// Purpose: Call update if screen size has changed
//-----------------------------------------------------------------------------
void MenuPanel::OnScreenSizeChanged(int oldWide, int oldTall)
{
//Hey! i need update!
Init();
}
//-----------------------------------------------------------------------------
// Purpose: Check for updates
//-----------------------------------------------------------------------------
void MenuPanel::OnThink()
{
bool inGame = engine->IsInGame() && !engine->IsLevelMainMenuBackground();
//Hey! i need update!
if(inGame != m_bWasInGame) {
m_bWasInGame = inGame;
Init();
}
}
//-----------------------------------------------------------------------------
// Purpose: Deallocates all memory for panels
//-----------------------------------------------------------------------------
void MenuPanel::Clear(void)
{
for(int i = 0; i < m_vpButtons.Count(); i++) {
if(m_vpButtons[i]) {
m_vpButtons[i]->SetParent((Panel *)NULL);
delete m_vpButtons[i];
}
}
m_vpButtons.RemoveAll();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment