Difference between revisions of "Most important events"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
Line 151: Line 151:
  
 
listBox->subscribeEvent(Listbox::EventSelectionChanged, Event::Subscriber(&EventGalore::onSelectionChanged, this));
 
listBox->subscribeEvent(Listbox::EventSelectionChanged, Event::Subscriber(&EventGalore::onSelectionChanged, this));
<code><cpp/>
+
</code>
 
<code><cpp/>
 
<code><cpp/>
 
bool onSelectionChanged(const CEGUI::EventArgs &e)
 
bool onSelectionChanged(const CEGUI::EventArgs &e)

Revision as of 15:48, 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, 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.

<cpp/> 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)); <cpp/> bool onPushButtonClicked(const CEGUI::EventArgs &e) { // Our button has been clicked! CEGUI::PushButton * pushButton = static_cast<CEGUI::PushButton*>(CEGUI::WindowManager::getSingletonPtr()->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::getSingletonPtr()->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::getSingletonPtr()->getWindow("Button1")); // Back to its original state! pushButton->setText("Hey! Come here!"); return true; }

Listbox

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

  • EventSelectionChanged - Fired when a or another item has been selected.

<cpp/> 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)); <cpp/> bool onSelectionChanged(const CEGUI::EventArgs &e) {

       // The selection has changed.

CEGUI::Listbox * listBox = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingletonPtr()->getWindow("Listbox1")); // Get the item we selected CEGUI::ListboxItem * selectedItem = listBox->getFirstSelectedItem(); selectedItem->setText("Oh we got selected!");

return true; }