Skip to content

Commit

Permalink
Support update(reimport) files on web platform
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Nov 5, 2024
1 parent 3178f23 commit e0f3718
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 1 deletion.
7 changes: 7 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ void EditorNode::_notification(int p_what) {
#endif
get_tree()->get_root()->connect("files_dropped", callable_mp(this, &EditorNode::_dropped_files));
get_tree()->get_root()->connect("delete_files", callable_mp(this, &EditorNode::_delete_files));
get_tree()->get_root()->connect("update_files", callable_mp(this, &EditorNode::_update_files));

command_palette->register_shortcuts_as_command();

Expand Down Expand Up @@ -5996,9 +5997,15 @@ void EditorNode::remove_tool_menu_item(const String &p_name) {
PopupMenu *EditorNode::get_export_as_menu() {
return export_as_menu;
}

void EditorNode::_delete_files(const Vector<String> &p_files) {
FileSystemDock::get_singleton()->_delete_files(p_files);
}

void EditorNode::_update_files(const Vector<String> &p_files) {
FileSystemDock::get_singleton()->_update_files(p_files);
}

void EditorNode::_dropped_files(const Vector<String> &p_files) {
String to_path = ProjectSettings::get_singleton()->globalize_path(FileSystemDock::get_singleton()->get_current_directory());

Expand Down
1 change: 1 addition & 0 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ class EditorNode : public Node {
void _add_to_recent_scenes(const String &p_scene);
void _update_recent_scenes();
void _open_recent_scene(int p_idx);
void _update_files(const Vector<String> &p_files);
void _delete_files(const Vector<String> &p_files);
void _dropped_files(const Vector<String> &p_files);
void _add_dropped_files_recursive(const Vector<String> &p_files, String to_path);
Expand Down
4 changes: 4 additions & 0 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,10 @@ void FileSystemDock::_delete_files(const Vector<String> &p_files) {
_file_option(FILE_REMOVE, p_files);
}

void FileSystemDock::_update_files(const Vector<String> &p_files) {
_file_option(FILE_REIMPORT, p_files);
}

void FileSystemDock::_resource_created() {
String fpath = current_path;
if (!fpath.ends_with("/")) {
Expand Down
3 changes: 2 additions & 1 deletion editor/filesystem_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ class FileSystemDock : public VBoxContainer {
void remove_resource_tooltip_plugin(const Ref<EditorResourceTooltipPlugin> &p_plugin);
Control *create_tooltip_for_path(const String &p_path) const;

void _delete_files(const Vector<String> &p_files) ;
void _delete_files(const Vector<String> &p_files);
void _update_files(const Vector<String> &p_files);

FileSystemDock();
~FileSystemDock();
Expand Down
34 changes: 34 additions & 0 deletions platform/web/display_server_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ void DisplayServerWeb::_fullscreen_change_callback(int p_fullscreen) {
display->window_mode = WINDOW_MODE_WINDOWED;
}
}

// Update files callback.
void DisplayServerWeb::update_files_js_callback(const char **p_filev, int p_filec) {
Vector<String> files;
for (int i = 0; i < p_filec; i++) {
files.push_back(String::utf8(p_filev[i]));
}

#ifdef PROXY_TO_PTHREAD_ENABLED
if (!Thread::is_main_thread()) {
callable_mp_static(DisplayServerWeb::_update_files_js_callback).bind(files).call_deferred();
return;
}
#endif

_update_files_js_callback(files);
}

void DisplayServerWeb::_update_files_js_callback(const Vector<String> &p_files) {
DisplayServerWeb *ds = get_singleton();
if (!ds) {
ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active");
}
if (ds->update_files_callback.is_null()) {
return;
}
ds->update_files_callback.call(p_files);
}

// Delete files callback.
void DisplayServerWeb::delete_files_js_callback(const char **p_filev, int p_filec) {
Vector<String> files;
Expand Down Expand Up @@ -1030,6 +1059,7 @@ DisplayServerWeb::DisplayServerWeb(const String &p_rendering_driver, WindowMode
godot_js_input_paste_cb(&DisplayServerWeb::update_clipboard_callback);
godot_js_input_drop_files_cb(&DisplayServerWeb::drop_files_js_callback);
godot_js_delete_files_cb(&DisplayServerWeb::delete_files_js_callback);
godot_js_update_files_cb(&DisplayServerWeb::update_files_js_callback);
godot_js_input_gamepad_cb(&DisplayServerWeb::gamepad_callback);

// JS Display interface (js/libs/library_godot_display.js)
Expand Down Expand Up @@ -1167,6 +1197,10 @@ void DisplayServerWeb::window_set_delete_files_callback(const Callable &p_callab
delete_files_callback = p_callable;
}

void DisplayServerWeb::window_set_update_files_callback(const Callable &p_callable, WindowID p_window) {
update_files_callback = p_callable;
}

void DisplayServerWeb::window_set_title(const String &p_title, WindowID p_window) {
godot_js_display_window_title_set(p_title.utf8().get_data());
}
Expand Down
2 changes: 2 additions & 0 deletions platform/web/display_server_web.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DisplayServerWeb : public DisplayServer {
Callable input_text_callback;
Callable drop_files_callback;
Callable delete_files_callback;
Callable update_files_callback;

String clipboard;
Point2 touches[32];
Expand Down Expand Up @@ -200,6 +201,7 @@ class DisplayServerWeb : public DisplayServer {

virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_delete_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_update_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;

virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;

Expand Down
1 change: 1 addition & 0 deletions platform/web/godot_js.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ extern int godot_js_input_gamepad_sample_get(int p_idx, float r_btns[16], int32_
extern void godot_js_input_paste_cb(void (*p_callback)(const char *p_text));
extern void godot_js_input_drop_files_cb(void (*p_callback)(const char **p_filev, int p_filec));
extern void godot_js_delete_files_cb(void (*p_callback)(const char **p_filev, int p_filec));
extern void godot_js_update_files_cb(void (*p_callback)(const char **p_filev, int p_filec));

// TTS
extern int godot_js_tts_is_speaking();
Expand Down
20 changes: 20 additions & 0 deletions platform/web/js/libs/library_godot_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,26 @@ const GodotInput = {
return 0;
},


godot_js_update_files_cb__proxy: 'sync',
godot_js_update_files_cb__sig: 'vi',
godot_js_update_files_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
const update_files = function (ev) {
const files = ev.detail;
const args = files || [];
if (!args.length) {
return;
}
const argc = args.length;
const argv = GodotRuntime.allocStringArray(args);
func(argv, argc);
GodotRuntime.freeStringArray(argv, argc);
};
const canvas = GodotConfig.canvas;
GodotEventListeners.add(canvas, "update_files", update_files);
},

godot_js_delete_files_cb__proxy: 'sync',
godot_js_delete_files_cb__sig: 'vi',
godot_js_delete_files_cb: function (callback) {
Expand Down
7 changes: 7 additions & 0 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ void Window::_update_window_callbacks() {
DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
DisplayServer::get_singleton()->window_set_delete_files_callback(callable_mp(this, &Window::_window_delete_files), window_id);
DisplayServer::get_singleton()->window_set_update_files_callback(callable_mp(this, &Window::_window_update_files), window_id);
}

Viewport *Window::get_embedder() const {
Expand Down Expand Up @@ -1573,6 +1574,10 @@ void Window::_window_input_text(const String &p_text) {
push_text_input(p_text);
}

void Window::_window_update_files(const Vector<String> &p_files) {
emit_signal(SNAME("update_files"), p_files);
}

void Window::_window_delete_files(const Vector<String> &p_files) {
emit_signal(SNAME("delete_files"), p_files);
}
Expand Down Expand Up @@ -2883,6 +2888,8 @@ void Window::_bind_methods() {
ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
ADD_SIGNAL(MethodInfo("delete_files", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
ADD_SIGNAL(MethodInfo("update_files", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));

ADD_SIGNAL(MethodInfo("mouse_entered"));
ADD_SIGNAL(MethodInfo("mouse_exited"));
ADD_SIGNAL(MethodInfo("focus_entered"));
Expand Down
2 changes: 2 additions & 0 deletions scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class Window : public Viewport {
void _window_input_text(const String &p_text);
void _window_drop_files(const Vector<String> &p_files);
void _window_delete_files(const Vector<String> &p_files);
void _window_update_files(const Vector<String> &p_files);

void _rect_changed_callback(const Rect2i &p_callback);
void _event_callback(DisplayServer::WindowEvent p_event);
virtual bool _can_consume_input_events() const override;
Expand Down
1 change: 1 addition & 0 deletions servers/display_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class DisplayServer : public Object {

virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) = 0;
virtual void window_set_delete_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) {};
virtual void window_set_update_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) {};

virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) = 0;
virtual Size2i window_get_title_size(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) const { return Size2i(); }
Expand Down

0 comments on commit e0f3718

Please sign in to comment.