Exposed AnalogState and ButtonState
Published Analog- and ButtonState to be accessible from python Added functions to find Controls and buttons to simplify access Refactored highscore search for setting the device class
This commit is contained in:
parent
cf63a6cccb
commit
e1661111c2
|
|
@ -324,16 +324,11 @@ init_device() {
|
|||
}
|
||||
|
||||
// Check which device type got the most points
|
||||
bool highscore_found = false;
|
||||
size_t highest_score = 0;
|
||||
while (!highscore_found) {
|
||||
highscore_found = true;
|
||||
for (size_t i = 0; i < DC_COUNT; i++) {
|
||||
if (device_scores[i] > highest_score) {
|
||||
highest_score = device_scores[i];
|
||||
_device_class = (DeviceClass)i;
|
||||
highscore_found = false;
|
||||
}
|
||||
for (size_t i = 0; i < DC_COUNT; i++) {
|
||||
if (device_scores[i] > highest_score) {
|
||||
highest_score = device_scores[i];
|
||||
_device_class = (DeviceClass)i;
|
||||
}
|
||||
}
|
||||
//cerr << "Found highscore class " << _device_class << " with this score: " << highest_score << "\n";
|
||||
|
|
@ -354,12 +349,12 @@ init_device() {
|
|||
set_button_map(bi, map_button(i));
|
||||
//cerr << "Button " << bi << " is mapped by the driver to " << i << "\n";
|
||||
if (test_bit(i, states)) {
|
||||
_buttons[bi]._state = S_down;
|
||||
_buttons[bi].state = S_down;
|
||||
all_values_zero = false;
|
||||
} else {
|
||||
_buttons[bi]._state = S_up;
|
||||
_buttons[bi].state = S_up;
|
||||
}
|
||||
if (_buttons[bi]._handle == GamepadButton::dpad_left()) {
|
||||
if (_buttons[bi].handle == GamepadButton::dpad_left()) {
|
||||
emulate_dpad = false;
|
||||
}
|
||||
++bi;
|
||||
|
|
@ -419,7 +414,7 @@ init_device() {
|
|||
|
||||
_axis_ranges[i]._scale = factor;
|
||||
_axis_ranges[i]._bias = bias;
|
||||
_controls[i]._state = fma(absinfo.value, factor, bias);
|
||||
_controls[i].state = fma(absinfo.value, factor, bias);
|
||||
|
||||
if (absinfo.value != 0) {
|
||||
all_values_zero = false;
|
||||
|
|
|
|||
|
|
@ -197,7 +197,7 @@ set_button_map(int index, ButtonHandle button) {
|
|||
_buttons.resize(index + 1, ButtonState());
|
||||
}
|
||||
|
||||
_buttons[index]._handle = button;
|
||||
_buttons[index].handle = button;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -208,7 +208,7 @@ set_button_map(int index, ButtonHandle button) {
|
|||
INLINE ButtonHandle InputDevice::
|
||||
get_button_map(int index) const {
|
||||
if (index >= 0 && index < (int)_buttons.size()) {
|
||||
return _buttons[index]._handle;
|
||||
return _buttons[index].handle;
|
||||
} else {
|
||||
return ButtonHandle::none();
|
||||
}
|
||||
|
|
@ -221,7 +221,7 @@ get_button_map(int index) const {
|
|||
INLINE bool InputDevice::
|
||||
get_button_state(int index) const {
|
||||
if (index >= 0 && index < (int)_buttons.size()) {
|
||||
return (_buttons[index]._state == S_down);
|
||||
return (_buttons[index].state == S_down);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -234,12 +234,43 @@ get_button_state(int index) const {
|
|||
INLINE bool InputDevice::
|
||||
is_button_known(int index) const {
|
||||
if (index >= 0 && index < (int)_buttons.size()) {
|
||||
return _buttons[index]._state != S_unknown;
|
||||
return _buttons[index].state != S_unknown;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ButtonState that is set at the given index, or throw an assection
|
||||
* if the index was not found in the list.
|
||||
*/
|
||||
INLINE InputDevice::ButtonState InputDevice::
|
||||
get_button(size_t index) const {
|
||||
if (index >= 0 && index < (int)_buttons.size()) {
|
||||
return _buttons[index];
|
||||
} else {
|
||||
/*device_cat.error()
|
||||
<< "Index " << index<< " was not found in the controls list\n";*/
|
||||
nassertr(false, ButtonState());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first ButtonState found with the given axis, or throw an assection
|
||||
* if the button handle was not found in the list.
|
||||
*/
|
||||
INLINE InputDevice::ButtonState InputDevice::
|
||||
find_button(ButtonHandle handle) const {
|
||||
for (int i; i < (int)_buttons.size(); i++) {
|
||||
if (_buttons[i].handle == handle) {
|
||||
return _buttons[i];
|
||||
}
|
||||
}
|
||||
/*device_cat.error()
|
||||
<< "Axis " << axis << " was not found in the controls list\n";*/
|
||||
nassertr(false, ButtonState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of analog controls known to the InputDevice. This number
|
||||
* may change as more controls are discovered.
|
||||
|
|
@ -264,7 +295,7 @@ set_control_map(int index, ControlAxis axis) {
|
|||
_controls.resize(index + 1, AnalogState());
|
||||
}
|
||||
|
||||
_controls[index]._axis = axis;
|
||||
_controls[index].axis = axis;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -275,7 +306,7 @@ set_control_map(int index, ControlAxis axis) {
|
|||
INLINE InputDevice::ControlAxis InputDevice::
|
||||
get_control_map(int index) const {
|
||||
if (index >= 0 && index < (int)_controls.size()) {
|
||||
return _controls[index]._axis;
|
||||
return _controls[index].axis;
|
||||
} else {
|
||||
return C_none;
|
||||
}
|
||||
|
|
@ -289,12 +320,43 @@ get_control_map(int index) const {
|
|||
INLINE double InputDevice::
|
||||
get_control_state(int index) const {
|
||||
if (index >= 0 && index < (int)_controls.size()) {
|
||||
return _controls[index]._state;
|
||||
return _controls[index].state;
|
||||
} else {
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the AnalogAxis that is set at the given index, or throw an assection
|
||||
* if the index was not found in the list.
|
||||
*/
|
||||
INLINE InputDevice::AnalogState InputDevice::
|
||||
get_control(size_t index) const {
|
||||
if (index >= 0 && index < (int)_controls.size()) {
|
||||
return _controls[index];
|
||||
} else {
|
||||
/*device_cat.error()
|
||||
<< "Index " << index<< " was not found in the controls list\n";*/
|
||||
nassertr(false, AnalogState());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first AnalogAxis found with the given axis, or throw an assection
|
||||
* if the axis was not found in the list.
|
||||
*/
|
||||
INLINE InputDevice::AnalogState InputDevice::
|
||||
find_control(ControlAxis axis) const {
|
||||
for (int i; i < (int)_controls.size(); i++) {
|
||||
if (_controls[i].axis == axis) {
|
||||
return _controls[i];
|
||||
}
|
||||
}
|
||||
/*device_cat.error()
|
||||
<< "Axis " << axis << " was not found in the controls list\n";*/
|
||||
nassertr(false, AnalogState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the state of the indicated analog control is known, or false
|
||||
* if we have never heard anything about this particular control.
|
||||
|
|
@ -302,7 +364,7 @@ get_control_state(int index) const {
|
|||
INLINE bool InputDevice::
|
||||
is_control_known(int index) const {
|
||||
if (index >= 0 && index < (int)_controls.size()) {
|
||||
return _controls[index]._known;
|
||||
return _controls[index].known;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -377,8 +439,8 @@ operator < (const InputDevice &) const {
|
|||
*/
|
||||
INLINE InputDevice::ButtonState::
|
||||
ButtonState() :
|
||||
_handle(ButtonHandle::none()),
|
||||
_state(S_unknown)
|
||||
handle(ButtonHandle::none()),
|
||||
state(S_unknown)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -387,8 +449,8 @@ ButtonState() :
|
|||
*/
|
||||
INLINE InputDevice::ButtonState::
|
||||
ButtonState(ButtonHandle handle) :
|
||||
_handle(handle),
|
||||
_state(S_unknown)
|
||||
handle(handle),
|
||||
state(S_unknown)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -397,8 +459,8 @@ ButtonState(ButtonHandle handle) :
|
|||
*/
|
||||
INLINE InputDevice::AnalogState::
|
||||
AnalogState() :
|
||||
_axis(C_none),
|
||||
_state(0.0),
|
||||
_known(false)
|
||||
axis(C_none),
|
||||
state(0.0),
|
||||
known(false)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -171,12 +171,12 @@ set_button_state(int index, bool down) {
|
|||
}
|
||||
|
||||
State new_state = down ? S_down : S_up;
|
||||
if (_buttons[index]._state == new_state) {
|
||||
if (_buttons[index].state == new_state) {
|
||||
return;
|
||||
}
|
||||
_buttons[index]._state = new_state;
|
||||
_buttons[index].state = new_state;
|
||||
|
||||
ButtonHandle handle = _buttons[index]._handle;
|
||||
ButtonHandle handle = _buttons[index].handle;
|
||||
|
||||
if (device_cat.is_spam()) {
|
||||
device_cat.spam()
|
||||
|
|
@ -208,19 +208,19 @@ set_control_state(int index, double state) {
|
|||
_controls.resize(index + 1, AnalogState());
|
||||
}
|
||||
|
||||
if (device_cat.is_spam() && _controls[index]._state != state) {
|
||||
if (device_cat.is_spam() && _controls[index].state != state) {
|
||||
device_cat.spam()
|
||||
<< "Changed control " << index;
|
||||
|
||||
if (_controls[index]._axis != C_none) {
|
||||
device_cat.spam(false) << " (" << _controls[index]._axis << ")";
|
||||
if (_controls[index].known != C_none) {
|
||||
device_cat.spam(false) << " (" << _controls[index].known << ")";
|
||||
}
|
||||
|
||||
device_cat.spam(false) << " to " << state << "\n";
|
||||
}
|
||||
|
||||
_controls[index]._state = state;
|
||||
_controls[index]._known = true;
|
||||
_controls[index].state = state;
|
||||
_controls[index].known = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -308,13 +308,13 @@ output_buttons(ostream &out) const {
|
|||
Buttons::const_iterator bi;
|
||||
for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
|
||||
const ButtonState &state = (*bi);
|
||||
if (state._state != S_unknown) {
|
||||
if (state.state != S_unknown) {
|
||||
if (any_buttons) {
|
||||
out << ", ";
|
||||
}
|
||||
any_buttons = true;
|
||||
out << (int)(bi - _buttons.begin()) << "=";
|
||||
if (state._state == S_up) {
|
||||
if (state.state == S_up) {
|
||||
out << "up";
|
||||
} else {
|
||||
out << "down";
|
||||
|
|
@ -336,17 +336,17 @@ write_buttons(ostream &out, int indent_level) const {
|
|||
Buttons::const_iterator bi;
|
||||
for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
|
||||
const ButtonState &state = (*bi);
|
||||
if (state._state != S_unknown) {
|
||||
if (state.state != S_unknown) {
|
||||
any_buttons = true;
|
||||
|
||||
indent(out, indent_level)
|
||||
<< (int)(bi - _buttons.begin()) << ". ";
|
||||
|
||||
if (state._handle != ButtonHandle::none()) {
|
||||
out << "(" << state._handle << ") ";
|
||||
if (state.handle != ButtonHandle::none()) {
|
||||
out << "(" << state.handle << ") ";
|
||||
}
|
||||
|
||||
if (state._state == S_up) {
|
||||
if (state.state == S_up) {
|
||||
out << "up";
|
||||
} else {
|
||||
out << "down";
|
||||
|
|
@ -372,11 +372,11 @@ write_controls(ostream &out, int indent_level) const {
|
|||
Controls::const_iterator ai;
|
||||
for (ai = _controls.begin(); ai != _controls.end(); ++ai) {
|
||||
const AnalogState &state = (*ai);
|
||||
if (state._known) {
|
||||
if (state.known) {
|
||||
any_controls = true;
|
||||
|
||||
indent(out, indent_level)
|
||||
<< (int)(ai - _controls.begin()) << ". " << state._state << "\n";
|
||||
<< (int)(ai - _controls.begin()) << ". " << state.state << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -250,13 +250,15 @@ public:
|
|||
S_down
|
||||
};
|
||||
|
||||
PUBLISHED:
|
||||
class ButtonState {
|
||||
public:
|
||||
INLINE ButtonState();
|
||||
INLINE ButtonState(ButtonHandle handle);
|
||||
|
||||
ButtonHandle _handle;
|
||||
State _state;
|
||||
PUBLISHED:
|
||||
ButtonHandle handle;
|
||||
State state;
|
||||
};
|
||||
typedef pvector<ButtonState> Buttons;
|
||||
Buttons _buttons;
|
||||
|
|
@ -265,9 +267,10 @@ public:
|
|||
public:
|
||||
INLINE AnalogState();
|
||||
|
||||
ControlAxis _axis;
|
||||
double _state;
|
||||
bool _known;
|
||||
PUBLISHED:
|
||||
ControlAxis axis;
|
||||
double state;
|
||||
bool known;
|
||||
};
|
||||
typedef pvector<AnalogState> Controls;
|
||||
Controls _controls;
|
||||
|
|
@ -277,6 +280,16 @@ public:
|
|||
|
||||
TrackerData _tracker_data;
|
||||
|
||||
|
||||
INLINE ButtonState get_button(size_t index) const;
|
||||
INLINE ButtonState find_button(ButtonHandle handle) const;
|
||||
|
||||
INLINE AnalogState get_control(size_t index) const;
|
||||
INLINE AnalogState find_control(ControlAxis axis) const;
|
||||
|
||||
// Make device controls iterable
|
||||
MAKE_SEQ_PROPERTY(controls, get_num_controls, get_control);
|
||||
|
||||
public:
|
||||
static TypeHandle get_class_type() {
|
||||
return _type_handle;
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ open_device() {
|
|||
handle = ButtonHandle::none();
|
||||
break;
|
||||
}
|
||||
_buttons[i]._handle = handle;
|
||||
_buttons[i].handle = handle;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -327,7 +327,7 @@ open_device() {
|
|||
axis = C_none;
|
||||
break;
|
||||
}
|
||||
_controls[i]._axis = axis;
|
||||
_controls[i].axis = axis;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -389,7 +389,7 @@ open_device() {
|
|||
// this gamepad yet (which means it hasn't been plugged in for this session)
|
||||
if (strncmp(name, "Xbox 360 Wireless Receiver", 26) == 0) {
|
||||
for (int i = 0; i < _controls.size(); ++i) {
|
||||
if (_controls[i]._state != 0.0) {
|
||||
if (_controls[i].state != 0.0) {
|
||||
_is_connected = true;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -453,7 +453,7 @@ process_events() {
|
|||
set_button_state(_dpad_up_button+1, events[i].value > 1000);
|
||||
}
|
||||
|
||||
ControlAxis axis = _controls[index]._axis;
|
||||
ControlAxis axis = _controls[index].axis;
|
||||
|
||||
if (axis == C_left_trigger || axis == C_right_trigger || axis == C_trigger) {
|
||||
// We'd like to use 0.0 to indicate the resting position.
|
||||
|
|
|
|||
Loading…
Reference in New Issue