From c819b6ef17ecedcdcf0dd15df18575e19805b0b3 Mon Sep 17 00:00:00 2001 From: Tig Kindel Date: Mon, 8 Jan 2024 08:43:31 -0700 Subject: [PATCH] Fixed ColorPicker --- Terminal.Gui/Views/ColorPicker.cs | 536 ++++++++++++++--------------- UICatalog/Scenarios/ColorPicker.cs | 2 +- 2 files changed, 266 insertions(+), 272 deletions(-) diff --git a/Terminal.Gui/Views/ColorPicker.cs b/Terminal.Gui/Views/ColorPicker.cs index a19b9f758..f33ee067b 100644 --- a/Terminal.Gui/Views/ColorPicker.cs +++ b/Terminal.Gui/Views/ColorPicker.cs @@ -1,281 +1,275 @@ using System; using System.Text; -namespace Terminal.Gui { +namespace Terminal.Gui; + +/// +/// Event arguments for the events. +/// +public class ColorEventArgs : EventArgs { + /// + /// Initializes a new instance of + /// + public ColorEventArgs () { } /// - /// Event arguments for the events. + /// The new Thickness. /// - public class ColorEventArgs : EventArgs { - - /// - /// Initializes a new instance of - /// - public ColorEventArgs () - { - } - - /// - /// The new Thickness. - /// - public Color Color { get; set; } - - /// - /// The previous Thickness. - /// - public Color PreviousColor { get; set; } - } + public Color Color { get; set; } /// - /// The Color picker. + /// The previous Thickness. /// - public class ColorPicker : View { - private int _selectColorIndex = (int)Color.Black; - - - /// - /// Columns of color boxes - /// - private int _cols = 8; - - /// - /// Rows of color boxes - /// - private int _rows = 2; - - /// - /// Width of a color box - /// - public int BoxWidth { - get => _boxWidth; - set { - if (_boxWidth != value) { - _boxWidth = value; - if (IsInitialized) { - Bounds = new Rect (Bounds.Location, new Size (_cols * BoxWidth, _rows * BoxHeight)); - } - } - } - } - private int _boxWidth = 4; - - /// - /// Height of a color box - /// - public int BoxHeight { - get => _boxHeight; - set { - if (_boxHeight != value) { - _boxHeight = value; - if (IsInitialized) { - Bounds = new Rect (Bounds.Location, new Size (_cols * BoxWidth, _rows * BoxHeight)); - } - } - } - } - int _boxHeight = 2; - - /// - /// Cursor for the selected color. - /// - public Point Cursor { - get { - return new Point (_selectColorIndex % _cols, _selectColorIndex / _cols); - } - - set { - var colorIndex = value.Y * _cols + value.X; - SelectedColor = (ColorName)colorIndex; - } - } - - /// - /// Fired when a color is picked. - /// - public event EventHandler ColorChanged; - - /// - /// Selected color. - /// - public ColorName SelectedColor { - get { - return (ColorName)_selectColorIndex; - } - - set { - ColorName prev = (ColorName)_selectColorIndex; - _selectColorIndex = (int)value; - ColorChanged?.Invoke (this, new ColorEventArgs () { - PreviousColor = new Color (prev), - Color = new Color (value), - }); - SetNeedsDisplay (); - } - } - - /// - /// Initializes a new instance of . - /// - public ColorPicker () - { - SetInitialProperties (); - } - - private void SetInitialProperties () - { - CanFocus = true; - AddCommands (); - AddKeyBindings (); - LayoutStarted += (o, a) => { - Bounds = new Rect (Bounds.Location, new Size (_cols * BoxWidth, _rows * BoxHeight)); - }; - } - - /// - /// Add the commands. - /// - private void AddCommands () - { - AddCommand (Command.Left, () => MoveLeft ()); - AddCommand (Command.Right, () => MoveRight ()); - AddCommand (Command.LineUp, () => MoveUp ()); - AddCommand (Command.LineDown, () => MoveDown ()); - } - - /// - /// Add the KeyBindinds. - /// - private void AddKeyBindings () - { - KeyBindings.Add (KeyCode.CursorLeft, Command.Left); - KeyBindings.Add (KeyCode.CursorRight, Command.Right); - KeyBindings.Add (KeyCode.CursorUp, Command.LineUp); - KeyBindings.Add (KeyCode.CursorDown, Command.LineDown); - } - - /// - public override void OnDrawContent (Rect contentArea) - { - base.OnDrawContent (contentArea); - - Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ()); - var colorIndex = 0; - - for (var y = 0; y < (Bounds.Height / BoxHeight); y++) { - for (var x = 0; x < (Bounds.Width / BoxWidth); x++) { - var foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols; - Driver.SetAttribute (new Attribute ((ColorName)foregroundColorIndex, (ColorName)colorIndex)); - var selected = x == Cursor.X && y == Cursor.Y; - DrawColorBox (x, y, selected); - colorIndex++; - } - } - } - - /// - /// Draw a box for one color. - /// - /// X location. - /// Y location - /// - private void DrawColorBox (int x, int y, bool selected) - { - var index = 0; - - for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++) { - for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++) { - Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY); - Driver.AddRune ((Rune)' '); - index++; - } - } - - if (selected) { - DrawFocusRect (new Rect (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight)); - } - } - - private void DrawFocusRect (Rect rect) - { - var lc = new LineCanvas (); - if (rect.Width == 1) { - lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted); - } else if (rect.Height == 1) { - lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted); - } else { - lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted); - lc.AddLine (new Point (rect.Location.X, rect.Location.Y + rect.Height - 1), rect.Width, Orientation.Horizontal, LineStyle.Dotted); - - lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted); - lc.AddLine (new Point (rect.Location.X + rect.Width - 1, rect.Location.Y), rect.Height, Orientation.Vertical, LineStyle.Dotted); - } - foreach (var p in lc.GetMap ()) { - AddRune (p.Key.X, p.Key.Y, p.Value); - } - } - - /// - /// Moves the selected item index to the previous column. - /// - /// - public virtual bool MoveLeft () - { - if (Cursor.X > 0) SelectedColor--; - return true; - } - - /// - /// Moves the selected item index to the next column. - /// - /// - public virtual bool MoveRight () - { - if (Cursor.X < _cols - 1) SelectedColor++; - return true; - } - - /// - /// Moves the selected item index to the previous row. - /// - /// - public virtual bool MoveUp () - { - if (Cursor.Y > 0) SelectedColor -= _cols; - return true; - } - - /// - /// Moves the selected item index to the next row. - /// - /// - public virtual bool MoveDown () - { - if (Cursor.Y < _rows - 1) SelectedColor += _cols; - return true; - } - - /// - public override bool MouseEvent (MouseEvent me) - { - if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus) { - return false; - } - - SetFocus (); - if (me.X > Bounds.Width || me.Y > Bounds.Height) { - return true; - } - Cursor = new Point ((me.X ) / _boxWidth, (me.Y) / _boxHeight); - - return true; - } - - /// - public override bool OnEnter (View view) - { - Application.Driver.SetCursorVisibility (CursorVisibility.Invisible); - - return base.OnEnter (view); - } - } + public Color PreviousColor { get; set; } } + +/// +/// The Color picker. +/// +public class ColorPicker : View { + int _boxHeight = 2; + int _boxWidth = 4; + + + /// + /// Columns of color boxes + /// + readonly int _cols = 8; + + /// + /// Rows of color boxes + /// + readonly int _rows = 2; + + int _selectColorIndex = (int)Color.Black; + + /// + /// Initializes a new instance of . + /// + public ColorPicker () => SetInitialProperties (); + + /// + /// Width of a color box + /// + public int BoxWidth { + get => _boxWidth; + set { + if (_boxWidth != value) { + _boxWidth = value; + SetNeedsLayout (); + } + } + } + + /// + /// Height of a color box + /// + public int BoxHeight { + get => _boxHeight; + set { + if (_boxHeight != value) { + _boxHeight = value; + SetNeedsLayout (); + } + } + } + + /// + /// Cursor for the selected color. + /// + public Point Cursor { + get => new (_selectColorIndex % _cols, _selectColorIndex / _cols); + set { + var colorIndex = value.Y * _cols + value.X; + SelectedColor = (ColorName)colorIndex; + } + } + + /// + /// Selected color. + /// + public ColorName SelectedColor { + get => (ColorName)_selectColorIndex; + set { + var prev = (ColorName)_selectColorIndex; + _selectColorIndex = (int)value; + ColorChanged?.Invoke (this, new ColorEventArgs { + PreviousColor = new Color (prev), + Color = new Color (value) + }); + SetNeedsDisplay (); + } + } + + /// + /// Fired when a color is picked. + /// + public event EventHandler ColorChanged; + + void SetInitialProperties () + { + CanFocus = true; + AddCommands (); + AddKeyBindings (); + LayoutStarted += (o, a) => { + var thickness = GetFramesThickness (); + Width = _cols * BoxWidth + thickness.Vertical; + Height = _rows * BoxHeight + thickness.Horizontal; + }; + } + + /// + /// Add the commands. + /// + void AddCommands () + { + AddCommand (Command.Left, () => MoveLeft ()); + AddCommand (Command.Right, () => MoveRight ()); + AddCommand (Command.LineUp, () => MoveUp ()); + AddCommand (Command.LineDown, () => MoveDown ()); + } + + /// + /// Add the KeyBindinds. + /// + void AddKeyBindings () + { + KeyBindings.Add (KeyCode.CursorLeft, Command.Left); + KeyBindings.Add (KeyCode.CursorRight, Command.Right); + KeyBindings.Add (KeyCode.CursorUp, Command.LineUp); + KeyBindings.Add (KeyCode.CursorDown, Command.LineDown); + } + + /// + public override void OnDrawContent (Rect contentArea) + { + base.OnDrawContent (contentArea); + + Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ()); + var colorIndex = 0; + + for (var y = 0; y < Bounds.Height / BoxHeight; y++) { + for (var x = 0; x < Bounds.Width / BoxWidth; x++) { + var foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols; + Driver.SetAttribute (new Attribute ((ColorName)foregroundColorIndex, (ColorName)colorIndex)); + var selected = x == Cursor.X && y == Cursor.Y; + DrawColorBox (x, y, selected); + colorIndex++; + } + } + } + + /// + /// Draw a box for one color. + /// + /// X location. + /// Y location + /// + void DrawColorBox (int x, int y, bool selected) + { + var index = 0; + + for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++) { + for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++) { + Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY); + Driver.AddRune ((Rune)' '); + index++; + } + } + + if (selected) { + DrawFocusRect (new Rect (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight)); + } + } + + void DrawFocusRect (Rect rect) + { + var lc = new LineCanvas (); + if (rect.Width == 1) { + lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted); + } else if (rect.Height == 1) { + lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted); + } else { + lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted); + lc.AddLine (new Point (rect.Location.X, rect.Location.Y + rect.Height - 1), rect.Width, Orientation.Horizontal, LineStyle.Dotted); + + lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted); + lc.AddLine (new Point (rect.Location.X + rect.Width - 1, rect.Location.Y), rect.Height, Orientation.Vertical, LineStyle.Dotted); + } + foreach (var p in lc.GetMap ()) { + AddRune (p.Key.X, p.Key.Y, p.Value); + } + } + + /// + /// Moves the selected item index to the previous column. + /// + /// + public virtual bool MoveLeft () + { + if (Cursor.X > 0) { + SelectedColor--; + } + return true; + } + + /// + /// Moves the selected item index to the next column. + /// + /// + public virtual bool MoveRight () + { + if (Cursor.X < _cols - 1) { + SelectedColor++; + } + return true; + } + + /// + /// Moves the selected item index to the previous row. + /// + /// + public virtual bool MoveUp () + { + if (Cursor.Y > 0) { + SelectedColor -= _cols; + } + return true; + } + + /// + /// Moves the selected item index to the next row. + /// + /// + public virtual bool MoveDown () + { + if (Cursor.Y < _rows - 1) { + SelectedColor += _cols; + } + return true; + } + + /// + public override bool MouseEvent (MouseEvent me) + { + if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus) { + return false; + } + + SetFocus (); + if (me.X > Bounds.Width || me.Y > Bounds.Height) { + return true; + } + Cursor = new Point (me.X / _boxWidth, me.Y / _boxHeight); + + return true; + } + + /// + public override bool OnEnter (View view) + { + Application.Driver.SetCursorVisibility (CursorVisibility.Invisible); + + return base.OnEnter (view); + } +} \ No newline at end of file diff --git a/UICatalog/Scenarios/ColorPicker.cs b/UICatalog/Scenarios/ColorPicker.cs index 241c07f1b..8e1e991b5 100644 --- a/UICatalog/Scenarios/ColorPicker.cs +++ b/UICatalog/Scenarios/ColorPicker.cs @@ -58,7 +58,7 @@ namespace UICatalog.Scenarios { Win.Add (_foregroundColorLabel); // Background ColorPicker. - backgroundColorPicker = new ColorPicker () { + backgroundColorPicker = new ColorPicker () { Title = "Background Color", Y = 0, X = 0,