Beispiel #1
0
    public override void SetHandle(int index, Camera camera, Vector2 point)
    {
        Curve3D c = path.curve;

        if (!IsInstanceValid(c))
        {
            GD.Print("Invalid curve");
            return;
        }

        Transform gt       = path.GlobalTransform;
        Transform gi       = gt.AffineInverse();
        Vector3   ray_from = camera.ProjectRayOrigin(point);
        Vector3   ray_dir  = camera.ProjectRayNormal(point);

        // Setting curve point positions
        if (index < c.GetPointCount())
        {
            var   org = gt.Xform(original);
            Plane pp  = new Plane();
            pp.Normal = camera.Transform.basis.GetColumn(2);
            pp.D      = pp.Normal.Dot(org);

            Vector3?_inters = pp.IntersectRay(ray_from, ray_dir);

            if (_inters != null)
            {
                Vector3 _nnInters = (Vector3)_inters;
                if (plugin.snapEnabled)
                {
                    _nnInters = _nnInters.Snapped(new Vector3(plugin.snapLength, plugin.snapLength, plugin.snapLength));
                }

                _nnInters = plugin.RestrictPoint(org, _nnInters);

                Vector3 local = gi.Xform(_nnInters);
                c.SetPointPosition(index, local);
            }
            return;
        }

        index = index - c.GetPointCount() + 1;

        int idx = index / 2;
        int t   = index % 2;

        var     porig   = gt.Xform(original);
        Vector3 basePos = c.GetPointPosition(idx);

        Plane p = new Plane();

        p.Normal = camera.Transform.basis.GetColumn(2);
        p.D      = p.Normal.Dot(porig);

        Vector3?inters = p.IntersectRay(ray_from, ray_dir);

        // Setting curve in/out positions
        if (inters != null)
        {
            Vector3 nnInters = (Vector3)inters;
            if (!plugin.handleClicked)
            {
                origInLength  = c.GetPointIn(idx).Length();
                origOutLenght = c.GetPointOut(idx).Length();
                plugin.SetHandleClicked(true);
            }
            Vector3 orig  = gi.Xform(porig) - basePos;
            Vector3 local = gi.Xform(nnInters) - basePos;
            if (plugin.snapEnabled)
            {
                local = local.Snapped(new Vector3(plugin.snapLength, plugin.snapLength, plugin.snapLength));
            }

            local = plugin.RestrictPoint(orig, local);

            if (t == 0)
            {
                c.SetPointIn(idx, local);
                if (plugin.mirrorHandleAngle)
                {
                    c.SetPointOut(idx, plugin.mirrorHandleLength ? -local : (-local.Normalized() * origOutLenght));
                }
            }
            else
            {
                c.SetPointOut(idx, local);
                if (plugin.mirrorHandleAngle)
                {
                    c.SetPointIn(idx, plugin.mirrorHandleLength ? -local : (-local.Normalized() * origInLength));
                }
            }
        }
    }
Beispiel #2
0
    public override void CommitHandle(int index, object restore, bool cancel)
    {
        Curve3D c = path.curve;

        if (!IsInstanceValid(c))
        {
            return;
        }

        UndoRedo ur = plugin.GetUndoRedo();

        if (index < c.GetPointCount())
        {
            if (cancel)
            {
                c.SetPointPosition(index, (Vector3)restore);
                return;
            }
            ur.CreateAction(("Set Curve Point Position"));
            ur.AddDoMethod(c, "set_point_position", index, c.GetPointPosition(index));
            ur.AddUndoMethod(c, "set_point_position", index, restore);
            ur.CommitAction();

            return;
        }

        index = index - c.GetPointCount() + 1;

        int idx = index / 2;
        int t   = index % 2;

        if (t == 0)
        {
            if (cancel)
            {
                c.SetPointIn(index, (Vector3)restore);
                return;
            }

            ur.CreateAction(("Set Curve In Position"));
            ur.AddDoMethod(c, "set_point_in", idx, c.GetPointIn(idx));
            ur.AddUndoMethod(c, "set_point_in", idx, restore);

            if (plugin.mirrorHandleAngle)
            {
                ur.AddDoMethod(c, "set_point_out", idx, plugin.mirrorHandleLength ? -c.GetPointIn(idx) : (-c.GetPointIn(idx).Normalized() * origOutLenght));
                ur.AddUndoMethod(c, "set_point_out", idx, plugin.mirrorHandleLength ? -(Vector3)restore : (-((Vector3)restore).Normalized() * origOutLenght));
            }
            ur.CommitAction();
        }
        else
        {
            if (cancel)
            {
                c.SetPointOut(idx, (Vector3)restore);
                return;
            }

            ur.CreateAction(("Set Curve Out Position"));
            ur.AddDoMethod(c, "set_point_out", idx, c.GetPointOut(idx));
            ur.AddUndoMethod(c, "set_point_out", idx, restore);

            if (plugin.mirrorHandleAngle)
            {
                ur.AddDoMethod(c, "set_point_in", idx, plugin.mirrorHandleLength ? -c.GetPointOut(idx) : (-c.GetPointOut(idx).Normalized() * origInLength));
                ur.AddUndoMethod(c, "set_point_in", idx, plugin.mirrorHandleLength ? -(Vector3)(restore) : (-((Vector3)restore).Normalized() * origInLength));
            }
            ur.CommitAction();
        }
    }