Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input event callback to DisplayServerHeadless #92806

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions servers/display_server_headless.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ class DisplayServerHeadless : public DisplayServer {
return memnew(DisplayServerHeadless());
}

static void _dispatch_input_events(const Ref<InputEvent> &p_event) {
static_cast<DisplayServerHeadless *>(get_singleton())->_dispatch_input_event(p_event);
}

void _dispatch_input_event(const Ref<InputEvent> &p_event) {
if (input_event_callback.is_valid()) {
input_event_callback.call(p_event);
}
}

NativeMenu *native_menu = nullptr;
Callable input_event_callback;

public:
bool has_feature(Feature p_feature) const override { return false; }
Expand Down Expand Up @@ -86,7 +97,11 @@ class DisplayServerHeadless : public DisplayServer {
void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {}

void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {}
void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {}

void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
input_event_callback = p_callable;
}

void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {}
void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {}

Expand Down Expand Up @@ -137,7 +152,9 @@ class DisplayServerHeadless : public DisplayServer {

int64_t window_get_native_handle(HandleType p_handle_type, WindowID p_window = MAIN_WINDOW_ID) const override { return 0; }

void process_events() override {}
void process_events() override {
Input::get_singleton()->flush_buffered_events();
}

void set_native_icon(const String &p_filename) override {}
void set_icon(const Ref<Image> &p_icon) override {}
Expand Down Expand Up @@ -178,7 +195,9 @@ class DisplayServerHeadless : public DisplayServer {

DisplayServerHeadless() {
native_menu = memnew(NativeMenu);
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
}

~DisplayServerHeadless() {
if (native_menu) {
memdelete(native_menu);
Expand Down
22 changes: 1 addition & 21 deletions tests/display_server_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "servers/rendering/dummy/rasterizer_dummy.h"

// Specialized DisplayServer for unittests based on DisplayServerHeadless, that
// additionally supports rudimentary InputEvent handling and mouse position.
// additionally supports things like mouse enter/exit events and clipboard.
class DisplayServerMock : public DisplayServerHeadless {
private:
friend class DisplayServer;
Expand All @@ -45,7 +45,6 @@ class DisplayServerMock : public DisplayServerHeadless {
CursorShape cursor_shape = CursorShape::CURSOR_ARROW;
bool window_over = false;
Callable event_callback;
Callable input_event_callback;

String clipboard_text;
String primary_clipboard_text;
Expand All @@ -62,16 +61,6 @@ class DisplayServerMock : public DisplayServerHeadless {
return memnew(DisplayServerMock());
}

static void _dispatch_input_events(const Ref<InputEvent> &p_event) {
static_cast<DisplayServerMock *>(get_singleton())->_dispatch_input_event(p_event);
}

void _dispatch_input_event(const Ref<InputEvent> &p_event) {
if (input_event_callback.is_valid()) {
input_event_callback.call(p_event);
}
}

void _set_mouse_position(const Point2i &p_position) {
if (mouse_position == p_position) {
return;
Expand Down Expand Up @@ -153,18 +142,9 @@ class DisplayServerMock : public DisplayServerHeadless {
event_callback = p_callable;
}

virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
input_event_callback = p_callable;
}

static void register_mock_driver() {
register_create_function("mock", create_func, get_rendering_drivers_func);
}

DisplayServerMock() {
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
}
~DisplayServerMock() {}
};

#endif // DISPLAY_SERVER_MOCK_H
Loading