summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorAaron Franke <arnfranke@yahoo.com>2021-03-25 16:56:12 -0400
committerAaron Franke <arnfranke@yahoo.com>2021-06-20 11:54:24 -0400
commit0ce49800acf464c2242c3f7e021b4ab8f49ec366 (patch)
treeba1b10099dfbaf9798a16f619493a49a37701dbc /core
parente919d894f84ea86ee43a3e1b8a675b9fec28f01c (diff)
Use mouse and joypad enums instead of plain integers
Also MIDIMessage
Diffstat (limited to 'core')
-rw-r--r--core/input/input.cpp54
-rw-r--r--core/input/input.h28
-rw-r--r--core/input/input_event.cpp18
-rw-r--r--core/input/input_event.h26
-rw-r--r--core/os/midi_driver.cpp8
5 files changed, 68 insertions, 66 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp
index 384b617d13..a712394b35 100644
--- a/core/input/input.cpp
+++ b/core/input/input.cpp
@@ -227,7 +227,7 @@ bool Input::is_key_pressed(int p_keycode) const {
return keys_pressed.has(p_keycode);
}
-bool Input::is_mouse_button_pressed(int p_button) const {
+bool Input::is_mouse_button_pressed(MouseButton p_button) const {
_THREAD_SAFE_METHOD_
return (mouse_button_mask & (1 << (p_button - 1))) != 0;
}
@@ -236,7 +236,7 @@ static int _combine_device(int p_value, int p_device) {
return p_value | (p_device << 20);
}
-bool Input::is_joy_button_pressed(int p_device, int p_button) const {
+bool Input::is_joy_button_pressed(int p_device, JoyButton p_button) const {
_THREAD_SAFE_METHOD_
return joy_buttons_pressed.has(_combine_device(p_button, p_device));
}
@@ -352,7 +352,7 @@ Vector2 Input::get_vector(const StringName &p_negative_x, const StringName &p_po
return vector;
}
-float Input::get_joy_axis(int p_device, int p_axis) const {
+float Input::get_joy_axis(int p_device, JoyAxis p_axis) const {
_THREAD_SAFE_METHOD_
int c = _combine_device(p_axis, p_device);
if (_joy_axis.has(c)) {
@@ -433,7 +433,7 @@ void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, S
joy_buttons_pressed.erase(c);
}
for (int i = 0; i < JOY_AXIS_MAX; i++) {
- set_joy_axis(p_idx, i, 0.0f);
+ set_joy_axis(p_idx, (JoyAxis)i, 0.0f);
}
}
joy_names[p_idx] = js;
@@ -488,9 +488,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
if (mb.is_valid()) {
if (mb->is_pressed()) {
- mouse_button_mask |= (1 << (mb->get_button_index() - 1));
+ mouse_button_mask |= (MouseButton)(1 << (mb->get_button_index() - 1));
} else {
- mouse_button_mask &= ~(1 << (mb->get_button_index() - 1));
+ mouse_button_mask &= (MouseButton) ~(1 << (mb->get_button_index() - 1));
}
Point2 pos = mb->get_global_position();
@@ -563,9 +563,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
button_event->set_pressed(st->is_pressed());
button_event->set_button_index(MOUSE_BUTTON_LEFT);
if (st->is_pressed()) {
- button_event->set_button_mask(mouse_button_mask | (1 << (MOUSE_BUTTON_LEFT - 1)));
+ button_event->set_button_mask(MouseButton(mouse_button_mask | MOUSE_BUTTON_MASK_LEFT));
} else {
- button_event->set_button_mask(mouse_button_mask & ~(1 << (MOUSE_BUTTON_LEFT - 1)));
+ button_event->set_button_mask(MouseButton(mouse_button_mask & ~MOUSE_BUTTON_MASK_LEFT));
}
_parse_input_event_impl(button_event, true);
@@ -644,7 +644,7 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
}
}
-void Input::set_joy_axis(int p_device, int p_axis, float p_value) {
+void Input::set_joy_axis(int p_device, JoyAxis p_axis, float p_value) {
_THREAD_SAFE_METHOD_
int c = _combine_device(p_axis, p_device);
_joy_axis[c] = p_value;
@@ -794,7 +794,7 @@ void Input::ensure_touch_mouse_raised() {
button_event->set_global_position(mouse_pos);
button_event->set_pressed(false);
button_event->set_button_index(MOUSE_BUTTON_LEFT);
- button_event->set_button_mask(mouse_button_mask & ~(1 << (MOUSE_BUTTON_LEFT - 1)));
+ button_event->set_button_mask(MouseButton(mouse_button_mask & ~MOUSE_BUTTON_MASK_LEFT));
_parse_input_event_impl(button_event, true);
}
@@ -882,7 +882,7 @@ void Input::set_event_dispatch_function(EventDispatchFunc p_function) {
event_dispatch_function = p_function;
}
-void Input::joy_button(int p_device, int p_button, bool p_pressed) {
+void Input::joy_button(int p_device, JoyButton p_button, bool p_pressed) {
_THREAD_SAFE_METHOD_;
Joypad &joy = joy_names[p_device];
//printf("got button %i, mapping is %i\n", p_button, joy.mapping);
@@ -898,17 +898,17 @@ void Input::joy_button(int p_device, int p_button, bool p_pressed) {
JoyEvent map = _get_mapped_button_event(map_db[joy.mapping], p_button);
if (map.type == TYPE_BUTTON) {
- _button_event(p_device, map.index, p_pressed);
+ _button_event(p_device, (JoyButton)map.index, p_pressed);
return;
}
if (map.type == TYPE_AXIS) {
- _axis_event(p_device, map.index, p_pressed ? map.value : 0.0);
+ _axis_event(p_device, (JoyAxis)map.index, p_pressed ? map.value : 0.0);
}
// no event?
}
-void Input::joy_axis(int p_device, int p_axis, const JoyAxisValue &p_value) {
+void Input::joy_axis(int p_device, JoyAxis p_axis, const JoyAxisValue &p_value) {
_THREAD_SAFE_METHOD_;
ERR_FAIL_INDEX(p_axis, JOY_AXIS_MAX);
@@ -949,7 +949,7 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxisValue &p_value) {
// Button already pressed or released; so ignore.
return;
}
- _button_event(p_device, map.index, pressed);
+ _button_event(p_device, (JoyButton)map.index, pressed);
// Ensure opposite D-Pad button is also released.
switch (map.index) {
@@ -981,7 +981,7 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxisValue &p_value) {
}
if (map.type == TYPE_AXIS) {
- _axis_event(p_device, map.index, map.value);
+ _axis_event(p_device, (JoyAxis)map.index, map.value);
return;
}
//printf("invalid mapping\n");
@@ -1010,7 +1010,7 @@ void Input::joy_hat(int p_device, int p_val) {
map[HatDir::HAT_LEFT].value = 0;
if (joy.mapping != -1) {
- _get_mapped_hat_events(map_db[joy.mapping], 0, map);
+ _get_mapped_hat_events(map_db[joy.mapping], (HatDir)0, map);
}
int cur_val = joy_names[p_device].hat_current;
@@ -1018,10 +1018,10 @@ void Input::joy_hat(int p_device, int p_val) {
for (int hat_direction = 0, hat_mask = 1; hat_direction < HAT_MAX; hat_direction++, hat_mask <<= 1) {
if ((p_val & hat_mask) != (cur_val & hat_mask)) {
if (map[hat_direction].type == TYPE_BUTTON) {
- _button_event(p_device, map[hat_direction].index, p_val & hat_mask);
+ _button_event(p_device, (JoyButton)map[hat_direction].index, p_val & hat_mask);
}
if (map[hat_direction].type == TYPE_AXIS) {
- _axis_event(p_device, map[hat_direction].index, (p_val & hat_mask) ? map[hat_direction].value : 0.0);
+ _axis_event(p_device, (JoyAxis)map[hat_direction].index, (p_val & hat_mask) ? map[hat_direction].value : 0.0);
}
}
}
@@ -1029,7 +1029,7 @@ void Input::joy_hat(int p_device, int p_val) {
joy_names[p_device].hat_current = p_val;
}
-void Input::_button_event(int p_device, int p_index, bool p_pressed) {
+void Input::_button_event(int p_device, JoyButton p_index, bool p_pressed) {
Ref<InputEventJoypadButton> ievent;
ievent.instantiate();
ievent->set_device(p_device);
@@ -1039,7 +1039,7 @@ void Input::_button_event(int p_device, int p_index, bool p_pressed) {
parse_input_event(ievent);
}
-void Input::_axis_event(int p_device, int p_axis, float p_value) {
+void Input::_axis_event(int p_device, JoyAxis p_axis, float p_value) {
Ref<InputEventJoypadMotion> ievent;
ievent.instantiate();
ievent->set_device(p_device);
@@ -1049,7 +1049,7 @@ void Input::_axis_event(int p_device, int p_axis, float p_value) {
parse_input_event(ievent);
}
-Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button) {
+Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button) {
JoyEvent event;
event.type = TYPE_MAX;
@@ -1085,7 +1085,7 @@ Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping,
return event;
}
-Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, int p_axis, float p_value) {
+Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value) {
JoyEvent event;
event.type = TYPE_MAX;
@@ -1156,7 +1156,7 @@ Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, i
return event;
}
-void Input::_get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, JoyEvent r_events[]) {
+void Input::_get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[]) {
for (int i = 0; i < mapping.bindings.size(); i++) {
const JoyBinding binding = mapping.bindings[i];
if (binding.inputType == TYPE_HAT && binding.input.hat.hat == p_hat) {
@@ -1300,11 +1300,11 @@ void Input::parse_mapping(String p_mapping) {
switch (input[0]) {
case 'b':
binding.inputType = TYPE_BUTTON;
- binding.input.button = input.substr(1).to_int();
+ binding.input.button = (JoyButton)input.substr(1).to_int();
break;
case 'a':
binding.inputType = TYPE_AXIS;
- binding.input.axis.axis = input.substr(1).to_int();
+ binding.input.axis.axis = (JoyAxis)input.substr(1).to_int();
binding.input.axis.range = input_range;
binding.input.axis.invert = invert_axis;
break;
@@ -1312,7 +1312,7 @@ void Input::parse_mapping(String p_mapping) {
ERR_CONTINUE_MSG(input.length() != 4 || input[2] != '.',
String(entry[idx] + "\nInvalid hat input: " + input));
binding.inputType = TYPE_HAT;
- binding.input.hat.hat = input.substr(1, 1).to_int();
+ binding.input.hat.hat = (HatDir)input.substr(1, 1).to_int();
binding.input.hat.hat_mask = static_cast<HatMask>(input.substr(3).to_int());
break;
default:
diff --git a/core/input/input.h b/core/input/input.h
index 0d9976cb40..fbcd5836ea 100644
--- a/core/input/input.h
+++ b/core/input/input.h
@@ -167,16 +167,16 @@ private:
struct JoyBinding {
JoyType inputType;
union {
- int button;
+ JoyButton button;
struct {
- int axis;
+ JoyAxis axis;
JoyAxisRange range;
bool invert;
} axis;
struct {
- int hat;
+ HatDir hat;
HatMask hat_mask;
} hat;
@@ -202,13 +202,13 @@ private:
Vector<JoyDeviceMapping> map_db;
- JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button);
- JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, int p_axis, float p_value);
- void _get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, JoyEvent r_events[HAT_MAX]);
+ JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button);
+ JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value);
+ void _get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[HAT_MAX]);
JoyButton _get_output_button(String output);
JoyAxis _get_output_axis(String output);
- void _button_event(int p_device, int p_index, bool p_pressed);
- void _axis_event(int p_device, int p_axis, float p_value);
+ void _button_event(int p_device, JoyButton p_index, bool p_pressed);
+ void _axis_event(int p_device, JoyAxis p_axis, float p_value);
void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
@@ -245,8 +245,8 @@ public:
static Input *get_singleton();
bool is_key_pressed(int p_keycode) const;
- bool is_mouse_button_pressed(int p_button) const;
- bool is_joy_button_pressed(int p_device, int p_button) const;
+ bool is_mouse_button_pressed(MouseButton p_button) const;
+ bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
@@ -256,7 +256,7 @@ public:
float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const;
Vector2 get_vector(const StringName &p_negative_x, const StringName &p_positive_x, const StringName &p_negative_y, const StringName &p_positive_y, float p_deadzone = -1.0f) const;
- float get_joy_axis(int p_device, int p_axis) const;
+ float get_joy_axis(int p_device, JoyAxis p_axis) const;
String get_joy_name(int p_idx);
Array get_connected_joypads();
Vector2 get_joy_vibration_strength(int p_device);
@@ -283,7 +283,7 @@ public:
void set_accelerometer(const Vector3 &p_accel);
void set_magnetometer(const Vector3 &p_magnetometer);
void set_gyroscope(const Vector3 &p_gyroscope);
- void set_joy_axis(int p_device, int p_axis, float p_value);
+ void set_joy_axis(int p_device, JoyAxis p_axis, float p_value);
void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
void stop_joy_vibration(int p_device);
@@ -309,8 +309,8 @@ public:
void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
void parse_mapping(String p_mapping);
- void joy_button(int p_device, int p_button, bool p_pressed);
- void joy_axis(int p_device, int p_axis, const JoyAxisValue &p_value);
+ void joy_button(int p_device, JoyButton p_button, bool p_pressed);
+ void joy_axis(int p_device, JoyAxis p_axis, const JoyAxisValue &p_value);
void joy_hat(int p_device, int p_val);
void add_joy_mapping(String p_mapping, bool p_update_existing = false);
diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp
index 14762f955c..52a6c5d64f 100644
--- a/core/input/input_event.cpp
+++ b/core/input/input_event.cpp
@@ -516,11 +516,11 @@ float InputEventMouseButton::get_factor() const {
return factor;
}
-void InputEventMouseButton::set_button_index(int p_index) {
+void InputEventMouseButton::set_button_index(MouseButton p_index) {
button_index = p_index;
}
-int InputEventMouseButton::get_button_index() const {
+MouseButton InputEventMouseButton::get_button_index() const {
return button_index;
}
@@ -845,11 +845,11 @@ void InputEventMouseMotion::_bind_methods() {
///////////////////////////////////
-void InputEventJoypadMotion::set_axis(int p_axis) {
+void InputEventJoypadMotion::set_axis(JoyAxis p_axis) {
axis = p_axis;
}
-int InputEventJoypadMotion::get_axis() const {
+JoyAxis InputEventJoypadMotion::get_axis() const {
return axis;
}
@@ -947,11 +947,11 @@ void InputEventJoypadMotion::_bind_methods() {
///////////////////////////////////
-void InputEventJoypadButton::set_button_index(int p_index) {
+void InputEventJoypadButton::set_button_index(JoyButton p_index) {
button_index = p_index;
}
-int InputEventJoypadButton::get_button_index() const {
+JoyButton InputEventJoypadButton::get_button_index() const {
return button_index;
}
@@ -1046,7 +1046,7 @@ String InputEventJoypadButton::to_string() {
return vformat("InputEventJoypadButton: button_index=%d, pressed=%s, pressure=%.2f", button_index, p, pressure);
}
-Ref<InputEventJoypadButton> InputEventJoypadButton::create_reference(int p_btn_index) {
+Ref<InputEventJoypadButton> InputEventJoypadButton::create_reference(JoyButton p_btn_index) {
Ref<InputEventJoypadButton> ie;
ie.instantiate();
ie->set_button_index(p_btn_index);
@@ -1403,11 +1403,11 @@ int InputEventMIDI::get_channel() const {
return channel;
}
-void InputEventMIDI::set_message(const int p_message) {
+void InputEventMIDI::set_message(const MIDIMessage p_message) {
message = p_message;
}
-int InputEventMIDI::get_message() const {
+MIDIMessage InputEventMIDI::get_message() const {
return message;
}
diff --git a/core/input/input_event.h b/core/input/input_event.h
index d5a034f44d..76a45c04a4 100644
--- a/core/input/input_event.h
+++ b/core/input/input_event.h
@@ -230,7 +230,7 @@ class InputEventMouseButton : public InputEventMouse {
GDCLASS(InputEventMouseButton, InputEventMouse);
float factor = 1;
- int button_index = 0;
+ MouseButton button_index = MOUSE_BUTTON_NONE;
bool pressed = false; //otherwise released
bool double_click = false; //last even less than double click time
@@ -241,8 +241,8 @@ public:
void set_factor(float p_factor);
float get_factor() const;
- void set_button_index(int p_index);
- int get_button_index() const;
+ void set_button_index(MouseButton p_index);
+ MouseButton get_button_index() const;
void set_pressed(bool p_pressed);
virtual bool is_pressed() const override;
@@ -297,15 +297,15 @@ public:
class InputEventJoypadMotion : public InputEvent {
GDCLASS(InputEventJoypadMotion, InputEvent);
- int axis = 0; ///< Joypad axis
+ JoyAxis axis = (JoyAxis)0; ///< Joypad axis
float axis_value = 0; ///< -1 to 1
protected:
static void _bind_methods();
public:
- void set_axis(int p_axis);
- int get_axis() const;
+ void set_axis(JoyAxis p_axis);
+ JoyAxis get_axis() const;
void set_axis_value(float p_value);
float get_axis_value() const;
@@ -325,15 +325,15 @@ public:
class InputEventJoypadButton : public InputEvent {
GDCLASS(InputEventJoypadButton, InputEvent);
- int button_index = 0;
+ JoyButton button_index = (JoyButton)0;
bool pressed = false;
float pressure = 0; //0 to 1
protected:
static void _bind_methods();
public:
- void set_button_index(int p_index);
- int get_button_index() const;
+ void set_button_index(JoyButton p_index);
+ JoyButton get_button_index() const;
void set_pressed(bool p_pressed);
virtual bool is_pressed() const override;
@@ -349,7 +349,7 @@ public:
virtual String as_text() const override;
virtual String to_string() override;
- static Ref<InputEventJoypadButton> create_reference(int p_btn_index);
+ static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
InputEventJoypadButton() {}
};
@@ -496,7 +496,7 @@ class InputEventMIDI : public InputEvent {
GDCLASS(InputEventMIDI, InputEvent);
int channel = 0;
- int message = 0;
+ MIDIMessage message = MIDI_MESSAGE_NONE;
int pitch = 0;
int velocity = 0;
int instrument = 0;
@@ -511,8 +511,8 @@ public:
void set_channel(const int p_channel);
int get_channel() const;
- void set_message(const int p_message);
- int get_message() const;
+ void set_message(const MIDIMessage p_message);
+ MIDIMessage get_message() const;
void set_pitch(const int p_pitch);
int get_pitch() const;
diff --git a/core/os/midi_driver.cpp b/core/os/midi_driver.cpp
index a33bc6b4c3..ee87346dfc 100644
--- a/core/os/midi_driver.cpp
+++ b/core/os/midi_driver.cpp
@@ -52,16 +52,16 @@ void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_
if (data[0] >= 0xF0) {
// channel does not apply to system common messages
event->set_channel(0);
- event->set_message(data[0]);
+ event->set_message(MIDIMessage(data[0]));
last_received_message = data[0];
} else if ((data[0] & 0x80) == 0x00) {
// running status
event->set_channel(last_received_message & 0xF);
- event->set_message(last_received_message >> 4);
+ event->set_message(MIDIMessage(last_received_message >> 4));
param_position = 0;
} else {
event->set_channel(data[0] & 0xF);
- event->set_message(data[0] >> 4);
+ event->set_message(MIDIMessage(data[0] >> 4));
param_position = 1;
last_received_message = data[0];
}
@@ -112,6 +112,8 @@ void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_
event->set_pressure(data[param_position]);
}
break;
+ default:
+ break;
}
Input *id = Input::get_singleton();