Fixes to auto triangle editing in BlendSpace2D

This commit is contained in:
Juan Linietsky 2019-01-10 21:40:46 -03:00
parent 27d7772381
commit 2cd15849f9
4 changed files with 42 additions and 10 deletions

View file

@ -47,11 +47,19 @@ bool AnimationNodeBlendSpace2DEditor::can_edit(const Ref<AnimationNode> &p_node)
return bs2d.is_valid();
}
void AnimationNodeBlendSpace2DEditor::_blend_space_changed() {
blend_space_draw->update();
}
void AnimationNodeBlendSpace2DEditor::edit(const Ref<AnimationNode> &p_node) {
if (blend_space.is_valid()) {
blend_space->disconnect("triangles_updated", this, "_blend_space_changed");
}
blend_space = p_node;
if (!blend_space.is_null()) {
blend_space->connect("triangles_updated", this, "_blend_space_changed");
_update_space();
}
}
@ -837,6 +845,7 @@ void AnimationNodeBlendSpace2DEditor::_bind_methods() {
ClassDB::bind_method("_removed_from_graph", &AnimationNodeBlendSpace2DEditor::_removed_from_graph);
ClassDB::bind_method("_auto_triangles_toggled", &AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled);
ClassDB::bind_method("_blend_space_changed", &AnimationNodeBlendSpace2DEditor::_blend_space_changed);
ClassDB::bind_method("_file_opened", &AnimationNodeBlendSpace2DEditor::_file_opened);
}

View file

@ -138,6 +138,8 @@ class AnimationNodeBlendSpace2DEditor : public AnimationTreeNodeEditorPlugin {
MENU_LOAD_FILE_CONFIRM = 1002
};
void _blend_space_changed();
protected:
void _notification(int p_what);
static void _bind_methods();

View file

@ -80,18 +80,15 @@ void AnimationNodeBlendSpace2D::add_blend_point(const Ref<AnimationRootNode> &p_
blend_points[p_at_index].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
blend_points_used++;
if (auto_triangles) {
trianges_dirty = true;
}
_queue_auto_triangles();
emit_signal("tree_changed");
}
void AnimationNodeBlendSpace2D::set_blend_point_position(int p_point, const Vector2 &p_position) {
ERR_FAIL_INDEX(p_point, blend_points_used);
blend_points[p_point].position = p_position;
if (auto_triangles) {
trianges_dirty = true;
}
_queue_auto_triangles();
}
void AnimationNodeBlendSpace2D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
ERR_FAIL_INDEX(p_point, blend_points_used);
@ -309,15 +306,27 @@ Vector<int> AnimationNodeBlendSpace2D::_get_triangles() const {
return t;
}
void AnimationNodeBlendSpace2D::_queue_auto_triangles() {
if (!auto_triangles || trianges_dirty) {
return;
}
trianges_dirty = true;
call_deferred("_update_triangles");
}
void AnimationNodeBlendSpace2D::_update_triangles() {
if (!auto_triangles || !trianges_dirty)
return;
print_line("updating triangles");
trianges_dirty = false;
triangles.clear();
if (blend_points_used < 3)
if (blend_points_used < 3) {
emit_signal("triangles_updated");
return;
}
Vector<Vector2> points;
points.resize(blend_points_used);
@ -327,9 +336,12 @@ void AnimationNodeBlendSpace2D::_update_triangles() {
Vector<Delaunay2D::Triangle> triangles = Delaunay2D::triangulate(points);
print_line("triangles generated: " + itos(triangles.size()));
for (int i = 0; i < triangles.size(); i++) {
add_triangle(triangles[i].points[0], triangles[i].points[1], triangles[i].points[2]);
}
emit_signal("triangles_updated");
}
Vector2 AnimationNodeBlendSpace2D::get_closest_point(const Vector2 &p_point) {
@ -546,6 +558,10 @@ String AnimationNodeBlendSpace2D::get_caption() const {
}
void AnimationNodeBlendSpace2D::_validate_property(PropertyInfo &property) const {
if (auto_triangles && property.name == "triangles") {
property.usage = 0;
}
if (property.name.begins_with("blend_point_")) {
String left = property.name.get_slicec('/', 0);
int idx = left.get_slicec('_', 2).to_int();
@ -557,10 +573,12 @@ void AnimationNodeBlendSpace2D::_validate_property(PropertyInfo &property) const
}
void AnimationNodeBlendSpace2D::set_auto_triangles(bool p_enable) {
auto_triangles = p_enable;
if (auto_triangles) {
trianges_dirty = true;
if (auto_triangles == p_enable) {
return;
}
auto_triangles = p_enable;
_queue_auto_triangles();
}
bool AnimationNodeBlendSpace2D::get_auto_triangles() const {
@ -625,6 +643,7 @@ void AnimationNodeBlendSpace2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace2D::get_blend_mode);
ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeBlendSpace2D::_tree_changed);
ClassDB::bind_method(D_METHOD("_update_triangles"), &AnimationNodeBlendSpace2D::_update_triangles);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_triangles", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_auto_triangles", "get_auto_triangles");
@ -642,6 +661,7 @@ void AnimationNodeBlendSpace2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "y_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_y_label", "get_y_label");
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NOEDITOR), "set_blend_mode", "get_blend_mode");
ADD_SIGNAL(MethodInfo("triangles_updated"));
BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);
BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);
BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);

View file

@ -82,6 +82,7 @@ protected:
bool trianges_dirty;
void _update_triangles();
void _queue_auto_triangles();
void _tree_changed();