Skip to content

Commit

Permalink
Merge pull request #63249 from V-Sekai/animation_tree_editor_read_only
Browse files Browse the repository at this point in the history
Add read-only mode to AnimationTreeEditor plugins
  • Loading branch information
akien-mga authored Aug 27, 2022
2 parents 488c501 + 75f1357 commit f999845
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 202 deletions.
3 changes: 3 additions & 0 deletions doc/classes/GraphEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@
</method>
</methods>
<members>
<member name="arrange_nodes_button_hidden" type="bool" setter="set_arrange_nodes_button_hidden" getter="is_arrange_nodes_button_hidden" default="false">
If [code]true[/code], the Arrange Nodes button is hidden.
</member>
<member name="clip_contents" type="bool" setter="set_clip_contents" getter="is_clipping_contents" overrides="Control" default="true" />
<member name="connection_lines_antialiased" type="bool" setter="set_connection_lines_antialiased" getter="is_connection_lines_antialiased" default="true">
If [code]true[/code], the lines between nodes will use antialiasing.
Expand Down
12 changes: 9 additions & 3 deletions doc/classes/GraphNode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
<description>
Sets properties of the slot with ID [param idx].
If [param enable_left]/[param enable_right], a port will appear and the slot will be able to be connected from this side.
[param type_left]/[param type_right] is an arbitrary type of the port. Only ports with the same type values can be connected.
[param type_left]/[param type_right] is an arbitrary type of the port. Only ports with the same type values can be connected and negative values will disallow all connections to be made via user inputs.
[param color_left]/[param color_right] is the tint of the port's icon on this side.
[param custom_left]/[param custom_right] is a custom texture for this side's port.
[b]Note:[/b] This method only sets properties of the slot. To create the slot, add a [Control]-derived child to the GraphNode.
Expand Down Expand Up @@ -208,22 +208,25 @@
<param index="0" name="idx" type="int" />
<param index="1" name="type_left" type="int" />
<description>
Sets the left (input) type of the slot [param idx] to [param type_left].
Sets the left (input) type of the slot [param idx] to [param type_left]. If the value is negative, all connections will be disallowed to be created via user inputs.
</description>
</method>
<method name="set_slot_type_right">
<return type="void" />
<param index="0" name="idx" type="int" />
<param index="1" name="type_right" type="int" />
<description>
Sets the right (output) type of the slot [param idx] to [param type_right].
Sets the right (output) type of the slot [param idx] to [param type_right]. If the value is negative, all connections will be disallowed to be created via user inputs.
</description>
</method>
</methods>
<members>
<member name="comment" type="bool" setter="set_comment" getter="is_comment" default="false">
If [code]true[/code], the GraphNode is a comment node.
</member>
<member name="draggable" type="bool" setter="set_draggable" getter="is_draggable" default="true">
If [code]true[/code], the user can drag the GraphNode.
</member>
<member name="language" type="String" setter="set_language" getter="get_language" default="&quot;&quot;">
Language code used for line-breaking and text shaping algorithms, if left empty current locale is used instead.
</member>
Expand All @@ -239,6 +242,9 @@
If [code]true[/code], the user can resize the GraphNode.
[b]Note:[/b] Dragging the handle will only emit the [signal resize_request] signal, the GraphNode needs to be resized manually.
</member>
<member name="selectable" type="bool" setter="set_selectable" getter="is_selectable" default="true">
If [code]true[/code], the user can select the GraphNode.
</member>
<member name="selected" type="bool" setter="set_selected" getter="is_selected" default="false">
If [code]true[/code], the GraphNode is selected.
</member>
Expand Down
4 changes: 4 additions & 0 deletions editor/editor_spin_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ void EditorSpinSlider::gui_input(const Ref<InputEvent> &p_event) {
}

void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
if (read_only) {
return;
}

Ref<InputEventMouseButton> mb = p_event;

if (grabbing_grabber) {
Expand Down
160 changes: 91 additions & 69 deletions editor/plugins/animation_blend_space_1d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,70 +48,74 @@ void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEven

if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
if (selected_point != -1) {
_erase_selected();
if (!read_only) {
_erase_selected();
}
accept_event();
}
}

Ref<InputEventMouseButton> mb = p_event;

if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (mb->get_button_index() == MouseButton::LEFT && tool_create->is_pressed()))) {
menu->clear();
animations_menu->clear();
animations_to_add.clear();
if (!read_only) {
menu->clear();
animations_menu->clear();
animations_to_add.clear();

List<StringName> classes;
ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
classes.sort_custom<StringName::AlphCompare>();
List<StringName> classes;
ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
classes.sort_custom<StringName::AlphCompare>();

menu->add_submenu_item(TTR("Add Animation"), "animations");
menu->add_submenu_item(TTR("Add Animation"), "animations");

AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
ERR_FAIL_COND(!gp);
AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
ERR_FAIL_COND(!gp);

if (gp->has_node(gp->get_animation_player())) {
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
if (gp->has_node(gp->get_animation_player())) {
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));

if (ap) {
List<StringName> names;
ap->get_animation_list(&names);
if (ap) {
List<StringName> names;
ap->get_animation_list(&names);

for (const StringName &E : names) {
animations_menu->add_icon_item(get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")), E);
animations_to_add.push_back(E);
for (const StringName &E : names) {
animations_menu->add_icon_item(get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")), E);
animations_to_add.push_back(E);
}
}
}
}

for (const StringName &E : classes) {
String name = String(E).replace_first("AnimationNode", "");
if (name == "Animation" || name == "StartState" || name == "EndState") {
continue;
}
for (const StringName &E : classes) {
String name = String(E).replace_first("AnimationNode", "");
if (name == "Animation" || name == "StartState" || name == "EndState") {
continue;
}

int idx = menu->get_item_count();
menu->add_item(vformat(TTR("Add %s"), name), idx);
menu->set_item_metadata(idx, E);
}
int idx = menu->get_item_count();
menu->add_item(vformat(TTR("Add %s"), name), idx);
menu->set_item_metadata(idx, E);
}

Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
if (clipb.is_valid()) {
Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
if (clipb.is_valid()) {
menu->add_separator();
menu->add_item(TTR("Paste"), MENU_PASTE);
}
menu->add_separator();
menu->add_item(TTR("Paste"), MENU_PASTE);
}
menu->add_separator();
menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
menu->add_item(TTR("Load..."), MENU_LOAD_FILE);

menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());
menu->reset_size();
menu->popup();
menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());
menu->reset_size();
menu->popup();

add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
add_point_pos += blend_space->get_min_space();
add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
add_point_pos += blend_space->get_min_space();

if (snap->is_pressed()) {
add_point_pos = Math::snapped(add_point_pos, blend_space->get_snap());
if (snap->is_pressed()) {
add_point_pos = Math::snapped(add_point_pos, blend_space->get_snap());
}
}
}

Expand All @@ -138,31 +142,33 @@ void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEven
}

if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {
if (dragging_selected) {
// move
float point = blend_space->get_blend_point_position(selected_point);
point += drag_ofs.x;
if (!read_only) {
if (dragging_selected) {
// move
float point = blend_space->get_blend_point_position(selected_point);
point += drag_ofs.x;

if (snap->is_pressed()) {
point = Math::snapped(point, blend_space->get_snap());
}

if (snap->is_pressed()) {
point = Math::snapped(point, blend_space->get_snap());
updating = true;
undo_redo->create_action(TTR("Move Node Point"));
undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
undo_redo->add_do_method(this, "_update_space");
undo_redo->add_undo_method(this, "_update_space");
undo_redo->add_do_method(this, "_update_edited_point_pos");
undo_redo->add_undo_method(this, "_update_edited_point_pos");
undo_redo->commit_action();
updating = false;
_update_edited_point_pos();
}

updating = true;
undo_redo->create_action(TTR("Move Node Point"));
undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
undo_redo->add_do_method(this, "_update_space");
undo_redo->add_undo_method(this, "_update_space");
undo_redo->add_do_method(this, "_update_edited_point_pos");
undo_redo->add_undo_method(this, "_update_edited_point_pos");
undo_redo->commit_action();
updating = false;
_update_edited_point_pos();
dragging_selected_attempt = false;
dragging_selected = false;
blend_space_draw->update();
}

dragging_selected_attempt = false;
dragging_selected = false;
blend_space_draw->update();
}

// *set* the blend
Expand Down Expand Up @@ -255,10 +261,12 @@ void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {
for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
float point = blend_space->get_blend_point_position(i);

if (dragging_selected && selected_point == i) {
point += drag_ofs.x;
if (snap->is_pressed()) {
point = Math::snapped(point, blend_space->get_snap());
if (!read_only) {
if (dragging_selected && selected_point == i) {
point += drag_ofs.x;
if (snap->is_pressed()) {
point = Math::snapped(point, blend_space->get_snap());
}
}
}

Expand Down Expand Up @@ -475,7 +483,7 @@ void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {

void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();
tool_erase->set_disabled(!point_valid);
tool_erase->set_disabled(!point_valid || read_only);

if (point_valid) {
Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
Expand All @@ -486,7 +494,11 @@ void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
open_editor->hide();
}

edit_hb->show();
if (!read_only) {
edit_hb->show();
} else {
edit_hb->hide();
}
} else {
edit_hb->hide();
}
Expand Down Expand Up @@ -590,10 +602,20 @@ bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node)

void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {
blend_space = p_node;
read_only = false;

if (!blend_space.is_null()) {
read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);

_update_space();
}

tool_create->set_disabled(read_only);
edit_value->set_editable(!read_only);
label_value->set_editable(!read_only);
min_value->set_editable(!read_only);
max_value->set_editable(!read_only);
sync->set_disabled(read_only);
}

AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = nullptr;
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/animation_blend_space_1d_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AnimationNodeBlendSpace1DEditor : public AnimationTreeNodeEditorPlugin {
GDCLASS(AnimationNodeBlendSpace1DEditor, AnimationTreeNodeEditorPlugin);

Ref<AnimationNodeBlendSpace1D> blend_space;
bool read_only = false;

HBoxContainer *goto_parent_hb = nullptr;
Button *goto_parent = nullptr;
Expand Down
Loading

0 comments on commit f999845

Please sign in to comment.