コード例 #1
0
ファイル: Console.cs プロジェクト: PWidmann/OpenGL_Demo
 public Console(BMFont font)
     : base(new Point(0, 0), new Point(500, 300), nameof(Console) + (object)UserInterface.GetUniqueElementID())
 {
     this.textBox                 = new TextBox(font, (Texture)null, -1);
     this.textBox.RelativeTo      = Corner.TopLeft;
     this.textBox.BackgroundColor = new Vector4(0.0f, 0.0f, 0.0f, 0.5f);
     this.textBox.AllowScrollBar  = true;
     this.AddElement((UIElement)this.textBox);
     this.textEntry                 = new TextInput(font, "");
     this.textEntry.RelativeTo      = Corner.BottomLeft;
     this.textEntry.BackgroundColor = new Vector4(0.0f, 0.0f, 0.0f, 0.7f);
     this.AddElement((UIElement)this.textEntry);
     this.textEntry.OnCarriageReturn = new TextInput.OnTextEvent(this.ExecuteCommand);
 }
コード例 #2
0
        public Console(BMFont font)
            : base(new Point(0, 0), new Point(500, 300), "Console" + UserInterface.GetUniqueElementID())
        {
            textBox                 = new TextBox(font, null);
            textBox.RelativeTo      = Corner.TopLeft;
            textBox.BackgroundColor = new Vector4(0, 0, 0, 0.5f);
            textBox.AllowScrollBar  = true;
            this.AddElement(textBox);

            textEntry                 = new TextInput(font);
            textEntry.RelativeTo      = Corner.BottomLeft;
            textEntry.BackgroundColor = new Vector4(0, 0, 0, 0.7f);
            this.AddElement(textEntry);

            textEntry.OnCarriageReturn = new TextInput.OnTextEvent(ExecuteCommand);
        }
コード例 #3
0
ファイル: UIContainer.cs プロジェクト: PWidmann/OpenGL_Demo
 public void AddElement(UIElement Element)
 {
     if (Element.Name == null || Element.Name.Length == 0)
     {
         Element.Name = Element.ToString() + (object)UserInterface.GetUniqueElementID();
     }
     if (UserInterface.Elements.ContainsKey(Element.Name))
     {
         return;
     }
     UserInterface.Elements.Add(Element.Name, Element);
     Element.Parent = this;
     this.elements.Add(Element);
     if (this != UserInterface.UIWindow)
     {
         return;
     }
     Element.OnResize();
 }
コード例 #4
0
        public void AddElement(UIElement Element)
        {
            if (Element.Name == null || Element.Name.Length == 0)
            {
                //Logger.Instance.WriteLine(LogFlags.Warning | LogFlags.UI, "Element of type " + Element.ToString() + " has no name!");
                Element.Name = Element.ToString() + UserInterface.GetUniqueElementID();
                //Logger.Instance.WriteLine(LogFlags.UI, "Assigned the name \"" + Element.Name + "\" to the Element.");
            }
            if (UserInterface.Elements.ContainsKey(Element.Name))
            {
                //Logger.Instance.WriteLine(LogFlags.Error | LogFlags.UI, "The element " + Element.Name +
                //    " already exists in the UI.  Could not add the UIElement to UIContainer " + this.Name);
                return;
            }
            UserInterface.Elements.Add(Element.Name, Element);
            Element.Parent = this;
            elements.Add(Element);

            if (this == UserInterface.UIWindow)
            {
                Element.OnResize();
            }
        }
コード例 #5
0
ファイル: CheckBox.cs プロジェクト: PWidmann/OpenGL_Demo
        public CheckBox(Texture uncheckedTexture, Texture checkedTexture, BMFont font, string text) : base(new OpenGL.Point(0, 0), new OpenGL.Point(uncheckedTexture.Size.Width, uncheckedTexture.Size.Height), nameof(CheckBox) + (object)UserInterface.GetUniqueElementID())
        {
            this.UncheckedTexture         = uncheckedTexture;
            this.CheckedTexture           = checkedTexture;
            this.checkBox                 = new Button(this.UncheckedTexture);
            this.checkBox.BackgroundColor = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
            this.checkBox.RelativeTo      = Corner.BottomLeft;
            Button checkBox = this.checkBox;
            Size   size2    = uncheckedTexture.Size;
            int    width2   = size2.Width;

            size2 = uncheckedTexture.Size;
            int height2 = size2.Height;

            OpenGL.Point point = new OpenGL.Point(width2, height2);
            checkBox.Size = point;
            this.AddElement((UIElement)this.checkBox);
            this.Font = font;
            this.Text = text;
            this.checkBox.OnMouseClick = (OnMouse)((o, e) => this.Checked = !this.Checked);
        }
コード例 #6
0
 public TextInput(BMFont font, string s = "")
     : base(new Point(0, 0), new Point(200, font.Height), "TextEntry" + (object)UserInterface.GetUniqueElementID())
 {
     this.text              = new Text(Shaders.FontShader, font, s, BMFont.Justification.Left);
     this.text.RelativeTo   = Corner.Fill;
     this.text.Padding      = new Point(5, 0);
     this.OnMouseClick      = (OnMouse)((o, e) => this.text.OnMouseClick(o, e));
     this.text.OnMouseClick = (OnMouse)((o, e) =>
     {
         if (this.hasFocus)
         {
             return;
         }
         this.hasFocus = true;
         Input.PushKeyBindings();
         Input.SubscribeAll(new Event((Event.KeyEvent)((key, state) =>
         {
             if (!state || this.text.TextSize.X > this.Size.X - 16)
             {
                 return;
             }
             this.text.String += key.ToString();
             if (this.OnTextEntry == null)
             {
                 return;
             }
             this.OnTextEntry(this, this.String);
         })));
         Input.Subscribe('\b', new Event((Action)(() =>
         {
             if (this.text.String.Length == 0)
             {
                 return;
             }
             this.text.String = this.text.String.Substring(0, this.text.String.Length - 1);
             if (this.OnTextEntry == null)
             {
                 return;
             }
             this.OnTextEntry(this, this.String);
         })));
         Input.Subscribe('\r', new Event((Action)(() =>
         {
             if (this.OnCarriageReturn == null)
             {
                 return;
             }
             this.OnCarriageReturn(this, this.String);
         })));
         Input.Subscribe('\x001B', new Event((Action)(() => this.text.OnLoseFocus((object)null, (IMouseInput)null))));
         this.text.OnLoseFocus = (OnFocus)((sender, newFocus) =>
         {
             if (!this.hasFocus)
             {
                 return;
             }
             this.hasFocus = false;
             Input.PopKeyBindings();
         });
     });
     this.AddElement((UIElement)this.text);
 }
コード例 #7
0
ファイル: UIContainer.cs プロジェクト: PWidmann/OpenGL_Demo
 public UIContainer()
     : this(new Point(0, 0), UserInterface.UIWindow.Size, "Container" + (object)UserInterface.GetUniqueElementID())
 {
     this.RelativeTo = Corner.Fill;
 }
コード例 #8
0
        /// <summary>
        /// Creates a slider with a given texture, minimum, maximum and default values.
        /// </summary>
        /// <param name="sliderTexture">The texture to apply to the slider.  This also defines the slider size.</param>
        /// <param name="min">The minimum value of the slider.</param>
        /// <param name="max">The maximum value of the slider.</param>
        /// <param name="value">The default value of the slider.</param>
        public Slider(Texture sliderTexture, int min = 0, int max = 10, int value = 0)
            : base(new Point(0, 0), new Point(200, sliderTexture.Size.Height), "Slider" + UserInterface.GetUniqueElementID())
        {
            this.min   = min;
            this.max   = max;
            this.value = value;

            sliderButton = new Button(sliderTexture);
            sliderButton.BackgroundColor = new Vector4(0, 0, 0, 0);

            sliderButton.OnMouseUp   = (sender, eventArgs) => sliderMouseDown = false;
            sliderButton.OnMouseDown = (sender, eventArgs) =>
            {
                sliderMouseDown = (eventArgs.Button == MouseButton.Left);
                sliderDown      = eventArgs.Location.X;
            };
            sliderButton.OnMouseMove = (sender, eventArgs) => this.OnMouseMove(sender, eventArgs);
            this.OnMouseMove         = (sender, eventArgs) =>
            {
                if (!sliderMouseDown)
                {
                    return;
                }

                if (eventArgs.Location.X < CorrectedPosition.X)
                {
                    // handle case where the mouse has gone too far to the left
                    sliderButton.Position = new Point(0, 0);
                    this.Value            = Minimum;
                }
                else if (eventArgs.Location.X > CorrectedPosition.X + Size.X)
                {
                    // handle case where the mouse has gone too far to the right
                    sliderButton.Position = new Point(Size.X - sliderButton.Size.X, 0);
                    this.Value            = Maximum;
                }
                else
                {
                    int dx = eventArgs.Location.X - sliderDown;

                    int    x       = eventArgs.Location.X - CorrectedPosition.X - (sliderButton.Size.X / 2);
                    double percent = Math.Max(0, (double)x / (Size.X - sliderButton.Size.X));

                    // take care of locking to the closest step
                    if (LockToSteps)
                    {
                        x = (int)(Math.Round(percent * (Maximum - Minimum)) * (Size.X - sliderButton.Size.X) / (Maximum - Minimum));
                    }
                    else
                    {
                        x = Math.Max(0, Math.Min(Size.X - sliderButton.Size.X, x));
                    }

                    if (x == sliderButton.Position.X)
                    {
                        return;
                    }
                    sliderButton.Position = new Point(x, 0);

                    sliderDown = eventArgs.Location.X;

                    int clampedValue = Math.Max(Minimum, Math.Min(Maximum, (int)Math.Round((Maximum - Minimum) * percent) + Minimum));

                    if (this.value != clampedValue)
                    {
                        this.value = clampedValue;
                        if (OnValueChanged != null)
                        {
                            OnValueChanged(this, new MouseEventArgs());
                        }
                    }
                }
                sliderButton.OnResize();
            };

            sliderButton.RelativeTo = Corner.BottomLeft;
            sliderButton.Position   = new Point(value * (Size.X - sliderButton.Size.X) / (max - min), 0);
            this.AddElement(sliderButton);
        }
コード例 #9
0
ファイル: Slider.cs プロジェクト: PWidmann/OpenGL_Demo
 public Slider(Texture sliderTexture, int min = 0, int max = 10, int value = 0)
     : base(new Point(0, 0), new Point(200, sliderTexture.Size.Height), nameof(Slider) + (object)UserInterface.GetUniqueElementID())
 {
     this.min          = min;
     this.max          = max;
     this.value        = value;
     this.sliderButton = new Button(sliderTexture);
     this.sliderButton.BackgroundColor = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
     this.sliderButton.OnMouseUp       = (OnMouse)((sender, eventArgs) => this.sliderMouseDown = false);
     this.sliderButton.OnMouseDown     = (OnMouse)((sender, eventArgs) =>
     {
         this.sliderMouseDown = eventArgs.Button == MouseButton.Left;
         this.sliderDown = eventArgs.Location.X;
     });
     this.sliderButton.OnMouseMove = (OnMouse)((sender, eventArgs) => this.OnMouseMove(sender, eventArgs));
     this.OnMouseMove = (OnMouse)((sender, eventArgs) =>
     {
         if (!this.sliderMouseDown)
         {
             return;
         }
         if (eventArgs.Location.X < this.CorrectedPosition.X)
         {
             this.sliderButton.Position = new Point(0, 0);
             this.Value = this.Minimum;
         }
         else if (eventArgs.Location.X > this.CorrectedPosition.X + this.Size.X)
         {
             this.sliderButton.Position = new Point(this.Size.X - this.sliderButton.Size.X, 0);
             this.Value = this.Maximum;
         }
         else
         {
             int num1 = eventArgs.Location.X - this.sliderDown;
             int val2 = eventArgs.Location.X - this.CorrectedPosition.X - this.sliderButton.Size.X / 2;
             double num2 = Math.Max(0.0, (double)val2 / (double)(this.Size.X - this.sliderButton.Size.X));
             int x = !this.LockToSteps ? Math.Max(0, Math.Min(this.Size.X - this.sliderButton.Size.X, val2)) : (int)(Math.Round(num2 * (double)(this.Maximum - this.Minimum)) * (double)(this.Size.X - this.sliderButton.Size.X) / (double)(this.Maximum - this.Minimum));
             if (x == this.sliderButton.Position.X)
             {
                 return;
             }
             this.sliderButton.Position = new Point(x, 0);
             this.sliderDown = eventArgs.Location.X;
             int num3 = Math.Max(this.Minimum, Math.Min(this.Maximum, (int)Math.Round((double)(this.Maximum - this.Minimum) * num2) + this.Minimum));
             if (this.value != num3)
             {
                 this.value = num3;
                 if (this.OnValueChanged != null)
                 {
                     this.OnValueChanged((object)this, new MouseEventArgs());
                 }
             }
         }
         this.sliderButton.OnResize();
     });
     this.sliderButton.RelativeTo = Corner.BottomLeft;
     this.sliderButton.Position   = new Point(value * (this.Size.X - this.sliderButton.Size.X) / (max - min), 0);
     this.AddElement((UIElement)this.sliderButton);
 }
コード例 #10
0
        /// <summary>
        /// Creates a simple checkbox user interface element which can be used
        /// for setting/modifying the state of a boolean value.
        /// This can be useful in preferences as well as other places in the user interface.
        /// </summary>
        /// <param name="uncheckedTexture">A texture to use when the checkbox is unchecked.  Should be the same size as checkedTexture.</param>
        /// <param name="checkedTexture">A texture to use when the checkbox is checked.  Should be the same size as uncheckedTexture.</param>
        /// <param name="font">A BMFont to use when rendering the descriptive text of this checkbox.</param>
        /// <param name="text">The descriptive text to render to the right of this checkbox.</param>
        public CheckBox(Texture uncheckedTexture, Texture checkedTexture, BMFont font, string text)
            : base(new Point(0, 0), new Point(uncheckedTexture.Size.Width, uncheckedTexture.Size.Height), "CheckBox" + UserInterface.GetUniqueElementID())
        {
            this.UncheckedTexture = uncheckedTexture;
            this.CheckedTexture   = checkedTexture;

            checkBox = new Button(UncheckedTexture);
            checkBox.BackgroundColor = new Vector4(0, 0, 0, 0);
            checkBox.RelativeTo      = Corner.BottomLeft;
            checkBox.Size            = new Point(uncheckedTexture.Size.Width, uncheckedTexture.Size.Height);
            this.AddElement(checkBox);

            this.Font = font;
            this.Text = text;

            checkBox.OnMouseClick = new OnMouse((o, e) => this.Checked = !this.Checked);
        }
コード例 #11
0
        public TextInput(BMFont font, string s = "")
            : base(new Point(0, 0), new Point(200, font.Height), "TextEntry" + UserInterface.GetUniqueElementID())
        {
            text            = new Text(Shaders.FontShader, font, s, BMFont.Justification.Left);
            text.RelativeTo = Corner.Fill;
            text.Padding    = new Point(5, 0);

            this.OnMouseClick = new OnMouse((o, e) => text.OnMouseClick(o, e));

            text.OnMouseClick = new OnMouse((o, e) =>
            {
                if (hasFocus)
                {
                    return;
                }
                hasFocus = true;

                // take control of the key bindings
                Input.PushKeyBindings();

                Input.SubscribeAll(new Event((key, state) =>
                {
                    // give 16 pixels of padding on the right
                    if (!state || text.TextSize.X > Size.X - 16)
                    {
                        return;
                    }

                    text.String += key;

                    if (OnTextEntry != null)
                    {
                        OnTextEntry(this, String);
                    }
                }));

                // delete key
                Input.Subscribe((char)8, new Event(() =>
                {
                    if (text.String.Length == 0)
                    {
                        return;
                    }
                    else
                    {
                        text.String = text.String.Substring(0, text.String.Length - 1);
                    }

                    if (OnTextEntry != null)
                    {
                        OnTextEntry(this, String);
                    }
                }));

                // carriage return
                Input.Subscribe((char)13, new Event(() =>
                {
                    if (OnCarriageReturn != null)
                    {
                        OnCarriageReturn(this, String);
                    }
                }));

                // escape key
                Input.Subscribe((char)27, new Event(() => text.OnLoseFocus(null, null)));

                // restore the key bindings when we lose focus
                text.OnLoseFocus = new OnFocus((sender, newFocus) =>
                {
                    if (!hasFocus)
                    {
                        return;
                    }
                    hasFocus = false;

                    Input.PopKeyBindings();
                });
            });

            this.AddElement(text);
        }