예제 #1
0
파일: Font.cs 프로젝트: KSLcom/duality
        /// <summary>
        /// Returns the index of the glyph that is located at a certain location within a text.
        /// </summary>
        /// <param name="text">The text from which to pick a glyph.</param>
        /// <param name="x">X-Coordinate of the position where to look for a glyph.</param>
        /// <param name="y">Y-Coordinate of the position where to look for a glyph.</param>
        /// <returns>The index of the glyph that is located at the specified position.</returns>
        public int PickTextGlyph(string text, float x, float y)
        {
            float curOffset = 0.0f;
            GlyphData glyphData;
            Rect uvRect;
            Rect glyphRect;
            float glyphXOff;
            float glyphXAdv;
            for (int i = 0; i < text.Length; i++)
            {
                this.ProcessTextAdv(text, i, out glyphData, out uvRect, out glyphXAdv, out glyphXOff);

                glyphRect = new Rect(curOffset + glyphXOff, 0, glyphData.width, glyphData.height);
                if (glyphRect.Contains(x, y)) return i;

                curOffset += glyphXAdv;
            }

            return -1;
        }
        private List<ShapeInfo> PickShapes(RigidBody body, Vector2 worldCoord, Vector2 worldSize)
        {
            Rect worldRect = new Rect(worldCoord.X, worldCoord.Y, worldSize.X, worldSize.Y);

            // Do a physical picking operation
            List<ShapeInfo> result = body.PickShapes(worldCoord, worldSize);

            // Special case for LoopShapes, because they are by definition unpickable
            foreach (LoopShapeInfo loop in body.Shapes.OfType<LoopShapeInfo>())
            {
                bool hit = false;
                for (int i = 0; i < loop.Vertices.Length; i++)
                {
                    Vector2 worldV1 = body.GameObj.Transform.GetWorldPoint(loop.Vertices[i]);
                    Vector2 worldV2 = body.GameObj.Transform.GetWorldPoint(loop.Vertices[(i + 1) % loop.Vertices.Length]);
                    hit = hit || MathF.LinesCross(
                        worldRect.TopLeft.X,
                        worldRect.TopLeft.Y,
                        worldRect.TopRight.X,
                        worldRect.TopRight.Y,
                        worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                    hit = hit || MathF.LinesCross(
                        worldRect.TopLeft.X,
                        worldRect.TopLeft.Y,
                        worldRect.BottomLeft.X,
                        worldRect.BottomLeft.Y,
                        worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                    hit = hit || MathF.LinesCross(
                        worldRect.BottomRight.X,
                        worldRect.BottomRight.Y,
                        worldRect.TopRight.X,
                        worldRect.TopRight.Y,
                        worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                    hit = hit || MathF.LinesCross(
                        worldRect.BottomRight.X,
                        worldRect.BottomRight.Y,
                        worldRect.BottomLeft.X,
                        worldRect.BottomLeft.Y,
                        worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                    hit = hit || worldRect.Contains(worldV1) || worldRect.Contains(worldV2);
                    if (hit) break;
                }
                if (hit)
                {
                    result.Add(loop);
                    continue;
                }
            }

            return result;
        }
        private bool IsOutlineBoxIntersection(Transform transform, Vector2[] vertices, Rect box)
        {
            bool hit = false;
            for (int i = 0; i < vertices.Length; i++)
            {
                Vector2 worldV1 = transform.GetWorldPoint(vertices[i]);
                Vector2 worldV2 = transform.GetWorldPoint(vertices[(i + 1) % vertices.Length]);
                hit = hit || MathF.LinesCross(
                    box.TopLeft.X,
                    box.TopLeft.Y,
                    box.TopRight.X,
                    box.TopRight.Y,
                    worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                hit = hit || MathF.LinesCross(
                    box.TopLeft.X,
                    box.TopLeft.Y,
                    box.BottomLeft.X,
                    box.BottomLeft.Y,
                    worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                hit = hit || MathF.LinesCross(
                    box.BottomRight.X,
                    box.BottomRight.Y,
                    box.TopRight.X,
                    box.TopRight.Y,
                    worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                hit = hit || MathF.LinesCross(
                    box.BottomRight.X,
                    box.BottomRight.Y,
                    box.BottomLeft.X,
                    box.BottomLeft.Y,
                    worldV1.X, worldV1.Y, worldV2.X, worldV2.Y);
                hit = hit || box.Contains(worldV1) || box.Contains(worldV2);
                if (hit) return true;
            }

            return false;
        }