User talk:DaFox

From Valve Developer Community
Jump to: navigation, search

Custom VGUI Panel

tTSzW.jpg

OPEN \game\client\vgui_int.cpp


FIND

void MP3Player_Destroy();


AFTER, ADD

void Blog_Destroy();


FIND

#ifndef _X360
	MP3Player_Destroy();
#endif


AFTER, ADD

	Blog_Destroy();


CREATE AND OPEN \game\client\sdk\blog.h (Add it to your Client Project in VS as well!)


ADD

#include "vgui_helpers.h"

using namespace vgui;

class CBlog : public Frame {
	DECLARE_CLASS_SIMPLE(CBlog, Frame);

public:
	CBlog(VPANEL parent);
	~CBlog();

protected:
	void OnCommand(char const *cmd);

};


CREATE AND OPEN \game\client\sdk\blog.cpp


ADD

#include "cbase.h"
#include "ienginevgui.h"
#include "sdk_blog.h"

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

using namespace vgui;

static CBlog *g_pBlog = NULL;

static void blog_f() {
	if(!g_pBlog) {
		g_pBlog = new CBlog(enginevgui->GetPanel(PANEL_GAMEUIDLL));
	}
	else {
		g_pBlog->Activate();
	}
}

static ConCommand blog("blog", blog_f, "Show/hide the blog."); // Is this the appropriate method to have a command to open something?

CBlog::CBlog(VPANEL parent) : BaseClass(NULL, "Blog") {
	SetParent(parent);

	SetScheme(scheme()->LoadSchemeFromFile("resource/SourceScheme.res", "SourceScheme"));
	LoadControlSettings("resource/ui/blog.res");

	SetMoveable(true); //todo: which of these default to their current values?
	SetSizeable(false);
	SetProportional(false);
	SetMenuButtonVisible(false);
	SetMinimizeButtonVisible(false);
	SetMaximizeButtonVisible(false);
	SetCloseButtonVisible(true);
	SetVisible(true);

	MoveToCenterOfScreen();
	Activate();

	Label *bloglabel = dynamic_cast<Label *>(FindChildByName("BlogLabel"));

	if(bloglabel) {
		bloglabel->SetText("This \nIs \na \nwhole \nlot \nof \ntext.\n\nLorem ipsum");
	}
}

CBlog::~CBlog() {
	g_pBlog = NULL;
}

void CBlog::OnCommand(char const *cmd) {
	if (!Q_stricmp(cmd, "Close")) {
		if (g_pBlog) { //todo: Can we safely assume that this will always be true in this function?
			g_pBlog->SetParent((Panel *)NULL); //What is the point of this?
			//delete g_pBlog; // Some vgui examples use delete rather than MarkForDeletion(), what is preferable?
			g_pBlog->MarkForDeletion();
		}
	}
	else {
		BaseClass::OnCommand(cmd);
	}
}

void Blog_Destroy() { //This is pretty nasty, It is called in vgui_int in VGui_Shutdown(), Is there a way to Destroy the panel on shutdown without editing vgui_int.cpp?
	if (g_pBlog) {
		g_pBlog->SetParent((Panel *)NULL);
		g_pBlog->MarkForDeletion();
	}
}