From a5d720f01a05260161f2f4db76fcfc74fcc605bf Mon Sep 17 00:00:00 2001 From: Roman Shapiro Date: Mon, 13 Nov 2023 22:57:51 +0700 Subject: [PATCH] Fixes #437 --- src/Myra/Graphics2D/UI/Simple/ButtonBase2.cs | 2 +- src/Myra/Graphics2D/UI/Simple/CheckButton.cs | 22 ++++++--- src/Myra/Graphics2D/UI/Simple/RadioButton.cs | 24 ++------- src/Myra/Graphics2D/UI/Widget.Input.cs | 2 +- src/Myra/Graphics2D/UI/Widget.cs | 6 ++- src/MyraPad/Studio.cs | 2 +- src/MyraPad/UI/NewProjectWizard.Generated.cs | 51 +++++++++++++++----- src/MyraPad/UI/newProjectWizard.xmmp | 38 +++++++++++---- 8 files changed, 93 insertions(+), 54 deletions(-) diff --git a/src/Myra/Graphics2D/UI/Simple/ButtonBase2.cs b/src/Myra/Graphics2D/UI/Simple/ButtonBase2.cs index 4496b6e7..5a2e6634 100644 --- a/src/Myra/Graphics2D/UI/Simple/ButtonBase2.cs +++ b/src/Myra/Graphics2D/UI/Simple/ButtonBase2.cs @@ -124,7 +124,7 @@ public override IBrush GetCurrentBackground() { result = PressedBackground; } - else if (IsMouseInside && OverBackground != null) + else if (UseOverBackground && OverBackground != null) { result = OverBackground; } diff --git a/src/Myra/Graphics2D/UI/Simple/CheckButton.cs b/src/Myra/Graphics2D/UI/Simple/CheckButton.cs index 5485e2ea..a5f8af94 100644 --- a/src/Myra/Graphics2D/UI/Simple/CheckButton.cs +++ b/src/Myra/Graphics2D/UI/Simple/CheckButton.cs @@ -2,6 +2,7 @@ using Myra.Graphics2D.UI.Styles; using System.ComponentModel; using System; +using System.Xml.Serialization; #if MONOGAME || FNA @@ -21,11 +22,11 @@ public enum CheckPosition } [StyleTypeName("CheckBox")] - public class CheckButton: ButtonBase2 + public class CheckButton : ButtonBase2 { - private class CheckImage: Image + private class CheckImageInternal : Image { - public override bool IsMouseInside + protected override bool UseOverBackground { get { @@ -42,7 +43,7 @@ public override bool IsMouseInside private readonly StackPanelLayout _layout = new StackPanelLayout(Orientation.Horizontal); private CheckPosition _checkPosition = CheckPosition.Left; - private readonly CheckImage _check = new CheckImage + private readonly CheckImageInternal _check = new CheckImageInternal { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, @@ -95,6 +96,10 @@ public override Widget Content } } + [Browsable(false)] + [XmlIgnore] + public Image CheckImage => _check; + public event EventHandler IsCheckedChanged { add @@ -149,7 +154,14 @@ public override void OnKeyDown(Keys k) protected override void InternalSetStyle(Stylesheet stylesheet, string name) { + base.InternalSetStyle(stylesheet, name); + var style = stylesheet.CheckBoxStyles.SafelyGetStyle(name); + ApplyCheckButtonStyle(style); + } + + public void ApplyCheckButtonStyle(ImageTextButtonStyle style) + { ApplyButtonStyle(style); if (style.ImageStyle != null) @@ -158,8 +170,6 @@ protected override void InternalSetStyle(Stylesheet stylesheet, string name) } CheckContentSpacing = style.ImageTextSpacing; - - ApplyButtonStyle(stylesheet.CheckBoxStyles.SafelyGetStyle(name)); } private void UpdateChildren() diff --git a/src/Myra/Graphics2D/UI/Simple/RadioButton.cs b/src/Myra/Graphics2D/UI/Simple/RadioButton.cs index 71fb543c..13c537ed 100644 --- a/src/Myra/Graphics2D/UI/Simple/RadioButton.cs +++ b/src/Myra/Graphics2D/UI/Simple/RadioButton.cs @@ -1,28 +1,11 @@ using System.ComponentModel; -using System.Linq; using System.Xml.Serialization; using Myra.Graphics2D.UI.Styles; namespace Myra.Graphics2D.UI { - public class RadioButton : ImageTextButton + public class RadioButton : CheckButton { - [Browsable(false)] - [XmlIgnore] - [Category("Behavior")] - [DefaultValue(true)] - public override bool Toggleable - { - get - { - return base.Toggleable; - } - set - { - base.Toggleable = value; - } - } - public override bool IsPressed { get => base.IsPressed; @@ -61,14 +44,13 @@ public override bool IsPressed public RadioButton(string styleName = Stylesheet.DefaultStyleName): base(styleName) { - Toggleable = true; } public override void OnPressedChanged() { base.OnPressedChanged(); - if (!IsPressed) + if (Parent == null || !IsPressed) { return; } @@ -89,7 +71,7 @@ public override void OnPressedChanged() protected override void InternalSetStyle(Stylesheet stylesheet, string name) { - ApplyImageTextButtonStyle(stylesheet.RadioButtonStyles.SafelyGetStyle(name)); + ApplyCheckButtonStyle(stylesheet.RadioButtonStyles.SafelyGetStyle(name)); } } } \ No newline at end of file diff --git a/src/Myra/Graphics2D/UI/Widget.Input.cs b/src/Myra/Graphics2D/UI/Widget.Input.cs index 43e04ff4..505fb97c 100644 --- a/src/Myra/Graphics2D/UI/Widget.Input.cs +++ b/src/Myra/Graphics2D/UI/Widget.Input.cs @@ -29,7 +29,7 @@ partial class Widget: IInputEventsProcessor [Browsable(false)] [XmlIgnore] - public virtual bool IsMouseInside => _localMousePosition != null; + public bool IsMouseInside => _localMousePosition != null; [Browsable(false)] [XmlIgnore] diff --git a/src/Myra/Graphics2D/UI/Widget.cs b/src/Myra/Graphics2D/UI/Widget.cs index 42d2942c..02ea45c3 100644 --- a/src/Myra/Graphics2D/UI/Widget.cs +++ b/src/Myra/Graphics2D/UI/Widget.cs @@ -762,6 +762,8 @@ internal Transform Transform } } + protected virtual bool UseOverBackground => IsMouseInside; + [Browsable(false)] [XmlIgnore] public Action BeforeRender, AfterRender; @@ -787,7 +789,7 @@ public virtual IBrush GetCurrentBackground() { result = FocusedBackground; } - else if (IsMouseInside && OverBackground != null) + else if (UseOverBackground && OverBackground != null) { result = OverBackground; } @@ -1411,7 +1413,7 @@ public virtual Widget HitTest(Point p) } var localPos = ToLocal(p); - if (result == null && !InputFallsThrough(localPos)) + if (result == null && !InputFallsThrough(localPos)) { result = this; } diff --git a/src/MyraPad/Studio.cs b/src/MyraPad/Studio.cs index 89f80101..66d334c6 100644 --- a/src/MyraPad/Studio.cs +++ b/src/MyraPad/Studio.cs @@ -33,7 +33,6 @@ public class Studio : Game private static readonly string[] SimpleWidgets = new[] { "ImageTextButton", - "RadioButton", "SpinButton", "HorizontalProgressBar", "VerticalProgressBar", @@ -52,6 +51,7 @@ public class Studio : Game "Button", "ToggleButton", "CheckButton", + "RadioButton", "Window", "Grid", "Panel", diff --git a/src/MyraPad/UI/NewProjectWizard.Generated.cs b/src/MyraPad/UI/NewProjectWizard.Generated.cs index e2f97c9c..3ac81c4c 100644 --- a/src/MyraPad/UI/NewProjectWizard.Generated.cs +++ b/src/MyraPad/UI/NewProjectWizard.Generated.cs @@ -1,4 +1,4 @@ -/* Generated by MyraPad at 10/23/2023 7:09:21 AM */ +/* Generated by MyraPad at 11/13/2023 10:44:41 PM */ using Myra; using Myra.Graphics2D; using Myra.Graphics2D.TextureAtlases; @@ -26,41 +26,68 @@ partial class NewProjectWizard: Dialog { private void BuildUI() { + var label1 = new Label(); + label1.Text = "Grid"; + _radioButtonGrid = new RadioButton(); - _radioButtonGrid.Text = "Grid"; _radioButtonGrid.Id = "_radioButtonGrid"; + _radioButtonGrid.Content = label1; + + var label2 = new Label(); + label2.Text = "HorizontalStackPanel"; _radioButtonHorizontalStackPanel = new RadioButton(); - _radioButtonHorizontalStackPanel.Text = "HorizontalStackPanel"; _radioButtonHorizontalStackPanel.Id = "_radioButtonHorizontalStackPanel"; + _radioButtonHorizontalStackPanel.Content = label2; + + var label3 = new Label(); + label3.Text = "VerticalStackPanel"; _radioButtonVerticalStackPanel = new RadioButton(); - _radioButtonVerticalStackPanel.Text = "VerticalStackPanel"; _radioButtonVerticalStackPanel.Id = "_radioButtonVerticalStackPanel"; + _radioButtonVerticalStackPanel.Content = label3; + + var label4 = new Label(); + label4.Text = "Panel"; _radioButtonPanel = new RadioButton(); - _radioButtonPanel.Text = "Panel"; _radioButtonPanel.Id = "_radioButtonPanel"; + _radioButtonPanel.Content = label4; + + var label5 = new Label(); + label5.Text = "ScrollViewer"; _radioButtonScrollViewer = new RadioButton(); - _radioButtonScrollViewer.Text = "ScrollViewer"; _radioButtonScrollViewer.Id = "_radioButtonScrollViewer"; + _radioButtonScrollViewer.Content = label5; + + var label6 = new Label(); + label6.Text = "HorizontalSplitPane"; _radioButtonHorizontalSplitPane = new RadioButton(); - _radioButtonHorizontalSplitPane.Text = "HorizontalSplitPane"; _radioButtonHorizontalSplitPane.Id = "_radioButtonHorizontalSplitPane"; + _radioButtonHorizontalSplitPane.Content = label6; + + var label7 = new Label(); + label7.Text = "VerticalSplitPane"; _radioButtonVerticalSplitPane = new RadioButton(); - _radioButtonVerticalSplitPane.Text = "VerticalSplitPane"; _radioButtonVerticalSplitPane.Id = "_radioButtonVerticalSplitPane"; + _radioButtonVerticalSplitPane.Content = label7; + + var label8 = new Label(); + label8.Text = "Window"; _radioButtonWindow = new RadioButton(); - _radioButtonWindow.Text = "Window"; _radioButtonWindow.Id = "_radioButtonWindow"; + _radioButtonWindow.Content = label8; + + var label9 = new Label(); + label9.Text = "Dialog"; _radioButtonDialog = new RadioButton(); - _radioButtonDialog.Text = "Dialog"; _radioButtonDialog.Id = "_radioButtonDialog"; + _radioButtonDialog.Content = label9; var verticalStackPanel1 = new VerticalStackPanel(); verticalStackPanel1.Widgets.Add(_radioButtonGrid); @@ -75,8 +102,8 @@ private void BuildUI() Title = "Choose Root Container"; - Left = 1162; - Top = 540; + Left = 425; + Top = 74; Content = verticalStackPanel1; } diff --git a/src/MyraPad/UI/newProjectWizard.xmmp b/src/MyraPad/UI/newProjectWizard.xmmp index 137b99e8..fc040208 100644 --- a/src/MyraPad/UI/newProjectWizard.xmmp +++ b/src/MyraPad/UI/newProjectWizard.xmmp @@ -1,16 +1,34 @@ - + - - - - - - - - - + + + + + + + + + + + + + + + + + + \ No newline at end of file