Beispiel #1
0
        public override void Update(Graphics g, double deltaTime)
        {
            base.Update(g, deltaTime);

            if (FocusedControl != this)
            {
                return;
            }
            if (Readonly)
            {
                return;
            }

            if (Input.IsAnyKeyDown())
            {
                if (!tt.Trigger(deltaTime, true) && Input.PressedKeys.Length == 0)
                {
                    return;
                }
                Key key = Input.DownKeys[0];
                if (Input.PressedKeys.Length > 0)
                {
                    key = Input.PressedKeys[0];
                }

                int idx = -1;
                if (AllowAlphabet && Text.Length < MaxLength && (idx = Array.IndexOf(Key_AtoZ, key)) >= 0)
                {
                    char c = 'a';
                    if (Input.Shift)
                    {
                        c = 'A';
                    }
                    c   += (char)idx;
                    Text = Text.Insert(seek, c.ToString());
                    seek++;
                }
                else if (AllowNumber && Text.Length < MaxLength && (idx = Array.IndexOf(Key_0to9, key)) >= 0 && !Input.Shift)
                {
                    char c = '0';
                    c   += (char)idx;
                    Text = Text.Insert(seek, c.ToString());
                    seek++;
                }
                else if (AllowSpecial && Text.Length < MaxLength && key == Key.Period)
                {
                    Text = Text.Insert(seek, ".");
                    seek++;
                }
                else if (AllowSpecial && Text.Length < MaxLength && key == Key.BackSlash && Input.Shift)
                {
                    Text = Text.Insert(seek, "_");
                    seek++;
                }
                else if (AllowSpecial && Text.Length < MaxLength && key == Key.Space)
                {
                    Text = Text.Insert(seek, " ");
                    seek++;
                }
                else if (key == Key.Enter && Text.Length < MaxLength)
                {
                    Text  = Text.Insert(seek, "\r\n");
                    seek += 2;
                }
                else if (key == Key.BackSpace && Text.Length > 0 && seek > 0)
                {
                    if (Text[seek - 1] == '\n')
                    {
                        Text  = Text.Remove(seek - 2, 2);
                        seek -= 2;
                        if (Text.Length > 0 && seek > 0)
                        {
                            Text = Text.Remove(seek - 1, 1);
                            seek--;
                        }
                    }
                    else
                    {
                        Text = Text.Remove(seek - 1, 1);
                        seek--;
                    }
                }
                else if (key == Key.Delete && Text.Length > 0)
                {
                    Text = "";
                    seek = 0;
                }

                else if (key == Key.Left && seek > 0)
                {
                    seek--;
                }
                else if (key == Key.Right && seek < Text.Length)
                {
                    seek++;
                }
            }
            else
            {
                tt.Trigger(deltaTime, false);
            }

            if (IsMouseOn && Input.IsButtonPressed(MouseButton.Left))
            {
                g = Drawer.GetGraphics();

                var x  = GetLocation(Size.X, Size.Y, Alignment).X + WorldLocation.X;
                var px = Input.MousePosition.X - x;

                var idx = 0;
                while (idx < Text.Length)
                {
                    var str = new string(Text.Take(idx + 1).ToArray());
                    var w   = g.MeasureString(str, Font).Width;
                    if (px < w)
                    {
                        break;
                    }
                    idx++;
                }
                seek = idx;
            }
        }