Browse Source

device: a few InputDevice API tweaks

find_button() and find_axis() should not error since there is no way to find out whether a button/axis exists or not
rdb 7 years ago
parent
commit
29170278e9
2 changed files with 10 additions and 18 deletions
  1. 4 18
      panda/src/device/inputDevice.I
  2. 6 0
      panda/src/device/inputDevice.h

+ 4 - 18
panda/src/device/inputDevice.I

@@ -250,13 +250,8 @@ is_button_known(size_t index) const {
  */
  */
 INLINE InputDevice::ButtonState InputDevice::
 INLINE InputDevice::ButtonState InputDevice::
 get_button(size_t index) const {
 get_button(size_t index) const {
-  if (index < _buttons.size()) {
-    return _buttons[index];
-  } else {
-    device_cat.error()
-      << "Index " << index << " was not found in the axes list\n";
-    return ButtonState();
-  }
+  nassertr_always(index < _buttons.size(), ButtonState());
+  return _buttons[index];
 }
 }
 
 
 /**
 /**
@@ -270,8 +265,6 @@ find_button(ButtonHandle handle) const {
       return _buttons[i];
       return _buttons[i];
     }
     }
   }
   }
-  device_cat.error()
-    << "Handle " << handle.get_name() << " was not found in the axes list\n";
   return ButtonState();
   return ButtonState();
 }
 }
 
 
@@ -321,13 +314,8 @@ get_axis_value(size_t index) const {
  */
  */
 INLINE InputDevice::AxisState InputDevice::
 INLINE InputDevice::AxisState InputDevice::
 get_axis(size_t index) const {
 get_axis(size_t index) const {
-  if (index < _axes.size()) {
-    return _axes[index];
-  } else {
-    device_cat.error()
-      << "Index " << index << " was not found in the axes list\n";
-    return AxisState();
-  }
+  nassertr_always(index < _axes.size(), AxisState());
+  return _axes[index];
 }
 }
 
 
 /**
 /**
@@ -341,8 +329,6 @@ find_axis(InputDevice::Axis axis) const {
       return _axes[i];
       return _axes[i];
     }
     }
   }
   }
-  device_cat.error()
-    << "Axis " << axis << " was not found in the axes list\n";
   return AxisState();
   return AxisState();
 }
 }
 
 

+ 6 - 0
panda/src/device/inputDevice.h

@@ -142,6 +142,8 @@ PUBLISHED:
     ALWAYS_INLINE bool is_pressed() const;
     ALWAYS_INLINE bool is_pressed() const;
 
 
   PUBLISHED:
   PUBLISHED:
+    operator bool() { return _state != S_unknown; }
+
     MAKE_PROPERTY(known, is_known);
     MAKE_PROPERTY(known, is_known);
     MAKE_PROPERTY(pressed, is_pressed);
     MAKE_PROPERTY(pressed, is_pressed);
 
 
@@ -156,6 +158,8 @@ PUBLISHED:
     constexpr AxisState() = default;
     constexpr AxisState() = default;
 
 
   PUBLISHED:
   PUBLISHED:
+    operator bool() { return known && value != 0.0; }
+
     Axis axis = Axis::none;
     Axis axis = Axis::none;
     double value = 0.0;
     double value = 0.0;
     bool known = false;
     bool known = false;
@@ -268,6 +272,8 @@ PUBLISHED:
   PT(PointerEventList) get_pointer_events();
   PT(PointerEventList) get_pointer_events();
 
 
   virtual void output(std::ostream &out) const;
   virtual void output(std::ostream &out) const;
+
+public:
   static std::string format_device_class(DeviceClass dc);
   static std::string format_device_class(DeviceClass dc);
   static std::string format_axis(Axis axis);
   static std::string format_axis(Axis axis);