Most important events

From CEGUI Wiki - Crazy Eddie's Gui System for Games (Open Source)

Jump to: navigation, search

Written for CEGUI 0.5


Works with versions 0.5.x (obsolete)

Written for CEGUI 0.6


Works with versions 0.6.x (obsolete)

This article is going to show you the most important and useful events of each widget.

Contents

Getting started

I'm only going to change the initialiseSample function for each widget + adding some others.

EventGalore.h

#ifndef _EventGalore_h_
#define _EventGalore_h_
 
#include "CEGuiSample.h"
#include "CEGUI.h"
 
class EventGalore : public CEGuiSample
{
public:
   bool initialiseSample()
   {
       try
       {
		using namespace CEGUI;
 
		WindowManager& winMgr = WindowManager::getSingleton();
 
		// Load the TaharezLook scheme and set up the default mouse cursor and font
		SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
		System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
		if(!FontManager::getSingleton().isFontPresent("Commonwealth-10"))
			FontManager::getSingleton().createFont("Commonwealth-10.font");
 
		// Set the GUI Sheet
		Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd");
		System::getSingleton().setGUISheet(sheet);
 
		// Place here the code from the widgets, place functions below this function.
 
	}
	catch(CEGUI::Exception &e)
	{
		#if defined( __WIN32__ ) || defined( _WIN32 )
			MessageBox(NULL, e.getMessage().c_str(), "Error initializing the demo", MB_OK | MB_ICONERROR | MB_TASKMODAL);
		#else
			std::cerr << "Error initializing the demo:" << e.getMessage().c_str() << "\n";
		#endif
	}
	return true;
   }
   void cleanupSample(void)
   {
   }
};
#endif // _EventGalore_h_

main.cpp

#if defined( __WIN32__ ) || defined( _WIN32 )
	#define WIN32_LEAN_AND_MEAN
	#define NOMINMAX
	#include "windows.h"
#endif
 
#include "EventGalore.h"
 
 
#if defined( __WIN32__ ) || defined( _WIN32 )
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
#else
int main(int argc, char *argv[])
#endif
{
    EventGalore app;
    return app.run();
}

Widgets

Window

The Window widget is a important baseclass, all widgets inherit this class. It has lots 'n lots of events, where a few are introduced by other widgets, and where many are unimportant. If a event is taken from this Window class, it is mentioned.

PushButton

The button is a simple, expected widget. It contains only one important event, but we are going to add a few more.

PushButton * pushButton = static_cast<PushButton*>(winMgr.createWindow("TaharezLook/Button", "Button1"));
sheet->addChildWindow(pb);
pushButton->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
pushButton->setSize(UVector2(cegui_reldim(0.2f), cegui_reldim(0.08f)));
pushButton->setText("Hey! Come here!");
 
pushButton->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&EventGalore::onPushButtonClicked, this));
pushButton->subscribeEvent(PushButton::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
pushButton->subscribeEvent(PushButton::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
bool onPushButtonClicked(const CEGUI::EventArgs &e)
{
	// Our button has been clicked!
	CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingleton().getWindow("Button1"));
	pushButton->setText("We got clicked!");
	return true;
}
 
bool onMouseEnters(const CEGUI::EventArgs &e)
{
	// Mouse has entered the button. (Hover)
	CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingleton().getWindow("Button1"));
	pushButton->setText("Now click!");
	return true;
}
 
bool onMouseLeaves(const CEGUI::EventArgs &e)
{
	// Mouse has left the button.
	CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingleton().getWindow("Button1"));
	// Back to its original state!
	pushButton->setText("Hey! Come here!");
	return true;
}

Listbox

The listbox is a very useful listing widget. Only has one important event.

Listbox * listBox = static_cast<Listbox*>(winMgr.createWindow("TaharezLook/Listbox", "Listbox1"));
sheet->addChildWindow(listBox);
 
listBox->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
listBox->setSize(UVector2(cegui_reldim(0.4f), cegui_reldim(0.4f)));
 
ListboxTextItem * listBoxItem = new ListboxTextItem("Our very first item.", 1);
listBoxItem->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
listBox->addItem(listBoxItem);
 
listBoxItem = new ListboxTextItem("Our second item.", 2);
listBoxItem->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
listBox->addItem(listBoxItem);
 
listBox->subscribeEvent(Listbox::EventSelectionChanged, Event::Subscriber(&EventGalore::onSelectionChanged, this));
bool onSelectionChanged(const CEGUI::EventArgs &e)
{
        // The selection has changed.
	CEGUI::Listbox * listBox = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingleton().getWindow("Listbox1"));
	// Get the item we selected
	CEGUI::ListboxItem * selectedItem = listBox->getFirstSelectedItem();
	selectedItem->setText("Oh we got selected!");
 
	return true;
}

Checkbox

A checkable item useful for boolean(true or false) 'questions'.

Checkbox * checkBox = static_cast<Checkbox*>(winMgr.createWindow("TaharezLook/Checkbox", "Checkbox1"));
sheet->addChildWindow(checkBox);
checkBox->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
checkBox->setSize(UVector2(cegui_reldim(0.4f), cegui_reldim(0.1f)));
 
// A question where people can only answer yes or no (true or false)
checkBox->setText("Hey! Do you want to be rich?");
 
checkBox->subscribeEvent(Checkbox::EventCheckStateChanged, Event::Subscriber(&EventGalore::onCheckStateChanged, this));
checkBox->subscribeEvent(Checkbox::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
checkBox->subscribeEvent(Checkbox::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
bool onCheckStateChanged(const CEGUI::EventArgs &e)
{
	// Our item has been checked or unchecked, update our item accordingly.
	updateCheckbox();
	return true;
}
 
bool onMouseEnters(const CEGUI::EventArgs &e)
{
	// The mouse has entered, update the checkbox accordingly.
	updateCheckbox();		
	return true;
}
 
bool onMouseLeaves(const CEGUI::EventArgs &e)
{
	CEGUI::Checkbox * checkBox = static_cast<CEGUI::Checkbox*>(CEGUI::WindowManager::getSingleton().getWindow("Checkbox1"));
	// Reset
	checkBox->setText("Hey! Do you want to be rich?");
	return true;
}
 
void updateCheckbox()
{
	CEGUI::Checkbox * checkBox = static_cast<CEGUI::Checkbox*>(CEGUI::WindowManager::getSingleton().getWindow("Checkbox1"));
 
	if (checkBox->isSelected())
	{
		// Our checkbox is selected, so someone has previously said 'yes'.
		checkBox->setText("Click to choose no!");
	}
	else
	{
		// Our item is not selected, so someone hasn't done anything yet, or it has been previously
		// unchecked.
		checkBox->setText("Click to choose yes!");
	}
}

This might confuse at first sight. Let me go through this here, in steps.

RadioButton

Similar to Checkbox, only it can be grouped, and once selected, it cannot be unselected (except when another button has been selected). Note, in this case, both functions are called when you click the unselected radiobutton. This is because one is unselected and one is selected.

RadioButton * radioButton = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "RadioButton1"));
sheet->addChildWindow(radioButton);
 
radioButton->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
radioButton->setSize(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
radioButton->setText("Yes");
radioButton->setGroupID(0);
 
radioButton->subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&EventGalore::onButton1SelectChanged, this));
// To make sure the event is fired.
radioButton->setSelected(true);
 
 
radioButton = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "RadioButton2"));
sheet->addChildWindow(radioButton);
 
radioButton->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.2f)));
radioButton->setSize(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
radioButton->setText("No");
radioButton->setGroupID(0);
 
radioButton->subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&EventGalore::onButton2SelectChanged, this));
bool onButton1SelectChanged(const CEGUI::EventArgs &e)
{
	CEGUI::RadioButton * radioButton1 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingleton().getWindow("RadioButton1"));
 
	if (radioButton1->isSelected())
	{
		// This one just got selected.
		radioButton1->setText("Ok then. Yes it is.");
	}
	else
	{
		// This one got unselected. Reset it.
		radioButton1->setText("Yes");
	}
	return true;
}
 
bool onButton2SelectChanged(const CEGUI::EventArgs &e)
{
	// We have chosen no.
	CEGUI::RadioButton * radioButton2 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingleton().getWindow("RadioButton2"));
 
	if (radioButton2->isSelected())
	{
		// This one just got selected.
		radioButton2->setText("Ok then. No it is.");
	}
	else
	{
		// This one got unselected. Reset it.
		radioButton2->setText("No");
	}
	return true;
}

Editbox

A simple editable box where text can be entered.

Editbox * editBox = static_cast<Editbox*>(winMgr.createWindow("TaharezLook/Editbox", "Editbox1"));
sheet->addChildWindow(editBox);
 
editBox->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
editBox->setSize(UVector2(cegui_reldim(0.4f), cegui_reldim(0.1f)));
editBox->setText("Edit me!");
 
editBox->subscribeEvent(Editbox::EventTextAccepted, Event::Subscriber(&EventGalore::onTextAccepted, this));
bool onTextAccepted(const CEGUI::EventArgs &e)
{
	// Our text has been accepted by either deactivating it or pressing tab or enter.
	CEGUI::Editbox * editBox = static_cast<CEGUI::Editbox*>(CEGUI::WindowManager::getSingleton().getWindow("Editbox1"));
	// So we got the text here, do something with it. Lets just..put the text back in reverse order.
	CEGUI::String currentText = editBox->getText();
 
	std::string finalString;
 
	CEGUI::String::reverse_iterator ppkNode = currentText.rbegin();
	CEGUI::String::reverse_iterator ppkEnd = currentText.rend();
 
	for (; ppkNode != ppkEnd; ++ppkNode)
	{
		finalString.push_back((*ppkNode));
	}
	editBox->setText(finalString);
	return true;
}

MultiLineEditbox

This widget is pretty much the same as editbox, but allows more lines.

This widget doesn't have the event TextAccepted, so you'll need to 'apply' the input by a button for example.

MultiLineEditbox * multiLineEditbox = static_cast<MultiLineEditbox*>(winMgr.createWindow("TaharezLook/MultiLineEditbox", "MultiEditbox1"));
sheet->addChildWindow(multiLineEditbox);
multiLineEditbox->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
multiLineEditbox->setSize(UVector2(cegui_reldim(0.3f), cegui_reldim(0.3f)));
multiLineEditbox->setText("Now edit me!");
 
multiLineEditbox->subscribeEvent(MultiLineEditbox::EventTextChanged, Event::Subscriber(&EventGalore::onTextChanged, this));
 
// This text is going to show the result.
Window * textWindow = winMgr.createWindow("TaharezLook/StaticText", "StaticText1");
sheet->addChildWindow(textWindow);
textWindow->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.5f)));
textWindow->setSize(UVector2(cegui_reldim(0.6f), cegui_reldim(0.1f)));
textWindow->setText("Now edit me!");
bool onTextChanged(const CEGUI::EventArgs &e)
{
	CEGUI::WindowManager * winMgr = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::MultiLineEditbox * multiLineEditbox = static_cast<CEGUI::MultiLineEditbox*>(winMgr->getWindow("MultiEditbox1"));
	CEGUI::Window * textWindow = winMgr->getWindow("StaticText1");
 
	textWindow->setText(multiLineEditbox->getText());
	return true;
}

Notice what happens when you enter text on another line though :)

FrameWindow

This is a window where you usually put your widgets on.

FrameWindow * frameWindow = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "FrameWindow1"));
sheet->addChildWindow(frameWindow)
frameWindow->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
frameWindow->setSize(UVector2(cegui_reldim(0.4f), cegui_reldim(0.4f)));
frameWindow->setText("Our window");
 
frameWindow->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventGalore::onCloseClicked, this));
frameWindow->subscribeEvent(FrameWindow::EventRollupToggled, Event::Subscriber(&EventGalore::onRollupToggled, this));
bool onCloseClicked(const CEGUI::EventArgs &e)
{
	// Someone has clicked the upper right 'X'.
 
	// We don't HAVE to cast to FrameWindow, as the method we are going to use to close the window
	// is available in Window, but for the sake of clarity...
	CEGUI::FrameWindow * frameWindow = static_cast<CEGUI::FrameWindow*>(CEGUI::WindowManager::getSingleton().getWindow("FrameWindow1"));
	frameWindow->destroy();
	// Note: Dont try to delete the pointer.
	return true;
}
 
bool onRollupToggled(const CEGUI::EventArgs &e)
{
	CEGUI::FrameWindow * frameWindow = static_cast<CEGUI::FrameWindow*>(CEGUI::WindowManager::getSingleton().getWindow("FrameWindow1"));
 
	if (frameWindow->isRolledup())
	{
		frameWindow->setText("Rolled up");
	}
	else
	{
		frameWindow->setText("Our window");
	}
	return true;
}

ProgressBar

A progress widget.

ProgressBar * progressBar = static_cast<ProgressBar*>(winMgr.createWindow("TaharezLook/ProgressBar", "ProgressBar1"));
sheet->addChildWindow(progressBar);
progressBar->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
progressBar->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim(0.04f)));
 
progressBar->subscribeEvent(ProgressBar::EventProgressDone, Event::Subscriber(&EventGalore::onProgressDone, this));
progressBar->subscribeEvent(ProgressBar::EventProgressChanged, Event::Subscriber(&EventGalore::onProgressChanged, this));
progressBar->subscribeEvent(ProgressBar::EventMouseClick, Event::Subscriber(&EventGalore::onMouseClick, this));
 
Window * resultWindow = winMgr.createWindow("TaharezLook/StaticText", "StaticText1");
sheet->addChildWindow(resultWindow);
resultWindow->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.05f)));
resultWindow->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim(0.06f)));
resultWindow->setText("Lets start progressing!");
resultWindow->setProperty("FrameEnabled", "false");
resultWindow->setProperty("BackgroundEnabled", "false");
bool onProgressDone(const CEGUI::EventArgs &e)
{
	// The progress bar is full.
	CEGUI::Window * resultWindow = CEGUI::WindowManager::getSingleton().getWindow("StaticText1");
 
	resultWindow->setText("We are done!");
	return true;
}
 
bool onProgressChanged(const CEGUI::EventArgs &e)
{
	// The progress changed.
	CEGUI::WindowManager * winMgr = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::ProgressBar * progressBar = static_cast<CEGUI::ProgressBar*>(winMgr->getWindow("ProgressBar1"));
	CEGUI::Window * resultWindow = winMgr->getWindow("StaticText1");
 
	float progress = progressBar->getProgress();
	int finalProgress = static_cast<int>(progress*100);
	std::stringstream resultStr;
	resultStr << "Progress: " << finalProgress << "%";
 
	resultWindow->setText(resultStr.str());
	return true;
}
 
bool onMouseClick(const CEGUI::EventArgs &e)
{
	CEGUI::ProgressBar * progressBar = static_cast<CEGUI::ProgressBar*>(CEGUI::WindowManager::getSingleton().getWindow("ProgressBar1"));
	progressBar->setProgress(progressBar->getProgress() + 0.05f);
	return true;
}

Slider

A widget which accepts input though moving a 'thumb' between a range.

Slider * slider = static_cast<Slider*>(winMgr.createWindow("TaharezLook/Slider", "Slider1"));
sheet->addChildWindow(slider);
slider->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
slider->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim(0.04f)));
slider->setMaxValue(1.0f);
slider->setClickStep(0.1f);
slider->setCurrentValue(0.5f); // Start half way
 
slider->subscribeEvent(Slider::EventValueChanged, Event::Subscriber(&EventGalore::onSliderValueChanged, this));
slider->subscribeEvent(Slider::EventProgressChanged, Event::Subscriber(&EventGalore::onSliderChangeEnded, this));
bool onSliderValueChanged(const CEGUI::EventArgs &e)
{
	// The slider moved.
	CEGUI::WindowManager * winMgr = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::Slider * slider = static_cast<CEGUI::Slider*>(winMgr->getWindow("Slider1"));
	CEGUI::Window * resultWindow = winMgr->getWindow("StaticText1");
 
        std::stringstream resultStr;
	resultStr << "Moved to: " << slider->getCurrentValue();
 
	resultWindow->setText(resultStr);
	return true;
}
 
bool onSliderChangeEnded(const CEGUI::EventArgs &e)
{
	// The slider stopped moving (user released mouse)
	CEGUI::WindowManager * winMgr = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::Slider * slider = static_cast<CEGUI::Slider*>(winMgr->getWindow("Slider1"));
	CEGUI::Window * resultWindow = winMgr->getWindow("StaticText1");
 
        std::stringstream resultStr;
	resultStr << "Moving stopped at: " << slider->getCurrentValue();
 
	resultWindow->setText(resultStr);
	return true;
}

Spinner

MultiColumnList

Personal tools
Namespaces
Variants
Actions
Main Menu
Community
Documentation
Toolbox