Page 1 of 1

Windows closing

Posted: Wed Sep 21, 2011 15:58
by pista75
Hi guys.
My problem is very simple.
I write popup windows,and close with the X button in the corner.
I so far always write new function to all windows.
e.g.:
If two window:

Code: Select all


//...
PopupW1->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW1, EventDown));
//...
 bool EventButtonDown::PopupW1(const CEGUI::EventArgs& /*e*/)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
 
  wmgr.destroyWindow(PopupW1);
  return true;
 }

//...
PopupW2->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW2, EventDown));
//...
 bool EventButtonDown::PopupW2(const CEGUI::EventArgs& /*e*/)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
 
  wmgr.destroyWindow(PopupW2);
  return true;
 }


If ten window:

Code: Select all

 
//...
PopupW1->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW1, EventDown));
//...
 bool EventButtonDown::PopupW1(const CEGUI::EventArgs& /*e*/)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
 
  wmgr.destroyWindow(PopupW1);
  return true;
 }

//...
PopupW2->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW2, EventDown));
//...
 bool EventButtonDown::PopupW2(const CEGUI::EventArgs& /*e*/)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
 
  wmgr.destroyWindow(PopupW2);
  return true;
 }

//...
PopupW3->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW3, EventDown));
//...
 bool EventButtonDown::PopupW3(const CEGUI::EventArgs& /*e*/)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
 
  wmgr.destroyWindow(PopupW2);
  return true;
 }
//...
PopupW4->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW4, EventDown));
//...
 bool EventButtonDown::PopupW4(const CEGUI::EventArgs& /*e*/)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
 
  wmgr.destroyWindow(PopupW2);
  return true;
 }
//And the other to 6 ...


It's very difficult that each window plus function in my code.

Is there a simpler way,that close windows?

Re: Windows closing

Posted: Wed Sep 21, 2011 17:02
by Kulik
1) Subclass and subscribe the callback in constructor
2) Templated callback (fancy but I wouldn't use this)

These are two solutions that popped up in my head right now. Maybe there are more :-)

Re: Windows closing

Posted: Wed Sep 21, 2011 17:43
by Jamarr
Another option is to subscribe to the event globally via the CEGUI::GlobalEventSet. There are plenty of references demonstrating how to subscribe to global events, which you can find by searching these forums.

Re: Windows closing

Posted: Fri Sep 23, 2011 05:17
by ShadowTiger
This is how I would do it. I think it is the simplest solution.

Code: Select all

PopupW1->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW, EventDown));
PopupW2->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW, EventDown));
PopupW3->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&EventButtonDown::PopupW, EventDown));

bool EventButtonDown::PopupW(const CEGUI::EventArgs& eArgs)
 {
  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();

  const CEGUI::WindowEventArgs& we = static_cast<const CEGUI::WindowEventArgs&>(eArgs);

  wmgr.destroyWindow(we.window);
  return true;
 }

Re: Windows closing

Posted: Fri Sep 23, 2011 06:25
by Kulik
ShadowTiger is right, templating is not needed for this.