Difference between revisions of "Most important events"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
m (Bot: Automated text replacement (-<code>[:space:]?<cpp/>(.*?)</code> +<syntaxhighlight lang="cpp">\1</syntaxhighlight>))
m (because references rule)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This article is going to show you the most important and usefull events of each widget.
+
{{VersionBadge|0.5}} {{VersionBadge|0.6}}
  
===Getting started===
+
This article is going to show you the most important and useful events of each widget.
 +
 
 +
=== Getting started ===
 
I'm only going to change the initialiseSample function for each widget + adding some others.
 
I'm only going to change the initialiseSample function for each widget + adding some others.
====EventGalore.h====
+
==== EventGalore.h ====
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
#ifndef _EventGalore_h_
 
#ifndef _EventGalore_h_
 
#define _EventGalore_h_
 
#define _EventGalore_h_
Line 50: Line 52:
 
};
 
};
 
#endif // _EventGalore_h_
 
#endif // _EventGalore_h_
</syntaxhighlight>
+
</source>
  
====main.cpp====
+
==== main.cpp ====
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
#if defined( __WIN32__ ) || defined( _WIN32 )
 
#if defined( __WIN32__ ) || defined( _WIN32 )
 
#define WIN32_LEAN_AND_MEAN
 
#define WIN32_LEAN_AND_MEAN
Line 72: Line 74:
 
     return app.run();
 
     return app.run();
 
}
 
}
</syntaxhighlight>
+
</source>
  
===Widgets===
+
=== Widgets ===
  
====Window====
+
==== 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.
 
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.
Line 84: Line 86:
 
* EventDragDropItemDropped - Fired when the item has been dropped on the window.
 
* EventDragDropItemDropped - Fired when the item has been dropped on the window.
  
====PushButton====
+
==== PushButton ====
  
 
The button is a simple, expected widget. It contains only one important event, but we are going to add a few more.
 
The button is a simple, expected widget. It contains only one important event, but we are going to add a few more.
Line 92: Line 94:
 
* EventMouseLeaves - Fired when the mouse leaves the widget. Inherited from Window.
 
* EventMouseLeaves - Fired when the mouse leaves the widget. Inherited from Window.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
PushButton * pushButton = static_cast<PushButton*>(winMgr.createWindow("TaharezLook/Button", "Button1"));
 
PushButton * pushButton = static_cast<PushButton*>(winMgr.createWindow("TaharezLook/Button", "Button1"));
 
sheet->addChildWindow(pb);
 
sheet->addChildWindow(pb);
Line 102: Line 104:
 
pushButton->subscribeEvent(PushButton::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
 
pushButton->subscribeEvent(PushButton::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
 
pushButton->subscribeEvent(PushButton::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
 
pushButton->subscribeEvent(PushButton::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onPushButtonClicked(const CEGUI::EventArgs &e)
 
bool onPushButtonClicked(const CEGUI::EventArgs &e)
 
{
 
{
 
// Our button has been clicked!
 
// Our button has been clicked!
CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Button1"));
+
CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingleton().getWindow("Button1"));
 
pushButton->setText("We got clicked!");
 
pushButton->setText("We got clicked!");
 
return true;
 
return true;
Line 115: Line 117:
 
{
 
{
 
// Mouse has entered the button. (Hover)
 
// Mouse has entered the button. (Hover)
CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Button1"));
+
CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingleton().getWindow("Button1"));
 
pushButton->setText("Now click!");
 
pushButton->setText("Now click!");
 
return true;
 
return true;
Line 123: Line 125:
 
{
 
{
 
// Mouse has left the button.
 
// Mouse has left the button.
CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Button1"));
+
CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingleton().getWindow("Button1"));
 
// Back to its original state!
 
// Back to its original state!
 
pushButton->setText("Hey! Come here!");
 
pushButton->setText("Hey! Come here!");
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
  
====Listbox====
+
==== Listbox ====
  
The listbox is a very usefull listing widget. Only has one important event.
+
The listbox is a very useful listing widget. Only has one important event.
  
 
* EventSelectionChanged - Fired when a or another item has been selected.
 
* EventSelectionChanged - Fired when a or another item has been selected.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
Listbox * listBox = static_cast<Listbox*>(winMgr.createWindow("TaharezLook/Listbox", "Listbox1"));
 
Listbox * listBox = static_cast<Listbox*>(winMgr.createWindow("TaharezLook/Listbox", "Listbox1"));
 
sheet->addChildWindow(listBox);
 
sheet->addChildWindow(listBox);
Line 152: Line 154:
  
 
listBox->subscribeEvent(Listbox::EventSelectionChanged, Event::Subscriber(&EventGalore::onSelectionChanged, this));
 
listBox->subscribeEvent(Listbox::EventSelectionChanged, Event::Subscriber(&EventGalore::onSelectionChanged, this));
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onSelectionChanged(const CEGUI::EventArgs &e)
 
bool onSelectionChanged(const CEGUI::EventArgs &e)
 
{
 
{
 
         // The selection has changed.
 
         // The selection has changed.
CEGUI::Listbox * listBox = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Listbox1"));
+
CEGUI::Listbox * listBox = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingleton().getWindow("Listbox1"));
 
// Get the item we selected
 
// Get the item we selected
 
CEGUI::ListboxItem * selectedItem = listBox->getFirstSelectedItem();
 
CEGUI::ListboxItem * selectedItem = listBox->getFirstSelectedItem();
Line 164: Line 166:
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
  
====Checkbox====
+
==== Checkbox ====
  
 
A checkable item useful for boolean(true or false) 'questions'.
 
A checkable item useful for boolean(true or false) 'questions'.
Line 174: Line 176:
 
* EventMouseLeaves - Fired when the mouse moves out of the checkbox. Inherited from Window.
 
* EventMouseLeaves - Fired when the mouse moves out of the checkbox. Inherited from Window.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
Checkbox * checkBox = static_cast<Checkbox*>(winMgr.createWindow("TaharezLook/Checkbox", "Checkbox1"));
 
Checkbox * checkBox = static_cast<Checkbox*>(winMgr.createWindow("TaharezLook/Checkbox", "Checkbox1"));
 
sheet->addChildWindow(checkBox);
 
sheet->addChildWindow(checkBox);
Line 186: Line 188:
 
checkBox->subscribeEvent(Checkbox::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
 
checkBox->subscribeEvent(Checkbox::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
 
checkBox->subscribeEvent(Checkbox::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
 
checkBox->subscribeEvent(Checkbox::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onCheckStateChanged(const CEGUI::EventArgs &e)
 
bool onCheckStateChanged(const CEGUI::EventArgs &e)
 
{
 
{
Line 204: Line 206:
 
bool onMouseLeaves(const CEGUI::EventArgs &e)
 
bool onMouseLeaves(const CEGUI::EventArgs &e)
 
{
 
{
CEGUI::Checkbox * checkBox = static_cast<CEGUI::Checkbox*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Checkbox1"));
+
CEGUI::Checkbox * checkBox = static_cast<CEGUI::Checkbox*>(CEGUI::WindowManager::getSingleton().getWindow("Checkbox1"));
 
// Reset
 
// Reset
 
checkBox->setText("Hey! Do you want to be rich?");
 
checkBox->setText("Hey! Do you want to be rich?");
Line 212: Line 214:
 
void updateCheckbox()
 
void updateCheckbox()
 
{
 
{
CEGUI::Checkbox * checkBox = static_cast<CEGUI::Checkbox*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Checkbox1"));
+
CEGUI::Checkbox * checkBox = static_cast<CEGUI::Checkbox*>(CEGUI::WindowManager::getSingleton().getWindow("Checkbox1"));
  
 
if (checkBox->isSelected())
 
if (checkBox->isSelected())
Line 222: Line 224:
 
{
 
{
 
// Our item is not selected, so someone hasn't done anything yet, or it has been previously
 
// Our item is not selected, so someone hasn't done anything yet, or it has been previously
// unchecked.
+
// unchecked.
 
checkBox->setText("Click to choose yes!");
 
checkBox->setText("Click to choose yes!");
 
}
 
}
 
}
 
}
</syntaxhighlight>
+
</source>
 
This might confuse at first sight. Let me go through this here, in steps.
 
This might confuse at first sight. Let me go through this here, in steps.
 
* First, our checkbox is created and our mouse remains still.
 
* First, our checkbox is created and our mouse remains still.
Line 233: Line 235:
 
* Fourth, we move our mouse away from the checkbox. The onMouseLeaves function is called, where we reset the text to our question.
 
* Fourth, we move our mouse away from the checkbox. The onMouseLeaves function is called, where we reset the text to our question.
  
====RadioButton====
+
==== 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.
 
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.
Line 239: Line 241:
 
* EventSelectStateChanged - Fired when the radiobutton has been selected.
 
* EventSelectStateChanged - Fired when the radiobutton has been selected.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
RadioButton * radioButton = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "RadioButton1"));
 
RadioButton * radioButton = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "RadioButton1"));
 
sheet->addChildWindow(radioButton);
 
sheet->addChildWindow(radioButton);
Line 262: Line 264:
  
 
radioButton->subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&EventGalore::onButton2SelectChanged, this));
 
radioButton->subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&EventGalore::onButton2SelectChanged, this));
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onButton1SelectChanged(const CEGUI::EventArgs &e)
 
bool onButton1SelectChanged(const CEGUI::EventArgs &e)
 
{
 
{
CEGUI::RadioButton * radioButton1 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("RadioButton1"));
+
CEGUI::RadioButton * radioButton1 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingleton().getWindow("RadioButton1"));
  
 
if (radioButton1->isSelected())
 
if (radioButton1->isSelected())
Line 284: Line 286:
 
{
 
{
 
// We have chosen no.
 
// We have chosen no.
CEGUI::RadioButton * radioButton2 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("RadioButton2"));
+
CEGUI::RadioButton * radioButton2 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingleton().getWindow("RadioButton2"));
  
 
if (radioButton2->isSelected())
 
if (radioButton2->isSelected())
Line 298: Line 300:
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
  
====Editbox====
+
==== Editbox ====
  
 
A simple editable box where text can be entered.
 
A simple editable box where text can be entered.
Line 306: Line 308:
 
* EventTextAccepted - Fired when someone has pressed TAB or RETURN, or when someone has clicked another window.
 
* EventTextAccepted - Fired when someone has pressed TAB or RETURN, or when someone has clicked another window.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
Editbox * editBox = static_cast<Editbox*>(winMgr.createWindow("TaharezLook/Editbox", "Editbox1"));
 
Editbox * editBox = static_cast<Editbox*>(winMgr.createWindow("TaharezLook/Editbox", "Editbox1"));
 
sheet->addChildWindow(editBox);
 
sheet->addChildWindow(editBox);
Line 315: Line 317:
  
 
editBox->subscribeEvent(Editbox::EventTextAccepted, Event::Subscriber(&EventGalore::onTextAccepted, this));
 
editBox->subscribeEvent(Editbox::EventTextAccepted, Event::Subscriber(&EventGalore::onTextAccepted, this));
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onTextAccepted(const CEGUI::EventArgs &e)
 
bool onTextAccepted(const CEGUI::EventArgs &e)
 
{
 
{
 
// Our text has been accepted by either deactivating it or pressing tab or enter.
 
// Our text has been accepted by either deactivating it or pressing tab or enter.
CEGUI::Editbox * editBox = static_cast<CEGUI::Editbox*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Editbox1"));
+
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.
 
// So we got the text here, do something with it. Lets just..put the text back in reverse order.
 
CEGUI::String currentText = editBox->getText();
 
CEGUI::String currentText = editBox->getText();
Line 336: Line 338:
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
  
====MultiLineEditbox====
+
==== MultiLineEditbox ====
  
 
This widget is pretty much the same as editbox, but allows more lines.
 
This widget is pretty much the same as editbox, but allows more lines.
Line 346: Line 348:
 
This widget doesn't have the event TextAccepted, so you'll need to 'apply' the input by a button for example.
 
This widget doesn't have the event TextAccepted, so you'll need to 'apply' the input by a button for example.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
MultiLineEditbox * multiLineEditbox = static_cast<MultiLineEditbox*>(winMgr.createWindow("TaharezLook/MultiLineEditbox", "MultiEditbox1"));
 
MultiLineEditbox * multiLineEditbox = static_cast<MultiLineEditbox*>(winMgr.createWindow("TaharezLook/MultiLineEditbox", "MultiEditbox1"));
 
sheet->addChildWindow(multiLineEditbox);
 
sheet->addChildWindow(multiLineEditbox);
Line 361: Line 363:
 
textWindow->setSize(UVector2(cegui_reldim(0.6f), cegui_reldim(0.1f)));
 
textWindow->setSize(UVector2(cegui_reldim(0.6f), cegui_reldim(0.1f)));
 
textWindow->setText("Now edit me!");
 
textWindow->setText("Now edit me!");
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onTextChanged(const CEGUI::EventArgs &e)
 
bool onTextChanged(const CEGUI::EventArgs &e)
 
{
 
{
Line 372: Line 374:
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
 
Notice what happens when you enter text on another line though :)
 
Notice what happens when you enter text on another line though :)
  
====FrameWindow====
+
==== FrameWindow ====
  
 
This is a window where you usually put your widgets on.
 
This is a window where you usually put your widgets on.
Line 382: Line 384:
 
* EventRollupToggled - Fired when the window gets rolled up, or rolled down.
 
* EventRollupToggled - Fired when the window gets rolled up, or rolled down.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
FrameWindow * frameWindow = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "FrameWindow1"));
 
FrameWindow * frameWindow = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "FrameWindow1"));
 
sheet->addChildWindow(frameWindow)
 
sheet->addChildWindow(frameWindow)
Line 391: Line 393:
 
frameWindow->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventGalore::onCloseClicked, this));
 
frameWindow->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventGalore::onCloseClicked, this));
 
frameWindow->subscribeEvent(FrameWindow::EventRollupToggled, Event::Subscriber(&EventGalore::onRollupToggled, this));
 
frameWindow->subscribeEvent(FrameWindow::EventRollupToggled, Event::Subscriber(&EventGalore::onRollupToggled, this));
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onCloseClicked(const CEGUI::EventArgs &e)
 
bool onCloseClicked(const CEGUI::EventArgs &e)
 
{
 
{
Line 398: Line 400:
  
 
// We don't HAVE to cast to FrameWindow, as the method we are going to use to close the window
 
// 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...
+
// is available in Window, but for the sake of clarity...
CEGUI::FrameWindow * frameWindow = static_cast<CEGUI::FrameWindow*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("FrameWindow1"));
+
CEGUI::FrameWindow * frameWindow = static_cast<CEGUI::FrameWindow*>(CEGUI::WindowManager::getSingleton().getWindow("FrameWindow1"));
 
frameWindow->destroy();
 
frameWindow->destroy();
 
// Note: Dont try to delete the pointer.
 
// Note: Dont try to delete the pointer.
Line 407: Line 409:
 
bool onRollupToggled(const CEGUI::EventArgs &e)
 
bool onRollupToggled(const CEGUI::EventArgs &e)
 
{
 
{
CEGUI::FrameWindow * frameWindow = static_cast<CEGUI::FrameWindow*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("FrameWindow1"));
+
CEGUI::FrameWindow * frameWindow = static_cast<CEGUI::FrameWindow*>(CEGUI::WindowManager::getSingleton().getWindow("FrameWindow1"));
  
 
if (frameWindow->isRolledup())
 
if (frameWindow->isRolledup())
Line 419: Line 421:
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
  
====ProgressBar====
+
==== ProgressBar ====
  
 
A progress widget.
 
A progress widget.
Line 429: Line 431:
 
* EventMouseClick - Fired when someone clicked on the window. Inherited from Window.
 
* EventMouseClick - Fired when someone clicked on the window. Inherited from Window.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
ProgressBar * progressBar = static_cast<ProgressBar*>(winMgr.createWindow("TaharezLook/ProgressBar", "ProgressBar1"));
 
ProgressBar * progressBar = static_cast<ProgressBar*>(winMgr.createWindow("TaharezLook/ProgressBar", "ProgressBar1"));
 
sheet->addChildWindow(progressBar);
 
sheet->addChildWindow(progressBar);
Line 446: Line 448:
 
resultWindow->setProperty("FrameEnabled", "false");
 
resultWindow->setProperty("FrameEnabled", "false");
 
resultWindow->setProperty("BackgroundEnabled", "false");
 
resultWindow->setProperty("BackgroundEnabled", "false");
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onProgressDone(const CEGUI::EventArgs &e)
 
bool onProgressDone(const CEGUI::EventArgs &e)
 
{
 
{
 
// The progress bar is full.
 
// The progress bar is full.
CEGUI::Window * resultWindow = CEGUI::WindowManager::getSingletonPtr()->getWindow("StaticText1");
+
CEGUI::Window * resultWindow = CEGUI::WindowManager::getSingleton().getWindow("StaticText1");
  
 
resultWindow->setText("We are done!");
 
resultWindow->setText("We are done!");
Line 475: Line 477:
 
bool onMouseClick(const CEGUI::EventArgs &e)
 
bool onMouseClick(const CEGUI::EventArgs &e)
 
{
 
{
CEGUI::ProgressBar * progressBar = static_cast<CEGUI::ProgressBar*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("ProgressBar1"));
+
CEGUI::ProgressBar * progressBar = static_cast<CEGUI::ProgressBar*>(CEGUI::WindowManager::getSingleton().getWindow("ProgressBar1"));
 
progressBar->setProgress(progressBar->getProgress() + 0.05f);
 
progressBar->setProgress(progressBar->getProgress() + 0.05f);
 
return true;
 
return true;
 
}
 
}
</syntaxhighlight>
+
</source>
  
====Slider====
+
==== Slider ====
  
 
A widget which accepts input though moving a 'thumb' between a range.
 
A widget which accepts input though moving a 'thumb' between a range.
Line 488: Line 490:
 
* EventThumbTrackEnded - Fired when the user stops moving the slider's thumb. Handy when a change causes an CPU expensive operation, because now you only catch the 'final' change.
 
* EventThumbTrackEnded - Fired when the user stops moving the slider's thumb. Handy when a change causes an CPU expensive operation, because now you only catch the 'final' change.
  
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
Slider * slider = static_cast<Slider*>(winMgr.createWindow("TaharezLook/Slider", "Slider1"));
 
Slider * slider = static_cast<Slider*>(winMgr.createWindow("TaharezLook/Slider", "Slider1"));
 
sheet->addChildWindow(slider);
 
sheet->addChildWindow(slider);
Line 500: Line 502:
 
slider->subscribeEvent(Slider::EventProgressChanged, Event::Subscriber(&EventGalore::onSliderChangeEnded, this));
 
slider->subscribeEvent(Slider::EventProgressChanged, Event::Subscriber(&EventGalore::onSliderChangeEnded, this));
  
</syntaxhighlight>
+
</source>
<syntaxhighlight lang="cpp">
+
<source lang="cpp">
 
bool onSliderValueChanged(const CEGUI::EventArgs &e)
 
bool onSliderValueChanged(const CEGUI::EventArgs &e)
 
{
 
{
Line 530: Line 532:
 
}
 
}
  
</syntaxhighlight>
+
</source>
  
====Spinner====
+
==== Spinner ====
  
====MultiColumnList====
+
==== MultiColumnList ====
  
[[Category:Uncategorised]]
+
[[Category:Manuals]]
 +
[[Category:WIP]]
 +
[[Category:Update requested]]

Latest revision as of 01:38, 6 March 2011

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.

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.

  • EventDragDropItemEnters - Fired when a drag and drop item floats over the window.
  • EventDragDropItemLeaves - Fired when a drag and drop item floats out of the window.
  • EventDragDropItemDropped - Fired when the item has been dropped on the window.

PushButton

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

  • EventClicked - Fired when the button has been clicked.
  • EventMouseEnters - Fired when the mouse enters the widget. Inherited from Window.
  • EventMouseLeaves - Fired when the mouse leaves the widget. Inherited from Window.
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.

  • EventSelectionChanged - Fired when a or another item has been selected.
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'.

  • EventCheckStateChanged - Fired when the checkbox got checked or un-checked.
  • EventMouseEnters - Fired when the mouse hovers over the checkbox. Inherited from Window.
  • EventMouseLeaves - Fired when the mouse moves out of the checkbox. Inherited from Window.
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.

  • First, our checkbox is created and our mouse remains still.
  • Second, the mouse enters the checkbox, updateCheckbox is called. In this case, our checkbox isn't selected by default, so the text becomes 'Click to choose yes!'.
  • Third, we click. The onCheckStateChanged function is called, and that function calls updateCheckbox again to instantly change the text again. We have clicked the checkbox, while it wasn't previously checked, this means its checked now. Text becomes 'Click to choose no!'.
  • Fourth, we move our mouse away from the checkbox. The onMouseLeaves function is called, where we reset the text to our question.

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.

  • EventSelectStateChanged - Fired when the radiobutton has been 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.

  • EventTextAccepted - Fired when someone has pressed TAB or RETURN, or when someone has clicked another window.
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.

  • EventTextChanged - Fired when text has been changed. Inherited from window.

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.

  • EventCloseClicked - Fired when the 'X' in the upper right corner is clicked (of the Framewindow).
  • EventRollupToggled - Fired when the window gets rolled up, or rolled down.
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.

  • EventProgressDone - Fired when the progress bar reaches 100%(1.0f).
  • EventProgressChanged - Fired when the progress changes.
  • EventMouseClick - Fired when someone clicked on the window. Inherited from Window.
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.

  • EventValueChanged - Fired when the slider's thumb has moved one step left or right.
  • EventThumbTrackEnded - Fired when the user stops moving the slider's thumb. Handy when a change causes an CPU expensive operation, because now you only catch the 'final' change.
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