From e4ef1283aa40d6726cdbb6e4cf101eb815363ab2 Mon Sep 17 00:00:00 2001 From: Cary Sandvig Date: Thu, 8 Feb 2001 02:04:42 +0000 Subject: [PATCH] one step closer --- panda/src/gui/Sources.pp | 9 +++-- panda/src/gui/config_gui.cxx | 4 ++ panda/src/gui/guiBehavior.cxx | 11 +++++- panda/src/gui/guiBehavior.h | 36 ++++++++++++++++- panda/src/gui/guiButton.cxx | 74 +++++++++++++++++++++++++++++------ panda/src/gui/guiButton.h | 19 +++++++-- panda/src/gui/guiListBox.cxx | 26 ++++++++---- panda/src/gui/guiListBox.h | 12 ++++-- 8 files changed, 159 insertions(+), 32 deletions(-) diff --git a/panda/src/gui/Sources.pp b/panda/src/gui/Sources.pp index 94d99343bd..ad71859106 100644 --- a/panda/src/gui/Sources.pp +++ b/panda/src/gui/Sources.pp @@ -18,7 +18,8 @@ guiFrame.h guiFrame.I guiFrame.cxx \ guiSign.h guiSign.I guiSign.cxx \ guiListBox.h guiListBox.I guiListBox.cxx \ - guiBackground.h guiBackground.I guiBackground.cxx + guiBackground.h guiBackground.I guiBackground.cxx \ + guiBehavior.h guiBehavior.I guiBehavior.cxx #define INSTALL_HEADERS \ guiManager.h guiManager.I \ @@ -30,7 +31,8 @@ guiFrame.h guiFrame.I \ guiSign.h guiSign.I \ guiListBox.h guiListBox.I \ - guiBackground.h guiBackground.I + guiBackground.h guiBackground.I \ + guiBehavior.h guiBehavior.I #define IGATESCAN \ guiManager.h guiManager.I \ @@ -42,6 +44,7 @@ guiFrame.h guiFrame.I \ guiSign.h guiSign.I \ guiListBox.h guiListBox.I \ - guiBackground.h guiBackground.I + guiBackground.h guiBackground.I \ + guiBehavior.h guiBehavior.I #end lib_target diff --git a/panda/src/gui/config_gui.cxx b/panda/src/gui/config_gui.cxx index 790d7420c0..ac8c4c5f3e 100644 --- a/panda/src/gui/config_gui.cxx +++ b/panda/src/gui/config_gui.cxx @@ -12,6 +12,8 @@ #include "guiButton.h" #include "guiFrame.h" #include "guiListBox.h" +#include "guiBehavior.h" +#include "guiBackground.h" #include @@ -22,11 +24,13 @@ ConfigureFn(config_gui) { GuiLabel::init_type(); GuiRegion::init_type(); GuiItem::init_type(); + GuiBehavior::init_type(); GuiSign::init_type(); GuiRollover::init_type(); GuiButton::init_type(); GuiFrame::init_type(); GuiListBox::init_type(); + GuiBackground::init_type(); } float simple_text_margin_top = diff --git a/panda/src/gui/guiBehavior.cxx b/panda/src/gui/guiBehavior.cxx index 4441cd5d26..53f881ef55 100644 --- a/panda/src/gui/guiBehavior.cxx +++ b/panda/src/gui/guiBehavior.cxx @@ -7,6 +7,15 @@ TypeHandle GuiBehavior::_type_handle; +GuiBehavior::BehaviorFunctor::BehaviorFunctor(void) { +} + +GuiBehavior::BehaviorFunctor::~BehaviorFunctor(void) { +} + +void GuiBehavior::BehaviorFunctor::doit(GuiBehavior*) { +} + GuiBehavior::GuiBehavior(const string& name) : GuiItem(name), _eh((EventHandler*)0L) { } @@ -22,7 +31,7 @@ void GuiBehavior::manage(GuiManager* mgr, EventHandler& eh) { void GuiBehavior::unmanage(void) { if (_behavior_running) this->stop_behavior(); - _eh = (EventHandler)0L; + _eh = (EventHandler*)0L; GuiItem::unmanage(); } diff --git a/panda/src/gui/guiBehavior.h b/panda/src/gui/guiBehavior.h index fc5c06d7c9..d77c9af80a 100644 --- a/panda/src/gui/guiBehavior.h +++ b/panda/src/gui/guiBehavior.h @@ -8,9 +8,19 @@ #include "guiItem.h" -claas EXPCL_PANDA GuiBehavior : public GuiItem { +class EXPCL_PANDA GuiBehavior : public GuiItem { protected: EventHandler* _eh; + bool _behavior_running; + + INLINE GuiBehavior(void); +PUBLISHED: + class EXPCL_PANDA BehaviorFunctor { + public: + BehaviorFunctor(void); + ~BehaviorFunctor(void); + virtual void doit(GuiBehavior*) = 0; + }; PUBLISHED: GuiBehavior(const string&); virtual ~GuiBehavior(void); @@ -20,6 +30,30 @@ PUBLISHED: virtual void start_behavior(void) = 0; virtual void stop_behavior(void) = 0; + virtual void reset_behavior(void) = 0; + + virtual void output(ostream&) const = 0; +public: + // type interface + static TypeHandle get_class_type(void) { + return _type_handle; + } + static void init_type(void) { + GuiItem::init_type(); + register_type(_type_handle, "GuiItem", + GuiItem::get_class_type()); + } + virtual TypeHandle get_type(void) const { + return get_class_type(); + } + virtual TypeHandle force_init_type(void) { + init_type(); + return get_class_type(); + } +private: + static TypeHandle _type_handle; }; +#include "guiBehavior.I" + #endif /* __GUIBEHAVIOR_H__ */ diff --git a/panda/src/gui/guiButton.cxx b/panda/src/gui/guiButton.cxx index 31e7b429ef..66e6213601 100644 --- a/panda/src/gui/guiButton.cxx +++ b/panda/src/gui/guiButton.cxx @@ -194,7 +194,7 @@ void GuiButton::switch_state(GuiButton::States nstate) { } void GuiButton::recompute_frame(void) { - GuiItem::recompute_frame(); + GuiBehavior::recompute_frame(); _up->recompute(); _down->recompute(); if (_up_rollover != (GuiLabel*)0L) @@ -208,14 +208,43 @@ void GuiButton::recompute_frame(void) { _rgn->set_region(_left, _right, _bottom, _top); } +void GuiButton::behavior_up(CPT_Event, void* data) { + GuiButton* button = (GuiButton*)data; + button->run_button_up(); +} + +void GuiButton::behavior_down(CPT_Event, void* data) { + GuiButton* button = (GuiButton*)data; + button->run_button_down(); +} + +void GuiButton::run_button_up(void) { + if (_eh == (EventHandler*)0L) + return; + _eh->remove_hook(_up_event, GuiButton::behavior_up, (void*)this); + _eh->remove_hook(_up_rollover_event, GuiButton::behavior_up, (void*)this); + if (!_behavior_event.empty()) + throw_event(_behavior_event); + if (_behavior_functor != (GuiBehavior::BehaviorFunctor*)0L) + _behavior_functor->doit(this); +} + +void GuiButton::run_button_down(void) { + if (_eh == (EventHandler*)0L) + return; + _eh->add_hook(_up_event, GuiButton::behavior_up, (void*)this); + _eh->add_hook(_up_rollover_event, GuiButton::behavior_up, (void*)this); +} + GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down) - : GuiItem(name), _up(up), _up_rollover((GuiLabel*)0L), _down(down), + : GuiBehavior(name), _up(up), _up_rollover((GuiLabel*)0L), _down(down), _down_rollover((GuiLabel*)0L), _inactive((GuiLabel*)0L), _up_event(name + "-up"), _up_rollover_event(""), _down_event(name +"-down"), _down_rollover_event(""), _inactive_event(""), _up_scale(up->get_scale()), _upr_scale(1.), _down_scale(down->get_scale()), _downr_scale(1.), _inactive_scale(1.), - _state(GuiButton::NONE) { + _state(GuiButton::NONE), + _behavior_functor((GuiBehavior::BehaviorFunctor*)0L) { GetExtents(up, down, _up_rollover, _down_rollover, _inactive, _left, _right, _bottom, _top); _rgn = new GuiRegion("button-" + name, _left, _right, _bottom, _top, true); @@ -228,13 +257,14 @@ GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down) GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down, GuiLabel* inactive) - : GuiItem(name), _up(up), _up_rollover((GuiLabel*)0L), _down(down), + : GuiBehavior(name), _up(up), _up_rollover((GuiLabel*)0L), _down(down), _down_rollover((GuiLabel*)0L), _inactive(inactive), _up_event(name + "-up"), _up_rollover_event(""), _down_event(name +"-down"), _down_rollover_event(""), _inactive_event(name + "-inactive"), _up_scale(up->get_scale()), _upr_scale(1.), _down_scale(down->get_scale()), _downr_scale(1.), - _inactive_scale(inactive->get_scale()), _state(GuiButton::NONE) { + _inactive_scale(inactive->get_scale()), _state(GuiButton::NONE), + _behavior_functor((GuiBehavior::BehaviorFunctor*)0L) { GetExtents(up, down, _up_rollover, _down_rollover, inactive, _left, _right, _bottom, _top); _rgn = new GuiRegion("button-" + name, _left, _right, _bottom, _top, true); @@ -247,14 +277,15 @@ GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down, GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* up_roll, GuiLabel* down, GuiLabel* down_roll, GuiLabel* inactive) - : GuiItem(name), _up(up), _up_rollover(up_roll), _down(down), + : GuiBehavior(name), _up(up), _up_rollover(up_roll), _down(down), _down_rollover(down_roll), _inactive(inactive), _up_event(name + "-up"), _up_rollover_event(name + "-up-rollover"), _down_event(name +"-down"), _down_rollover_event(name + "-down-rollover"), _inactive_event(name + "-inactive"), _up_scale(up->get_scale()), _upr_scale(up_roll->get_scale()), _down_scale(down->get_scale()), _downr_scale(down_roll->get_scale()), - _inactive_scale(inactive->get_scale()), _state(GuiButton::NONE) { + _inactive_scale(inactive->get_scale()), _state(GuiButton::NONE), + _behavior_functor((GuiBehavior::BehaviorFunctor*)0L) { GetExtents(up, down, up_roll, down_roll, inactive, _left, _right, _bottom, _top); _rgn = new GuiRegion("button-" + name, _left, _right, _bottom, _top, true); @@ -276,6 +307,9 @@ GuiButton::~GuiButton(void) { buttons.erase("gui-button-" + name + "-mouse1"); buttons.erase("gui-button-" + name + "-mouse2"); buttons.erase("gui-button-" + name + "-mouse3"); + + if (_behavior_functor != (GuiBehavior::BehaviorFunctor*)0L) + delete _behavior_functor; } void GuiButton::manage(GuiManager* mgr, EventHandler& eh) { @@ -289,7 +323,9 @@ void GuiButton::manage(GuiManager* mgr, EventHandler& eh) { } if (_mgr == (GuiManager*)0L) { mgr->add_region(_rgn); - GuiItem::manage(mgr, eh); + GuiBehavior::manage(mgr, eh); + if (_behavior_running) + this->start_behavior(); switch_state(UP); } else gui_cat->warning() << "tried to manage button (0x" << (void*)this @@ -299,8 +335,10 @@ void GuiButton::manage(GuiManager* mgr, EventHandler& eh) { void GuiButton::unmanage(void) { if (_mgr != (GuiManager*)0L) _mgr->remove_region(_rgn); + if (_behavior_running) + this->stop_behavior(); switch_state(NONE); - GuiItem::unmanage(); + GuiBehavior::unmanage(); } int GuiButton::freeze() { @@ -338,7 +376,7 @@ void GuiButton::set_scale(float f) { _down_rollover->set_scale(f * _downr_scale); if (_inactive != (GuiLabel*)0L) _inactive->set_scale(f * _inactive_scale); - GuiItem::set_scale(f); + GuiBehavior::set_scale(f); this->recompute_frame(); } @@ -351,12 +389,24 @@ void GuiButton::set_pos(const LVector3f& p) { _down_rollover->set_pos(p); if (_inactive != (GuiLabel*)0L) _inactive->set_pos(p); - GuiItem::set_pos(p); + GuiBehavior::set_pos(p); this->recompute_frame(); } +void GuiButton::start_behavior(void) { + GuiBehavior::start_behavior(); +} + +void GuiButton::stop_behavior(void) { + GuiBehavior::stop_behavior(); +} + +void GuiButton::reset_behavior(void) { + GuiBehavior::reset_behavior(); +} + void GuiButton::output(ostream& os) const { - GuiItem::output(os); + GuiBehavior::output(os); os << " Button data:" << endl; os << " up - 0x" << (void*)_up << endl; os << " up_rollover - 0x" << (void*)_up_rollover << endl; diff --git a/panda/src/gui/guiButton.h b/panda/src/gui/guiButton.h index e5f1e96110..d4e7a8609b 100644 --- a/panda/src/gui/guiButton.h +++ b/panda/src/gui/guiButton.h @@ -6,12 +6,12 @@ #ifndef __GUIBUTTON_H__ #define __GUIBUTTON_H__ -#include "guiItem.h" +#include "guiBehavior.h" #include "guiRegion.h" #include "guiLabel.h" #include "guiManager.h" -class EXPCL_PANDA GuiButton : public GuiItem { +class EXPCL_PANDA GuiButton : public GuiBehavior { private: PT(GuiLabel) _up; PT(GuiLabel) _up_rollover; @@ -32,10 +32,17 @@ private: INACTIVE_ROLLOVER }; States _state; + string _behavior_event; + GuiBehavior::BehaviorFunctor* _behavior_functor; + INLINE GuiButton(void); void switch_state(States); virtual void recompute_frame(void); + static void behavior_up(CPT_Event, void*); + static void behavior_down(CPT_Event, void*); + void run_button_up(void); + void run_button_down(void); PUBLISHED: GuiButton(const string&, GuiLabel*, GuiLabel*); GuiButton(const string&, GuiLabel*, GuiLabel*, GuiLabel*); @@ -80,6 +87,10 @@ PUBLISHED: virtual void set_scale(float); virtual void set_pos(const LVector3f&); + virtual void start_behavior(void); + virtual void stop_behavior(void); + virtual void reset_behavior(void); + virtual void output(ostream&) const; public: @@ -88,9 +99,9 @@ public: return _type_handle; } static void init_type(void) { - GuiItem::init_type(); + GuiBehavior::init_type(); register_type(_type_handle, "GuiButton", - GuiItem::get_class_type()); + GuiBehavior::get_class_type()); } virtual TypeHandle get_type(void) const { return get_class_type(); diff --git a/panda/src/gui/guiListBox.cxx b/panda/src/gui/guiListBox.cxx index f38f9019ec..fbbcebea0d 100644 --- a/panda/src/gui/guiListBox.cxx +++ b/panda/src/gui/guiListBox.cxx @@ -8,7 +8,7 @@ TypeHandle GuiListBox::_type_handle; void GuiListBox::recompute_frame(void) { - GuiItem::recompute_frame(); + GuiBehavior::recompute_frame(); LVector3f p = _pos; float lft = 100000.; float rgt = -100000.; @@ -91,7 +91,7 @@ void GuiListBox::visible_patching(void) { } GuiListBox::GuiListBox(const string& name, int N, GuiItem* up, GuiItem* down) - : GuiItem(name), _arrow_top(false), _arrow_bottom(false), _up_arrow(up), + : GuiBehavior(name), _arrow_top(false), _arrow_bottom(false), _up_arrow(up), _down_arrow(down), _n_visible(N) { if (N < 4) { gui_cat->warning() << "ListBoxes should have at least 4 visible slots" @@ -199,7 +199,7 @@ void GuiListBox::manage(GuiManager* mgr, EventHandler& eh) { for (ItemVector::iterator i=_visible.begin(); i!=_visible.end(); ++i) (*i)->manage(mgr, eh); _eh = &eh; - GuiItem::manage(mgr, eh); + GuiBehavior::manage(mgr, eh); } else gui_cat->warning() << "tried to manage listbox (0x" << (void*)this << ") that is already managed" << endl; @@ -208,7 +208,7 @@ void GuiListBox::manage(GuiManager* mgr, EventHandler& eh) { void GuiListBox::unmanage(void) { for (ItemVector::iterator i=_visible.begin(); i!=_visible.end(); ++i) (*i)->unmanage(); - GuiItem::unmanage(); + GuiBehavior::unmanage(); } void GuiListBox::set_scale(float f) { @@ -220,7 +220,7 @@ void GuiListBox::set_scale(float f) { for (ItemDeque::iterator j=_bottom_stack.begin(); j!=_bottom_stack.end(); ++j) (*j)->set_scale(f); - GuiItem::set_scale(f); + GuiBehavior::set_scale(f); } void GuiListBox::set_pos(const LVector3f& p) { @@ -232,11 +232,23 @@ void GuiListBox::set_pos(const LVector3f& p) { for (ItemDeque::iterator j=_bottom_stack.begin(); j!=_bottom_stack.end(); ++j) (*j)->set_pos(p); - GuiItem::set_pos(p); + GuiBehavior::set_pos(p); +} + +void GuiListBox::start_behavior(void) { + GuiBehavior::start_behavior(); +} + +void GuiListBox::stop_behavior(void) { + GuiBehavior::stop_behavior(); +} + +void GuiListBox::reset_behavior(void) { + GuiBehavior::reset_behavior(); } void GuiListBox::output(ostream& os) const { - GuiItem::output(os); + GuiBehavior::output(os); os << " Listbox data:" << endl; os << " There is "; if (!_arrow_top) diff --git a/panda/src/gui/guiListBox.h b/panda/src/gui/guiListBox.h index 4122d6549a..6301236536 100644 --- a/panda/src/gui/guiListBox.h +++ b/panda/src/gui/guiListBox.h @@ -6,12 +6,12 @@ #ifndef __GUILISTBOX_H__ #define __GUILISTBOX_H__ -#include "guiItem.h" +#include "guiBehavior.h" #include #include -class EXPCL_PANDA GuiListBox : public GuiItem { +class EXPCL_PANDA GuiListBox : public GuiBehavior { private: typedef vector< PT(GuiItem) > ItemVector; typedef deque< PT(GuiItem) > ItemDeque; @@ -46,6 +46,10 @@ PUBLISHED: virtual void set_scale(float); virtual void set_pos(const LVector3f&); + virtual void start_behavior(void); + virtual void stop_behavior(void); + virtual void reset_behavior(void); + virtual void output(ostream&) const; public: // type interface @@ -53,9 +57,9 @@ public: return _type_handle; } static void init_type(void) { - GuiItem::init_type(); + GuiBehavior::init_type(); register_type(_type_handle, "GuiListBox", - GuiItem::get_class_type()); + GuiBehavior::get_class_type()); } virtual TypeHandle get_type(void) const { return get_class_type();