Browse Source

making priorities work better

Cary Sandvig 25 years ago
parent
commit
be7c5c62d8

+ 8 - 4
panda/src/gui/guiBackground.cxx

@@ -21,12 +21,20 @@ void GuiBackground::set_priority(GuiLabel* l, const GuiItem::Priority p) {
 GuiBackground::GuiBackground(const string& name, GuiItem* item)
   : GuiItem(name), _item(item) {
   _bg = GuiLabel::make_simple_card_label();
+  _bg->set_width(_item->get_width());
+  _bg->set_height(_item->get_height());
+  _bg->set_pos(LVector3f::rfu((_item->get_left() + _item->get_right())*0.5, 0.,
+			      (_item->get_bottom() + _item->get_top())*0.5));
   item->set_priority(_bg, P_High);
 }
 
 GuiBackground::GuiBackground(const string& name, GuiItem* item, Texture* tex)
   : GuiItem(name), _item(item) {
   _bg = GuiLabel::make_simple_texture_label(tex);
+  _bg->set_width(_item->get_width());
+  _bg->set_height(_item->get_height());
+  _bg->set_pos(LVector3f::rfu((_item->get_left() + _item->get_right())*0.5, 0.,
+			      (_item->get_bottom() + _item->get_top())*0.5));
   item->set_priority(_bg, P_High);
 }
 
@@ -38,10 +46,6 @@ void GuiBackground::manage(GuiManager* mgr, EventHandler& eh) {
   if (!_added_hooks)
     _added_hooks = true;
   if (_mgr == (GuiManager*)0L) {
-    _bg->freeze();
-    _bg->set_width(_item->get_width());
-    _bg->set_height(_item->get_height());
-    _bg->thaw();
     mgr->add_label(_bg);
     _item->manage(mgr, eh);
     GuiItem::manage(mgr, eh);

+ 47 - 1
panda/src/gui/guiChooser.cxx

@@ -23,9 +23,50 @@ void GuiChooser::ChooseFunctor::doit(GuiBehavior* b) {
 }
 
 void GuiChooser::recompute_frame(void) {
+  float r, l, t, b;
+
+  r = t = -1.;
+  l = b = 1.;
+  for (ItemVector::iterator i=_items.begin(); i!=_items.end(); ++i) {
+    if (r < (*i)->get_right())
+      r = (*i)->get_right();
+    if (l > (*i)->get_left())
+      l = (*i)->get_left();
+    if (b > (*i)->get_bottom())
+      b = (*i)->get_bottom();
+    if (t < (*i)->get_top())
+      t = (*i)->get_top();
+  }
+  if (r < _prev_button->get_right())
+    r = _prev_button->get_right();
+  if (l > _prev_button->get_left())
+    l = _prev_button->get_left();
+  if (b > _prev_button->get_bottom())
+    b = _prev_button->get_bottom();
+  if (t < _prev_button->get_top())
+    t = _prev_button->get_top();
+  if (r < _next_button->get_right())
+    r = _next_button->get_right();
+  if (l > _next_button->get_left())
+    l = _next_button->get_left();
+  if (b > _next_button->get_bottom())
+    b = _next_button->get_bottom();
+  if (t < _next_button->get_top())
+    t = _next_button->get_top();
+
+  _left = l;
+  _right = r;
+  _bottom = b;
+  _top = t;
+
+  GuiBehavior::recompute_frame();
 }
 
-void GuiChooser::set_priority(GuiLabel*, GuiItem::Priority) {
+void GuiChooser::set_priority(GuiLabel* l, GuiItem::Priority p) {
+  for (ItemVector::iterator i=_items.begin(); i!=_items.end(); ++i)
+    (*i)->set_priority(l, p);
+  _prev_button->set_priority(l, p);
+  _next_button->set_priority(l, p);
 }
 
 GuiChooser::GuiChooser(const string& name, GuiButton* prev, GuiButton* next)
@@ -72,6 +113,8 @@ void GuiChooser::move_prev(void) {
     }
   }
   _curr = tmp;
+  if (_mgr != (GuiManager*)0L)
+    _mgr->recompute_priorities();
 }
 
 void GuiChooser::move_next(void) {
@@ -109,12 +152,15 @@ void GuiChooser::move_next(void) {
     }
   }
   _curr = tmp;
+  if (_mgr != (GuiManager*)0L)
+    _mgr->recompute_priorities();
 }
 
 void GuiChooser::add_item(GuiItem* item) {
   _items.push_back(item);
   if (_curr == -1)
     _curr = 0;
+  this->recompute_frame();
 }
 
 int GuiChooser::freeze(void) {

+ 7 - 1
panda/src/gui/guiFrame.cxx

@@ -210,7 +210,13 @@ void GuiFrame::recompute_frame(void) {
   thaw();
 }
 
-void GuiFrame::set_priority(GuiLabel*, const GuiItem::Priority) {
+void GuiFrame::set_priority(GuiLabel* l, const GuiItem::Priority p) {
+  Boxes::iterator i;
+
+  for (i=_items.begin(); i!=_items.end(); ++i) {
+    GuiItem* here = (*i).get_item();
+    here->set_priority(l, p);
+  }
 }
 
 GuiFrame::GuiFrame(const string& name) : GuiItem(name), _align_to_left(false),

+ 38 - 0
panda/src/gui/guiLabel.cxx

@@ -376,6 +376,8 @@ void GuiLabel::set_text(const string& val) {
 bool GuiLabel::operator<(const GuiLabel& c) const {
   if (_highest_pri)
     return false;
+  if (c._highest_pri)
+    return true;
   PriorityMap::const_iterator pi;
   pi = _priorities.find((GuiLabel*)(&c));
   if (pi != _priorities.end()) {
@@ -384,5 +386,41 @@ bool GuiLabel::operator<(const GuiLabel& c) const {
     else
       return false;
   }
+  pi = c._priorities.find((GuiLabel*)this);
+  if (pi != c._priorities.end()) {
+    if ((*pi).second == P_LOWER)
+      return false;
+    else
+      return true;
+  }
   return ((void*)this) < ((void*)&c);
 }
+
+#include <geomBinTransition.h>
+
+int GuiLabel::set_draw_order(int order) {
+  int ret = order+1;
+  this->freeze();
+  _hard_pri = order;
+  switch (_type) {
+  case SIMPLE_TEXT:
+    {
+      TextNode* n = DCAST(TextNode, _geom);
+      n->set_bin("fixed");
+      n->set_draw_order(order);
+      ret += 2;
+    }
+    break;
+  case SIMPLE_TEXTURE:
+    _arc->set_transition(new GeomBinTransition("fixed", order));
+    break;
+  case SIMPLE_CARD:
+    _arc->set_transition(new GeomBinTransition("fixed", order));
+    break;
+  default:
+    gui_cat->warning() << "trying to set draw order on an unknown label type ("
+		       << (int)_type << ")" << endl;
+  }
+  this->thaw();
+  return ret;
+}

+ 1 - 0
panda/src/gui/guiLabel.h

@@ -101,6 +101,7 @@ PUBLISHED:
   // used for the priority system
   bool operator<(const GuiLabel&) const;
   INLINE void set_priority(GuiLabel*, const PriorityType);
+  int set_draw_order(int);
 
 public:
   // type interface

+ 30 - 0
panda/src/gui/guiListBox.cxx

@@ -126,6 +126,32 @@ void GuiListBox::visible_patching(void) {
 }
 
 void GuiListBox::set_priority(GuiLabel* l, const GuiItem::Priority p) {
+  ItemVector::iterator i;
+  ItemDeque::iterator j;
+
+  for (i=_top_stack.begin(); i!=_top_stack.end(); ++i) {
+    if (*i == _up_arrow)
+      continue;
+    if (*i == _down_arrow)
+      continue;
+    (*i)->set_priority(l, p);
+  }
+  for (i=_visible.begin(); i!=_visible.end(); ++i) {
+    if (*i == _up_arrow)
+      continue;
+    if (*i == _down_arrow)
+      continue;
+    (*i)->set_priority(l, p);
+  }
+  for (j=_bottom_stack.begin(); j!=_bottom_stack.end(); ++j) {
+    if (*j == _up_arrow)
+      continue;
+    if (*j == _down_arrow)
+      continue;
+    (*j)->set_priority(l, p);
+  }
+  _up_arrow->set_priority(l, p);
+  _down_arrow->set_priority(l, p);
 }
 
 GuiListBox::GuiListBox(const string& name, int N, GuiItem* up, GuiItem* down)
@@ -170,6 +196,8 @@ void GuiListBox::scroll_down(void) {
   visible_patching();
   // finally recompute all the possitions
   this->recompute_frame();
+  if (_mgr != (GuiManager*)0L)
+    _mgr->recompute_priorities();
 }
 
 void GuiListBox::scroll_up(void) {
@@ -198,6 +226,8 @@ void GuiListBox::scroll_up(void) {
   visible_patching();
   // finally recompute all the possitions
   this->recompute_frame();
+  if (_mgr != (GuiManager*)0L)
+    _mgr->recompute_priorities();
 }
 
 void GuiListBox::add_item(GuiItem* item) {

+ 2 - 5
panda/src/gui/guiManager.cxx

@@ -177,15 +177,12 @@ void GuiManager::remove_label(GuiLabel* label) {
   }
 }
 
-#include <geomBinTransition.h>
-
 void GuiManager::recompute_priorities(void) {
   _sorts.clear();
   for (LabelSet::iterator i=_labels.begin(); i!=_labels.end(); ++i)
     _sorts.insert(*i);
   int p=0;
-  for (SortSet::iterator j=_sorts.begin(); j!=_sorts.end(); ++j, ++p) {
-    (*j)->_hard_pri = p;
-    (*j)->get_arc()->set_transition(new GeomBinTransition("fixed", p));
+  for (SortSet::iterator j=_sorts.begin(); j!=_sorts.end(); ++j) {
+    p = (*j)->set_draw_order(p);
   }
 }

+ 1 - 0
panda/src/testbed/gui_demo.cxx

@@ -953,6 +953,7 @@ static void test16(GuiManager* mgr, Node* font) {
   bg->set_color(1., 0., 1., 1.);
   bg->thaw();
   bg->manage(mgr, event_handler);
+  mgr->recompute_priorities();
   ch1->start_behavior();
 }