Cary Sandvig 25 лет назад
Родитель
Сommit
aa19efb995
4 измененных файлов с 188 добавлено и 20 удалено
  1. 1 0
      panda/src/gui/guiButton.cxx
  2. 31 10
      panda/src/gui/guiChooser.cxx
  3. 61 6
      panda/src/gui/guiListBox.cxx
  4. 95 4
      panda/src/testbed/gui_demo.cxx

+ 1 - 0
panda/src/gui/guiButton.cxx

@@ -470,6 +470,7 @@ void GuiButton::reset_behavior(void) {
   GuiBehavior::reset_behavior();
   GuiBehavior::reset_behavior();
   if (_mgr == (GuiManager*)0L)
   if (_mgr == (GuiManager*)0L)
     return;
     return;
+  this->start_behavior();
   _eh->remove_hook(_up_event, GuiButton::behavior_up, (void*)this);
   _eh->remove_hook(_up_event, GuiButton::behavior_up, (void*)this);
   _eh->remove_hook(_up_rollover_event, GuiButton::behavior_up, (void*)this);
   _eh->remove_hook(_up_rollover_event, GuiButton::behavior_up, (void*)this);
 }
 }

+ 31 - 10
panda/src/gui/guiChooser.cxx

@@ -39,9 +39,11 @@ void GuiChooser::move_prev(void) {
   if (_curr == -1)
   if (_curr == -1)
     return;
     return;
   int tmp = _curr - 1;
   int tmp = _curr - 1;
-  if (_loop) {
-    if (tmp < 0)
+  if (tmp < 0) {
+    if (_loop)
       tmp += _items.size();
       tmp += _items.size();
+    else
+      return;
   }
   }
   if (_mgr != (GuiManager*)0L) {
   if (_mgr != (GuiManager*)0L) {
     _items[_curr]->unmanage();
     _items[_curr]->unmanage();
@@ -63,10 +65,12 @@ void GuiChooser::move_next(void) {
   if (_curr == -1)
   if (_curr == -1)
     return;
     return;
   int tmp = _curr + 1;
   int tmp = _curr + 1;
-  if (_loop) {
-    int foo = _items.size();
-    if (tmp == foo)
+  int foo = _items.size();
+  if (tmp == foo) {
+    if (_loop)
       tmp = 0;
       tmp = 0;
+    else
+      return;
   }
   }
   if (_mgr != (GuiManager*)0L) {
   if (_mgr != (GuiManager*)0L) {
     _items[_curr]->unmanage();
     _items[_curr]->unmanage();
@@ -92,16 +96,24 @@ void GuiChooser::add_item(GuiItem* item) {
 int GuiChooser::freeze(void) {
 int GuiChooser::freeze(void) {
   int result = 0;
   int result = 0;
 
 
-  if (_curr != -1)
-    result = _items[_curr]->freeze();
+  _prev_button->freeze();
+  _next_button->freeze();
+  for (ItemVector::iterator i=_items.begin(); i!=_items.end(); ++i) {
+    int count = (*i)->freeze();
+    result = max(result, count);
+  }
   return result;
   return result;
 }
 }
 
 
 int GuiChooser::thaw(void) {
 int GuiChooser::thaw(void) {
   int result = 0;
   int result = 0;
 
 
-  if (_curr != -1)
-    result = _items[_curr]->thaw();
+  _prev_button->thaw();
+  _next_button->thaw();
+  for (ItemVector::iterator i=_items.begin(); i!=_items.end(); ++i) {
+    int count = (*i)->thaw();
+    result = max(result, count);
+  }
   return result;
   return result;
 }
 }
 
 
@@ -109,8 +121,17 @@ void GuiChooser::manage(GuiManager* mgr, EventHandler& eh) {
   if (_mgr == (GuiManager*)0L) {
   if (_mgr == (GuiManager*)0L) {
     _prev_button->manage(mgr, eh);
     _prev_button->manage(mgr, eh);
     _next_button->manage(mgr, eh);
     _next_button->manage(mgr, eh);
-    if (_curr != -1)
+    if (_curr != -1) {
       _items[_curr]->manage(mgr, eh);
       _items[_curr]->manage(mgr, eh);
+      if (_curr == 0)
+	_prev_button->inactive();
+      int foo = _items.size() - 1;
+      if (_curr == foo)
+	_next_button->inactive();
+    } else {
+      _prev_button->inactive();
+      _next_button->inactive();
+    }
     GuiBehavior::manage(mgr, eh);
     GuiBehavior::manage(mgr, eh);
   } else
   } else
     gui_cat->warning() << "tried to manage chooser (0x" << (void*)this
     gui_cat->warning() << "tried to manage chooser (0x" << (void*)this

+ 61 - 6
panda/src/gui/guiListBox.cxx

@@ -16,10 +16,17 @@ GuiListBox::ListFunctor::~ListFunctor(void) {
 }
 }
 
 
 void GuiListBox::ListFunctor::doit(GuiBehavior* b) {
 void GuiListBox::ListFunctor::doit(GuiBehavior* b) {
-  if (b == this->_lb->_up_arrow)
+  gui_cat->debug() << "in GuiListBox::ListFunctor::doit" << endl;
+  gui_cat->debug() << "b = 0x" << (void*)b << " (" << b->get_name() << ")"
+		   << endl;
+  if (b == this->_lb->_up_arrow) {
+    gui_cat->debug() << "scrolling up" << endl;
     this->_lb->scroll_up();
     this->_lb->scroll_up();
-  if (b == this->_lb->_down_arrow)
+  }
+  if (b == this->_lb->_down_arrow) {
+    gui_cat->debug() << "scrolling down" << endl;
     this->_lb->scroll_down();
     this->_lb->scroll_down();
+  }
 }
 }
 
 
 void GuiListBox::recompute_frame(void) {
 void GuiListBox::recompute_frame(void) {
@@ -80,8 +87,9 @@ void GuiListBox::visible_patching(void) {
       _visible[0]->unmanage();
       _visible[0]->unmanage();
       _top_stack.push_back(_visible[0]);
       _top_stack.push_back(_visible[0]);
       _visible[0] = _up_arrow;
       _visible[0] = _up_arrow;
-      if (_mgr != (GuiManager*)0L)
+      if (_mgr != (GuiManager*)0L) {
 	_up_arrow->manage(_mgr, *_eh);
 	_up_arrow->manage(_mgr, *_eh);
+      }
     }
     }
   }
   }
 
 
@@ -106,10 +114,15 @@ void GuiListBox::visible_patching(void) {
       _visible[last]->unmanage();
       _visible[last]->unmanage();
       _bottom_stack.push_back(_visible[last]);
       _bottom_stack.push_back(_visible[last]);
       _visible[last] = _down_arrow;
       _visible[last] = _down_arrow;
-      if (_mgr != (GuiManager*)0L)
+      if (_mgr != (GuiManager*)0L) {
 	_down_arrow->manage(_mgr, *_eh);
 	_down_arrow->manage(_mgr, *_eh);
+      }
     }
     }
   }
   }
+
+  // and restart any behavior
+  if (_behavior_running)
+    this->reset_behavior();
 }
 }
 
 
 GuiListBox::GuiListBox(const string& name, int N, GuiItem* up, GuiItem* down)
 GuiListBox::GuiListBox(const string& name, int N, GuiItem* up, GuiItem* down)
@@ -128,7 +141,7 @@ GuiListBox::~GuiListBox(void) {
   this->unmanage();
   this->unmanage();
 }
 }
 
 
-void GuiListBox::scroll_up(void) {
+void GuiListBox::scroll_down(void) {
   if (_bottom_stack.size() == 0)
   if (_bottom_stack.size() == 0)
     return;   // nothing to scroll
     return;   // nothing to scroll
   // compute what the first and list item in the visible list are
   // compute what the first and list item in the visible list are
@@ -156,7 +169,7 @@ void GuiListBox::scroll_up(void) {
   this->recompute_frame();
   this->recompute_frame();
 }
 }
 
 
-void GuiListBox::scroll_down(void) {
+void GuiListBox::scroll_up(void) {
   if (_top_stack.size() == 0)
   if (_top_stack.size() == 0)
     return;  // nothing to scroll
     return;  // nothing to scroll
   // compute what the first and last item in the visible list are
   // compute what the first and last item in the visible list are
@@ -311,6 +324,16 @@ void GuiListBox::set_pos(const LVector3f& p) {
 
 
 #include "guiButton.h"
 #include "guiButton.h"
 
 
+static void handle_scroll_up(CPT_Event, void* lb) {
+  GuiListBox* b = (GuiListBox*)lb;
+  b->scroll_up();
+}
+
+static void handle_scroll_down(CPT_Event, void* lb) {
+  GuiListBox* b = (GuiListBox*)lb;
+  b->scroll_down();
+}
+
 void GuiListBox::start_behavior(void) {
 void GuiListBox::start_behavior(void) {
   GuiBehavior::start_behavior();
   GuiBehavior::start_behavior();
   if (_mgr == (GuiManager*)0L)
   if (_mgr == (GuiManager*)0L)
@@ -319,6 +342,7 @@ void GuiListBox::start_behavior(void) {
       _down_arrow->is_of_type(GuiButton::get_class_type())) {
       _down_arrow->is_of_type(GuiButton::get_class_type())) {
     GuiButton* up = DCAST(GuiButton, _up_arrow);
     GuiButton* up = DCAST(GuiButton, _up_arrow);
     GuiButton* dn = DCAST(GuiButton, _down_arrow);
     GuiButton* dn = DCAST(GuiButton, _down_arrow);
+    /*
     if (_up_functor != (GuiListBox::ListFunctor*)0L) {
     if (_up_functor != (GuiListBox::ListFunctor*)0L) {
       up->set_behavior_functor(_up_functor->get_prev());
       up->set_behavior_functor(_up_functor->get_prev());
       delete _up_functor;
       delete _up_functor;
@@ -326,7 +350,13 @@ void GuiListBox::start_behavior(void) {
     _up_functor = new GuiListBox::ListFunctor(this,
     _up_functor = new GuiListBox::ListFunctor(this,
 					      up->get_behavior_functor());
 					      up->get_behavior_functor());
     up->set_behavior_functor(_up_functor);
     up->set_behavior_functor(_up_functor);
+    */
+    string ev = this->get_name();
+    ev += "-scroll-up";
+    up->set_behavior_event(ev);
     up->start_behavior();
     up->start_behavior();
+    _eh->add_hook(ev, handle_scroll_up, (void*)this);
+    /*
     if (_down_functor != (GuiListBox::ListFunctor*)0L) {
     if (_down_functor != (GuiListBox::ListFunctor*)0L) {
       dn->set_behavior_functor(_down_functor->get_prev());
       dn->set_behavior_functor(_down_functor->get_prev());
       delete _down_functor;
       delete _down_functor;
@@ -334,7 +364,12 @@ void GuiListBox::start_behavior(void) {
     _down_functor = new GuiListBox::ListFunctor(this,
     _down_functor = new GuiListBox::ListFunctor(this,
 						dn->get_behavior_functor());
 						dn->get_behavior_functor());
     dn->set_behavior_functor(_down_functor);
     dn->set_behavior_functor(_down_functor);
+    */
+    ev = this->get_name();
+    ev += "-scroll-down";
+    dn->set_behavior_event(ev);
     dn->start_behavior();
     dn->start_behavior();
+    _eh->add_hook(ev, handle_scroll_down, (void*)this);
   } else
   } else
     gui_cat->error() << "tried to run behavior on listbox '"
     gui_cat->error() << "tried to run behavior on listbox '"
 		     << this->get_name()
 		     << this->get_name()
@@ -345,6 +380,10 @@ void GuiListBox::stop_behavior(void) {
   GuiBehavior::stop_behavior();
   GuiBehavior::stop_behavior();
   if (_mgr == (GuiManager*)0L)
   if (_mgr == (GuiManager*)0L)
     return;
     return;
+  string ev = this->get_name();
+  _eh->remove_hook(ev + "-scroll-up", handle_scroll_up, (void*)this);
+  _eh->remove_hook(ev + "-scroll-down", handle_scroll_down, (void*)this);
+  /*
   if (_up_functor != (GuiListBox::ListFunctor*)0L) {
   if (_up_functor != (GuiListBox::ListFunctor*)0L) {
     GuiButton* up = DCAST(GuiButton, _up_arrow);
     GuiButton* up = DCAST(GuiButton, _up_arrow);
     up->set_behavior_functor(_up_functor->get_prev());
     up->set_behavior_functor(_up_functor->get_prev());
@@ -359,12 +398,14 @@ void GuiListBox::stop_behavior(void) {
     _down_functor = (GuiListBox::ListFunctor*)0L;
     _down_functor = (GuiListBox::ListFunctor*)0L;
     dn->stop_behavior();
     dn->stop_behavior();
   }
   }
+  */
 }
 }
 
 
 void GuiListBox::reset_behavior(void) {
 void GuiListBox::reset_behavior(void) {
   GuiBehavior::reset_behavior();
   GuiBehavior::reset_behavior();
   if (_mgr == (GuiManager*)0L)
   if (_mgr == (GuiManager*)0L)
     return;
     return;
+  /*
   if (_up_functor != (GuiListBox::ListFunctor*)0L) {
   if (_up_functor != (GuiListBox::ListFunctor*)0L) {
     GuiButton* up = DCAST(GuiButton, _up_arrow);
     GuiButton* up = DCAST(GuiButton, _up_arrow);
     up->reset_behavior();
     up->reset_behavior();
@@ -373,6 +414,20 @@ void GuiListBox::reset_behavior(void) {
     GuiButton* dn = DCAST(GuiButton, _down_arrow);
     GuiButton* dn = DCAST(GuiButton, _down_arrow);
     dn->reset_behavior();
     dn->reset_behavior();
   }
   }
+  */
+  string ev = this->get_name();
+  _eh->add_hook(ev + "-scroll-up", handle_scroll_up, (void*)this);
+  _eh->add_hook(ev + "-scroll-down", handle_scroll_down, (void*)this);
+  if (_up_arrow->is_of_type(GuiButton::get_class_type())) {
+    GuiButton* up = DCAST(GuiButton, _up_arrow);
+    up->start_behavior();
+    up->set_behavior_event(ev + "-scroll-up");
+  }
+  if (_down_arrow->is_of_type(GuiButton::get_class_type())) {
+    GuiButton* down = DCAST(GuiButton, _down_arrow);
+    down->start_behavior();
+    down->set_behavior_event(ev + "-scroll-down");
+  }
 }
 }
 
 
 void GuiListBox::output(ostream& os) const {
 void GuiListBox::output(ostream& os) const {

+ 95 - 4
panda/src/testbed/gui_demo.cxx

@@ -48,6 +48,7 @@
 #include <guiFrame.h>
 #include <guiFrame.h>
 #include <guiSign.h>
 #include <guiSign.h>
 #include <guiListBox.h>
 #include <guiListBox.h>
+#include <guiChooser.h>
 
 
 //From framework
 //From framework
 extern PT(GeomNode) geomnode;
 extern PT(GeomNode) geomnode;
@@ -679,7 +680,9 @@ static void test13(GuiManager* mgr, Node* font) {
   GuiLabel* dl2 = GuiLabel::make_simple_text_label("dndn", font);
   GuiLabel* dl2 = GuiLabel::make_simple_text_label("dndn", font);
   GuiLabel* dl3 = GuiLabel::make_simple_text_label("dndn", font);
   GuiLabel* dl3 = GuiLabel::make_simple_text_label("dndn", font);
   GuiButton* db = new GuiButton("down_arrow", dl1, dl2, dl3);
   GuiButton* db = new GuiButton("down_arrow", dl1, dl2, dl3);
-  dl->set_scale(0.1);
+  db->set_scale(0.1);
+  ub->set_behavior_event("demo-event-thing");
+  db->set_behavior_event("demo-event-thing");
   lb1 = new GuiListBox("list_box", 4, ub, db);
   lb1 = new GuiListBox("list_box", 4, ub, db);
   GuiLabel* l1 = GuiLabel::make_simple_text_label("hyena", font);
   GuiLabel* l1 = GuiLabel::make_simple_text_label("hyena", font);
   GuiSign* s1 = new GuiSign("hyena", l1);
   GuiSign* s1 = new GuiSign("hyena", l1);
@@ -736,6 +739,78 @@ static void test13(GuiManager* mgr, Node* font) {
   lb1->start_behavior();
   lb1->start_behavior();
 }
 }
 
 
+PT(GuiChooser) ch1;
+
+static void test14(GuiManager* mgr, Node* font) {
+  GuiLabel* nl1 = GuiLabel::make_simple_text_label("next", font);
+  GuiLabel* nl2 = GuiLabel::make_simple_text_label("next", font);
+  GuiLabel* nl3 = GuiLabel::make_simple_text_label("next", font);
+  GuiButton* nb = new GuiButton("next_button", nl1, nl2, nl3);
+  nb->set_scale(0.1);
+  nb->set_pos(LVector3f::rfu(0.25, 0., -0.25));
+  GuiLabel* pl1 = GuiLabel::make_simple_text_label("prev", font);
+  GuiLabel* pl2 = GuiLabel::make_simple_text_label("prev", font);
+  GuiLabel* pl3 = GuiLabel::make_simple_text_label("prev", font);
+  GuiButton* pb = new GuiButton("prev_button", pl1, pl2, pl3);
+  pb->set_scale(0.1);
+  pb->set_pos(LVector3f::rfu(-0.25, 0., -0.25));
+  nb->set_behavior_event("demo-event-thing");
+  pb->set_behavior_event("demo-event-thing");
+  ch1 = new GuiChooser("chooser", pb, nb);
+  GuiLabel* l1 = GuiLabel::make_simple_text_label("hyena", font);
+  GuiSign* s1 = new GuiSign("hyena", l1);
+  s1->set_scale(0.1);
+  GuiLabel* l2 = GuiLabel::make_simple_text_label("dingo", font);
+  GuiSign* s2 = new GuiSign("dingo", l2);
+  s2->set_scale(0.1);
+  GuiLabel* l3 = GuiLabel::make_simple_text_label("jackal", font);
+  GuiSign* s3 = new GuiSign("jackal", l3);
+  s3->set_scale(0.1);
+  GuiLabel* l4 = GuiLabel::make_simple_text_label("wolf", font);
+  GuiSign* s4 = new GuiSign("wolf", l4);
+  s4->set_scale(0.1);
+  GuiLabel* l5 = GuiLabel::make_simple_text_label("fox", font);
+  GuiSign* s5 = new GuiSign("fox", l5);
+  s5->set_scale(0.1);
+  float w, w1, w2;
+  w1 = l1->get_width();
+  w2 = l2->get_width();
+  w = (w1>w2)?w1:w2;
+  w2 = l3->get_width();
+  w = (w>w2)?w:w2;
+  w2 = l4->get_width();
+  w = (w>w2)?w:w2;
+  w2 = l5->get_width();
+  w = (w>w2)?w:w2;
+  l1->set_width(w);
+  l2->set_width(w);
+  l3->set_width(w);
+  l4->set_width(w);
+  l5->set_width(w);
+  nl1->set_background_color(0., 0., 0., 1.);
+  nl2->set_background_color(0., 0., 0., 1.);
+  nl3->set_background_color(0., 0., 0., 1.);
+  nl2->set_foreground_color(1., 0., 0., 1.);
+  nl3->set_foreground_color(1., 1., 1., 0.5);
+  pl1->set_background_color(0., 0., 0., 1.);
+  pl2->set_background_color(0., 0., 0., 1.);
+  pl3->set_background_color(0., 0., 0., 1.);
+  pl2->set_foreground_color(1., 0., 0., 1.);
+  pl3->set_foreground_color(1., 1., 1., 0.5);
+  l1->set_background_color(0., 0., 0., 1.);
+  l2->set_background_color(0., 0., 0., 1.);
+  l3->set_background_color(0., 0., 0., 1.);
+  l4->set_background_color(0., 0., 0., 1.);
+  l5->set_background_color(0., 0., 0., 1.);
+  ch1->add_item(s1);
+  ch1->add_item(s2);
+  ch1->add_item(s3);
+  ch1->add_item(s4);
+  ch1->add_item(s5);
+  ch1->thaw();
+  ch1->manage(mgr, event_handler);
+}
+
 static void setup_gui(void) {
 static void setup_gui(void) {
   GuiManager* mgr = GuiManager::get_ptr(main_win, mak, (Node*)0L);
   GuiManager* mgr = GuiManager::get_ptr(main_win, mak, (Node*)0L);
   PT_Node font = ModelPool::load_model("ttf-comic");
   PT_Node font = ModelPool::load_model("ttf-comic");
@@ -767,7 +842,9 @@ static void setup_gui(void) {
   // test 12
   // test 12
   //  test12(mgr, font);
   //  test12(mgr, font);
   // test 13
   // test 13
-  test13(mgr, font);
+  //  test13(mgr, font);
+  // test 14
+  test14(mgr, font);
 }
 }
 
 
 static void event_2(CPT_Event) {
 static void event_2(CPT_Event) {
@@ -847,17 +924,31 @@ static void event_3(CPT_Event) {
 }
 }
 */
 */
 
 
+/*
 // for test 11, 13
 // for test 11, 13
 static void event_3(CPT_Event) {
 static void event_3(CPT_Event) {
   lb1->scroll_up();
   lb1->scroll_up();
   cout << *lb1;
   cout << *lb1;
 }
 }
+*/
+
+// for test 14
+static void event_3(CPT_Event) {
+  ch1->move_prev();
+}
 
 
+/*
 // for test11, 13
 // for test11, 13
 static void event_4(CPT_Event) {
 static void event_4(CPT_Event) {
   lb1->scroll_down();
   lb1->scroll_down();
   cout << *lb1;
   cout << *lb1;
 }
 }
+*/
+
+// for test 14
+static void event_4(CPT_Event) {
+  ch1->move_next();
+}
 
 
 static void event_demo(CPT_Event) {
 static void event_demo(CPT_Event) {
   cout << "got demo-event-thing event!" << endl;
   cout << "got demo-event-thing event!" << endl;
@@ -868,9 +959,9 @@ void gui_keys(EventHandler&) {
   have_dlight = true;
   have_dlight = true;
 
 
   event_handler.add_hook("2", event_2);
   event_handler.add_hook("2", event_2);
-  // for tests 7-11, 13
+  // for tests 7-11, 13-14
   event_handler.add_hook("3", event_3);
   event_handler.add_hook("3", event_3);
-  // for test 11, 13
+  // for test 11, 13-14
   event_handler.add_hook("4", event_4);
   event_handler.add_hook("4", event_4);
   event_handler.add_hook("demo-event-thing", event_demo);
   event_handler.add_hook("demo-event-thing", event_demo);
 }
 }