Add editor screenshot on control - f12.

This commit is contained in:
K. S. Ernest (iFire) Lee 2019-03-29 06:53:11 -07:00
parent e2bbf2cba3
commit 05de0eafab
4 changed files with 95 additions and 0 deletions

View file

@ -51,6 +51,35 @@ uint32_t OS::get_ticks_msec() const {
return get_ticks_usec() / 1000;
}
String OS::get_iso_date_time(bool local) const {
OS::Date date = get_date(local);
OS::Time time = get_time(local);
String timezone;
if (!local) {
TimeZoneInfo zone = get_time_zone_info();
if (zone.bias >= 0) {
timezone = "+";
}
timezone = timezone + itos(zone.bias / 60).pad_zeros(2) + itos(zone.bias % 60).pad_zeros(2);
} else {
timezone = "Z";
}
return itos(date.year).pad_zeros(2) +
"-" +
itos(date.month).pad_zeros(2) +
"-" +
itos(date.day).pad_zeros(2) +
"T" +
itos(time.hour).pad_zeros(2) +
":" +
itos(time.min).pad_zeros(2) +
":" +
itos(time.sec).pad_zeros(2) +
timezone;
}
uint64_t OS::get_splash_tick_msec() const {
return _msec_splash;
}

View file

@ -336,6 +336,7 @@ public:
virtual Date get_date(bool local = false) const = 0;
virtual Time get_time(bool local = false) const = 0;
virtual TimeZoneInfo get_time_zone_info() const = 0;
virtual String get_iso_date_time(bool local = false) const;
virtual uint64_t get_unix_time() const;
virtual uint64_t get_system_time_secs() const;
virtual uint64_t get_system_time_msecs() const;

View file

@ -2485,7 +2485,16 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
bool was_visible = OS::get_singleton()->is_console_visible();
OS::get_singleton()->set_console_visible(!was_visible);
EditorSettings::get_singleton()->set_setting("interface/editor/hide_console_window", !was_visible);
} break;
case EDITOR_SCREENSHOT: {
screenshot_timer->start();
} break;
case EDITOR_OPEN_SCREENSHOT: {
bool is_checked = settings_menu->get_popup()->is_item_checked(settings_menu->get_popup()->get_item_index(EDITOR_OPEN_SCREENSHOT));
settings_menu->get_popup()->set_item_checked(settings_menu->get_popup()->get_item_index(EDITOR_OPEN_SCREENSHOT), !is_checked);
EditorSettings::get_singleton()->set_project_metadata("screenshot_options", "open_screenshot", !is_checked);
} break;
case SETTINGS_PICK_MAIN_SCENE: {
@ -2540,6 +2549,30 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
}
}
void EditorNode::_request_screenshot() {
_screenshot();
}
void EditorNode::_screenshot(bool p_use_utc) {
String name = "editor_screenshot_" + OS::get_singleton()->get_iso_date_time(p_use_utc).replace(":", "") + ".png";
NodePath path = String("user://") + name;
_save_screenshot(path);
if (EditorSettings::get_singleton()->get_project_metadata("screenshot_options", "open_screenshot", true)) {
OS::get_singleton()->shell_open(String("file://") + ProjectSettings::get_singleton()->globalize_path(path));
}
}
void EditorNode::_save_screenshot(NodePath p_path) {
Viewport *viewport = EditorInterface::get_singleton()->get_editor_viewport()->get_viewport();
viewport->set_clear_mode(Viewport::CLEAR_MODE_ONLY_NEXT_FRAME);
Ref<Image> img = viewport->get_texture()->get_data();
img->flip_y();
viewport->set_clear_mode(Viewport::CLEAR_MODE_ALWAYS);
Error error = img->save_png(p_path);
ERR_FAIL_COND(error != OK);
}
void EditorNode::_tool_menu_option(int p_idx) {
switch (tool_menu->get_item_id(p_idx)) {
case TOOLS_ORPHAN_RESOURCES: {
@ -5160,6 +5193,10 @@ void EditorNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("_resources_changed"), &EditorNode::_resources_changed);
ClassDB::bind_method(D_METHOD("_feature_profile_changed"), &EditorNode::_feature_profile_changed);
ClassDB::bind_method("_screenshot", &EditorNode::_screenshot);
ClassDB::bind_method("_request_screenshot", &EditorNode::_request_screenshot);
ClassDB::bind_method("_save_screenshot", &EditorNode::_save_screenshot);
ADD_SIGNAL(MethodInfo("play_pressed"));
ADD_SIGNAL(MethodInfo("pause_pressed"));
ADD_SIGNAL(MethodInfo("stop_pressed"));
@ -5885,6 +5922,16 @@ EditorNode::EditorNode() {
p->add_child(editor_layouts);
editor_layouts->connect("id_pressed", this, "_layout_menu_option");
p->add_submenu_item(TTR("Editor Layout"), "Layouts");
#ifdef OSX_ENABLED
p->add_shortcut(ED_SHORTCUT("editor/take_screenshot", TTR("Take Screenshot"), KEY_MASK_CMD | KEY_F12), EDITOR_SCREENSHOT);
#else
p->add_shortcut(ED_SHORTCUT("editor/take_screenshot", TTR("Take Screenshot"), KEY_MASK_CTRL | KEY_F12), EDITOR_SCREENSHOT);
#endif
p->set_item_tooltip(p->get_item_count() - 1, TTR("Screenshots are stored in the Editor Data/Settings Folder."));
p->add_check_shortcut(ED_SHORTCUT("editor/open_screenshot", TTR("Automatically Open Screenshots")), EDITOR_OPEN_SCREENSHOT);
bool is_open_screenshot = EditorSettings::get_singleton()->get_project_metadata("screenshot_options", "open_screenshot", true);
p->set_item_checked(p->get_item_count() - 1, is_open_screenshot);
p->set_item_tooltip(p->get_item_count() - 1, TTR("Open in an external image editor."));
#ifdef OSX_ENABLED
p->add_shortcut(ED_SHORTCUT("editor/fullscreen_mode", TTR("Toggle Fullscreen"), KEY_MASK_CMD | KEY_MASK_CTRL | KEY_F), SETTINGS_TOGGLE_FULLSCREEN);
#else
@ -6477,6 +6524,13 @@ EditorNode::EditorNode() {
ED_SHORTCUT("editor/editor_assetlib", TTR("Open Asset Library"));
ED_SHORTCUT("editor/editor_next", TTR("Open the next Editor"));
ED_SHORTCUT("editor/editor_prev", TTR("Open the previous Editor"));
screenshot_timer = memnew(Timer);
screenshot_timer->set_one_shot(true);
screenshot_timer->set_wait_time(settings_menu->get_popup()->get_submenu_popup_delay() + 0.1f);
screenshot_timer->connect("timeout", this, "_request_screenshot");
add_child(screenshot_timer);
screenshot_timer->set_owner(get_owner());
}
EditorNode::~EditorNode() {

View file

@ -199,6 +199,9 @@ private:
SETTINGS_HELP,
SCENE_TAB_CLOSE,
EDITOR_SCREENSHOT,
EDITOR_OPEN_SCREENSHOT,
HELP_SEARCH,
HELP_DOCS,
HELP_QA,
@ -280,6 +283,8 @@ private:
ToolButton *search_button;
TextureProgress *audio_vu;
Timer *screenshot_timer;
PluginConfigDialog *plugin_config_dialog;
RichTextLabel *load_errors;
@ -448,6 +453,11 @@ private:
void _menu_option(int p_option);
void _menu_confirm_current();
void _menu_option_confirm(int p_option, bool p_confirmed);
void _request_screenshot();
void _screenshot(bool p_use_utc = false);
void _save_screenshot(NodePath p_path);
void _tool_menu_option(int p_idx);
void _update_debug_options();
@ -646,6 +656,7 @@ private:
protected:
void _notification(int p_what);
static void _bind_methods();
protected: