Most important events
This article is going to show you the most important and usefull events of each widget.
Getting started
I'm only going to change the initialiseSample function for each widget + adding some others.
EventGalore.h
<cpp/>
- 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"); 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
<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, but we are only going to handle a few.
- EventDragDropItemEnters - Called when a drag and drop item floats over the window.
- EventDragDropItemLeaves - Called when a drag and drop item floats out of the window.
- EventDragDropItemDropped - Called 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 - Called when the button has been clicked.
- EventMouseEnters - Called when the mouse enters the widget. Inherited from Window.
- EventMouseLeaves - Called when the mouse leaves the widget. Inherited from Window.
<cpp/>
PushButton * pb = (PushButton*)winMgr.createWindow("TaharezLook/Button", "Button1");
sheet->addChildWindow(pb);
pb->setPosition(UVector2(cegui_reldim(0.1f), cegui_reldim(0.1f)));
pb->setSize(UVector2(cegui_reldim(0.2f), cegui_reldim(0.08f)));
pb->setText("Hey! Come here!");
pb->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&EventGalore::onPushButtonClicked, this));
pb->subscribeEvent(PushButton::EventMouseEnters, Event::Subscriber(&EventGalore::onMouseEnters, this));
pb->subscribeEvent(PushButton::EventMouseLeaves, Event::Subscriber(&EventGalore::onMouseLeaves, this));
<cpp/>
bool onPushButtonClicked(const CEGUI::EventArgs &e)
{
// Our button has been clicked!
CEGUI::PushButton * pb = (CEGUI::PushButton*)CEGUI::WindowManager::getSingletonPtr()->getWindow("Button1");
pb->setText("We got clicked!");
return true;
}
bool onMouseEnters(const CEGUI::EventArgs &e) { // Mouse has entered the button. (Hover) CEGUI::PushButton * pb = (CEGUI::PushButton*)CEGUI::WindowManager::getSingletonPtr()->getWindow("Button1"); pb->setText("Now click!"); return true; }
bool onMouseLeaves(const CEGUI::EventArgs &e) { // Mouse has left the button. CEGUI::PushButton * pb = (CEGUI::PushButton*)CEGUI::WindowManager::getSingletonPtr()->getWindow("Button1"); // Back to its original state! pb->setText("Hey! Come here!"); return true; }