Пример #1
0
        public PathSelector(WidgetManager manager, PathSelectorMode mode) : base(manager)
        {
            this.mode = mode;

            Add(layout = new LinearLayout(manager)
            {
                AnchorFrom  = BoxAlignment.Centre,
                AnchorTo    = BoxAlignment.Centre,
                Horizontal  = true,
                Fill        = true,
                FitChildren = true,
                Children    = new Widget[]
                {
                    textbox = new Textbox(manager)
                    {
                        AnchorFrom = BoxAlignment.BottomLeft,
                        AnchorTo   = BoxAlignment.BottomLeft,
                    },
                    button = new Button(manager)
                    {
                        Icon       = IconFont.FolderOpen,
                        Tooltip    = "Browse",
                        AnchorFrom = BoxAlignment.BottomRight,
                        AnchorTo   = BoxAlignment.BottomRight,
                        CanGrow    = false,
                    },
                },
            });

            textbox.OnValueChanged  += (sender, e) => OnValueChanged?.Invoke(this, EventArgs.Empty);
            textbox.OnValueCommited += (sender, e) => OnValueCommited?.Invoke(this, EventArgs.Empty);
            button.OnClick          += (sender, e) =>
            {
                switch (mode)
                {
                case PathSelectorMode.Folder:
                    Manager.ScreenLayerManager.OpenFolderPicker(LabelText, textbox.Value, (path) => textbox.Value = path);
                    break;

                case PathSelectorMode.OpenFile:
                    Manager.ScreenLayerManager.OpenFilePicker(LabelText, textbox.Value, null, Filter, (path) => textbox.Value = path);
                    break;

                case PathSelectorMode.OpenDirectory:
                    Manager.ScreenLayerManager.OpenFilePicker(LabelText, "", textbox.Value, Filter, (path) => textbox.Value = path);
                    break;

                case PathSelectorMode.SaveFile:
                    Manager.ScreenLayerManager.OpenSaveLocationPicker(LabelText, textbox.Value, SaveExtension, Filter, (path) => textbox.Value = path);
                    break;
                }
            };
        }
Пример #2
0
 public Slider(WidgetManager manager) : base(manager)
 {
     OnHovered += (sender, e) =>
     {
         hovered = e.Hovered;
         if (!disabled)
         {
             RefreshStyle();
         }
     };
     OnClickDown += (sender, e) =>
     {
         if (disabled || dragged)
         {
             return(false);
         }
         dragButton = e.Button;
         dragged    = true;
         Value      = GetValueForPosition(new Vector2(e.X, e.Y));
         DragStart(dragButton);
         return(true);
     };
     OnClickUp += (sender, e) =>
     {
         if (disabled || !dragged)
         {
             return;
         }
         if (e.Button != dragButton)
         {
             return;
         }
         dragged = false;
         RefreshStyle();
         DragEnd(dragButton);
         OnValueCommited?.Invoke(this, e);
     };
     OnDrag += (sender, e) =>
     {
         if (disabled || !dragged)
         {
             return;
         }
         Value = GetValueForPosition(new Vector2(e.X, e.Y));
         DragUpdate(dragButton);
     };
 }
Пример #3
0
        private void xTextbox_OnValueCommited(object sender, EventArgs e)
        {
            var xCommit = xTextbox.Value;

            float x;

            try
            {
                x = float.Parse(xCommit, CultureInfo.InvariantCulture);
            }
            catch
            {
                updateWidgets();
                return;
            }
            Value = new Vector2(x, value.Y);
            OnValueCommited?.Invoke(this, EventArgs.Empty);
        }
Пример #4
0
        private void yTextbox_OnValueCommited(object sender, EventArgs e)
        {
            var yCommit = yTextbox.Value;

            float y;

            try
            {
                y = float.Parse(yCommit, CultureInfo.InvariantCulture);
            }
            catch
            {
                updateWidgets();
                return;
            }
            Value = new Vector2(value.X, y);
            OnValueCommited?.Invoke(this, EventArgs.Empty);
        }
Пример #5
0
        private void zTextbox_OnValueCommited(object sender, EventArgs e)
        {
            var zCommit = zTextbox.Value;

            float z;

            try
            {
                z = float.Parse(zCommit, CultureInfo.InvariantCulture);
            }
            catch
            {
                updateWidgets();
                return;
            }
            Value = new Vector3(value.X, value.Y, z);
            OnValueCommited?.Invoke(this, EventArgs.Empty);
        }
Пример #6
0
        private void htmlTextbox_OnValueCommited(object sender, EventArgs e)
        {
            var htmlColor = htmlTextbox.Value.Trim();

            if (!htmlColor.StartsWith("#"))
            {
                htmlColor = "#" + htmlColor;
            }

            Color color;

            try
            {
                color = ColorTranslator.FromHtml(htmlColor);
            }
            catch
            {
                updateWidgets();
                return;
            }
            Value = new Color4(color.R / 255f, color.G / 255f, color.B / 255f, alphaSlider.Value);
            OnValueCommited?.Invoke(this, EventArgs.Empty);
        }
Пример #7
0
 private void slider_OnValueCommited(object sender, EventArgs e)
 => OnValueCommited?.Invoke(this, EventArgs.Empty);
Пример #8
0
        public Textbox(WidgetManager manager) : base(manager)
        {
            DefaultSize = new Vector2(200, 0);

            cursorLine = new Sprite()
            {
                Texture   = DrawState.WhitePixel,
                ScaleMode = ScaleMode.Fill,
                Color     = Color4.White,
            };

            Add(content = new Label(manager)
            {
                AnchorFrom = BoxAlignment.BottomLeft,
                AnchorTo   = BoxAlignment.BottomLeft,
            });
            Add(label = new Label(manager)
            {
                AnchorFrom = BoxAlignment.TopLeft,
                AnchorTo   = BoxAlignment.TopLeft,
            });

            OnFocusChange += (sender, e) =>
            {
                if (hasFocus == e.HasFocus)
                {
                    return;
                }
                if (hasFocus && hasCommitPending)
                {
                    OnValueCommited?.Invoke(this, EventArgs.Empty);
                    hasCommitPending = false;
                }

                hasFocus = e.HasFocus;
                RefreshStyle();
            };
            OnHovered += (sender, e) =>
            {
                hovered = e.Hovered;
                RefreshStyle();
            };
            OnKeyDown += (sender, e) =>
            {
                if (!hasFocus)
                {
                    return(false);
                }

                var inputManager = manager.InputManager;
                switch (e.Key)
                {
                case Key.Escape:
                    if (hasFocus)
                    {
                        manager.KeyboardFocus = null;
                    }
                    break;

                case Key.BackSpace:
                    if (selectionStart > 0 && selectionStart == cursorPosition)
                    {
                        selectionStart--;
                    }
                    ReplaceSelection("");
                    break;

                case Key.Delete:
                    if (selectionStart < Value.Length && selectionStart == cursorPosition)
                    {
                        cursorPosition++;
                    }
                    ReplaceSelection("");
                    break;

                case Key.A:
                    if (inputManager.ControlOnly)
                    {
                        SelectAll();
                    }
                    break;

                case Key.C:
                    if (inputManager.ControlOnly)
                    {
                        if (selectionStart != cursorPosition)
                        {
                            ClipboardHelper.SetText(Value.Substring(SelectionLeft, SelectionLength), System.Windows.Forms.TextDataFormat.UnicodeText);
                        }
                        else
                        {
                            ClipboardHelper.SetText(Value, System.Windows.Forms.TextDataFormat.UnicodeText);
                        }
                    }
                    break;

                case Key.V:
                    if (inputManager.ControlOnly)
                    {
                        var clipboardText = ClipboardHelper.GetText(System.Windows.Forms.TextDataFormat.UnicodeText);
                        if (clipboardText != null)
                        {
                            if (!AcceptMultiline)
                            {
                                clipboardText = clipboardText.Replace("\n", "");
                            }
                            ReplaceSelection(clipboardText);
                        }
                    }
                    break;

                case Key.X:
                    if (inputManager.ControlOnly)
                    {
                        if (selectionStart == cursorPosition)
                        {
                            SelectAll();
                        }

                        ClipboardHelper.SetText(Value.Substring(SelectionLeft, SelectionLength), System.Windows.Forms.TextDataFormat.UnicodeText);
                        ReplaceSelection("");
                    }
                    break;

                case Key.Left:
                    if (inputManager.Shift)
                    {
                        if (cursorPosition > 0)
                        {
                            cursorPosition--;
                        }
                    }
                    else if (selectionStart != cursorPosition)
                    {
                        SelectionRight = SelectionLeft;
                    }
                    else if (cursorPosition > 0)
                    {
                        cursorPosition = --selectionStart;
                    }
                    break;

                case Key.Right:
                    if (inputManager.Shift)
                    {
                        if (cursorPosition < Value.Length)
                        {
                            cursorPosition++;
                        }
                    }
                    else if (selectionStart != cursorPosition)
                    {
                        SelectionLeft = SelectionRight;
                    }
                    else if (cursorPosition < Value.Length)
                    {
                        selectionStart = ++cursorPosition;
                    }
                    break;

                case Key.Up:
                    cursorPosition = content.GetCharacterIndexAbove(cursorPosition);
                    if (!inputManager.Shift)
                    {
                        selectionStart = cursorPosition;
                    }
                    break;

                case Key.Down:
                    cursorPosition = content.GetCharacterIndexBelow(cursorPosition);
                    if (!inputManager.Shift)
                    {
                        selectionStart = cursorPosition;
                    }
                    break;

                case Key.Home:
                    cursorPosition = 0;
                    if (!inputManager.Shift)
                    {
                        selectionStart = cursorPosition;
                    }
                    break;

                case Key.End:
                    cursorPosition = Value.Length;
                    if (!inputManager.Shift)
                    {
                        selectionStart = cursorPosition;
                    }
                    break;

                case Key.Enter:
                case Key.KeypadEnter:
                    if (AcceptMultiline && (!EnterCommits || inputManager.Shift))
                    {
                        ReplaceSelection("\n");
                    }
                    else if (EnterCommits && hasCommitPending)
                    {
                        OnValueCommited?.Invoke(this, EventArgs.Empty);
                        hasCommitPending = false;
                    }
                    break;
                }
                return(true);
            };
            OnKeyUp += (sender, e) =>
            {
                return(hasFocus);
            };
            OnKeyPress += (sender, e) =>
            {
                if (!hasFocus)
                {
                    return(false);
                }
                ReplaceSelection(e.KeyChar.ToString());
                return(true);
            };
            OnClickDown += (sender, e) =>
            {
                manager.KeyboardFocus = this;
                selectionStart        = cursorPosition = content.GetCharacterIndexAt(Manager.Camera.FromScreen(new Vector2(e.X, e.Y)).Xy);
                return(true);
            };
            OnDrag += (sender, e) =>
            {
                cursorPosition = content.GetCharacterIndexAt(Manager.Camera.FromScreen(new Vector2(e.X, e.Y)).Xy);
            };
        }