Exemplo n.º 1
0
        public override void UpdateShapeStats()
        {
            Vector2[] vertices = this.VertexShape.Vertices;

            this.center = Vector2.Zero;
            for (int i = 0; i < vertices.Length; i++)
            {
                this.center += vertices[i];
            }
            this.center /= vertices.Length;

            this.scale = Vector2.Zero;
            for (int i = 0; i < vertices.Length; i++)
            {
                this.scale.X = MathF.Max(this.scale.X, MathF.Abs(vertices[i].X - this.center.X));
                this.scale.Y = MathF.Max(this.scale.Y, MathF.Abs(vertices[i].Y - this.center.Y));
            }

            this.boundRad = 0.0f;
            for (int i = 0; i < vertices.Length; i++)
            {
                this.boundRad = MathF.Max(this.boundRad, (vertices[i] - this.center).Length);
            }

            this.angle = MathF.Angle(this.center.X, this.center.Y, vertices[0].X, vertices[0].Y);
        }
Exemplo n.º 2
0
        public void Update(CamViewState state, Point mouseLoc, Point beginLoc, Vector3 beginLocSpace, Vector3 lastLocSpace, Vector3 selectionCenter, float selectionRadius, CamViewState.LockedAxis lockedAxis)
        {
            state.ValidateSelectionStats();

            Vector3 spaceCoord = state.GetSpaceCoord(new Vector3(mouseLoc.X, mouseLoc.Y, selectionCenter.Z));
            float   lastAngle  = MathF.Angle(selectionCenter.X, selectionCenter.Y, lastLocSpace.X, lastLocSpace.Y);
            float   curAngle   = MathF.Angle(selectionCenter.X, selectionCenter.Y, spaceCoord.X, spaceCoord.Y);
            float   rotation   = curAngle - lastAngle;

            state.RotateSelectionBy(rotation);

            state.ActionLastLocSpace = spaceCoord;
        }
        private void UpdateObjRotate(Point mouseLoc)
        {
            this.ValidateSelectionStats();

            Vector3 spaceCoord = this.GetSpaceCoord(new Vector3(mouseLoc.X, mouseLoc.Y, this.selectionCenter.Z));
            float   lastAngle  = MathF.Angle(this.selectionCenter.X, this.selectionCenter.Y, this.actionLastLocSpace.X, this.actionLastLocSpace.Y);
            float   curAngle   = MathF.Angle(this.selectionCenter.X, this.selectionCenter.Y, spaceCoord.X, spaceCoord.Y);
            float   rotation   = curAngle - lastAngle;

            this.RotateSelectionBy(rotation);

            this.actionLastLocSpace = spaceCoord;
        }
Exemplo n.º 4
0
        private void TilesetView_MouseMove(object sender, MouseEventArgs e)
        {
            Size  tileSize   = this.TilesetView.DisplayedTileSize;
            Point tilePos    = this.TilesetView.GetTileIndexLocation(this.TilesetView.HoveredTileIndex);
            Point posOnTile  = new Point(e.X - tilePos.X, e.Y - tilePos.Y);
            Size  centerSize = new Size(
                tileSize.Width - TilemapsResCache.TilesetCollisionVertical.Width * 2 - 2,
                tileSize.Height - TilemapsResCache.TilesetCollisionHorizontal.Height * 2 - 2);

            // Determine the hovered tile hotspot for user interaction
            TileHotSpot lastHoveredArea = this.hoveredArea;

            if (posOnTile.X > (tileSize.Width - centerSize.Width) / 2 &&
                posOnTile.Y > (tileSize.Height - centerSize.Height) / 2 &&
                posOnTile.X < (tileSize.Width + centerSize.Width) / 2 &&
                posOnTile.Y < (tileSize.Height + centerSize.Height) / 2)
            {
                this.hoveredArea = TileHotSpot.Center;
            }
            else
            {
                float angle = MathF.Angle(tileSize.Width / 2, tileSize.Height / 2, posOnTile.X, posOnTile.Y);
                if (MathF.CircularDist(angle, 0.0f) < MathF.RadAngle45)
                {
                    this.hoveredArea = TileHotSpot.Top;
                }
                else if (MathF.CircularDist(angle, MathF.RadAngle90) < MathF.RadAngle45)
                {
                    this.hoveredArea = TileHotSpot.Right;
                }
                else if (MathF.CircularDist(angle, MathF.RadAngle180) < MathF.RadAngle45)
                {
                    this.hoveredArea = TileHotSpot.Bottom;
                }
                else
                {
                    this.hoveredArea = TileHotSpot.Left;
                }
            }

            // If the user is in the process of setting or clearing bits, perform the drawing operation
            if (this.isUserDrawing)
            {
                this.PerformUserDrawAction();
            }

            if (lastHoveredArea != this.hoveredArea)
            {
                this.TilesetView.InvalidateTile(this.TilesetView.HoveredTileIndex, 0);
            }
        }
            public void UpdateLoopStats()
            {
                this.center = Vector2.Zero;
                for (int i = 0; i < this.loop.Vertices.Length; i++)
                {
                    this.center += this.loop.Vertices[i];
                }
                this.center /= this.loop.Vertices.Length;

                this.scale = Vector2.Zero;
                for (int i = 0; i < this.loop.Vertices.Length; i++)
                {
                    this.scale.X = MathF.Max(this.scale.X, MathF.Abs(this.loop.Vertices[i].X - this.center.X));
                    this.scale.Y = MathF.Max(this.scale.Y, MathF.Abs(this.loop.Vertices[i].Y - this.center.Y));
                }

                this.boundRad = 0.0f;
                for (int i = 0; i < this.loop.Vertices.Length; i++)
                {
                    this.boundRad = MathF.Max(this.boundRad, (this.loop.Vertices[i] - this.center).Length);
                }

                this.angle = MathF.Angle(this.center.X, this.center.Y, this.loop.Vertices[0].X, this.loop.Vertices[0].Y);
            }
Exemplo n.º 6
0
        private void TilesetView_MouseMove(object sender, MouseEventArgs e)
        {
            Size  tileSize   = this.TilesetView.DisplayedTileSize;
            Point tilePos    = this.TilesetView.GetTileIndexLocation(this.TilesetView.HoveredTileIndex);
            Point posOnTile  = new Point(e.X - tilePos.X, e.Y - tilePos.Y);
            Size  centerSize = new Size(tileSize.Width / 2, tileSize.Height / 2);

            // Determine the hovered tile hotspot for user interaction
            TileConnection lastHoveredArea = this.hoveredArea;

            if (posOnTile.X > (tileSize.Width - centerSize.Width) / 2 &&
                posOnTile.Y > (tileSize.Height - centerSize.Height) / 2 &&
                posOnTile.X < (tileSize.Width + centerSize.Width) / 2 &&
                posOnTile.Y < (tileSize.Height + centerSize.Height) / 2)
            {
                this.hoveredArea = TileConnection.None;
            }
            else
            {
                float angle     = MathF.Angle(tileSize.Width / 2, tileSize.Height / 2, posOnTile.X, posOnTile.Y);
                float threshold = MathF.DegToRad(22.5f);
                if (MathF.CircularDist(angle, MathF.DegToRad(315.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.TopLeft;
                }
                else if (MathF.CircularDist(angle, MathF.DegToRad(0.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.Top;
                }
                else if (MathF.CircularDist(angle, MathF.DegToRad(45.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.TopRight;
                }
                else if (MathF.CircularDist(angle, MathF.DegToRad(270.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.Left;
                }
                else if (MathF.CircularDist(angle, MathF.DegToRad(90.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.Right;
                }
                else if (MathF.CircularDist(angle, MathF.DegToRad(225.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.BottomLeft;
                }
                else if (MathF.CircularDist(angle, MathF.DegToRad(180.0f)) <= threshold)
                {
                    this.hoveredArea = TileConnection.Bottom;
                }
                else
                {
                    this.hoveredArea = TileConnection.BottomRight;
                }
            }

            // Update action state
            TilesetAutoTileInput autoTile = this.currentAutoTile;

            if (autoTile != null)
            {
                this.isBaseTileDraw =
                    autoTile.BaseTileIndex == -1 ||
                    autoTile.BaseTileIndex == this.TilesetView.HoveredTileIndex;
            }
            this.UpdateExternalDrawMode();

            // If the user is in the process of setting or clearing bits, perform the drawing operation
            if (this.isUserDrawing)
            {
                this.PerformUserDrawAction();
            }

            if (lastHoveredArea != this.hoveredArea)
            {
                this.TilesetView.InvalidateTile(this.TilesetView.HoveredTileIndex, 0);
            }
        }
Exemplo n.º 7
0
        private FarseerPhysics.Common.Vertices CreateVertices(float scale)
        {
            if (this.vertices == null || this.vertices.Length < 3)
            {
                return(null);
            }
            if (!MathF.IsPolygonConvex(this.vertices))
            {
                return(null);
            }

            // Be sure to not exceed the maximum vertex count
            Vector2[] sortedVertices = this.vertices.ToArray();
            if (sortedVertices.Length > MaxVertices)
            {
                Array.Resize(ref sortedVertices, MaxVertices);
                Log.Core.WriteWarning("Maximum Polygon Shape vertex count exceeded: {0} > {1}", this.vertices.Length, MaxVertices);
            }

            // Don't let all vertices be aligned on one axis (zero-area polygons)
            if (sortedVertices.Length > 0)
            {
                Vector2 firstVertex = sortedVertices[0];
                bool    alignX      = true;
                bool    alignY      = true;
                for (int i = 0; i < sortedVertices.Length; i++)
                {
                    if (sortedVertices[i].X != firstVertex.X)
                    {
                        alignX = false;
                    }
                    if (sortedVertices[i].Y != firstVertex.Y)
                    {
                        alignY = false;
                    }
                    if (!alignX && !alignY)
                    {
                        break;
                    }
                }
                if (alignX)
                {
                    sortedVertices[0].X += 0.01f;
                }
                if (alignY)
                {
                    sortedVertices[0].Y += 0.01f;
                }
            }

            // Sort vertices clockwise before submitting them to Farseer
            Vector2 centroid = Vector2.Zero;

            for (int i = 0; i < sortedVertices.Length; i++)
            {
                centroid += sortedVertices[i];
            }
            centroid /= sortedVertices.Length;
            sortedVertices.StableSort(delegate(Vector2 first, Vector2 second)
            {
                return(MathF.RoundToInt(
                           1000000.0f * MathF.Angle(centroid.X, centroid.Y, first.X, first.Y) -
                           1000000.0f * MathF.Angle(centroid.X, centroid.Y, second.X, second.Y)));
            });

            // Shrink a little bit
            //for (int i = 0; i < sortedVertices.Length; i++)
            //{
            //    Vector2 rel = (sortedVertices[i] - centroid);
            //    float len = rel.Length;
            //    sortedVertices[i] = centroid + rel.Normalized * MathF.Max(0.0f, len - 1.5f);
            //}

            // Submit vertices
            FarseerPhysics.Common.Vertices v = new FarseerPhysics.Common.Vertices(sortedVertices.Length);
            for (int i = 0; i < sortedVertices.Length; i++)
            {
                v.Add(PhysicsUnit.LengthToPhysical * sortedVertices[i] * scale);
            }
            return(v);
        }