summaryrefslogtreecommitdiff
path: root/platform/javascript
diff options
context:
space:
mode:
authorHinsbart <liu.gam3@gmail.com>2016-01-21 02:23:15 +0100
committerhondres <liu.gam3@gmail.com>2016-01-21 02:30:24 +0100
commit77858b66d3f357b8a9b581ae84a52dc802a9d3ad (patch)
treef91ddcd75e4f38cb2aac286d53bd29a8a4fec53f /platform/javascript
parentfcae3fb3bab25b588921e84ce43924c830b18dc5 (diff)
html5 gamepad support
Diffstat (limited to 'platform/javascript')
-rw-r--r--platform/javascript/os_javascript.cpp68
-rw-r--r--platform/javascript/os_javascript.h8
2 files changed, 75 insertions, 1 deletions
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index 7bb47881a3..ffbcd98240 100644
--- a/platform/javascript/os_javascript.cpp
+++ b/platform/javascript/os_javascript.cpp
@@ -76,6 +76,14 @@ void OS_JavaScript::set_opengl_extensions(const char* p_gl_extensions) {
gl_extensions=p_gl_extensions;
}
+static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
+ OS_JavaScript *os = (OS_JavaScript*) OS::get_singleton();
+ if (os) {
+ return os->joy_connection_changed(p_type, p_event);
+ }
+ return false;
+}
+
void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
print_line("Init OS");
@@ -142,6 +150,8 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
input = memnew( InputDefault );
+ emscripten_set_gamepadconnected_callback(NULL, true, &joy_callback_func);
+ emscripten_set_gamepaddisconnected_callback(NULL, true, &joy_callback_func);
}
void OS_JavaScript::set_main_loop( MainLoop * p_main_loop ) {
@@ -296,7 +306,7 @@ bool OS_JavaScript::main_loop_iterate() {
}
-
+ process_joysticks();
return Main::iteration();
}
@@ -605,6 +615,62 @@ void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags)
}
}
+void OS_JavaScript::process_joysticks() {
+
+ int joy_count = emscripten_get_num_gamepads();
+ for (int i = 0; i < joy_count; i++) {
+ EmscriptenGamepadEvent state;
+ emscripten_get_gamepad_status(i, &state);
+ if (state.connected) {
+
+ int num_buttons = MIN(state.numButtons, 16);
+ int num_axes = MIN(state.numAxes, 8);
+ for (int j = 0; j < num_buttons; j++) {
+
+ float value = state.analogButton[j];
+ if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
+ InputDefault::JoyAxis jx;
+ jx.min = 0;
+ jx.value = value;
+ last_id = input->joy_axis(last_id, i, j, jx);
+ }
+ else {
+ last_id = input->joy_button(last_id, i, j, value);
+ }
+ }
+ for (int j = 0; j < num_axes; j++) {
+
+ InputDefault::JoyAxis jx;
+ jx.min = -1;
+ jx.value = state.axis[j];
+ last_id = input->joy_axis(last_id, i, j, jx);
+ }
+ }
+ }
+}
+
+bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
+ if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
+
+ String guid = "";
+ if (String(p_event->mapping) == "standard")
+ guid = "Default HTML5 Gamepad";
+ input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
+ }
+ else {
+ input->joy_connection_changed(p_event->index, false, "");
+ }
+ return true;
+}
+
+bool OS_JavaScript::is_joy_known(int p_device) {
+ return input->is_joy_mapped(p_device);
+}
+
+String OS_JavaScript::get_joy_guid(int p_device) const {
+ return input->get_joy_guid_remapped(p_device);
+}
+
OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func) {
diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h
index 61eecd8f32..ec3a6cf911 100644
--- a/platform/javascript/os_javascript.h
+++ b/platform/javascript/os_javascript.h
@@ -41,6 +41,7 @@
#include "audio_server_javascript.h"
#include "audio_driver_javascript.h"
#include "main/input_default.h"
+#include "emscripten/html5.h"
typedef void (*GFXInitFunc)(void *ud,bool gl2,int w, int h, bool fs);
typedef int (*OpenURIFunc)(const String&);
@@ -90,6 +91,8 @@ private:
static void _close_notification_funcs(const String& p_file,int p_flags);
+ void process_joysticks();
+
public:
// functions used by main to initialize/deintialize the OS
@@ -163,6 +166,11 @@ public:
void process_accelerometer(const Vector3& p_accelerometer);
void process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points);
void push_input(const InputEvent& p_ev);
+
+ virtual bool is_joy_known(int p_device);
+ virtual String get_joy_guid(int p_device) const;
+ bool joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event);
+
OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func);
~OS_JavaScript();