Skip to content

Commit 43b4109

Browse files
committed
Add support for taking embedded window screenshots.
1 parent 8f87e60 commit 43b4109

File tree

10 files changed

+184
-13
lines changed

10 files changed

+184
-13
lines changed

editor/editor_node.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#include "editor/editor_property_name_processor.h"
9797
#include "editor/editor_resource_picker.h"
9898
#include "editor/editor_resource_preview.h"
99+
#include "editor/editor_run.h"
99100
#include "editor/editor_script.h"
100101
#include "editor/editor_settings.h"
101102
#include "editor/editor_settings_dialog.h"
@@ -3419,14 +3420,39 @@ void EditorNode::_request_screenshot() {
34193420

34203421
void EditorNode::_screenshot(bool p_use_utc) {
34213422
String name = "editor_screenshot_" + Time::get_singleton()->get_datetime_string_from_system(p_use_utc).remove_char(':') + ".png";
3422-
NodePath path = String("user://") + name;
3423-
_save_screenshot(path);
3423+
String path = String("user://") + name;
3424+
3425+
if (!EditorRun::request_screenshot(callable_mp(this, &EditorNode::_save_screenshot_with_embedded_process).bind(path))) {
3426+
_save_screenshot(path);
3427+
}
3428+
}
3429+
3430+
void EditorNode::_save_screenshot_with_embedded_process(int64_t p_w, int64_t p_h, const String &p_emb_path, const Rect2i &p_rect, const String &p_path) {
3431+
Control *main_screen_control = editor_main_screen->get_control();
3432+
ERR_FAIL_NULL_MSG(main_screen_control, "Cannot get the editor main screen control.");
3433+
Viewport *viewport = main_screen_control->get_viewport();
3434+
ERR_FAIL_NULL_MSG(viewport, "Cannot get a viewport from the editor main screen.");
3435+
Ref<ViewportTexture> texture = viewport->get_texture();
3436+
ERR_FAIL_COND_MSG(texture.is_null(), "Cannot get a viewport texture from the editor main screen.");
3437+
Ref<Image> img = texture->get_image();
3438+
ERR_FAIL_COND_MSG(img.is_null(), "Cannot get an image from a viewport texture of the editor main screen.");
3439+
img->convert(Image::FORMAT_RGBA8);
3440+
ERR_FAIL_COND(p_emb_path.is_empty());
3441+
Ref<Image> overlay = Image::load_from_file(p_emb_path);
3442+
DirAccess::remove_absolute(p_emb_path);
3443+
ERR_FAIL_COND_MSG(overlay.is_null(), "Cannot get an image from a embedded process.");
3444+
overlay->convert(Image::FORMAT_RGBA8);
3445+
overlay->resize(p_rect.size.x, p_rect.size.y);
3446+
img->blend_rect(overlay, Rect2i(0, 0, p_w, p_h), p_rect.position);
3447+
Error error = img->save_png(p_path);
3448+
ERR_FAIL_COND_MSG(error != OK, "Cannot save screenshot to file '" + p_path + "'.");
3449+
34243450
if (EDITOR_GET("interface/editor/automatically_open_screenshots")) {
3425-
OS::get_singleton()->shell_show_in_file_manager(ProjectSettings::get_singleton()->globalize_path(path), true);
3451+
OS::get_singleton()->shell_show_in_file_manager(ProjectSettings::get_singleton()->globalize_path(p_path), true);
34263452
}
34273453
}
34283454

3429-
void EditorNode::_save_screenshot(NodePath p_path) {
3455+
void EditorNode::_save_screenshot(const String &p_path) {
34303456
Control *main_screen_control = editor_main_screen->get_control();
34313457
ERR_FAIL_NULL_MSG(main_screen_control, "Cannot get the editor main screen control.");
34323458
Viewport *viewport = main_screen_control->get_viewport();
@@ -3437,6 +3463,10 @@ void EditorNode::_save_screenshot(NodePath p_path) {
34373463
ERR_FAIL_COND_MSG(img.is_null(), "Cannot get an image from a viewport texture of the editor main screen.");
34383464
Error error = img->save_png(p_path);
34393465
ERR_FAIL_COND_MSG(error != OK, "Cannot save screenshot to file '" + p_path + "'.");
3466+
3467+
if (EDITOR_GET("interface/editor/automatically_open_screenshots")) {
3468+
OS::get_singleton()->shell_show_in_file_manager(ProjectSettings::get_singleton()->globalize_path(p_path), true);
3469+
}
34403470
}
34413471

34423472
void EditorNode::_check_system_theme_changed() {

editor/editor_node.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ class EditorNode : public Node {
548548

549549
void _request_screenshot();
550550
void _screenshot(bool p_use_utc = false);
551-
void _save_screenshot(NodePath p_path);
551+
void _save_screenshot(const String &p_path);
552+
void _save_screenshot_with_embedded_process(int64_t p_w, int64_t p_h, const String &p_emb_path, const Rect2i &p_rect, const String &p_path);
552553

553554
void _check_system_theme_changed();
554555

editor/editor_run.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ Error EditorRun::run(const String &p_scene, const String &p_write_movie, const V
189189
return OK;
190190
}
191191

192+
bool EditorRun::request_screenshot(const Callable &p_callback) {
193+
if (instance_rq_screenshot_callback) {
194+
return instance_rq_screenshot_callback(p_callback);
195+
} else {
196+
return false;
197+
}
198+
}
199+
192200
bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
193201
for (const OS::ProcessID &E : pids) {
194202
if (E == p_pid) {

editor/editor_run.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "core/os/os.h"
3434

3535
typedef void (*EditorRunInstanceStarting)(int p_index, List<String> &r_arguments);
36+
typedef bool (*EditorRunInstanceRequestScreenshot)(const Callable &p_callback);
3637

3738
class EditorRun {
3839
public:
@@ -58,6 +59,7 @@ class EditorRun {
5859

5960
public:
6061
inline static EditorRunInstanceStarting instance_starting_callback = nullptr;
62+
inline static EditorRunInstanceRequestScreenshot instance_rq_screenshot_callback = nullptr;
6163

6264
Status get_status() const;
6365
String get_running_scene() const;
@@ -71,6 +73,8 @@ class EditorRun {
7173
int get_child_process_count() const { return pids.size(); }
7274
OS::ProcessID get_current_process() const;
7375

76+
static bool request_screenshot(const Callable &p_callback);
77+
7478
static WindowPlacement get_window_placement();
7579

7680
EditorRun();

editor/plugins/game_view_plugin.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/config/project_settings.h"
3434
#include "core/debugger/debugger_marshalls.h"
35+
#include "core/debugger/engine_debugger.h"
3536
#include "core/string/translation_server.h"
3637
#include "editor/debugger/editor_debugger_node.h"
3738
#include "editor/debugger/script_editor_debugger.h"
@@ -225,6 +226,61 @@ void GameViewDebugger::_bind_methods() {
225226
ADD_SIGNAL(MethodInfo("session_stopped"));
226227
}
227228

229+
bool GameViewDebugger::add_screenshot_callback(const Callable &p_callaback, const Rect2i &p_rect) {
230+
bool found = false;
231+
for (Ref<EditorDebuggerSession> &I : sessions) {
232+
if (I->is_active()) {
233+
ScreenshotCB sd;
234+
sd.cb = p_callaback;
235+
sd.rect = p_rect;
236+
screenshot_callbacks[scr_rq_id] = sd;
237+
238+
Array arr;
239+
arr.append(scr_rq_id);
240+
I->send_message("scene:rq_screenshot", arr);
241+
scr_rq_id++;
242+
found = true;
243+
}
244+
}
245+
return found;
246+
}
247+
248+
bool GameViewDebugger::_msg_get_screenshot(const Array &p_args) {
249+
ERR_FAIL_COND_V_MSG(p_args.size() != 4, false, "get_screenshot: invalid number of arguments");
250+
251+
int64_t id = p_args[0];
252+
int64_t w = p_args[1];
253+
int64_t h = p_args[2];
254+
const String &path = p_args[3];
255+
256+
if (screenshot_callbacks.has(id)) {
257+
if (screenshot_callbacks[id].cb.is_valid()) {
258+
screenshot_callbacks[id].cb.call(w, h, path, screenshot_callbacks[id].rect);
259+
}
260+
screenshot_callbacks.erase(id);
261+
}
262+
return true;
263+
}
264+
265+
bool GameViewDebugger::capture(const String &p_message, const Array &p_data, int p_session) {
266+
Ref<EditorDebuggerSession> session = get_session(p_session);
267+
ERR_FAIL_COND_V(session.is_null(), true);
268+
269+
if (p_message == "game_view:get_screenshot") {
270+
return _msg_get_screenshot(p_data);
271+
} else {
272+
// Any other messages with this prefix should be ignored.
273+
WARN_PRINT("GameViewDebugger unknown message: " + p_message);
274+
return false;
275+
}
276+
277+
return true;
278+
}
279+
280+
bool GameViewDebugger::has_capture(const String &p_capture) const {
281+
return p_capture == "game_view";
282+
}
283+
228284
GameViewDebugger::GameViewDebugger() {
229285
EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameViewDebugger::_feature_profile_changed));
230286
}
@@ -280,6 +336,23 @@ void GameView::_instance_starting(int p_idx, List<String> &r_arguments) {
280336
_update_arguments_for_instance(p_idx, r_arguments);
281337
}
282338

339+
bool GameView::_instance_rq_screenshot_static(const Callable &p_callback) {
340+
ERR_FAIL_NULL_V(singleton, false);
341+
return singleton->_instance_rq_screenshot(p_callback);
342+
}
343+
344+
bool GameView::_instance_rq_screenshot(const Callable &p_callback) {
345+
if (debugger.is_null() || window_wrapper->get_window_enabled() || !embedded_process || !embedded_process->is_embedding_completed()) {
346+
return false;
347+
}
348+
Rect2 r = embedded_process->get_adjusted_embedded_window_rect(embedded_process->get_rect());
349+
r.position += embedded_process->get_global_position();
350+
#ifndef MACOS_ENABLED
351+
r.position -= embedded_process->get_window()->get_position();
352+
#endif
353+
return debugger->add_screenshot_callback(p_callback, r);
354+
}
355+
283356
void GameView::_show_update_window_wrapper() {
284357
EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
285358
Point2 position = floating_window_rect.position;
@@ -749,6 +822,7 @@ void GameView::_notification(int p_what) {
749822
EditorRunBar::get_singleton()->connect("play_pressed", callable_mp(this, &GameView::_play_pressed));
750823
EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &GameView::_stop_pressed));
751824
EditorRun::instance_starting_callback = _instance_starting_static;
825+
EditorRun::instance_rq_screenshot_callback = _instance_rq_screenshot_static;
752826

753827
// Listen for project settings changes to update the window size and aspect ratio.
754828
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed));

editor/plugins/game_view_plugin.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,25 @@ class GameViewDebugger : public EditorDebuggerPlugin {
6060

6161
void _feature_profile_changed();
6262

63+
struct ScreenshotCB {
64+
Callable cb;
65+
Rect2i rect;
66+
};
67+
68+
int64_t scr_rq_id = 0;
69+
HashMap<uint64_t, ScreenshotCB> screenshot_callbacks;
70+
71+
bool _msg_get_screenshot(const Array &p_args);
72+
6373
protected:
6474
static void _bind_methods();
6575

6676
public:
77+
virtual bool capture(const String &p_message, const Array &p_data, int p_session) override;
78+
virtual bool has_capture(const String &p_capture) const override;
79+
80+
bool add_screenshot_callback(const Callable &p_callaback, const Rect2i &p_rect);
81+
6782
void set_suspend(bool p_enabled);
6883
void next_frame();
6984

@@ -173,6 +188,8 @@ class GameView : public VBoxContainer {
173188
void _play_pressed();
174189
static void _instance_starting_static(int p_idx, List<String> &r_arguments);
175190
void _instance_starting(int p_idx, List<String> &r_arguments);
191+
static bool _instance_rq_screenshot_static(const Callable &p_callback);
192+
bool _instance_rq_screenshot(const Callable &p_callback);
176193
void _stop_pressed();
177194
void _embedding_completed();
178195
void _embedding_failed();

platform/macos/editor/embedded_game_view_plugin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class GameViewDebuggerMacOS : public GameViewDebugger {
6060

6161
public:
6262
virtual bool capture(const String &p_message, const Array &p_data, int p_session) override;
63-
virtual bool has_capture(const String &p_capture) const override;
6463

6564
GameViewDebuggerMacOS(EmbeddedProcessMacOS *p_embedded_process);
6665
};

platform/macos/editor/embedded_game_view_plugin.mm

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@
109109
parse_message_handlers["game_view:joy_stop"] = &GameViewDebuggerMacOS::_msg_joy_stop;
110110
}
111111

112-
bool GameViewDebuggerMacOS::has_capture(const String &p_capture) const {
113-
return p_capture == "game_view";
114-
}
115-
116112
bool GameViewDebuggerMacOS::capture(const String &p_message, const Array &p_data, int p_session) {
117113
Ref<EditorDebuggerSession> session = get_session(p_session);
118114
ERR_FAIL_COND_V(session.is_null(), true);
@@ -121,9 +117,7 @@
121117
if (fn_ptr) {
122118
return (this->**fn_ptr)(p_data);
123119
} else {
124-
// Any other messages with this prefix should be ignored.
125-
WARN_PRINT("GameViewDebuggerMacOS unknown message: " + p_message);
126-
return ERR_SKIP;
120+
return GameViewDebugger::capture(p_message, p_data, p_session);
127121
}
128122

129123
return true;

scene/debugger/scene_debugger.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232

3333
#include "core/debugger/debugger_marshalls.h"
3434
#include "core/debugger/engine_debugger.h"
35+
#include "core/io/dir_access.h"
3536
#include "core/io/marshalls.h"
3637
#include "core/math/math_fieldwise.h"
3738
#include "core/object/script_language.h"
39+
#include "core/os/time.h"
3840
#include "core/templates/local_vector.h"
3941
#include "scene/gui/popup_menu.h"
4042
#include "scene/main/canvas_layer.h"
@@ -423,6 +425,46 @@ Error SceneDebugger::_msg_runtime_node_select_reset_camera_3d(const Array &p_arg
423425

424426
// endregion
425427

428+
// region Embedded process screenshot.
429+
430+
Error SceneDebugger::_msg_rq_screenshot(const Array &p_args) {
431+
ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
432+
433+
Viewport *viewport = SceneTree::get_singleton()->get_root();
434+
ERR_FAIL_NULL_V_MSG(viewport, ERR_UNCONFIGURED, "Cannot get a viewport from the main screen.");
435+
Ref<ViewportTexture> texture = viewport->get_texture();
436+
ERR_FAIL_COND_V_MSG(texture.is_null(), ERR_UNCONFIGURED, "Cannot get a viewport texture from the main screen.");
437+
Ref<Image> img = texture->get_image();
438+
ERR_FAIL_COND_V_MSG(img.is_null(), ERR_UNCONFIGURED, "Cannot get an image from a viewport texture of the main screen.");
439+
img->clear_mipmaps();
440+
441+
const String TEMP_DIR = OS::get_singleton()->get_temp_path();
442+
uint32_t suffix_i = 0;
443+
String path;
444+
while (true) {
445+
String datetime = Time::get_singleton()->get_datetime_string_from_system().remove_chars("-T:");
446+
datetime += itos(Time::get_singleton()->get_ticks_usec());
447+
String suffix = datetime + (suffix_i > 0 ? itos(suffix_i) : "");
448+
path = TEMP_DIR.path_join("scr-" + suffix + ".png");
449+
if (!DirAccess::exists(path)) {
450+
break;
451+
}
452+
suffix_i += 1;
453+
}
454+
img->save_png(path);
455+
456+
Array arr;
457+
arr.append(p_args[0]);
458+
arr.append(img->get_width());
459+
arr.append(img->get_height());
460+
arr.append(path);
461+
EngineDebugger::get_singleton()->send_message("game_view:get_screenshot", arr);
462+
463+
return OK;
464+
}
465+
466+
// endregion
467+
426468
HashMap<String, SceneDebugger::ParseMessageFunc> SceneDebugger::message_handlers;
427469

428470
Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
@@ -490,6 +532,7 @@ void SceneDebugger::_init_message_handlers() {
490532
#ifndef _3D_DISABLED
491533
message_handlers["runtime_node_select_reset_camera_3d"] = _msg_runtime_node_select_reset_camera_3d;
492534
#endif
535+
message_handlers["rq_screenshot"] = _msg_rq_screenshot;
493536
}
494537

495538
void SceneDebugger::_save_node(ObjectID id, const String &p_path) {

scene/debugger/scene_debugger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class SceneDebugger {
119119
#ifndef _3D_DISABLED
120120
static Error _msg_runtime_node_select_reset_camera_3d(const Array &p_args);
121121
#endif
122+
static Error _msg_rq_screenshot(const Array &p_args);
122123

123124
public:
124125
static Error parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);

0 commit comments

Comments
 (0)