summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorL. Krause <eska@eska.me>2017-04-29 04:54:48 +0200
committerL. Krause <eska@eska.me>2017-05-02 04:01:34 +0200
commit101c542b7709a3c2f0f57cf774abf67ccf503ce0 (patch)
treefb3e1e2778ee735992c7a57186478a154eecfb6a
parentabe09919bf5664353bea904116c6bc5bb856cd7f (diff)
Implement mouse capture and hiding in HTML5 export
MOUSE_MODE_CONFINED cannot be implemented.
-rw-r--r--platform/javascript/os_javascript.cpp56
-rw-r--r--platform/javascript/os_javascript.h8
2 files changed, 54 insertions, 10 deletions
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index e44f33f997..77abb686d8 100644
--- a/platform/javascript/os_javascript.cpp
+++ b/platform/javascript/os_javascript.cpp
@@ -513,20 +513,62 @@ void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
/* clang-format on */
}
-void OS_JavaScript::set_mouse_show(bool p_show) {
+void OS_JavaScript::set_css_cursor(const char *p_cursor) {
- //javascript has no mouse...
+ /* clang-format off */
+ EM_ASM_({
+ Module.canvas.style.cursor = Module.UTF8ToString($0);
+ }, p_cursor);
+ /* clang-format on */
}
-void OS_JavaScript::set_mouse_grab(bool p_grab) {
+const char *OS_JavaScript::get_css_cursor() const {
- //it really has no mouse...!
+ char cursor[16];
+ /* clang-format off */
+ EM_ASM_INT({
+ Module.stringToUTF8(Module.canvas.style.cursor ? Module.canvas.style.cursor : 'auto', $0, 16);
+ }, cursor);
+ /* clang-format on */
+ return cursor;
}
-bool OS_JavaScript::is_mouse_grab_enabled() const {
+void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
- //*sigh* technology has evolved so much since i was a kid..
- return false;
+ ERR_FAIL_INDEX(p_mode, MOUSE_MODE_CONFINED + 1);
+ ERR_EXPLAIN("MOUSE_MODE_CONFINED is not supported for the HTML5 platform");
+ ERR_FAIL_COND(p_mode == MOUSE_MODE_CONFINED);
+ if (p_mode == get_mouse_mode())
+ return;
+
+ if (p_mode == MOUSE_MODE_VISIBLE) {
+
+ set_css_cursor("auto");
+ emscripten_exit_pointerlock();
+
+ } else if (p_mode == MOUSE_MODE_HIDDEN) {
+
+ set_css_cursor("none");
+ emscripten_exit_pointerlock();
+
+ } else if (p_mode == MOUSE_MODE_CAPTURED) {
+
+ EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false);
+ ERR_EXPLAIN("MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback");
+ ERR_FAIL_COND(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED);
+ ERR_FAIL_COND(result != EMSCRIPTEN_RESULT_SUCCESS);
+ set_css_cursor("auto");
+ }
+}
+
+OS::MouseMode OS_JavaScript::get_mouse_mode() const {
+
+ if (!strcmp(get_css_cursor(), "none"))
+ return MOUSE_MODE_HIDDEN;
+
+ EmscriptenPointerlockChangeEvent ev;
+ emscripten_get_pointerlock_status(&ev);
+ return ev.isActive && (strcmp(ev.id, "canvas") == 0) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
}
Point2 OS_JavaScript::get_mouse_position() const {
diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h
index 0c956ecba6..b50d0030d2 100644
--- a/platform/javascript/os_javascript.h
+++ b/platform/javascript/os_javascript.h
@@ -81,6 +81,9 @@ class OS_JavaScript : public OS_Unix {
void process_joypads();
+ void set_css_cursor(const char *);
+ const char *get_css_cursor() const;
+
public:
// functions used by main to initialize/deintialize the OS
virtual int get_video_driver_count() const;
@@ -110,9 +113,8 @@ public:
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
- virtual void set_mouse_show(bool p_show);
- virtual void set_mouse_grab(bool p_grab);
- virtual bool is_mouse_grab_enabled() const;
+ virtual void set_mouse_mode(MouseMode p_mode);
+ virtual MouseMode get_mouse_mode() const;
virtual Point2 get_mouse_position() const;
virtual int get_mouse_button_state() const;
virtual void set_window_title(const String &p_title);