コード例 #1
0
        private static int LayoutRow(LGuiTextFieldState State, LGuiFont Font, int StartIndex, ref LGuiVec2 TextSize)
        {
            var Index     = StartIndex;
            var LineWidth = 0.0f;

            TextSize.X = 0;
            TextSize.Y = 0;

            while (Index < State.TextLength)
            {
                var Ch = State.GetCharacter(Index++);

                if (Ch == '\n')
                {
                    TextSize.X = LGuiMisc.Max(TextSize.X, LineWidth);
                    TextSize.Y = TextSize.Y + Font.FontHeight + State.Spacing.Y;
                    LineWidth  = 0.0f;
                    break;
                }

                if (Ch == '\r')
                {
                    continue;
                }

                LineWidth = LineWidth + Font.FontWidth + State.Spacing.X;
            }

            if (TextSize.X < LineWidth)
            {
                TextSize.X = LineWidth;
            }

            if (LineWidth > 0 || TextSize.Y == 0.0f)
            {
                TextSize.Y = TextSize.Y + Font.FontHeight + State.Spacing.Y;
            }

            return(Index - StartIndex);
        }
コード例 #2
0
        private static int LocateCoord(LGuiTextFieldState State, LGuiFont Font, LGuiVec2 Pos)
        {
            var Length   = State.TextLength;
            var Index    = 0;
            var BaseY    = 0.0f;
            var Size     = LGuiVec2.Zero;
            var NumChars = 0;

            while (Index < Length)
            {
                NumChars = LayoutRow(State, Font, Index, ref Size);
                if (NumChars <= 0)
                {
                    return(Length);
                }

                if (Index == 0 && Pos.Y < BaseY)
                {
                    return(0);
                }

                if (Pos.Y < BaseY + Size.Y)
                {
                    break;
                }

                Index += NumChars;
                BaseY += Size.Y;
            }

            if (Index >= Length)
            {
                return(Length);
            }

            if (Pos.X < 0)
            {
                return(Index);
            }

            if (Pos.X < Size.X)
            {
                var PrevX = 0.0f;
                for (var N = 0; N < NumChars; ++N)
                {
                    var Width = Font.FontWidth + State.Spacing.X;
                    if (Pos.X < PrevX + Width)
                    {
                        if (Pos.X < PrevX + Width / 2.0f)
                        {
                            return(Index + N);
                        }
                        else
                        {
                            return(Index + N + 1);
                        }
                    }

                    PrevX += Width;
                }
            }

            if (State.GetCharacter(Index + NumChars - 1) == '\n')
            {
                return(Index + NumChars - 1);
            }

            return(Index + NumChars);
        }