Difference between revisions of "Most important events"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
(MultiLineEditbox)
Line 343: Line 343:
 
* EventTextChanged - Fired when text has been changed. Inherited from window.
 
* EventTextChanged - Fired when text has been changed. Inherited from window.
  
This widget doesn't have the even 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.
  
 
<code><cpp/>
 
<code><cpp/>

Revision as of 12:47, 7 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; }

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.

<cpp/> 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)); <cpp/> 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::getSingletonPtr()->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::getSingletonPtr()->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.

<cpp/> 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)); <cpp/> bool onButton1SelectChanged(const CEGUI::EventArgs &e) { CEGUI::RadioButton * radioButton1 = static_cast<CEGUI::RadioButton*>(CEGUI::WindowManager::getSingletonPtr()->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::getSingletonPtr()->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.

<cpp/> 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)); <cpp/> 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::getSingletonPtr()->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.

<cpp/> 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!"); <cpp/> 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 :)