1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

[HTML5] Implement mouse/touch/key events in JS library.

This makes us more independent from emscripten libraries, giving us more
control on the application lifecycle.
This commit is contained in:
Fabio Alessandrelli
2021-09-10 21:46:22 +02:00
parent b1c6826b9f
commit b2d30c725d
7 changed files with 261 additions and 199 deletions

View File

@@ -38,6 +38,19 @@
class DisplayServerJavaScript : public DisplayServer {
private:
struct JSTouchEvent {
uint32_t identifier[32] = { 0 };
double coords[64] = { 0 };
};
JSTouchEvent touch_event;
struct JSKeyEvent {
char code[32] = { 0 };
char key[32] = { 0 };
uint8_t modifiers[4] = { 0 };
};
JSKeyEvent key_event;
WindowMode window_mode = WINDOW_MODE_WINDOWED;
ObjectID window_attached_instance_id = {};
@@ -47,44 +60,33 @@ private:
Callable drop_files_callback;
String clipboard;
Ref<InputEventKey> deferred_key_event;
Point2 touches[32];
char canvas_id[256] = { 0 };
bool cursor_inside_canvas = true;
CursorShape cursor_shape = CURSOR_ARROW;
Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
double last_click_ms = 0;
uint64_t last_click_ms = 0;
int last_click_button_index = -1;
bool swap_cancel_ok = false;
// utilities
static Point2 compute_position_in_canvas(int p_x, int p_y);
static void focus_canvas();
static bool is_canvas_focused();
template <typename T>
static void dom2godot_mod(T *emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event);
static Ref<InputEventKey> setup_key_event(const EmscriptenKeyboardEvent *emscripten_event);
static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod);
static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
// events
static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
static EM_BOOL keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
static EM_BOOL keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
static EM_BOOL keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
static void touch_callback(int p_type, int p_count);
static void key_callback(int p_pressed, int p_repeat, int p_modifiers);
static void vk_input_text_callback(const char *p_text, int p_cursor);
static EM_BOOL mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
static EM_BOOL mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
static EM_BOOL wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data);
static EM_BOOL touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
static EM_BOOL touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
static EM_BOOL blur_callback(int p_event_type, const EmscriptenFocusEvent *p_event, void *p_user_data);
static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);