Merge pull request #8576 from eska014/html5-mousemodes

HTML5: Implement mouse lock/capture and hiding
This commit is contained in:
Rémi Verschelde 2017-05-02 07:34:52 +02:00 committed by GitHub
commit 64c31a3a9f
2 changed files with 54 additions and 10 deletions

View file

@ -513,20 +513,62 @@ void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
/* clang-format on */ /* 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.. ERR_FAIL_INDEX(p_mode, MOUSE_MODE_CONFINED + 1);
return false; 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 { Point2 OS_JavaScript::get_mouse_position() const {

View file

@ -81,6 +81,9 @@ class OS_JavaScript : public OS_Unix {
void process_joypads(); void process_joypads();
void set_css_cursor(const char *);
const char *get_css_cursor() const;
public: public:
// functions used by main to initialize/deintialize the OS // functions used by main to initialize/deintialize the OS
virtual int get_video_driver_count() const; 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 alert(const String &p_alert, const String &p_title = "ALERT!");
virtual void set_mouse_show(bool p_show); virtual void set_mouse_mode(MouseMode p_mode);
virtual void set_mouse_grab(bool p_grab); virtual MouseMode get_mouse_mode() const;
virtual bool is_mouse_grab_enabled() const;
virtual Point2 get_mouse_position() const; virtual Point2 get_mouse_position() const;
virtual int get_mouse_button_state() const; virtual int get_mouse_button_state() const;
virtual void set_window_title(const String &p_title); virtual void set_window_title(const String &p_title);