示例#1
0
        public static string InputField(RectangleShape rect, string textToRender, ref InputFieldStyle style)
        {
            if (style == null)
            {
                style = new InputFieldStyle();
            }

            // Instead of calling TextArea(), just run the dup code from the method, cause lazy
            //TextArea(rect, textToRender, style.GetTextStyle());

            Bitmap   textBitmap = new Bitmap((int)rect.Width, (int)rect.Height);
            Graphics g          = Graphics.FromImage(textBitmap);

            g.FillRectangle(ActiveInstance.GetColouredBrush(style.BackgroundColour), 0, 0, rect.Width, rect.Height);
            g.DrawString(textToRender, new Font(GetFontFamily(style.FontFamilyId), style.FontSize, FontStyle.Regular), ActiveInstance.GetColouredBrush(style.FontColour), new Rectangle(0, 0, (int)rect.Width, (int)rect.Height), ActiveInstance.GenerateTextFormat(style.HorizontalAlignment, style.VerticalAlignment));

            Sprite textSprite = Sprite.LoadFromBitmap(textBitmap);

            textSprite.Create(false);

            GUIOperation operation = new GUIOperation(1, 2);

            operation.Bounds    = rect;
            operation.DrawLevel = 1;
            operation.Sprite    = textSprite;
            operation.Colour    = Colour4b.White;

            g.Dispose();
            textBitmap.Dispose();

            ActiveInstance.m_guiOperations.Add(operation);
            // Finished creating text operation

            bool registeredADownClick = false;

            //Console.WriteLine("{0} mouse events on ({1})", ActiveInstance.m_currentMouseEvents.Count, TackEngine.RenderCycleCount);

            for (int i = 0; i < ActiveInstance.m_currentMouseEvents.Count; i++)
            {
                //Console.WriteLine("Pos: {0}, X: {1}, X + Width: {2}", ActiveInstance.m_currentMouseEvents[i].Position.X, rect.X, (rect.X + rect.Width));
                if (ActiveInstance.m_currentMouseEvents[i].Position.X >= rect.X && ActiveInstance.m_currentMouseEvents[i].Position.X <= (rect.X + rect.Width))
                {
                    //Console.WriteLine("Is X");
                    if (ActiveInstance.m_currentMouseEvents[i].Position.Y >= rect.Y && ActiveInstance.m_currentMouseEvents[i].Position.Y <= (rect.Y + rect.Height))
                    {
                        if (ActiveInstance.m_currentMouseEvents[i].EventType == 0)
                        {
                            registeredADownClick = true;
                            Console.WriteLine("Found a GUI mouse event of type: {0} involving the InputField", ActiveInstance.m_currentMouseEvents[i].EventType);
                        }
                    }
                }
            }

            if (registeredADownClick)
            {
                TackInput.GUIInputRequired = true;
            }
            else if (!registeredADownClick && ActiveInstance.m_currentMouseEvents.Count(x => x.EventType == 0) > 0)
            {
                Console.WriteLine("Found that a mouse down happened outside this inputfield");
                TackInput.GUIInputRequired = false;
            }

            KeyboardKey[] bufferOperations = TackInput.GetInputBufferArray();
            string        newString;

            if (textToRender == null)
            {
                newString = "";
            }
            else
            {
                newString = textToRender;
            }

            for (int i = 0; i < bufferOperations.Length; i++)
            {
                if (bufferOperations[i] == KeyboardKey.Left)
                {
                    if (style.CaretPosition > 0)
                    {
                        style.CaretPosition -= 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Right)
                {
                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.BackSpace)
                {
                    if (style.CaretPosition > 0)
                    {
                        newString = newString.Remove((int)style.CaretPosition - 1, 1);
                    }

                    if (style.CaretPosition > 0)
                    {
                        style.CaretPosition -= 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Delete)
                {
                    if (style.CaretPosition < newString.Length)
                    {
                        newString = newString.Remove((int)style.CaretPosition, 1);
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Space)
                {
                    newString = newString.Insert((int)style.CaretPosition, " ");

                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Period)
                {
                    newString = newString.Insert((int)style.CaretPosition, ".");

                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Quote)
                {
                    newString = newString.Insert((int)style.CaretPosition, "\"");

                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Minus)
                {
                    if (TackInput.InputBufferShift)
                    {
                        newString = newString.Insert((int)style.CaretPosition, "_");
                    }
                    else
                    {
                        newString = newString.Insert((int)style.CaretPosition, "-");
                    }

                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }

                else if (bufferOperations[i] >= KeyboardKey.Number0 && bufferOperations[i] <= KeyboardKey.Number9)
                {
                    newString = newString.Insert((int)style.CaretPosition, ((char)((int)bufferOperations[i] - 61)).ToString());

                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }
                else if (bufferOperations[i] >= KeyboardKey.A && bufferOperations[i] <= KeyboardKey.Z)
                {
                    if (TackInput.InputBufferCapsLock || TackInput.InputBufferShift)
                    {
                        newString = newString.Insert((int)style.CaretPosition, ((char)((int)bufferOperations[i] - 18)).ToString());
                    }
                    else
                    {
                        newString = newString.Insert((int)style.CaretPosition, ((char)((int)bufferOperations[i] + 14)).ToString());
                    }

                    if (style.CaretPosition < newString.Length)
                    {
                        style.CaretPosition += 1;
                    }
                }
            }

            TackInput.ClearInputBuffer();

            return(newString);
        }
        internal override void OnUpdate()
        {
            m_caretBox.Bounds = new RectangleShape(Bounds.X + 10, Bounds.Y + 10, 0, 0);

            Vector2f mousePosition = TackInput.MousePosition();

            if (TackInput.MouseButtonUp(MouseButtonKey.Left))
            {
                if (mousePosition.X >= Bounds.X && mousePosition.X <= (Bounds.X + Bounds.Width))
                {
                    if (mousePosition.Y >= Bounds.Y && mousePosition.Y <= (Bounds.Y + Bounds.Height))
                    {
                        RequiringInput             = true;
                        TackInput.GUIInputRequired = true;
                    }
                    else
                    {
                        RequiringInput             = false;
                        TackInput.GUIInputRequired = false;
                    }
                }
                else
                {
                    RequiringInput             = false;
                    TackInput.GUIInputRequired = false;
                }
            }

            KeyboardKey[] bufferOperations = TackInput.GetInputBufferArray();

            for (int i = 0; i < bufferOperations.Length; i++)
            {
                if (bufferOperations[i] == KeyboardKey.Enter)
                {
                    if (OnSubmit != null)
                    {
                        if (OnSubmit.GetInvocationList().Length > 0)
                        {
                            OnSubmit.Invoke(this, Text);
                        }
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Left)
                {
                    if (SelectionStart > 0)
                    {
                        SelectionStart -= 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Right)
                {
                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.BackSpace)
                {
                    if (SelectionStart > 0)
                    {
                        Text = Text.Remove((int)SelectionStart - 1, 1);
                    }

                    if (SelectionStart > 0)
                    {
                        SelectionStart -= 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Delete)
                {
                    if (SelectionStart < Text.Length)
                    {
                        Text = Text.Remove((int)SelectionStart, 1);
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Space)
                {
                    Text = Text.Insert((int)SelectionStart, " ");

                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Period)
                {
                    Text = Text.Insert((int)SelectionStart, ".");

                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Quote)
                {
                    Text = Text.Insert((int)SelectionStart, "\"");

                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }
                else if (bufferOperations[i] == KeyboardKey.Minus)
                {
                    if (TackInput.InputBufferShift)
                    {
                        Text = Text.Insert((int)SelectionStart, "_");
                    }
                    else
                    {
                        Text = Text.Insert((int)SelectionStart, "-");
                    }

                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }

                else if (bufferOperations[i] >= KeyboardKey.Number0 && bufferOperations[i] <= KeyboardKey.Number9)
                {
                    Text = Text.Insert((int)SelectionStart, ((char)((int)bufferOperations[i] - 61)).ToString());

                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }
                else if (bufferOperations[i] >= KeyboardKey.A && bufferOperations[i] <= KeyboardKey.Z)
                {
                    if (TackInput.InputBufferCapsLock || TackInput.InputBufferShift)
                    {
                        Text = Text.Insert((int)SelectionStart, ((char)((int)bufferOperations[i] - 18)).ToString());
                    }
                    else
                    {
                        Text = Text.Insert((int)SelectionStart, ((char)((int)bufferOperations[i] + 14)).ToString());
                    }

                    if (SelectionStart < Text.Length)
                    {
                        SelectionStart += 1;
                    }
                }
            }

            TackInput.ClearInputBuffer();
        }