From 87dab29f4b71c47cc13e9974dc9462e6109aef96 Mon Sep 17 00:00:00 2001 From: hondres Date: Fri, 18 Dec 2015 19:15:32 +0100 Subject: Use tabs instead of spaces for new gamepad code --- main/input_default.cpp | 624 ++++++++++++++++++++++++------------------------- 1 file changed, 312 insertions(+), 312 deletions(-) (limited to 'main/input_default.cpp') diff --git a/main/input_default.cpp b/main/input_default.cpp index cb740e9ce..c90d45686 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -127,48 +127,48 @@ String InputDefault::get_joy_name(int p_idx) { static String _hex_str(uint8_t p_byte) { - static const char* dict = "0123456789abcdef"; - char ret[3]; - ret[2] = 0; + static const char* dict = "0123456789abcdef"; + char ret[3]; + ret[2] = 0; - ret[0] = dict[p_byte>>4]; - ret[1] = dict[p_byte & 0xf]; + ret[0] = dict[p_byte>>4]; + ret[1] = dict[p_byte & 0xf]; - return ret; + return ret; }; void InputDefault::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) { _THREAD_SAFE_METHOD_ - Joystick js; - js.name = p_connected ? p_name : ""; - js.uid = p_connected ? p_guid : ""; - js.mapping = -1; - js.hat_current = 0; - - if (p_connected) { - - String uidname = p_guid; - if (p_guid == "") { - int uidlen = MIN(p_name.length(), 16); - for (int i=0; i::Element* el = map_db[joy.mapping].buttons.find(p_button); - if (!el) { - //don't process un-mapped events for now, it could mess things up badly for devices with additional buttons/axis - //return _button_event(p_last_id, p_device, p_button, p_pressed); - return p_last_id; - }; - - JoyEvent map = el->get(); - if (map.type == TYPE_BUTTON) { - - return _button_event(p_last_id, p_device, map.index, p_pressed); - }; - - if (map.type == TYPE_AXIS) { - return _axis_event(p_last_id, p_device, map.index, p_pressed ? 1.0 : 0.0); - }; - - return p_last_id; // no event? + _THREAD_SAFE_METHOD_; + Joystick& joy = joy_names[p_device]; + //printf("got button %i, mapping is %i\n", p_button, joy.mapping); + if (joy.last_buttons[p_button] == p_pressed) { + return p_last_id; + //printf("same button value\n"); + } + joy.last_buttons[p_button] = p_pressed; + if (joy.mapping == -1) { + return _button_event(p_last_id, p_device, p_button, p_pressed); + }; + + Map::Element* el = map_db[joy.mapping].buttons.find(p_button); + if (!el) { + //don't process un-mapped events for now, it could mess things up badly for devices with additional buttons/axis + //return _button_event(p_last_id, p_device, p_button, p_pressed); + return p_last_id; + }; + + JoyEvent map = el->get(); + if (map.type == TYPE_BUTTON) { + + return _button_event(p_last_id, p_device, map.index, p_pressed); + }; + + if (map.type == TYPE_AXIS) { + return _axis_event(p_last_id, p_device, map.index, p_pressed ? 1.0 : 0.0); + }; + + return p_last_id; // no event? }; uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, const JoyAxis& p_value) { - _THREAD_SAFE_METHOD_; + _THREAD_SAFE_METHOD_; - Joystick& joy = joy_names[p_device]; + Joystick& joy = joy_names[p_device]; - if (joy.last_axis[p_axis] == p_value.value) { - return p_last_id; - } + if (joy.last_axis[p_axis] == p_value.value) { + return p_last_id; + } - if (p_value.value > joy.last_axis[p_axis]) { + if (p_value.value > joy.last_axis[p_axis]) { - if (p_value.value < joy.last_axis[p_axis] + joy.filter ) { + if (p_value.value < joy.last_axis[p_axis] + joy.filter ) { - return p_last_id; - } - } - else if (p_value.value > joy.last_axis[p_axis] - joy.filter) { + return p_last_id; + } + } + else if (p_value.value > joy.last_axis[p_axis] - joy.filter) { - return p_last_id; - } + return p_last_id; + } - joy.last_axis[p_axis] = p_value.value; - if (joy.mapping == -1) { - float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value; - return _axis_event(p_last_id, p_device, p_axis, val); - }; + joy.last_axis[p_axis] = p_value.value; + if (joy.mapping == -1) { + float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value; + return _axis_event(p_last_id, p_device, p_axis, val); + }; - Map::Element* el = map_db[joy.mapping].axis.find(p_axis); - if (!el) { - //return _axis_event(p_last_id, p_device, p_axis, p_value); - return p_last_id; - }; + Map::Element* el = map_db[joy.mapping].axis.find(p_axis); + if (!el) { + //return _axis_event(p_last_id, p_device, p_axis, p_value); + return p_last_id; + }; - JoyEvent map = el->get(); + JoyEvent map = el->get(); - if (map.type == TYPE_BUTTON) { - float deadzone = p_value.min == 0 ? 0.5f : 0.0f; - return _button_event(p_last_id, p_device, map.index, p_value.value > deadzone ? true : false); - }; + if (map.type == TYPE_BUTTON) { + float deadzone = p_value.min == 0 ? 0.5f : 0.0f; + return _button_event(p_last_id, p_device, map.index, p_value.value > deadzone ? true : false); + }; - if (map.type == TYPE_AXIS) { + if (map.type == TYPE_AXIS) { - float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value; - return _axis_event(p_last_id, p_device, map.index, val ); - }; - //printf("invalid mapping\n"); - return p_last_id; + float val = p_value.min == 0 ? -1.0f + 2.0f * p_value.value : p_value.value; + return _axis_event(p_last_id, p_device, map.index, val ); + }; + //printf("invalid mapping\n"); + return p_last_id; }; uint32_t InputDefault::joy_hat(uint32_t p_last_id, int p_device, int p_val) { - _THREAD_SAFE_METHOD_; - const Joystick& joy = joy_names[p_device]; + _THREAD_SAFE_METHOD_; + const Joystick& joy = joy_names[p_device]; - JoyEvent* map; + JoyEvent* map; - if (joy.mapping == -1) { - map = hat_map_default; - } else { - map = map_db[joy.mapping].hat; - }; + if (joy.mapping == -1) { + map = hat_map_default; + } else { + map = map_db[joy.mapping].hat; + }; - int cur_val = joy_names[p_device].hat_current; + int cur_val = joy_names[p_device].hat_current; - if ( (p_val & HAT_MASK_UP) != (cur_val & HAT_MASK_UP) ) { - p_last_id = _button_event(p_last_id, p_device, map[HAT_UP].index, p_val & HAT_MASK_UP); - }; + if ( (p_val & HAT_MASK_UP) != (cur_val & HAT_MASK_UP) ) { + p_last_id = _button_event(p_last_id, p_device, map[HAT_UP].index, p_val & HAT_MASK_UP); + }; - if ( (p_val & HAT_MASK_RIGHT) != (cur_val & HAT_MASK_RIGHT) ) { - p_last_id = _button_event(p_last_id, p_device, map[HAT_RIGHT].index, p_val & HAT_MASK_RIGHT); - }; - if ( (p_val & HAT_MASK_DOWN) != (cur_val & HAT_MASK_DOWN) ) { - p_last_id = _button_event(p_last_id, p_device, map[HAT_DOWN].index, p_val & HAT_MASK_DOWN); - }; - if ( (p_val & HAT_MASK_LEFT) != (cur_val & HAT_MASK_LEFT) ) { - p_last_id = _button_event(p_last_id, p_device, map[HAT_LEFT].index, p_val & HAT_MASK_LEFT); - }; + if ( (p_val & HAT_MASK_RIGHT) != (cur_val & HAT_MASK_RIGHT) ) { + p_last_id = _button_event(p_last_id, p_device, map[HAT_RIGHT].index, p_val & HAT_MASK_RIGHT); + }; + if ( (p_val & HAT_MASK_DOWN) != (cur_val & HAT_MASK_DOWN) ) { + p_last_id = _button_event(p_last_id, p_device, map[HAT_DOWN].index, p_val & HAT_MASK_DOWN); + }; + if ( (p_val & HAT_MASK_LEFT) != (cur_val & HAT_MASK_LEFT) ) { + p_last_id = _button_event(p_last_id, p_device, map[HAT_LEFT].index, p_val & HAT_MASK_LEFT); + }; - joy_names[p_device].hat_current = p_val; + joy_names[p_device].hat_current = p_val; - return p_last_id; + return p_last_id; }; uint32_t InputDefault::_button_event(uint32_t p_last_id, int p_device, int p_index, bool p_pressed) { - InputEvent ievent; - ievent.type = InputEvent::JOYSTICK_BUTTON; - ievent.device = p_device; - ievent.ID = ++p_last_id; - ievent.joy_button.button_index = p_index; - ievent.joy_button.pressed = p_pressed; + InputEvent ievent; + ievent.type = InputEvent::JOYSTICK_BUTTON; + ievent.device = p_device; + ievent.ID = ++p_last_id; + ievent.joy_button.button_index = p_index; + ievent.joy_button.pressed = p_pressed; - parse_input_event(ievent); + parse_input_event(ievent); - return p_last_id; + return p_last_id; }; uint32_t InputDefault::_axis_event(uint32_t p_last_id, int p_device, int p_axis, float p_value) { - InputEvent ievent; - ievent.type = InputEvent::JOYSTICK_MOTION; - ievent.device = p_device; - ievent.ID = ++p_last_id; - ievent.joy_motion.axis = p_axis; - ievent.joy_motion.axis_value = p_value; + InputEvent ievent; + ievent.type = InputEvent::JOYSTICK_MOTION; + ievent.device = p_device; + ievent.ID = ++p_last_id; + ievent.joy_motion.axis = p_axis; + ievent.joy_motion.axis_value = p_value; - parse_input_event( ievent ); + parse_input_event( ievent ); - return p_last_id; + return p_last_id; }; InputDefault::JoyEvent InputDefault::_find_to_event(String p_to) { - // string names of the SDL buttons in the same order as input_event.h godot buttons - static const char* buttons[] = {"a", "b", "x", "y", "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger", "leftstick", "rightstick", "back", "start", "dpup", "dpdown", "dpleft", "dpright", "guide", NULL }; + // string names of the SDL buttons in the same order as input_event.h godot buttons + static const char* buttons[] = {"a", "b", "x", "y", "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger", "leftstick", "rightstick", "back", "start", "dpup", "dpdown", "dpleft", "dpright", "guide", NULL }; - static const char* axis[] = {"leftx", "lefty", "rightx", "righty", NULL }; + static const char* axis[] = {"leftx", "lefty", "rightx", "righty", NULL }; - JoyEvent ret; - ret.type = -1; + JoyEvent ret; + ret.type = -1; - int i=0; - while (buttons[i]) { + int i=0; + while (buttons[i]) { - if (p_to == buttons[i]) { - //printf("mapping button %s\n", buttons[i]); - ret.type = TYPE_BUTTON; - ret.index = i; - ret.value = 0; - return ret; - }; - ++i; - }; + if (p_to == buttons[i]) { + //printf("mapping button %s\n", buttons[i]); + ret.type = TYPE_BUTTON; + ret.index = i; + ret.value = 0; + return ret; + }; + ++i; + }; - i = 0; - while (axis[i]) { + i = 0; + while (axis[i]) { - if (p_to == axis[i]) { - ret.type = TYPE_AXIS; - ret.index = i; - ret.value = 0; - return ret; - }; - ++i; - }; + if (p_to == axis[i]) { + ret.type = TYPE_AXIS; + ret.index = i; + ret.value = 0; + return ret; + }; + ++i; + }; - return ret; + return ret; }; void InputDefault::parse_mapping(String p_mapping) { - _THREAD_SAFE_METHOD_; - JoyDeviceMapping mapping; + _THREAD_SAFE_METHOD_; + JoyDeviceMapping mapping; - Vector entry = p_mapping.split(","); - CharString uid; - uid.resize(17); + Vector entry = p_mapping.split(","); + CharString uid; + uid.resize(17); - mapping.uid = entry[0]; + mapping.uid = entry[0]; - int idx = 1; - while (++idx < entry.size()) { + int idx = 1; + while (++idx < entry.size()) { - if (entry[idx] == "") - continue; + if (entry[idx] == "") + continue; - String from = entry[idx].get_slice(":", 1); - String to = entry[idx].get_slice(":", 0); + String from = entry[idx].get_slice(":", 1); + String to = entry[idx].get_slice(":", 0); - JoyEvent to_event = _find_to_event(to); - if (to_event.type == -1) - continue; + JoyEvent to_event = _find_to_event(to); + if (to_event.type == -1) + continue; - String etype = from.substr(0, 1); - if (etype == "a") { + String etype = from.substr(0, 1); + if (etype == "a") { - int aid = from.substr(1, from.length()-1).to_int(); - mapping.axis[aid] = to_event; + int aid = from.substr(1, from.length()-1).to_int(); + mapping.axis[aid] = to_event; - } else if (etype == "b") { + } else if (etype == "b") { - int bid = from.substr(1, from.length()-1).to_int(); - mapping.buttons[bid] = to_event; + int bid = from.substr(1, from.length()-1).to_int(); + mapping.buttons[bid] = to_event; - } else if (etype == "h") { + } else if (etype == "h") { - int hat_value = from.get_slice(".", 1).to_int(); - switch (hat_value) { - case 1: - mapping.hat[HAT_UP] = to_event; - break; - case 2: - mapping.hat[HAT_RIGHT] = to_event; - break; - case 4: - mapping.hat[HAT_DOWN] = to_event; - break; - case 8: - mapping.hat[HAT_LEFT] = to_event; - break; - }; - }; - }; - map_db.push_back(mapping); - //printf("added mapping with uuid %ls\n", mapping.uid.c_str()); + int hat_value = from.get_slice(".", 1).to_int(); + switch (hat_value) { + case 1: + mapping.hat[HAT_UP] = to_event; + break; + case 2: + mapping.hat[HAT_RIGHT] = to_event; + break; + case 4: + mapping.hat[HAT_DOWN] = to_event; + break; + case 8: + mapping.hat[HAT_LEFT] = to_event; + break; + }; + }; + }; + map_db.push_back(mapping); + //printf("added mapping with uuid %ls\n", mapping.uid.c_str()); }; -- cgit v1.2.3-70-g09d2