From d0411b5ee1f70bbfc492e05cfa4217d2ec9088d6 Mon Sep 17 00:00:00 2001 From: Asnivor Date: Fri, 25 Oct 2024 16:02:05 +0100 Subject: [PATCH 1/4] HexEditor - HexLabelEx control that will paint multiple colours within a Label --- .../tools/HexEditor/HexEditor.Designer.cs | 5 +- .../tools/HexEditor/HexEditor.cs | 48 +++++++ .../BizHawk.WinForms.Controls.csproj | 5 + .../LabelEx/HexLabelEx.cs | 121 ++++++++++++++++++ 4 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs index cb3c8f8543e..64321a0b6dd 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs @@ -89,7 +89,7 @@ private void InitializeComponent() this.MemoryViewerBox = new System.Windows.Forms.GroupBox(); this.HexScrollBar = new System.Windows.Forms.VScrollBar(); this.AddressLabel = new BizHawk.WinForms.Controls.LocLabelEx(); - this.AddressesLabel = new BizHawk.WinForms.Controls.LocLabelEx(); + this.AddressesLabel = new BizHawk.WinForms.Controls.HexLabelEx(); this.Header = new BizHawk.WinForms.Controls.LocLabelEx(); this.HexMenuStrip.SuspendLayout(); this.ViewerContextMenuStrip.SuspendLayout(); @@ -454,6 +454,7 @@ private void InitializeComponent() this.AddressesLabel.MouseLeave += new System.EventHandler(this.AddressesLabel_MouseLeave); this.AddressesLabel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.AddressesLabel_MouseMove); this.AddressesLabel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.AddressesLabel_MouseUp); + this.AddressesLabel.Paint += new System.Windows.Forms.PaintEventHandler(this.AddressesLabel_Paint); // // Header // @@ -511,7 +512,7 @@ private void InitializeComponent() private BizHawk.WinForms.Controls.ToolStripMenuItemEx AddToRamWatchMenuItem; private BizHawk.WinForms.Controls.ToolStripMenuItemEx FreezeAddressMenuItem; public System.Windows.Forms.GroupBox MemoryViewerBox; - private BizHawk.WinForms.Controls.LocLabelEx AddressesLabel; + private BizHawk.WinForms.Controls.HexLabelEx AddressesLabel; private System.Windows.Forms.VScrollBar HexScrollBar; private BizHawk.WinForms.Controls.ToolStripMenuItemEx UnfreezeAllMenuItem; private BizHawk.WinForms.Controls.ToolStripMenuItemEx UnfreezeAllContextItem; diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 1fec01de636..46b3c698f70 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -2055,6 +2055,54 @@ private void HexEditor_MouseWheel(object sender, MouseEventArgs e) } } + private void AddressesLabel_Paint(object sender, PaintEventArgs e) + { + return; + + if (sender is not Label label) + return; + + char gap = ' '; + + //PointF point = new PointF(label.Location.X, label.Location.Y); + PointF point = new PointF(0, 0); + string text = label.Text; + Font font = label.Font; + + string[] lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + + Color color = label.ForeColor; + + foreach (string line in lines) + { + string[] words = line.Split(gap); + + foreach (string word in words) + { + if (word == "00") + { + color = Color.SlateGray; + } + else + { + color = label.ForeColor; + } + + SizeF size = e.Graphics.MeasureString(word, font); + + using (Brush brush = new SolidBrush(color)) + { + e.Graphics.DrawString(word, font, brush, point); + } + + point.X += size.Width + e.Graphics.MeasureString(gap.ToString(), font).Width; + } + + point.X = label.Location.X; + point.Y += e.Graphics.MeasureString(line, font).Height; + } + } + private void MemoryViewerBox_Paint(object sender, PaintEventArgs e) { var activeCheats = MainForm.CheatList.Where(x => x.Enabled); diff --git a/src/BizHawk.WinForms.Controls/BizHawk.WinForms.Controls.csproj b/src/BizHawk.WinForms.Controls/BizHawk.WinForms.Controls.csproj index 31744f1864b..e26be95279e 100644 --- a/src/BizHawk.WinForms.Controls/BizHawk.WinForms.Controls.csproj +++ b/src/BizHawk.WinForms.Controls/BizHawk.WinForms.Controls.csproj @@ -10,4 +10,9 @@ + + + Component + + diff --git a/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs b/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs new file mode 100644 index 00000000000..ab3e40a5681 --- /dev/null +++ b/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs @@ -0,0 +1,121 @@ +using System.ComponentModel; +using System.Drawing; +using System.Windows.Forms; +using BizHawk.Common; + + +namespace BizHawk.WinForms.Controls +{ + /// + public class HexLabelEx : LabelExBase + { + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new bool AutoSize => base.AutoSize; + + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new Size Size => base.Size; + + public Color ZeroColor { get; set; } = Color.SlateGray; + + private float spacingHexModifier; + private float spacingPreDivider; + private float spacingPostDividerModifier; + private float spacingAscii; + private float spacingLineModifier; + + public HexLabelEx() + { + base.AutoSize = true; + this.BackColor = Color.Transparent; + + spacingHexModifier = 0.7F; + spacingPreDivider = 3.0F; + spacingPostDividerModifier = 3.5F; + spacingAscii = 7.0F; + spacingLineModifier = 0.56F; + + if (OSTailoredCode.IsUnixHost) + { + // TODO: spacing values will probably be different on linux + } + } + + protected override void OnPaint(PaintEventArgs e) + { + string text = this.Text; + Font font = this.Font; + PointF point = new PointF(0, 0); + char gap = ' '; + + string[] lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + + Color color00 = this.ForeColor; + Color color = this.ForeColor; + + foreach (string line in lines) + { + // split left and right panes + string[] panes = line.Split('|'); + + if (panes.Length < 2) + { + // skip - last line appears to be empty + continue; + } + + // hex pane + string[] words = panes[0].Split(gap); + foreach (var word in words) + { + SizeF size = e.Graphics.MeasureString(word, font); + + switch (word) + { + case "00": + color = ZeroColor; + break; + + default: + color = this.ForeColor; + break; + } + using (Brush brush = new SolidBrush(color)) + { + e.Graphics.DrawString(word, font, brush, point); + } + + point.X += size.Width + e.Graphics.MeasureString(gap.ToString(), font).Width + spacingHexModifier; + } + + // divider + string div = "|"; + point.X -= spacingPreDivider; + SizeF sizeDiv = e.Graphics.MeasureString(div, font); + using (Brush brush = new SolidBrush(this.ForeColor)) + { + e.Graphics.DrawString(div, font, brush, point); + } + + point.X += e.Graphics.MeasureString(gap.ToString(), font).Width + spacingPostDividerModifier; + + // ascii pane + char[] chars = panes[1].ToCharArray(); + foreach (var c in chars) + { + string str = c.ToString(); + + using (Brush brush = new SolidBrush(this.ForeColor)) + { + e.Graphics.DrawString(str, font, brush, point); + } + + // fixed size + point.X += spacingAscii; + } + + point.X = 0; + point.Y += e.Graphics.MeasureString(line, font).Height + spacingLineModifier; + } + } + } +} From 1c7684c6e32769f7105371a90cf958fd14be4282 Mon Sep 17 00:00:00 2001 From: Asnivor Date: Fri, 25 Oct 2024 16:30:44 +0100 Subject: [PATCH 2/4] HexEditor - Add ZeroColor option to HexColor --- .../tools/HexEditor/HexColor.Designer.cs | 40 +++++++++---- .../tools/HexEditor/HexColor.cs | 10 ++++ .../tools/HexEditor/HexEditor.Designer.cs | 1 - .../tools/HexEditor/HexEditor.cs | 59 ++++--------------- 4 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.Designer.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.Designer.cs index bcc5d09b2bf..5f15793babe 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.Designer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.Designer.cs @@ -41,7 +41,9 @@ private void InitializeComponent() this.HexMenubar = new System.Windows.Forms.Panel(); this.label2 = new BizHawk.WinForms.Controls.LocLabelEx(); this.HexBackgrnd = new System.Windows.Forms.Panel(); - this.colorDialog1 = new System.Windows.Forms.ColorDialog(); + this.label7 = new BizHawk.WinForms.Controls.LocLabelEx(); + this.Hex00 = new System.Windows.Forms.Panel(); + this.colorDialog1 = new System.Windows.Forms.ColorDialog(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // @@ -59,9 +61,11 @@ private void InitializeComponent() this.groupBox1.Controls.Add(this.HexMenubar); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.HexBackgrnd); - this.groupBox1.Location = new System.Drawing.Point(3, 2); + this.groupBox1.Controls.Add(this.label7); + this.groupBox1.Controls.Add(this.Hex00); + this.groupBox1.Location = new System.Drawing.Point(3, 2); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(144, 192); + this.groupBox1.Size = new System.Drawing.Size(144, 222); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; // @@ -154,12 +158,27 @@ private void InitializeComponent() this.HexBackgrnd.Size = new System.Drawing.Size(20, 20); this.HexBackgrnd.TabIndex = 6; this.HexBackgrnd.MouseClick += new System.Windows.Forms.MouseEventHandler(this.HexBackground_Click); - // - // HexColors_Form - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + // + // label7 + // + this.label7.Location = new System.Drawing.Point(30, 200); + this.label7.Name = "label7"; + this.label7.Text = "Zero Color"; + // + // Hex00 + // + this.Hex00.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.Hex00.Location = new System.Drawing.Point(5, 196); + this.Hex00.Name = "Hex00"; + this.Hex00.Size = new System.Drawing.Size(20, 20); + this.Hex00.TabIndex = 18; + this.Hex00.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Hex00_Click); + // + // HexColors_Form + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(149, 197); + this.ClientSize = new System.Drawing.Size(149, 230); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; @@ -190,6 +209,7 @@ private void InitializeComponent() private System.Windows.Forms.Panel HexFreeze; private BizHawk.WinForms.Controls.LocLabelEx label4; private System.Windows.Forms.Panel HexHighlight; - - } + private BizHawk.WinForms.Controls.LocLabelEx label7; + private System.Windows.Forms.Panel Hex00; + } } \ No newline at end of file diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.cs index 1f33915efba..1a188cda8ae 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexColor.cs @@ -20,6 +20,7 @@ private void HexColors_Form_Load(object sender, EventArgs e) HexFreeze.BackColor = _hexEditor.Colors.Freeze; HexFreezeHL.BackColor = _hexEditor.Colors.HighlightFreeze; HexHighlight.BackColor = _hexEditor.Colors.Highlight; + Hex00.BackColor = _hexEditor.Colors.Foreground00; } private void HexBackground_Click(object sender, MouseEventArgs e) @@ -33,6 +34,15 @@ private void HexBackground_Click(object sender, MouseEventArgs e) } } + private void Hex00_Click(object sender, MouseEventArgs e) + { + if (colorDialog1.ShowDialog().IsOk()) + { + _hexEditor.Colors.Foreground00 = colorDialog1.Color; + Hex00.BackColor = colorDialog1.Color; + } + } + private void HexForeground_Click(object sender, MouseEventArgs e) { if (colorDialog1.ShowDialog().IsOk()) diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs index 64321a0b6dd..0da38ef6c8b 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.Designer.cs @@ -454,7 +454,6 @@ private void InitializeComponent() this.AddressesLabel.MouseLeave += new System.EventHandler(this.AddressesLabel_MouseLeave); this.AddressesLabel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.AddressesLabel_MouseMove); this.AddressesLabel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.AddressesLabel_MouseUp); - this.AddressesLabel.Paint += new System.Windows.Forms.PaintEventHandler(this.AddressesLabel_Paint); // // Header // diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 46b3c698f70..99349a158f1 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -133,6 +133,7 @@ internal class ColorConfig public Color Freeze { get; set; }= Color.LightBlue; public Color Highlight { get; set; } = Color.Pink; public Color HighlightFreeze { get; set; } = Color.Violet; + public Color Foreground00 { get; set; } = Color.SlateGray; } [ConfigPersist] @@ -219,11 +220,21 @@ private void HexEditor_Load(object sender, EventArgs e) protected override void UpdateAfter() { + if (AddressesLabel.ZeroColor != Colors.Foreground00) + { + AddressesLabel.ZeroColor = Colors.Foreground00; + } + AddressesLabel.Text = GenerateMemoryViewString(true); } protected override void GeneralUpdate() { + if (AddressesLabel.ZeroColor != Colors.Foreground00) + { + AddressesLabel.ZeroColor = Colors.Foreground00; + } + AddressesLabel.Text = GenerateMemoryViewString(true); AddressLabel.Text = GenerateAddressString(); } @@ -2055,54 +2066,6 @@ private void HexEditor_MouseWheel(object sender, MouseEventArgs e) } } - private void AddressesLabel_Paint(object sender, PaintEventArgs e) - { - return; - - if (sender is not Label label) - return; - - char gap = ' '; - - //PointF point = new PointF(label.Location.X, label.Location.Y); - PointF point = new PointF(0, 0); - string text = label.Text; - Font font = label.Font; - - string[] lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); - - Color color = label.ForeColor; - - foreach (string line in lines) - { - string[] words = line.Split(gap); - - foreach (string word in words) - { - if (word == "00") - { - color = Color.SlateGray; - } - else - { - color = label.ForeColor; - } - - SizeF size = e.Graphics.MeasureString(word, font); - - using (Brush brush = new SolidBrush(color)) - { - e.Graphics.DrawString(word, font, brush, point); - } - - point.X += size.Width + e.Graphics.MeasureString(gap.ToString(), font).Width; - } - - point.X = label.Location.X; - point.Y += e.Graphics.MeasureString(line, font).Height; - } - } - private void MemoryViewerBox_Paint(object sender, PaintEventArgs e) { var activeCheats = MainForm.CheatList.Where(x => x.Enabled); From 7ed690679a95500a00d30fb2e5b6717ca53ced7e Mon Sep 17 00:00:00 2001 From: Asnivor Date: Fri, 25 Oct 2024 17:05:03 +0100 Subject: [PATCH 3/4] HexEditor - mitigate potential | in ascii table ..if this is even a possibility --- src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs b/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs index ab3e40a5681..1e18e3b8356 100644 --- a/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs +++ b/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs @@ -49,7 +49,6 @@ protected override void OnPaint(PaintEventArgs e) string[] lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); - Color color00 = this.ForeColor; Color color = this.ForeColor; foreach (string line in lines) @@ -62,6 +61,14 @@ protected override void OnPaint(PaintEventArgs e) // skip - last line appears to be empty continue; } + else if (panes.Length > 2) + { + // pipe character present in the ascii pane? + for (int i = 2; i < panes.Length; i++) + { + panes[1] += "|" + panes[i]; + } + } // hex pane string[] words = panes[0].Split(gap); From f7bc67f393efb88f1200135a4dfdfd9a31717c53 Mon Sep 17 00:00:00 2001 From: Asnivor Date: Fri, 25 Oct 2024 17:23:15 +0100 Subject: [PATCH 4/4] HexEditor - some code tidy --- .../LabelEx/HexLabelEx.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs b/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs index 1e18e3b8356..be679551b49 100644 --- a/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs +++ b/src/BizHawk.WinForms.Controls/LabelEx/HexLabelEx.cs @@ -86,10 +86,8 @@ protected override void OnPaint(PaintEventArgs e) color = this.ForeColor; break; } - using (Brush brush = new SolidBrush(color)) - { - e.Graphics.DrawString(word, font, brush, point); - } + + DrawString(word, font, point, color); point.X += size.Width + e.Graphics.MeasureString(gap.ToString(), font).Width + spacingHexModifier; } @@ -98,10 +96,8 @@ protected override void OnPaint(PaintEventArgs e) string div = "|"; point.X -= spacingPreDivider; SizeF sizeDiv = e.Graphics.MeasureString(div, font); - using (Brush brush = new SolidBrush(this.ForeColor)) - { - e.Graphics.DrawString(div, font, brush, point); - } + + DrawString(div, font, point, this.ForeColor); point.X += e.Graphics.MeasureString(gap.ToString(), font).Width + spacingPostDividerModifier; @@ -110,11 +106,7 @@ protected override void OnPaint(PaintEventArgs e) foreach (var c in chars) { string str = c.ToString(); - - using (Brush brush = new SolidBrush(this.ForeColor)) - { - e.Graphics.DrawString(str, font, brush, point); - } + DrawString(str, font, point, this.ForeColor); // fixed size point.X += spacingAscii; @@ -123,6 +115,14 @@ protected override void OnPaint(PaintEventArgs e) point.X = 0; point.Y += e.Graphics.MeasureString(line, font).Height + spacingLineModifier; } + + void DrawString(string s, Font f, PointF p, Color color) + { + using (Brush brush = new SolidBrush(color)) + { + e.Graphics.DrawString(s, f, brush, p); + } + } } } }