Exemple #1
0
        public override void Initialize(object[] parameters)
        {
            base.Initialize();

            string text = "";

            if (parameters.Length >= 1 && parameters[0] is string txt)
            {
                text = txt;
            }

            FontFamily font = new FontFamily("Consolas");

            if (parameters.Length >= 2 && parameters[1] is FontFamily fnt)
            {
                font = fnt;
            }

            Color color = Color.BLACK;

            if (parameters.Length >= 3 && parameters[2] is Color c)
            {
                color = c;
            }

            ITexture tex;

            if (parameters.Length >= 4 && parameters[3] is ITexture t)
            {
                tex = t;
            }
            else
            {
                tex = GraphicsHandler.CreateDefaultTexture(1, 1, Color.WHITE);
            }

            this.sprite          = GameObject.AddComponent <Sprite>(Color.WHITE, tex);
            this.textSprite      = GameObject.AddComponent <TextSprite>(text, 16, font, color, (Width, Height));
            this.textSprite.Dock = GUIDock.LeftCenter;

            this.blinkTime = -CURSOR_BLINK_TIME;

            this.text     = text;
            MaxTextLength = 10;

            OnSizeChanged += UpdateMesh;

            InputHandler.AddKeyPressEventHandler(keyChar => {
                if (!HasFocus)
                {
                    return;
                }

                if (keyChar == '\0' || Text.Length == MaxTextLength)
                {
                    return;
                }

                Text += keyChar;
            });

            InputHandler.AddKeyDownEventHandler((key, modifiers) => {
                if (!HasFocus)
                {
                    return;
                }

                if (key == Key.Back && Text.Length > 0)
                {
                    Text = Text.Substring(0, Text.Length - 1);
                }

                if (key == Key.Enter)
                {
                    OnTextApplied?.Invoke(this);
                }
            });
        }