Example #1
0
        public override void OnChangeTargetHeight(int amount)
        {
            VisualSlope pivothandle = null;
            List <IVisualEventReceiver> selectedsectors = mode.GetSelectedObjects(true, false, false, false, false);
            List <SectorLevel>          levels          = new List <SectorLevel>();

            if (selectedsectors.Count == 0)
            {
                levels.Add(level);
            }
            else
            {
                foreach (BaseVisualGeometrySector bvgs in selectedsectors)
                {
                    levels.Add(bvgs.Level);
                }

                if (!levels.Contains(level))
                {
                    levels.Add(level);
                }
            }

            // Try to find a slope handle the user set to be the pivot handle
            // TODO: doing this every time is kind of stupid. Maybe store the pivot handle in the mode?
            foreach (KeyValuePair <Sector, List <VisualSlope> > kvp in mode.AllSlopeHandles)
            {
                foreach (VisualSlope handle in kvp.Value)
                {
                    if (handle.Pivot)
                    {
                        pivothandle = handle;
                        break;
                    }
                }
            }

            // User didn't set a pivot handle, try to find the smart pivot handle
            if (pivothandle == null)
            {
                pivothandle = GetSmartPivotHandle();
            }

            // Still no pivot handle, cancle
            if (pivothandle == null)
            {
                return;
            }

            mode.CreateUndo("Change slope");

            Plane originalplane = level.plane;

            Vector3D p1, p2, p3;

            if (pivothandle is VisualVertexSlope)
            {
                // Build a new plane. Since we only got 2 points (the pivot point of the pivot handle and the vertex slope vertex) we need
                // to create a third point. That's done by getting the perpendicular of the line between the aforementioned 2 points, then
                // add the perpendicular to the vertex position of the vertex slope vertex
                p3 = pivothandle.GetPivotPoint();
                Vector2D perp = new Line2D(vertex.Position, p3).GetPerpendicular();

                p1 = new Vector3D(vertex.Position, originalplane.GetZ(vertex.Position) + amount);
                p2 = new Vector3D(vertex.Position + perp, originalplane.GetZ(vertex.Position + perp) + amount);
            }
            else             // VisualSidedefSlope
            {
                List <Vector3D> pivotpoints = ((VisualSidedefSlope)pivothandle).GetPivotPoints();
                p1 = new Vector3D(vertex.Position, originalplane.GetZ(vertex.Position) + amount);
                p2 = pivotpoints[0];
                p3 = pivotpoints[1];
            }

            Plane plane = new Plane(p1, p2, p3, true);

            // Apply slope to surfaces
            foreach (SectorLevel l in levels)
            {
                VisualSidedefSlope.ApplySlope(l, plane, mode);
            }

            mode.SetActionResult("Changed slope.");
        }