From c632c13c66db715b816390f0734f2b1839a7ff3e Mon Sep 17 00:00:00 2001 From: hondres Date: Fri, 8 Jan 2016 00:40:41 +0100 Subject: [PATCH] Add some joystick functions to input. Enables manipulation of mappings at runtime --- core/os/input.cpp | 4 + core/os/input.h | 5 +- core/os/os.cpp | 7 ++ core/os/os.h | 3 + doc/base/classes.xml | 154 +++++++++++++++++++++++++++----- main/input_default.cpp | 46 ++++++++++ main/input_default.h | 9 ++ platform/windows/os_windows.cpp | 7 ++ platform/windows/os_windows.h | 3 + platform/x11/os_x11.cpp | 8 ++ platform/x11/os_x11.h | 4 + 11 files changed, 226 insertions(+), 24 deletions(-) diff --git a/core/os/input.cpp b/core/os/input.cpp index d8d746c8113..6e1e618d9a8 100644 --- a/core/os/input.cpp +++ b/core/os/input.cpp @@ -53,8 +53,12 @@ void Input::_bind_methods() { ObjectTypeDB::bind_method(_MD("is_mouse_button_pressed","button"),&Input::is_mouse_button_pressed); ObjectTypeDB::bind_method(_MD("is_joy_button_pressed","device","button"),&Input::is_joy_button_pressed); ObjectTypeDB::bind_method(_MD("is_action_pressed","action"),&Input::is_action_pressed); + ObjectTypeDB::bind_method(_MD("add_joy_mapping","mapping", "update_existing"),&Input::add_joy_mapping, DEFVAL(false)); + ObjectTypeDB::bind_method(_MD("remove_joy_mapping","guid"),&Input::remove_joy_mapping); + ObjectTypeDB::bind_method(_MD("is_joy_known","device"),&Input::is_joy_known); ObjectTypeDB::bind_method(_MD("get_joy_axis","device","axis"),&Input::get_joy_axis); ObjectTypeDB::bind_method(_MD("get_joy_name","device"),&Input::get_joy_name); + ObjectTypeDB::bind_method(_MD("get_joy_guid","device"),&Input::get_joy_guid); ObjectTypeDB::bind_method(_MD("get_accelerometer"),&Input::get_accelerometer); //ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&Input::get_mouse_pos); - this is not the function you want ObjectTypeDB::bind_method(_MD("get_mouse_speed"),&Input::get_mouse_speed); diff --git a/core/os/input.h b/core/os/input.h index 57eaa973d91..2dd4496f261 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -63,7 +63,10 @@ public: virtual float get_joy_axis(int p_device,int p_axis)=0; virtual String get_joy_name(int p_idx)=0; virtual void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid)=0; - + virtual void add_joy_mapping(String p_mapping, bool p_update_existing=false)=0; + virtual void remove_joy_mapping(String p_guid)=0; + virtual bool is_joy_known(int p_device)=0; + virtual String get_joy_guid(int p_device) const=0; virtual Point2 get_mouse_pos() const=0; virtual Point2 get_mouse_speed() const=0; diff --git a/core/os/os.cpp b/core/os/os.cpp index eb5f91167a2..9ff1eeb9f75 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -507,6 +507,13 @@ float OS::get_time_scale() const { return _time_scale; } +bool OS::is_joy_known(int p_device) { + return true; +} + +String OS::get_joy_guid(int p_device) const { + return "Default Joystick"; +} OS::OS() { last_error=NULL; diff --git a/core/os/os.h b/core/os/os.h index 94cb1d4ea47..670695f83a1 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -397,6 +397,9 @@ public: _FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; } + virtual bool is_joy_known(int p_device); + virtual String get_joy_guid(int p_device)const; + OS(); virtual ~OS(); diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 1b95899729c..4a1e4376948 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -432,7 +432,7 @@ - + @@ -443,9 +443,9 @@ - + - + Convert one or more arguments to strings in the best way possible. @@ -454,9 +454,9 @@ - + - + Convert one or more arguments to strings in the best way possible. @@ -465,9 +465,9 @@ - + - + Print one or more arguments to strings in the best way possible to a console line. @@ -476,9 +476,9 @@ - + - + Print one or more arguments to the console with a tab between each argument. @@ -487,9 +487,9 @@ - + - + @@ -497,9 +497,9 @@ - + - + Print one or more arguments to strings in the best way possible to standard error line. @@ -508,9 +508,9 @@ - + - + Print one or more arguments to strings in the best way possible to console. No newline is added at the end. @@ -519,25 +519,40 @@ - + Converts the value of a variable to a String. - - + + - Converts the value of a String to a Variant. + + + + + + + + + + + + + + + + - + Return an array with the given range. Range can be 1 argument N (0 to N-1), two arguments (initial, final-1) or three arguments (initial,final-1,increment). @@ -573,12 +588,26 @@ - + Hashes the variable passed and returns an integer. + + + + + + + + + + + + + + @@ -1371,6 +1400,10 @@ + + + + Functions that return [Error] return OK when everything went ok. Most functions don't return error anyway and/or just print errors to stdout. @@ -7561,6 +7594,14 @@ + + + + + + + + @@ -7968,6 +8009,8 @@ + + @@ -13625,6 +13668,7 @@ returns:= "username=user&password=pass" + Returns true if the joystick button at the given index is currently pressed. Returns false otherwise. (see JOY_* constans in [InputEvent]) @@ -13635,6 +13679,31 @@ returns:= "username=user&password=pass" + + + + + + + Add a new mapping entry (in SDL2 format) to the mapping database. Optionally update already connected devices. + + + + + + + Removes all mappings from the internal db that match the given uid. + + + + + + + + + Returns true if the specified device is known by the system. This means that it sets all button and axis indices exactly as defined in [InputEvent]. Unknown joysticks are not expected to match these constants, but you can still retrieve events from them. + + @@ -13643,6 +13712,7 @@ returns:= "username=user&password=pass" + Returns the current value of the joystick axis at given index (see JOY_* enum in [InputEvent]) @@ -13651,6 +13721,16 @@ returns:= "username=user&password=pass" + Returns the name of the joystick at the specified device index + + + + + + + + + Returns a SDL2 compatible device guid on platforms that use gamepad remapping. Returns "Default Gamepad" otherwise. @@ -15479,7 +15559,7 @@ returns:= "username=user&password=pass" - + Return the metadata of the shape that collided with this body. If there is no collision, it will return 0, so collisions must be checked first with [method is_colliding]. Aditionally, this metadata can not be set with [method Object.set_meta], it must be set with [method Physics2DServer.body_set_shape_metadata]. @@ -18971,6 +19051,8 @@ returns:= "username=user&password=pass" + + @@ -19138,7 +19220,7 @@ returns:= "username=user&password=pass" - + @@ -20178,6 +20260,14 @@ returns:= "username=user&password=pass" Return the list of signals as an array of dictionaries. + + + + + + + + @@ -21601,6 +21691,10 @@ returns:= "username=user&password=pass" + + + + @@ -29631,6 +29725,14 @@ This method controls whether the position between two cached points is interpola + + + + + + + + @@ -33335,6 +33437,12 @@ This method controls whether the position between two cached points is interpola + + + + + + diff --git a/main/input_default.cpp b/main/input_default.cpp index 68f7434d964..a79199580ba 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -781,3 +781,49 @@ void InputDefault::parse_mapping(String p_mapping) { map_db.push_back(mapping); //printf("added mapping with uuid %ls\n", mapping.uid.c_str()); }; + +void InputDefault::add_joy_mapping(String p_mapping, bool p_update_existing) { + parse_mapping(p_mapping); + if (p_update_existing) { + Vector entry = p_mapping.split(","); + String uid = entry[0]; + for (int i=0; i= 0;i--) { + if (p_guid == map_db[i].uid) { + map_db.remove(i); + } + } + for (int i=0; iis_joy_known(p_device); +} + +String InputDefault::get_joy_guid(int p_device) const { + return OS::get_singleton()->get_joy_guid(p_device); +} + +//platforms that use the remapping system can override and call to these ones +bool InputDefault::is_joy_mapped(int p_device) { + return joy_names[p_device].mapping != -1 ? true : false; +} + +String InputDefault::get_joy_guid_remapped(int p_device) const { + return joy_names[p_device].uid; +} + diff --git a/main/input_default.h b/main/input_default.h index 6645817b31c..bbfd9ced2c8 100644 --- a/main/input_default.h +++ b/main/input_default.h @@ -56,6 +56,7 @@ class InputDefault : public Input { } last_hat = HAT_MASK_CENTER; filter = 0.01f; + mapping = -1; } }; @@ -161,6 +162,14 @@ public: uint32_t joy_axis(uint32_t p_last_id, int p_device, int p_axis, const JoyAxis& p_value); uint32_t joy_hat(uint32_t p_last_id, int p_device, int p_val); + virtual void add_joy_mapping(String p_mapping, bool p_update_existing=false); + virtual void remove_joy_mapping(String p_guid); + virtual bool is_joy_known(int p_device); + virtual String get_joy_guid(int p_device) const; + + bool is_joy_mapped(int p_device); + String get_joy_guid_remapped(int p_device) const; + InputDefault(); }; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 95bfa2ea941..92154532cae 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2099,6 +2099,13 @@ String OS_Windows::get_data_dir() const { } +bool OS_Windows::is_joy_known(int p_device) { + return input->is_joy_mapped(p_device); +} + +String OS_Windows::get_joy_guid(int p_device) const { + return input->get_joy_guid_remapped(p_device); +} OS_Windows::OS_Windows(HINSTANCE _hInstance) { diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index dfa2b40595c..a738ca80280 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -270,6 +270,9 @@ public: virtual bool get_swap_ok_cancel() { return true; } + virtual bool is_joy_known(int p_device); + virtual String get_joy_guid(int p_device) const; + OS_Windows(HINSTANCE _hInstance); ~OS_Windows(); diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 437e41eeada..82df8dff606 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -1774,6 +1774,14 @@ void OS_X11::run() { main_loop->finish(); } +bool OS_X11::is_joy_known(int p_device) { + return input->is_joy_mapped(p_device); +} + +String OS_X11::get_joy_guid(int p_device) const { + return input->get_joy_guid_remapped(p_device); +} + OS_X11::OS_X11() { #ifdef RTAUDIO_ENABLED diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index a556ba49e31..91dbeac2840 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -222,6 +222,10 @@ public: virtual void move_window_to_foreground(); virtual void alert(const String& p_alert,const String& p_title="ALERT!"); + + virtual bool is_joy_known(int p_device); + virtual String get_joy_guid(int p_device) const; + void run(); OS_X11();