diff options
Diffstat (limited to 'main/input_default.cpp')
-rw-r--r-- | main/input_default.cpp | 347 |
1 files changed, 163 insertions, 184 deletions
diff --git a/main/input_default.cpp b/main/input_default.cpp index a9f643c785..bc409960d6 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -27,48 +27,46 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "input_default.h" -#include "servers/visual_server.h" -#include "os/os.h" #include "input_map.h" +#include "os/os.h" #include "scene/resources/texture.h" +#include "servers/visual_server.h" -void InputDefault::SpeedTrack::update(const Vector2& p_delta_p) { +void InputDefault::SpeedTrack::update(const Vector2 &p_delta_p) { uint64_t tick = OS::get_singleton()->get_ticks_usec(); - uint32_t tdiff = tick-last_tick; + uint32_t tdiff = tick - last_tick; float delta_t = tdiff / 1000000.0; - last_tick=tick; - + last_tick = tick; - accum+=p_delta_p; - accum_t+=delta_t; + accum += p_delta_p; + accum_t += delta_t; - if (accum_t>max_ref_frame*10) - accum_t=max_ref_frame*10; + if (accum_t > max_ref_frame * 10) + accum_t = max_ref_frame * 10; - while( accum_t>=min_ref_frame ) { + while (accum_t >= min_ref_frame) { float slice_t = min_ref_frame / accum_t; - Vector2 slice = accum*slice_t; - accum=accum-slice; - accum_t-=min_ref_frame; + Vector2 slice = accum * slice_t; + accum = accum - slice; + accum_t -= min_ref_frame; - speed=(slice/min_ref_frame).linear_interpolate(speed,min_ref_frame/max_ref_frame); + speed = (slice / min_ref_frame).linear_interpolate(speed, min_ref_frame / max_ref_frame); } - } void InputDefault::SpeedTrack::reset() { last_tick = OS::get_singleton()->get_ticks_usec(); - speed=Vector2(); - accum_t=0; + speed = Vector2(); + accum_t = 0; } InputDefault::SpeedTrack::SpeedTrack() { - min_ref_frame=0.1; - max_ref_frame=0.3; - reset(); + min_ref_frame = 0.1; + max_ref_frame = 0.3; + reset(); } bool InputDefault::is_key_pressed(int p_scancode) const { @@ -80,22 +78,21 @@ bool InputDefault::is_key_pressed(int p_scancode) const { bool InputDefault::is_mouse_button_pressed(int p_button) const { _THREAD_SAFE_METHOD_ - return (mouse_button_mask&(1<<p_button))!=0; + return (mouse_button_mask & (1 << p_button)) != 0; } +static int _combine_device(int p_value, int p_device) { -static int _combine_device(int p_value,int p_device) { - - return p_value|(p_device<<20); + return p_value | (p_device << 20); } -bool InputDefault::is_joy_button_pressed(int p_device, int p_button) const{ +bool InputDefault::is_joy_button_pressed(int p_device, int p_button) const { _THREAD_SAFE_METHOD_ - return joy_buttons_pressed.has(_combine_device(p_button,p_device)); + return joy_buttons_pressed.has(_combine_device(p_button, p_device)); } -bool InputDefault::is_action_pressed(const StringName& p_action) const{ +bool InputDefault::is_action_pressed(const StringName &p_action) const { return action_state.has(p_action) && action_state[p_action].pressed; #if 0 @@ -152,37 +149,36 @@ bool InputDefault::is_action_pressed(const StringName& p_action) const{ #endif } -bool InputDefault::is_action_just_pressed(const StringName& p_action) const { +bool InputDefault::is_action_just_pressed(const StringName &p_action) const { - const Map<StringName,Action>::Element *E=action_state.find(p_action); + const Map<StringName, Action>::Element *E = action_state.find(p_action); if (!E) return false; if (Engine::get_singleton()->is_in_fixed_frame()) { - return E->get().pressed && E->get().fixed_frame==Engine::get_singleton()->get_fixed_frames(); + return E->get().pressed && E->get().fixed_frame == Engine::get_singleton()->get_fixed_frames(); } else { - return E->get().pressed && E->get().idle_frame==Engine::get_singleton()->get_idle_frames(); + return E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames(); } } -bool InputDefault::is_action_just_released(const StringName& p_action) const{ +bool InputDefault::is_action_just_released(const StringName &p_action) const { - const Map<StringName,Action>::Element *E=action_state.find(p_action); + const Map<StringName, Action>::Element *E = action_state.find(p_action); if (!E) return false; if (Engine::get_singleton()->is_in_fixed_frame()) { - return !E->get().pressed && E->get().fixed_frame==Engine::get_singleton()->get_fixed_frames(); + return !E->get().pressed && E->get().fixed_frame == Engine::get_singleton()->get_fixed_frames(); } else { - return !E->get().pressed && E->get().idle_frame==Engine::get_singleton()->get_idle_frames(); + return !E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames(); } } - -float InputDefault::get_joy_axis(int p_device,int p_axis) const{ +float InputDefault::get_joy_axis(int p_device, int p_axis) const { _THREAD_SAFE_METHOD_ - int c = _combine_device(p_axis,p_device); + int c = _combine_device(p_axis, p_device); if (_joy_axis.has(c)) { return _joy_axis[c]; } else { @@ -222,11 +218,11 @@ float InputDefault::get_joy_vibration_duration(int p_device) { static String _hex_str(uint8_t p_byte) { - static const char* dict = "0123456789abcdef"; + static const char *dict = "0123456789abcdef"; char ret[3]; ret[2] = 0; - ret[0] = dict[p_byte>>4]; + ret[0] = dict[p_byte >> 4]; ret[1] = dict[p_byte & 0xf]; return ret; @@ -246,22 +242,21 @@ void InputDefault::joy_connection_changed(int p_idx, bool p_connected, String p_ String uidname = p_guid; if (p_guid == "") { int uidlen = MIN(p_name.length(), 16); - for (int i=0; i<uidlen; i++) { + for (int i = 0; i < uidlen; i++) { uidname = uidname + _hex_str(p_name[i]); }; }; js.uid = uidname; js.connected = true; int mapping = fallback_mapping; - for (int i=0; i < map_db.size(); i++) { + for (int i = 0; i < map_db.size(); i++) { if (js.uid == map_db[i].uid) { mapping = i; js.name = map_db[i].name; }; }; js.mapping = mapping; - } - else { + } else { js.connected = false; for (int i = 0; i < JOY_BUTTON_MAX; i++) { @@ -277,19 +272,19 @@ void InputDefault::joy_connection_changed(int p_idx, bool p_connected, String p_ emit_signal("joy_connection_changed", p_idx, p_connected); }; -Vector3 InputDefault::get_gravity() const{ +Vector3 InputDefault::get_gravity() const { _THREAD_SAFE_METHOD_ return gravity; } -Vector3 InputDefault::get_accelerometer() const{ +Vector3 InputDefault::get_accelerometer() const { _THREAD_SAFE_METHOD_ return accelerometer; } -Vector3 InputDefault::get_magnetometer() const{ +Vector3 InputDefault::get_magnetometer() const { _THREAD_SAFE_METHOD_ return magnetometer; @@ -301,16 +296,16 @@ Vector3 InputDefault::get_gyroscope() const { return gyroscope; } -void InputDefault::parse_input_event(const InputEvent& p_event) { +void InputDefault::parse_input_event(const InputEvent &p_event) { _THREAD_SAFE_METHOD_ - switch(p_event.type) { + switch (p_event.type) { case InputEvent::KEY: { if (p_event.key.echo) break; - if (p_event.key.scancode==0) + if (p_event.key.scancode == 0) break; //print_line(p_event); @@ -326,37 +321,37 @@ void InputDefault::parse_input_event(const InputEvent& p_event) { break; if (p_event.mouse_button.pressed) - mouse_button_mask|=(1<<p_event.mouse_button.button_index); + mouse_button_mask |= (1 << p_event.mouse_button.button_index); else - mouse_button_mask&=~(1<<p_event.mouse_button.button_index); + mouse_button_mask &= ~(1 << p_event.mouse_button.button_index); - if (main_loop && emulate_touch && p_event.mouse_button.button_index==1) { + if (main_loop && emulate_touch && p_event.mouse_button.button_index == 1) { InputEventScreenTouch touch_event; - touch_event.index=0; - touch_event.pressed=p_event.mouse_button.pressed; - touch_event.x=p_event.mouse_button.x; - touch_event.y=p_event.mouse_button.y; + touch_event.index = 0; + touch_event.pressed = p_event.mouse_button.pressed; + touch_event.x = p_event.mouse_button.x; + touch_event.y = p_event.mouse_button.y; InputEvent ev; - ev.type=InputEvent::SCREEN_TOUCH; - ev.screen_touch=touch_event; + ev.type = InputEvent::SCREEN_TOUCH; + ev.screen_touch = touch_event; main_loop->input_event(ev); } } break; case InputEvent::MOUSE_MOTION: { - if (main_loop && emulate_touch && p_event.mouse_motion.button_mask&1) { + if (main_loop && emulate_touch && p_event.mouse_motion.button_mask & 1) { InputEventScreenDrag drag_event; - drag_event.index=0; - drag_event.x=p_event.mouse_motion.x; - drag_event.y=p_event.mouse_motion.y; - drag_event.relative_x=p_event.mouse_motion.relative_x; - drag_event.relative_y=p_event.mouse_motion.relative_y; - drag_event.speed_x=p_event.mouse_motion.speed_x; - drag_event.speed_y=p_event.mouse_motion.speed_y; + drag_event.index = 0; + drag_event.x = p_event.mouse_motion.x; + drag_event.y = p_event.mouse_motion.y; + drag_event.relative_x = p_event.mouse_motion.relative_x; + drag_event.relative_y = p_event.mouse_motion.relative_y; + drag_event.speed_x = p_event.mouse_motion.speed_x; + drag_event.speed_y = p_event.mouse_motion.speed_y; InputEvent ev; - ev.type=InputEvent::SCREEN_DRAG; - ev.screen_drag=drag_event; + ev.type = InputEvent::SCREEN_DRAG; + ev.screen_drag = drag_event; main_loop->input_event(ev); } @@ -364,7 +359,7 @@ void InputDefault::parse_input_event(const InputEvent& p_event) { } break; case InputEvent::JOYPAD_BUTTON: { - int c = _combine_device(p_event.joy_button.button_index,p_event.device); + int c = _combine_device(p_event.joy_button.button_index, p_event.device); if (p_event.joy_button.pressed) joy_buttons_pressed.insert(c); @@ -374,33 +369,30 @@ void InputDefault::parse_input_event(const InputEvent& p_event) { case InputEvent::JOYPAD_MOTION: { set_joy_axis(p_event.device, p_event.joy_motion.axis, p_event.joy_motion.axis_value); } break; - } - if (!p_event.is_echo()) { - for (const Map<StringName,InputMap::Action>::Element *E=InputMap::get_singleton()->get_action_map().front();E;E=E->next()) { + for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) { - if (InputMap::get_singleton()->event_is_action(p_event,E->key()) && is_action_pressed(E->key()) != p_event.is_pressed()) { + if (InputMap::get_singleton()->event_is_action(p_event, E->key()) && is_action_pressed(E->key()) != p_event.is_pressed()) { Action action; - action.fixed_frame=Engine::get_singleton()->get_fixed_frames(); - action.idle_frame=Engine::get_singleton()->get_idle_frames(); - action.pressed=p_event.is_pressed(); - action_state[E->key()]=action; + action.fixed_frame = Engine::get_singleton()->get_fixed_frames(); + action.idle_frame = Engine::get_singleton()->get_idle_frames(); + action.pressed = p_event.is_pressed(); + action_state[E->key()] = action; } } } if (main_loop) main_loop->input_event(p_event); - } -void InputDefault::set_joy_axis(int p_device,int p_axis,float p_value) { +void InputDefault::set_joy_axis(int p_device, int p_axis, float p_value) { _THREAD_SAFE_METHOD_ - int c = _combine_device(p_axis,p_device); - _joy_axis[c]=p_value; + int c = _combine_device(p_axis, p_device); + _joy_axis[c] = p_value; } void InputDefault::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) { @@ -426,47 +418,42 @@ void InputDefault::stop_joy_vibration(int p_device) { joy_vibration[p_device] = vibration; } -void InputDefault::set_gravity(const Vector3& p_gravity) { +void InputDefault::set_gravity(const Vector3 &p_gravity) { _THREAD_SAFE_METHOD_ - gravity=p_gravity; - + gravity = p_gravity; } -void InputDefault::set_accelerometer(const Vector3& p_accel) { +void InputDefault::set_accelerometer(const Vector3 &p_accel) { _THREAD_SAFE_METHOD_ - accelerometer=p_accel; - + accelerometer = p_accel; } -void InputDefault::set_magnetometer(const Vector3& p_magnetometer) { +void InputDefault::set_magnetometer(const Vector3 &p_magnetometer) { _THREAD_SAFE_METHOD_ - magnetometer=p_magnetometer; - + magnetometer = p_magnetometer; } -void InputDefault::set_gyroscope(const Vector3& p_gyroscope) { +void InputDefault::set_gyroscope(const Vector3 &p_gyroscope) { _THREAD_SAFE_METHOD_ - gyroscope=p_gyroscope; - + gyroscope = p_gyroscope; } void InputDefault::set_main_loop(MainLoop *p_main_loop) { - main_loop=p_main_loop; - + main_loop = p_main_loop; } -void InputDefault::set_mouse_pos(const Point2& p_posf) { +void InputDefault::set_mouse_pos(const Point2 &p_posf) { - mouse_speed_track.update(p_posf-mouse_pos); - mouse_pos=p_posf; + mouse_speed_track.update(p_posf - mouse_pos); + mouse_pos = p_posf; if (custom_cursor.is_valid()) { VisualServer::get_singleton()->cursor_set_pos(get_mouse_pos()); } @@ -483,46 +470,42 @@ Point2 InputDefault::get_last_mouse_speed() const { int InputDefault::get_mouse_button_mask() const { - return mouse_button_mask;// do not trust OS implementaiton, should remove it - OS::get_singleton()->get_mouse_button_state(); + return mouse_button_mask; // do not trust OS implementaiton, should remove it - OS::get_singleton()->get_mouse_button_state(); } -void InputDefault::warp_mouse_pos(const Vector2& p_to) { +void InputDefault::warp_mouse_pos(const Vector2 &p_to) { OS::get_singleton()->warp_mouse_pos(p_to); } - void InputDefault::iteration(float p_step) { - - } -void InputDefault::action_press(const StringName& p_action) { +void InputDefault::action_press(const StringName &p_action) { Action action; - action.fixed_frame=Engine::get_singleton()->get_fixed_frames(); - action.idle_frame=Engine::get_singleton()->get_idle_frames(); - action.pressed=true; - - action_state[p_action]=action; + action.fixed_frame = Engine::get_singleton()->get_fixed_frames(); + action.idle_frame = Engine::get_singleton()->get_idle_frames(); + action.pressed = true; + action_state[p_action] = action; } -void InputDefault::action_release(const StringName& p_action){ +void InputDefault::action_release(const StringName &p_action) { Action action; - action.fixed_frame=Engine::get_singleton()->get_fixed_frames(); - action.idle_frame=Engine::get_singleton()->get_idle_frames(); - action.pressed=false; + action.fixed_frame = Engine::get_singleton()->get_fixed_frames(); + action.idle_frame = Engine::get_singleton()->get_idle_frames(); + action.pressed = false; - action_state[p_action]=action; + action_state[p_action] = action; } void InputDefault::set_emulate_touch(bool p_emulate) { - emulate_touch=p_emulate; + emulate_touch = p_emulate; } bool InputDefault::is_emulating_touchscreen() const { @@ -530,11 +513,11 @@ bool InputDefault::is_emulating_touchscreen() const { return emulate_touch; } -void InputDefault::set_custom_mouse_cursor(const RES& p_cursor,const Vector2& p_hotspot) { - if (custom_cursor==p_cursor) +void InputDefault::set_custom_mouse_cursor(const RES &p_cursor, const Vector2 &p_hotspot) { + if (custom_cursor == p_cursor) return; - custom_cursor=p_cursor; + custom_cursor = p_cursor; if (p_cursor.is_null()) { set_mouse_mode(MOUSE_MODE_VISIBLE); @@ -544,7 +527,7 @@ void InputDefault::set_custom_mouse_cursor(const RES& p_cursor,const Vector2& p_ Rect2 region = atex.is_valid() ? atex->get_region() : Rect2(); set_mouse_mode(MOUSE_MODE_HIDDEN); VisualServer::get_singleton()->cursor_set_visible(true); - VisualServer::get_singleton()->cursor_set_texture(custom_cursor->get_rid(),p_hotspot, 0, region); + VisualServer::get_singleton()->cursor_set_texture(custom_cursor->get_rid(), p_hotspot, 0, region); VisualServer::get_singleton()->cursor_set_pos(get_mouse_pos()); } } @@ -560,13 +543,12 @@ void InputDefault::set_mouse_in_window(bool p_in_window) { set_mouse_mode(MOUSE_MODE_VISIBLE); VisualServer::get_singleton()->cursor_set_visible(false); } - } } // from github.com/gabomdq/SDL_GameControllerDB -static const char *s_ControllerMappings [] = { - #ifdef WINDOWS_ENABLED +static const char *s_ControllerMappings[] = { +#ifdef WINDOWS_ENABLED "00f00300000000000000504944564944,RetroUSB.com RetroPad,a:b1,b:b5,x:b0,y:b4,back:b2,start:b3,leftshoulder:b6,rightshoulder:b7,leftx:a0,lefty:a1,", "00f0f100000000000000504944564944,RetroUSB.com Super RetroPort,a:b1,b:b5,x:b0,y:b4,back:b2,start:b3,leftshoulder:b6,rightshoulder:b7,leftx:a0,lefty:a1,", "02200090000000000000504944564944,8Bitdo NES30 PRO USB,a:b0,b:b1,x:b3,y:b4,leftshoulder:b6,rightshoulder:b7,lefttrigger:b8,righttrigger:b9,back:b10,start:b11,leftstick:b13,rightstick:b14,leftx:a0,lefty:a1,rightx:a3,righty:a4,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", @@ -613,9 +595,9 @@ static const char *s_ControllerMappings [] = { "ff113133000000000000504944564944,SVEN X-PAD,a:b2,b:b3,y:b1,x:b0,start:b5,back:b4,leftshoulder:b6,rightshoulder:b7,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a4,lefttrigger:b8,righttrigger:b9,", "ffff0000000000000000504944564944,GameStop Gamepad,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,", "__XINPUT_DEVICE__,XInput Gamepad,a:b12,b:b13,x:b14,y:b15,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpdown:b1,dpleft:b2,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,", - #endif +#endif - #ifdef OSX_ENABLED +#ifdef OSX_ENABLED "0500000047532047616d657061640000,GameStop Gamepad,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,", "050000005769696d6f74652028303000,Wii Remote,a:b4,b:b5,y:b9,x:b10,start:b6,guide:b8,back:b7,dpup:b2,dpleft:b0,dpdown:b3,dpright:b1,leftx:a0,lefty:a1,lefttrigger:b12,righttrigger:,leftshoulder:b11,", "050000005769696d6f74652028313800,Wii U Pro Controller,a:b16,b:b15,x:b18,y:b17,back:b7,guide:b8,start:b6,leftstick:b23,rightstick:b24,leftshoulder:b19,rightshoulder:b20,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b21,righttrigger:b22,", @@ -642,9 +624,9 @@ static const char *s_ControllerMappings [] = { "ad1b00000000000001f9000000000000,Gamestop BB-070 X360 Controller,a:b0,b:b1,back:b9,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b10,leftshoulder:b4,leftstick:b6,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b7,righttrigger:a5,rightx:a3,righty:a4,start:b8,x:b2,y:b3,", "b4040000000000000a01000000000000,Sega Saturn USB Gamepad,a:b0,b:b1,x:b3,y:b4,back:b5,guide:b2,start:b8,leftshoulder:b6,rightshoulder:b7,leftx:a0,lefty:a1,", "d814000000000000cecf000000000000,MC Cthulhu,leftx:,lefty:,rightx:,righty:,lefttrigger:b6,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,righttrigger:b7,", - #endif +#endif - #if X11_ENABLED +#if X11_ENABLED "0000000058626f782047616d65706100,Xbox Gamepad (userspace driver),a:b0,b:b1,x:b2,y:b3,start:b7,back:b6,guide:b8,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,lefttrigger:a5,righttrigger:a4,leftstick:b9,rightstick:b10,leftx:a0,lefty:a1,rightx:a2,righty:a3,", "0300000000f000000300000000010000,RetroUSB.com RetroPad,a:b1,b:b5,x:b0,y:b4,back:b2,start:b3,leftshoulder:b6,rightshoulder:b7,leftx:a0,lefty:a1,", "0300000000f00000f100000000010000,RetroUSB.com Super RetroPort,a:b1,b:b5,x:b0,y:b4,back:b2,start:b3,leftshoulder:b6,rightshoulder:b7,leftx:a0,lefty:a1,", @@ -727,9 +709,9 @@ static const char *s_ControllerMappings [] = { "050000007e0500003003000001000000,Nintendo Wii U Pro Controller,a:b0,b:b1,x:b3,y:b2,back:b8,start:b9,guide:b10,leftshoulder:b4,rightshoulder:b5,leftstick:b11,rightstick:b12,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,dpup:b13,dpleft:b15,dpdown:b14,dpright:b16,", "05000000d6200000ad0d000001000000,Moga Pro,a:b0,b:b1,y:b3,x:b2,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a5,righttrigger:a4,", "060000004c0500006802000000010000,PS3 Controller (Bluetooth),a:b14,b:b13,y:b12,x:b15,start:b3,guide:b16,back:b0,leftstick:b1,rightstick:b2,leftshoulder:b10,rightshoulder:b11,dpup:b4,dpleft:b7,dpdown:b6,dpright:b5,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b8,righttrigger:b9,", - #endif +#endif - #if defined(__ANDROID__) +#if defined(__ANDROID__) "Default Android Gamepad,Default Controller,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b8,rightshoulder:b10,rightx:a2,start:b6,righty:a3,dpleft:h0.8,lefttrigger:a4,x:b2,dpup:h0.1,back:b4,leftstick:b7,leftshoulder:b9,y:b3,a:b0,dpright:h0.2,righttrigger:a5,b:b1,", "47656e6572696320582d426f78207061,Logitech F-310,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b8,rightshoulder:b10,rightx:a2,start:b6,righty:a3,dpleft:h0.8,lefttrigger:a5,x:b2,dpup:h0.1,leftstick:b7,leftshoulder:b9,y:b3,a:b0,dpright:h0.2,righttrigger:a4,b:b1,", "484f524920434f2e2c4c544420205041,Hori Gem Pad 3,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b6,rightshoulder:b18,rightx:a2,start:b16,righty:a3,dpleft:h0.8,lefttrigger:b9,x:b0,dpup:h0.1,back:b15,leftstick:b4,leftshoulder:b3,y:b2,a:b1,dpright:h0.2,righttrigger:b10,b:b17,", @@ -737,9 +719,9 @@ static const char *s_ControllerMappings [] = { "4e564944494120436f72706f72617469,NVIDIA Controller,a:b0,b:b1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,", "532e542e442e20496e74657261637420,3dfx InterAct HammerHead FX,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b25,rightshoulder:b27,rightx:a2,start:b31,righty:a3,dpleft:h0.8,lefttrigger:b28,x:b20,dpup:h0.1,back:b30,leftstick:b22,leftshoulder:b26,y:b21,a:b23,dpright:h0.2,righttrigger:b29,b:b24,", "506572666f726d616e63652044657369,PDP Rock Candy Wireless Controller for PS3,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b6,rightshoulder:b18,rightx:a2,start:b16,righty:a3,dpleft:h0.8,lefttrigger:b9,x:b0,dpup:h0.1,back:h0.2,leftstick:b4,leftshoulder:b3,y:b2,a:b1,dpright:h0.2,righttrigger:b10,b:b17,", - #endif +#endif - #ifdef JAVASCRIPT_ENABLED +#ifdef JAVASCRIPT_ENABLED "Default HTML5 Gamepad, Default Mapping,leftx:a0,lefty:a1,dpdown:b13,rightstick:b11,rightshoulder:b5,rightx:a2,start:b9,righty:a3,dpleft:b14,lefttrigger:a6,x:b2,dpup:b12,back:b8,leftstick:b10,leftshoulder:b4,y:b3,a:b0,dpright:b15,righttrigger:a7,b:b1,", "303435652d303238652d4d6963726f73,Wired X360 Controller,leftx:a0,lefty:a1,dpdown:a7,rightstick:b10,rightshoulder:b5,rightx:a3,start:b7,righty:a4,dpleft:a6,lefttrigger:a2,x:b2,dpup:a7,back:b6,leftstick:b9,leftshoulder:b4,y:b3,a:b0,dpright:a6,righttrigger:a5,b:b1,", "303435652d303731392d58626f782033,Wireless X360 Controller,leftx:a0,lefty:a1,dpdown:b14,rightstick:b10,rightshoulder:b5,rightx:a3,start:b7,righty:a4,dpleft:b11,lefttrigger:a2,x:b2,dpup:b13,back:b6,leftstick:b9,leftshoulder:b4,y:b3,a:b0,dpright:b12,righttrigger:a5,b:b1,", @@ -747,19 +729,19 @@ static const char *s_ControllerMappings [] = { "303534632d303563342d536f6e792043,PS4 Controller USB/Linux,leftx:a0,lefty:a1,dpdown:a7,rightstick:b11,rightshoulder:b5,rightx:a2,start:b9,righty:a5,dpleft:a6,lefttrigger:a3,x:b0,dpup:a7,back:b8,leftstick:b10,leftshoulder:b4,y:b3,a:b1,dpright:a6,righttrigger:a4,b:b2,", "303534632d303563342d576972656c65,PS4 Controller USB/Win,leftx:a0,lefty:a1,dpdown:b15,rightstick:b11,rightshoulder:b5,rightx:a2,start:b9,righty:a5,lefttrigger:a3,x:b0,dpup:b14,dpleft:b16,dpright:b17,back:b8,leftstick:b10,leftshoulder:b4,y:b3,a:b1,righttrigger:b7,b:b2,", "c2a94d6963726f736f66742058626f78,Wireless X360 Controller,leftx:a0,lefty:a1,dpdown:b14,rightstick:b10,rightshoulder:b5,rightx:a3,start:b7,righty:a4,dpleft:b11,lefttrigger:a2,x:b2,dpup:b13,back:b6,leftstick:b9,leftshoulder:b4,y:b3,a:b0,dpright:b12,righttrigger:a5,b:b1,", - #endif +#endif - #ifdef UWP_ENABLED +#ifdef UWP_ENABLED "__UWP_GAMEPAD__,Xbox Controller,a:b2,b:b3,x:b4,y:b5,start:b0,back:b1,leftstick:b12,rightstick:b13,leftshoulder:b10,rightshoulder:b11,dpup:b6,dpdown:b7,dpleft:b8,dpright:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,", - #endif +#endif NULL }; InputDefault::InputDefault() { - mouse_button_mask=0; - emulate_touch=false; - main_loop=NULL; + mouse_button_mask = 0; + emulate_touch = false; + main_loop = NULL; hat_map_default[HAT_UP].type = TYPE_BUTTON; hat_map_default[HAT_UP].index = JOY_DPAD_UP; @@ -783,7 +765,7 @@ InputDefault::InputDefault() { if (env_mapping != "") { Vector<String> entries = env_mapping.split("\n"); - for (int i=0; i < entries.size(); i++) { + for (int i = 0; i < entries.size(); i++) { if (entries[i] == "") continue; parse_mapping(entries[i]); @@ -797,11 +779,10 @@ InputDefault::InputDefault() { }; } - uint32_t InputDefault::joy_button(uint32_t p_last_id, int p_device, int p_button, bool p_pressed) { _THREAD_SAFE_METHOD_; - Joypad& joy = joy_names[p_device]; + Joypad &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; @@ -812,7 +793,7 @@ uint32_t InputDefault::joy_button(uint32_t p_last_id, int p_device, int p_button return _button_event(p_last_id, p_device, p_button, p_pressed); }; - Map<int,JoyEvent>::Element* el = map_db[joy.mapping].buttons.find(p_button); + Map<int, JoyEvent>::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); @@ -837,11 +818,11 @@ uint32_t InputDefault::joy_button(uint32_t p_last_id, int p_device, int p_button 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) { +uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, const JoyAxis &p_value) { _THREAD_SAFE_METHOD_; - Joypad& joy = joy_names[p_device]; + Joypad &joy = joy_names[p_device]; if (joy.last_axis[p_axis] == p_value.value) { return p_last_id; @@ -849,12 +830,11 @@ uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, co 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) { + } else if (p_value.value > joy.last_axis[p_axis] - joy.filter) { return p_last_id; } @@ -874,13 +854,12 @@ uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, co return _axis_event(p_last_id, p_device, p_axis, val); }; - Map<int,JoyEvent>::Element* el = map_db[joy.mapping].axis.find(p_axis); + Map<int, JoyEvent>::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(); if (map.type == TYPE_BUTTON) { @@ -903,7 +882,7 @@ uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, co p_last_id = _button_event(p_last_id, p_device, JOY_DPAD_DOWN, false); } } - if ( pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) { + if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) { return p_last_id; } return _button_event(p_last_id, p_device, button, true); @@ -920,14 +899,14 @@ uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, co p_last_id = _button_event(p_last_id, p_device, JOY_DPAD_RIGHT, false); } } - if ( pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) { + if (pressed == joy_buttons_pressed.has(_combine_device(button, p_device))) { return p_last_id; } return _button_event(p_last_id, p_device, button, true); } float deadzone = p_value.min == 0 ? 0.5f : 0.0f; bool pressed = p_value.value > deadzone ? true : false; - if (pressed == joy_buttons_pressed.has(_combine_device(map.index,p_device))) { + if (pressed == joy_buttons_pressed.has(_combine_device(map.index, p_device))) { // button already pressed or released, this is an axis bounce value return p_last_id; }; @@ -936,7 +915,7 @@ uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, co if (map.type == TYPE_AXIS) { - return _axis_event(p_last_id, p_device, map.index, val ); + return _axis_event(p_last_id, p_device, map.index, val); }; //printf("invalid mapping\n"); return p_last_id; @@ -945,9 +924,9 @@ uint32_t InputDefault::joy_axis(uint32_t p_last_id, int p_device, int p_axis, co uint32_t InputDefault::joy_hat(uint32_t p_last_id, int p_device, int p_val) { _THREAD_SAFE_METHOD_; - const Joypad& joy = joy_names[p_device]; + const Joypad &joy = joy_names[p_device]; - JoyEvent* map; + JoyEvent *map; if (joy.mapping == -1) { map = hat_map_default; @@ -957,17 +936,17 @@ uint32_t InputDefault::joy_hat(uint32_t p_last_id, int p_device, int p_val) { int cur_val = joy_names[p_device].hat_current; - if ( (p_val & HAT_MASK_UP) != (cur_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) ) { + 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) ) { + 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) ) { + 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); }; @@ -999,7 +978,7 @@ uint32_t InputDefault::_axis_event(uint32_t p_last_id, int p_device, int p_axis, 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; }; @@ -1007,14 +986,14 @@ uint32_t InputDefault::_axis_event(uint32_t p_last_id, int p_device, int p_axis, 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 }; + 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; - int i=0; + int i = 0; while (buttons[i]) { if (p_to == buttons[i]) { @@ -1070,30 +1049,30 @@ void InputDefault::parse_mapping(String p_mapping) { String etype = from.substr(0, 1); if (etype == "a") { - int aid = from.substr(1, from.length()-1).to_int(); + int aid = from.substr(1, from.length() - 1).to_int(); mapping.axis[aid] = to_event; } else if (etype == "b") { - int bid = from.substr(1, from.length()-1).to_int(); + int bid = from.substr(1, from.length() - 1).to_int(); mapping.buttons[bid] = to_event; } 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; + 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; }; }; }; @@ -1106,21 +1085,21 @@ void InputDefault::add_joy_mapping(String p_mapping, bool p_update_existing) { if (p_update_existing) { Vector<String> entry = p_mapping.split(","); String uid = entry[0]; - for (int i=0; i<joy_names.size(); i++) { + for (int i = 0; i < joy_names.size(); i++) { if (uid == joy_names[i].uid) { - joy_names[i].mapping = map_db.size() -1; + joy_names[i].mapping = map_db.size() - 1; } } } } void InputDefault::remove_joy_mapping(String p_guid) { - for (int i=map_db.size()-1; i >= 0;i--) { + for (int i = map_db.size() - 1; i >= 0; i--) { if (p_guid == map_db[i].uid) { map_db.remove(i); } } - for (int i=0; i<joy_names.size(); i++) { + for (int i = 0; i < joy_names.size(); i++) { if (joy_names[i].uid == p_guid) { joy_names[i].mapping = -1; } @@ -1170,7 +1149,7 @@ Array InputDefault::get_connected_joypads() { return ret; } -static const char* _buttons[] = { +static const char *_buttons[] = { "Face Button Bottom", "Face Button Right", "Face Button Left", @@ -1189,7 +1168,7 @@ static const char* _buttons[] = { "DPAD Right" }; -static const char* _axes[] = { +static const char *_axes[] = { "Left Stick X", "Left Stick Y", "Right Stick X", @@ -1215,7 +1194,7 @@ int InputDefault::get_joy_button_index_from_string(String p_button) { } int InputDefault::get_unused_joy_id() { - for (int i=0;i<JOYPADS_MAX;i++) { + for (int i = 0; i < JOYPADS_MAX; i++) { if (!joy_names.has(i) || !joy_names[i].connected) { return i; } |