Browse Source

fix getting keys for mouse buttons

Cary Sandvig 25 years ago
parent
commit
2895d5bd66
2 changed files with 36 additions and 32 deletions
  1. 23 18
      panda/src/gui/guiButton.cxx
  2. 13 14
      panda/src/gui/guiRollover.cxx

+ 23 - 18
panda/src/gui/guiButton.cxx

@@ -8,21 +8,20 @@
 
 
 #include <throw_event.h>
 #include <throw_event.h>
 
 
-#include <set>
+#include <map>
 
 
-typedef set<GuiButton*> ButtonSet;
-static ButtonSet buttons;
+typedef map<const MouseWatcherRegion*, GuiButton*> ButtonMap;
+static ButtonMap buttons;
 static bool added_hooks = false;
 static bool added_hooks = false;
 
 
 TypeHandle GuiButton::_type_handle;
 TypeHandle GuiButton::_type_handle;
 
 
-
-static GuiButton *
-find_in_buttons_set(const MouseWatcherRegion* rgn) {
-  for (ButtonSet::const_iterator bi=buttons.begin(); bi!=buttons.end(); ++bi)
-    if ((*bi)->owns_region(rgn))
-      return *bi;
-  return (GuiButton*)0L;
+static inline GuiButton *
+find_in_buttons_map(const MouseWatcherRegion* rgn) {
+  ButtonMap::iterator i = buttons.find(rgn);
+  if (i == buttons.end())
+    return (GuiButton*)0L;
+  return (*i).second;
 }
 }
 
 
 inline void GetExtents(GuiLabel* v, GuiLabel* w, GuiLabel* x, GuiLabel* y,
 inline void GetExtents(GuiLabel* v, GuiLabel* w, GuiLabel* x, GuiLabel* y,
@@ -59,7 +58,7 @@ inline void GetExtents(GuiLabel* v, GuiLabel* w, GuiLabel* x, GuiLabel* y,
 
 
 static void enter_button(CPT_Event e) {
 static void enter_button(CPT_Event e) {
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
-  GuiButton* val = find_in_buttons_set(rgn);
+  GuiButton* val = find_in_buttons_map(rgn);
   if (val == (GuiButton *)0L)
   if (val == (GuiButton *)0L)
     return;  // this one wasn't for us
     return;  // this one wasn't for us
   val->test_ref_count_integrity();
   val->test_ref_count_integrity();
@@ -68,7 +67,7 @@ static void enter_button(CPT_Event e) {
 
 
 static void exit_button(CPT_Event e) {
 static void exit_button(CPT_Event e) {
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
-  GuiButton* val = find_in_buttons_set(rgn);
+  GuiButton* val = find_in_buttons_map(rgn);
   if (val == (GuiButton *)0L)
   if (val == (GuiButton *)0L)
     return;  // this one wasn't for us
     return;  // this one wasn't for us
   val->test_ref_count_integrity();
   val->test_ref_count_integrity();
@@ -77,7 +76,10 @@ static void exit_button(CPT_Event e) {
 
 
 static void click_button_down(CPT_Event e) {
 static void click_button_down(CPT_Event e) {
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
-  GuiButton* val = find_in_buttons_set(rgn);
+  string button = e->get_parameter(1).get_string_value();
+  if ((button != "mouse1") && (button != "mouse2") && (button != "mouse3"))
+    return;
+  GuiButton* val = find_in_buttons_map(rgn);
   if (val == (GuiButton *)0L)
   if (val == (GuiButton *)0L)
     return;  // this one wasn't for us
     return;  // this one wasn't for us
   val->test_ref_count_integrity();
   val->test_ref_count_integrity();
@@ -86,7 +88,10 @@ static void click_button_down(CPT_Event e) {
 
 
 static void click_button_up(CPT_Event e) {
 static void click_button_up(CPT_Event e) {
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
-  GuiButton* val = find_in_buttons_set(rgn);
+  string button = e->get_parameter(1).get_string_value();
+  if ((button != "mouse1") && (button != "mouse2") && (button != "mouse3"))
+    return;
+  GuiButton* val = find_in_buttons_map(rgn);
   if (val == (GuiButton *)0L)
   if (val == (GuiButton *)0L)
     return;  // this one wasn't for us
     return;  // this one wasn't for us
   val->test_ref_count_integrity();
   val->test_ref_count_integrity();
@@ -406,7 +411,7 @@ GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down)
   _rgn = new MouseWatcherRegion("button-" + name, _left, _right, _bottom,
   _rgn = new MouseWatcherRegion("button-" + name, _left, _right, _bottom,
 				_top);
 				_top);
   _rgn->set_suppress_below(true);
   _rgn->set_suppress_below(true);
-  buttons.insert(this);
+  buttons[this->_rgn] = this;
 }
 }
 
 
 GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down,
 GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down,
@@ -425,7 +430,7 @@ GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* down,
   _rgn = new MouseWatcherRegion("button-" + name, _left, _right, _bottom,
   _rgn = new MouseWatcherRegion("button-" + name, _left, _right, _bottom,
 				_top);
 				_top);
   _rgn->set_suppress_below(true);
   _rgn->set_suppress_below(true);
-  buttons.insert(this);
+  buttons[this->_rgn] = this;
 }
 }
 
 
 GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* up_roll,
 GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* up_roll,
@@ -445,7 +450,7 @@ GuiButton::GuiButton(const string& name, GuiLabel* up, GuiLabel* up_roll,
   _rgn = new MouseWatcherRegion("button-" + name, _left, _right, _bottom,
   _rgn = new MouseWatcherRegion("button-" + name, _left, _right, _bottom,
 				_top);
 				_top);
   _rgn->set_suppress_below(true);
   _rgn->set_suppress_below(true);
-  buttons.insert(this);
+  buttons[this->_rgn] = this;
 }
 }
 
 
 GuiButton::~GuiButton(void) {
 GuiButton::~GuiButton(void) {
@@ -454,7 +459,7 @@ GuiButton::~GuiButton(void) {
   // Remove the names from the buttons map, so we don't end up with
   // Remove the names from the buttons map, so we don't end up with
   // an invalid pointer.
   // an invalid pointer.
   string name = get_name();
   string name = get_name();
-  buttons.erase(this);
+  buttons.erase(this->_rgn);
   if ((buttons.size() == 0) && added_hooks) {
   if ((buttons.size() == 0) && added_hooks) {
     _eh->remove_hook("gui-enter", enter_button);
     _eh->remove_hook("gui-enter", enter_button);
     _eh->remove_hook("gui-exit" + get_name(), exit_button);
     _eh->remove_hook("gui-exit" + get_name(), exit_button);

+ 13 - 14
panda/src/gui/guiRollover.cxx

@@ -6,21 +6,20 @@
 #include "guiRollover.h"
 #include "guiRollover.h"
 #include "config_gui.h"
 #include "config_gui.h"
 
 
-#include <set>
+#include <map>
 
 
-typedef set<GuiRollover*> RolloverSet;
-static RolloverSet rollovers;
+typedef map<const MouseWatcherRegion*, GuiRollover*> RolloverMap;
+static RolloverMap rollovers;
 static bool added_hooks = false;
 static bool added_hooks = false;
 
 
 TypeHandle GuiRollover::_type_handle;
 TypeHandle GuiRollover::_type_handle;
 
 
-static GuiRollover *
-find_in_rollovers_set(const MouseWatcherRegion* rgn) {
-  for (RolloverSet::const_iterator bi=rollovers.begin(); bi!=rollovers.end();
-       ++bi)
-    if ((*bi)->owns_region(rgn))
-      return *bi;
-  return (GuiRollover*)0L;
+static inline GuiRollover *
+find_in_rollovers_map(const MouseWatcherRegion* rgn) {
+  RolloverMap::iterator i = rollovers.find(rgn);
+  if (i == rollovers.end())
+    return (GuiRollover*)0L;
+  return (*i).second;
 }
 }
 
 
 inline void GetExtents(GuiLabel* x, GuiLabel* y, float& l, float& r, float& b,
 inline void GetExtents(GuiLabel* x, GuiLabel* y, float& l, float& r, float& b,
@@ -36,7 +35,7 @@ inline void GetExtents(GuiLabel* x, GuiLabel* y, float& l, float& r, float& b,
 
 
 static void enter_rollover(CPT_Event e) {
 static void enter_rollover(CPT_Event e) {
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
-  GuiRollover* val = find_in_rollovers_set(rgn);
+  GuiRollover* val = find_in_rollovers_map(rgn);
   if (val == (GuiRollover *)0L)
   if (val == (GuiRollover *)0L)
     return;  // this wasn't for us
     return;  // this wasn't for us
   val->enter();
   val->enter();
@@ -44,7 +43,7 @@ static void enter_rollover(CPT_Event e) {
 
 
 static void exit_rollover(CPT_Event e) {
 static void exit_rollover(CPT_Event e) {
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
   const MouseWatcherRegion* rgn = DCAST(MouseWatcherRegion, e->get_parameter(0).get_ptr());
-  GuiRollover* val = find_in_rollovers_set(rgn);
+  GuiRollover* val = find_in_rollovers_map(rgn);
   if (val == (GuiRollover *)0L)
   if (val == (GuiRollover *)0L)
     return;  // this wasn't for us
     return;  // this wasn't for us
   val->exit();
   val->exit();
@@ -76,7 +75,7 @@ GuiRollover::GuiRollover(const string& name, GuiLabel* off, GuiLabel* on)
   _rgn = new MouseWatcherRegion("rollover-" + name, _left, _right, _bottom,
   _rgn = new MouseWatcherRegion("rollover-" + name, _left, _right, _bottom,
 				_top);
 				_top);
   _rgn->set_suppress_below(false);
   _rgn->set_suppress_below(false);
-  rollovers.insert(this);
+  rollovers[this->_rgn] = this;
 }
 }
 
 
 GuiRollover::~GuiRollover(void) {
 GuiRollover::~GuiRollover(void) {
@@ -85,7 +84,7 @@ GuiRollover::~GuiRollover(void) {
   // Remove the names from the rollovers map, so we don't end up with
   // Remove the names from the rollovers map, so we don't end up with
   // an invalid pointer.
   // an invalid pointer.
   string name = get_name();
   string name = get_name();
-  rollovers.erase(this);
+  rollovers.erase(this->_rgn);
   if ((rollovers.size() == 0) && added_hooks) {
   if ((rollovers.size() == 0) && added_hooks) {
     _eh->remove_hook("gui-enter", enter_rollover);
     _eh->remove_hook("gui-enter", enter_rollover);
     _eh->remove_hook("gui-exit", exit_rollover);
     _eh->remove_hook("gui-exit", exit_rollover);