Skip to content

Commit

Permalink
Add a Pause button to the 2D and 3D editor
Browse files Browse the repository at this point in the history
This can be used to decrease CPU/GPU usage if particles or shaders
are being displayed in the editor.

This closes godotengine/godot-proposals#942.

Co-authored-by: Chaosus <chaosus89@gmail.com>
  • Loading branch information
Chaosus authored and Calinou committed Mar 24, 2021
1 parent 9a64d6b commit 0625244
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
3 changes: 3 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6704,6 +6704,9 @@ EditorNode::EditorNode() {

preview_gen = memnew(AudioStreamPreviewGenerator);
add_child(preview_gen);

ED_SHORTCUT("editor/editor_timeline_pause", TTR("Pause Timeline"), KEY_PAUSE);

//plugin stuff

add_editor_plugin(memnew(DebuggerEditorPlugin(this, debug_menu)));
Expand Down
1 change: 1 addition & 0 deletions editor/icons/ToolFreeze.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 40 additions & 1 deletion editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4213,6 +4213,10 @@ void CanvasItemEditor::_notification(int p_what) {
zoom_minus->set_icon(get_theme_icon("ZoomLess", "EditorIcons"));
zoom_plus->set_icon(get_theme_icon("ZoomMore", "EditorIcons"));

// Use a different icon to avoid confusion with the project "pause" icon.
pause_button->set_icon(get_theme_icon("ToolFreeze", "EditorIcons"));
pause_button->set_tooltip(TTR("Pause the scene processing such as particles and shaders. This can be used to decrease CPU and GPU usage while in the editor."));

presets_menu->set_icon(get_theme_icon("ControlLayout", "EditorIcons"));
PopupMenu *p = presets_menu->get_popup();

Expand Down Expand Up @@ -5401,6 +5405,14 @@ void CanvasItemEditor::_focus_selection(int p_op) {
}
}

void CanvasItemEditor::_timeline_pause_button_toggled(bool pressed) {
if (pressed) {
Engine::get_singleton()->set_time_scale(0.0);
} else {
Engine::get_singleton()->set_time_scale(1.0);
}
}

void CanvasItemEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_update_override_camera_button", "game_running"), &CanvasItemEditor::_update_override_camera_button);
ClassDB::bind_method("_get_editor_data", &CanvasItemEditor::_get_editor_data);
Expand All @@ -5410,6 +5422,8 @@ void CanvasItemEditor::_bind_methods() {
ClassDB::bind_method("_reset_create_position", &CanvasItemEditor::_reset_create_position);
ClassDB::bind_method(D_METHOD("set_state"), &CanvasItemEditor::set_state);
ClassDB::bind_method(D_METHOD("update_viewport"), &CanvasItemEditor::update_viewport);
ClassDB::bind_method("_timeline_pause_button_toggled", &CanvasItemEditor::_timeline_pause_button_toggled);
ClassDB::bind_method(D_METHOD("set_pause_button_pressed", "pressed"), &CanvasItemEditor::set_pause_button_pressed);

ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
ADD_SIGNAL(MethodInfo("item_group_status_changed"));
Expand Down Expand Up @@ -5658,6 +5672,10 @@ VSplitContainer *CanvasItemEditor::get_bottom_split() {
return bottom_split;
}

void CanvasItemEditor::set_pause_button_pressed(bool p_pressed) {
pause_button->set_pressed(p_pressed);
}

void CanvasItemEditor::focus_selection() {
_focus_selection(VIEW_CENTER_TO_SELECTION);
}
Expand Down Expand Up @@ -5735,8 +5753,11 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) {
editor->call_deferred("connect", "play_pressed", Callable(this, "_update_override_camera_button"), make_binds(true));
editor->call_deferred("connect", "stop_pressed", Callable(this, "_update_override_camera_button"), make_binds(false));

main_hb = memnew(HBoxContainer);
add_child(main_hb);

hb = memnew(HBoxContainer);
add_child(hb);
main_hb->add_child(hb);
hb->set_anchors_and_offsets_preset(Control::PRESET_WIDE);

bottom_split = memnew(VSplitContainer);
Expand Down Expand Up @@ -6081,6 +6102,19 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) {
animation_hb->add_child(memnew(VSeparator));
animation_hb->hide();

main_hb->add_spacer();

extra_hb = memnew(HBoxContainer);
main_hb->add_child(extra_hb);

pause_button = memnew(Button);
extra_hb->add_child(pause_button);
pause_button->set_flat(true);
pause_button->set_toggle_mode(true);
pause_button->set_focus_mode(FOCUS_NONE);
pause_button->set_shortcut(ED_GET_SHORTCUT("editor/editor_timeline_pause"));
pause_button->connect("toggled", callable_mp(this, &CanvasItemEditor::_timeline_pause_button_toggled));

key_loc_button = memnew(Button);
key_loc_button->set_toggle_mode(true);
key_loc_button->set_flat(true);
Expand Down Expand Up @@ -6204,6 +6238,11 @@ void CanvasItemEditorPlugin::set_state(const Dictionary &p_state) {
canvas_item_editor->set_state(p_state);
}

void CanvasItemEditorPlugin::selected_notify() {
// Toggle pause.
canvas_item_editor->set_pause_button_pressed(Engine::get_singleton()->get_time_scale() <= CMP_EPSILON);
}

CanvasItemEditorPlugin::CanvasItemEditorPlugin(EditorNode *p_node) {
editor = p_node;
canvas_item_editor = memnew(CanvasItemEditor(editor));
Expand Down
10 changes: 10 additions & 0 deletions editor/plugins/canvas_item_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,17 @@ class CanvasItemEditor : public VBoxContainer {

HScrollBar *h_scroll;
VScrollBar *v_scroll;

HBoxContainer *main_hb;
HBoxContainer *hb;
HBoxContainer *extra_hb;

Button *zoom_minus;
Button *zoom_reset;
Button *zoom_plus;

Button *pause_button;

Map<Control *, Timer *> popup_temporarily_timers;

Label *warning_child_of_container;
Expand Down Expand Up @@ -447,6 +452,8 @@ class CanvasItemEditor : public VBoxContainer {
void _node_created(Node *p_node);
void _reset_create_position();

void _timeline_pause_button_toggled(bool pressed);

UndoRedo *undo_redo;
bool _build_bones_list(Node *p_node);
bool _get_bone_shape(Vector<Vector2> *shape, Vector<Vector2> *outline_shape, Map<BoneKey, BoneList>::Element *bone);
Expand Down Expand Up @@ -644,6 +651,8 @@ class CanvasItemEditor : public VBoxContainer {
void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
void edit(CanvasItem *p_canvas_item);

void set_pause_button_pressed(bool p_pressed);

void focus_selection();

bool is_anchors_mode_enabled() { return anchors_mode; };
Expand All @@ -665,6 +674,7 @@ class CanvasItemEditorPlugin : public EditorPlugin {
virtual void make_visible(bool p_visible) override;
virtual Dictionary get_state() const override;
virtual void set_state(const Dictionary &p_state) override;
virtual void selected_notify() override;

CanvasItemEditor *get_canvas_item_editor() { return canvas_item_editor; }

Expand Down
44 changes: 43 additions & 1 deletion editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4986,6 +4986,14 @@ void Node3DEditor::_menu_item_toggled(bool pressed, int p_option) {
}
}

void Node3DEditor::_timeline_pause_button_toggled(bool pressed) {
if (pressed) {
Engine::get_singleton()->set_time_scale(0.0);
} else {
Engine::get_singleton()->set_time_scale(1.0);
}
}

void Node3DEditor::_menu_gizmo_toggled(int p_option) {
const int idx = gizmos_menu->get_item_index(p_option);
gizmos_menu->toggle_item_multistate(idx);
Expand Down Expand Up @@ -6218,6 +6226,10 @@ void Node3DEditor::_notification(int p_what) {
tool_button[Node3DEditor::TOOL_GROUP_SELECTED]->set_icon(get_theme_icon("Group", "EditorIcons"));
tool_button[Node3DEditor::TOOL_UNGROUP_SELECTED]->set_icon(get_theme_icon("Ungroup", "EditorIcons"));

// Use a different icon to avoid confusion with the project "pause" icon.
pause_button->set_icon(get_theme_icon("ToolFreeze", "EditorIcons"));
pause_button->set_tooltip(TTR("Pause the scene processing such as particles and shaders. This can be used to decrease CPU and GPU usage while in the editor."));

tool_option_button[Node3DEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_theme_icon("Object", "EditorIcons"));
tool_option_button[Node3DEditor::TOOL_OPT_USE_SNAP]->set_icon(get_theme_icon("Snap", "EditorIcons"));
tool_option_button[Node3DEditor::TOOL_OPT_OVERRIDE_CAMERA]->set_icon(get_theme_icon("Camera3D", "EditorIcons"));
Expand Down Expand Up @@ -6272,6 +6284,9 @@ void Node3DEditor::_notification(int p_what) {
tool_button[Node3DEditor::TOOL_GROUP_SELECTED]->set_icon(get_theme_icon("Group", "EditorIcons"));
tool_button[Node3DEditor::TOOL_UNGROUP_SELECTED]->set_icon(get_theme_icon("Ungroup", "EditorIcons"));

// Use a different icon to avoid confusion with the project "pause" icon.
pause_button->set_icon(get_theme_icon("ToolFreeze", "EditorIcons"));

tool_option_button[Node3DEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_theme_icon("Object", "EditorIcons"));
tool_option_button[Node3DEditor::TOOL_OPT_USE_SNAP]->set_icon(get_theme_icon("Snap", "EditorIcons"));

Expand Down Expand Up @@ -6465,6 +6480,8 @@ void Node3DEditor::_bind_methods() {
ClassDB::bind_method("_get_editor_data", &Node3DEditor::_get_editor_data);
ClassDB::bind_method("_request_gizmo", &Node3DEditor::_request_gizmo);
ClassDB::bind_method("_refresh_menu_icons", &Node3DEditor::_refresh_menu_icons);
ClassDB::bind_method("_timeline_pause_button_toggled", &Node3DEditor::_timeline_pause_button_toggled);
ClassDB::bind_method("set_pause_button_pressed", &Node3DEditor::set_pause_button_pressed);

ADD_SIGNAL(MethodInfo("transform_key_request"));
ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
Expand Down Expand Up @@ -6618,6 +6635,10 @@ void Node3DEditor::_sun_direction_input(const Ref<InputEvent> &p_event) {
}
}

void Node3DEditor::set_pause_button_pressed(bool p_pressed) {
pause_button->set_pressed(p_pressed);
}

Node3DEditor::Node3DEditor(EditorNode *p_editor) {
gizmo.visible = true;
gizmo.scale = 1.0;
Expand All @@ -6638,8 +6659,11 @@ Node3DEditor::Node3DEditor(EditorNode *p_editor) {

camera_override_viewport_id = 0;

main_hbc_menu = memnew(HBoxContainer);
vbc->add_child(main_hbc_menu);

hbc_menu = memnew(HBoxContainer);
vbc->add_child(hbc_menu);
main_hbc_menu->add_child(hbc_menu);

Vector<Variant> button_binds;
button_binds.resize(1);
Expand Down Expand Up @@ -6823,6 +6847,19 @@ Node3DEditor::Node3DEditor(EditorNode *p_editor) {
view_menu->set_shortcut_context(this);
hbc_menu->add_child(view_menu);

main_hbc_menu->add_spacer();

extra_hbc_menu = memnew(HBoxContainer);
main_hbc_menu->add_child(extra_hbc_menu);

pause_button = memnew(Button);
extra_hbc_menu->add_child(pause_button);
pause_button->set_flat(true);
pause_button->set_toggle_mode(true);
pause_button->set_focus_mode(FOCUS_NONE);
pause_button->set_shortcut(ED_GET_SHORTCUT("editor/editor_timeline_pause"));
pause_button->connect("toggled", callable_mp(this, &Node3DEditor::_timeline_pause_button_toggled));

p = view_menu->get_popup();

accept = memnew(AcceptDialog);
Expand Down Expand Up @@ -7235,6 +7272,11 @@ float Node3DEditor::get_scale_snap() const {
return snap_value;
}

void Node3DEditorPlugin::selected_notify() {
// Toggle pause.
spatial_editor->set_pause_button_pressed(Engine::get_singleton()->get_time_scale() <= CMP_EPSILON);
}

void Node3DEditorPlugin::_bind_methods() {
ClassDB::bind_method("snap_cursor_to_plane", &Node3DEditorPlugin::snap_cursor_to_plane);
}
Expand Down
9 changes: 9 additions & 0 deletions editor/plugins/node_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ class Node3DEditor : public VBoxContainer {
PopupMenu *gizmos_menu;
MenuButton *view_menu;

Button *lock_button;
Button *unlock_button;
Button *pause_button;

AcceptDialog *accept;

ConfirmationDialog *snap_dialog;
Expand Down Expand Up @@ -707,8 +711,11 @@ class Node3DEditor : public VBoxContainer {
void _menu_gizmo_toggled(int p_option);
void _update_camera_override_button(bool p_game_running);
void _update_camera_override_viewport(Object *p_viewport);
void _timeline_pause_button_toggled(bool pressed);

HBoxContainer *main_hbc_menu;
HBoxContainer *hbc_menu;
HBoxContainer *extra_hbc_menu;

void _generate_selection_boxes();
UndoRedo *undo_redo;
Expand Down Expand Up @@ -875,6 +882,7 @@ class Node3DEditor : public VBoxContainer {

void edit(Node3D *p_spatial);
void clear();
void set_pause_button_pressed(bool p_pressed);

Node3DEditor(EditorNode *p_editor);
~Node3DEditor();
Expand All @@ -898,6 +906,7 @@ class Node3DEditorPlugin : public EditorPlugin {
virtual void make_visible(bool p_visible) override;
virtual void edit(Object *p_object) override;
virtual bool handles(Object *p_object) const override;
virtual void selected_notify() override;

virtual Dictionary get_state() const override;
virtual void set_state(const Dictionary &p_state) override;
Expand Down

0 comments on commit 0625244

Please sign in to comment.