Difference between revisions of "Most important events"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
Line 16: Line 16:
 
   bool initialiseSample()
 
   bool initialiseSample()
 
   {
 
   {
       using namespace CEGUI;
+
       try
 +
      {
 +
using namespace CEGUI;
 +
 +
WindowManager& winMgr = WindowManager::getSingleton();
  
      return true;
+
// 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)
 
   void cleanupSample(void)
Line 55: Line 79:
 
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.
 
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.
  
* EventTextChanged  Called whenever the text of the window has changed.
+
* 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.
 +
 
 +
<code><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));
 +
</code>
 +
<code><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;
 +
}
 +
</code>

Revision as of 11:59, 5 May 2007

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/>

  1. ifndef _EventGalore_h_
  2. define _EventGalore_h_
  1. include "CEGuiSample.h"
  2. 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)
  {
  }

};

  1. endif // _EventGalore_h_

main.cpp

<cpp/>

  1. if defined( __WIN32__ ) || defined( _WIN32 )

#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include "windows.h"

  1. endif
  1. include "EventGalore.h"


  1. if defined( __WIN32__ ) || defined( _WIN32 )

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)

  1. else

int main(int argc, char *argv[])

  1. 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; }