From 2c4df04c5c00e990d67897cddf660a02018acf8a Mon Sep 17 00:00:00 2001 From: Nikolai Smirnov Date: Fri, 8 Jul 2022 11:38:25 +0500 Subject: [PATCH] Refactoring WIP --- App.xaml | 6 +- Infrastructure/Enums/PrimitiveType.cs | 8 + Infrastructure/EventArgs.cs | 2 +- Infrastructure/EventTriggerBase.cs | 2 +- .../ObservableCollectionExtensions.cs | 2 +- Infrastructure/MouseBehaviour.cs | 2 +- Infrastructure/RelayCommand.cs | 2 +- Infrastructure/UI/DataContexts/Ellipse.cs | 30 ++ .../UI/DataContexts/PrimitiveBase.cs | 258 ++++++++++++++++ Infrastructure/UI/DataContexts/Rectangle.cs | 19 ++ .../UI/DataTemplates/EllipseTemplate.xaml | 15 + .../UI/DataTemplates/EllipseTemplate.xaml.cs | 16 + .../UI/DataTemplates/RectangleTemplate.xaml | 28 ++ .../DataTemplates/RectangleTemplate.xaml.cs | 16 + Infrastructure/UI/Styles.xaml | 89 ++++++ .../DoubleClickEventTrigger.cs | 2 +- .../MouseWheelDownEventTrigger.cs | 2 +- .../MouseWheelUpEventTrigger.cs | 2 +- .../UI/UserControls/PrimitivePopup.xaml | 142 +++++++++ .../UI/UserControls/PrimitivePopup.xaml.cs | 47 +++ Infrastructure/ViewModelBase.cs | 8 +- MaterialCatalogWindow/MaterialCatalogModel.cs | 3 +- .../MaterialCatalogView.xaml | 2 +- .../MaterialCatalogView.xaml.cs | 5 +- .../MaterialCatalogViewModel.cs | 10 +- .../ConcreteDefinition.cs | 2 +- .../MaterialDefinitionBase.cs | 2 +- .../RebarDefinition.cs | 2 +- .../PrimitiveDefinition/EllipseDefinition.cs | 103 ------- .../PrimitiveDefinitionBase.cs | 146 --------- .../RectangleDefinition.cs | 97 ------ Properties/Annotations.cs | 3 +- StructureHelper.csproj | 44 ++- .../AddMaterialWindow/AddMaterialView.xaml | 7 +- .../AddMaterialWindow/AddMaterialView.xaml.cs | 3 +- .../AddMaterialWindow/AddMaterialViewModel.cs | 4 +- .../ColorPickerWindow/ColorPickerView.xaml | 2 +- .../ColorPickerWindow/ColorPickerView.xaml.cs | 5 +- .../ColorPickerWindow/ColorPickerViewModel.cs | 72 +---- Windows/MainWindow/MainModel.cs | 2 +- Windows/MainWindow/MainView.xaml | 276 ++---------------- Windows/MainWindow/MainView.xaml.cs | 2 +- Windows/MainWindow/MainViewModel.cs | 175 ++++------- 43 files changed, 832 insertions(+), 833 deletions(-) create mode 100644 Infrastructure/Enums/PrimitiveType.cs create mode 100644 Infrastructure/UI/DataContexts/Ellipse.cs create mode 100644 Infrastructure/UI/DataContexts/PrimitiveBase.cs create mode 100644 Infrastructure/UI/DataContexts/Rectangle.cs create mode 100644 Infrastructure/UI/DataTemplates/EllipseTemplate.xaml create mode 100644 Infrastructure/UI/DataTemplates/EllipseTemplate.xaml.cs create mode 100644 Infrastructure/UI/DataTemplates/RectangleTemplate.xaml create mode 100644 Infrastructure/UI/DataTemplates/RectangleTemplate.xaml.cs create mode 100644 Infrastructure/UI/Styles.xaml rename Infrastructure/{ => UI/Triggers}/MouseEventTriggers/DoubleClickEventTrigger.cs (74%) rename Infrastructure/{ => UI/Triggers}/MouseEventTriggers/MouseWheelDownEventTrigger.cs (74%) rename Infrastructure/{ => UI/Triggers}/MouseEventTriggers/MouseWheelUpEventTrigger.cs (74%) create mode 100644 Infrastructure/UI/UserControls/PrimitivePopup.xaml create mode 100644 Infrastructure/UI/UserControls/PrimitivePopup.xaml.cs rename Models/{MaterialDefinition => Materials}/ConcreteDefinition.cs (94%) rename Models/{MaterialDefinition => Materials}/MaterialDefinitionBase.cs (96%) rename Models/{MaterialDefinition => Materials}/RebarDefinition.cs (93%) delete mode 100644 Models/PrimitiveDefinition/EllipseDefinition.cs delete mode 100644 Models/PrimitiveDefinition/PrimitiveDefinitionBase.cs delete mode 100644 Models/PrimitiveDefinition/RectangleDefinition.cs diff --git a/App.xaml b/App.xaml index 52c6648..6c920ed 100644 --- a/App.xaml +++ b/App.xaml @@ -4,6 +4,10 @@ xmlns:local="clr-namespace:StructureHelper" StartupUri="Windows/MainWindow/MainView.xaml"> - + + + + + diff --git a/Infrastructure/Enums/PrimitiveType.cs b/Infrastructure/Enums/PrimitiveType.cs new file mode 100644 index 0000000..6cd5693 --- /dev/null +++ b/Infrastructure/Enums/PrimitiveType.cs @@ -0,0 +1,8 @@ +namespace StructureHelper.Infrastructure.Enums +{ + public enum PrimitiveType + { + Ellipse, + Rectangle + } +} \ No newline at end of file diff --git a/Infrastructure/EventArgs.cs b/Infrastructure/EventArgs.cs index d22d52a..8a607dd 100644 --- a/Infrastructure/EventArgs.cs +++ b/Infrastructure/EventArgs.cs @@ -1,6 +1,6 @@ using System; -namespace StructureHelper +namespace StructureHelper.Infrastructure { public class EventArgs : EventArgs { diff --git a/Infrastructure/EventTriggerBase.cs b/Infrastructure/EventTriggerBase.cs index 55172f4..46bd6b2 100644 --- a/Infrastructure/EventTriggerBase.cs +++ b/Infrastructure/EventTriggerBase.cs @@ -2,7 +2,7 @@ using System.Windows; using EventTrigger = System.Windows.Interactivity.EventTrigger; -namespace StructureHelper +namespace StructureHelper.Infrastructure { public class EventTriggerBase : EventTrigger where T : RoutedEventArgs { diff --git a/Infrastructure/Extensions/ObservableCollectionExtensions.cs b/Infrastructure/Extensions/ObservableCollectionExtensions.cs index 4133fbd..1c49bb8 100644 --- a/Infrastructure/Extensions/ObservableCollectionExtensions.cs +++ b/Infrastructure/Extensions/ObservableCollectionExtensions.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; -namespace StructureHelper +namespace StructureHelper.Infrastructure.Extensions { public static class ObservableCollectionExtensions { diff --git a/Infrastructure/MouseBehaviour.cs b/Infrastructure/MouseBehaviour.cs index c6eae71..c261472 100644 --- a/Infrastructure/MouseBehaviour.cs +++ b/Infrastructure/MouseBehaviour.cs @@ -3,7 +3,7 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interactivity; -namespace StructureHelper +namespace StructureHelper.Infrastructure { public class MouseBehaviour : Behavior { diff --git a/Infrastructure/RelayCommand.cs b/Infrastructure/RelayCommand.cs index 61ccaf4..cee1a9a 100644 --- a/Infrastructure/RelayCommand.cs +++ b/Infrastructure/RelayCommand.cs @@ -1,7 +1,7 @@ using System; using System.Windows.Input; -namespace StructureHelper +namespace StructureHelper.Infrastructure { public class RelayCommand : ICommand { diff --git a/Infrastructure/UI/DataContexts/Ellipse.cs b/Infrastructure/UI/DataContexts/Ellipse.cs new file mode 100644 index 0000000..79e9eb6 --- /dev/null +++ b/Infrastructure/UI/DataContexts/Ellipse.cs @@ -0,0 +1,30 @@ +using System; +using System.Windows.Media; +using StructureHelper.Infrastructure.Enums; +using StructureHelper.Windows.MainWindow; + +namespace StructureHelper.Infrastructure.UI.DataContexts +{ + public class Ellipse : PrimitiveBase + { + private double square; + public double Square + { + get => square; + set + { + square = value; + PrimitiveWidth = Math.Round(Math.Sqrt(4 * value / Math.PI), 2); + OnPropertyChanged(nameof(PrimitiveWidth)); + OnPropertyChanged(); + } + } + + public Ellipse(double square, double ellipseX, double ellipseY, MainViewModel mainViewModel) : base(PrimitiveType.Ellipse, ellipseX, ellipseY, mainViewModel) + { + Square = square; + ShowedX = 0; + ShowedY = 0; + } + } +} diff --git a/Infrastructure/UI/DataContexts/PrimitiveBase.cs b/Infrastructure/UI/DataContexts/PrimitiveBase.cs new file mode 100644 index 0000000..88acc99 --- /dev/null +++ b/Infrastructure/UI/DataContexts/PrimitiveBase.cs @@ -0,0 +1,258 @@ +using System; +using System.Windows.Input; +using System.Windows.Media; +using StructureHelper.Infrastructure.Enums; +using StructureHelper.Models.Materials; +using StructureHelper.Windows.MainWindow; + +namespace StructureHelper.Infrastructure.UI.DataContexts +{ + public abstract class PrimitiveBase : ViewModelBase + { + #region Поля + + private readonly PrimitiveType type; + private bool captured, parameterCaptured, elementLock, paramsPanelVisibilty, popupCanBeClosed = true, borderCaptured; + private Brush brush; + private MaterialDefinitionBase material; + private double opacity = 1, showedOpacity = 0, x, y, xY1, yX1, primitiveWidth, primitiveHeight, showedX, showedY, delta = 0.5; + private int showedZIndex = 1, zIndex; + + #endregion + + #region Свойства + + public PrimitiveType Type => type; + public bool Captured + { + set => OnPropertyChanged(value, ref captured); + get => captured; + } + public bool ParameterCaptured + { + set => OnPropertyChanged(value, ref parameterCaptured); + get => parameterCaptured; + } + public bool ElementLock + { + get => elementLock; + set => OnPropertyChanged(value, ref elementLock); + } + public Brush Brush + { + get => brush; + set => OnPropertyChanged(value, ref brush); + } + public MaterialDefinitionBase Material + { + get => material; + set + { + MaterialName = material.MaterialClass; + OnPropertyChanged(value, ref material); + OnPropertyChanged(nameof(MaterialName)); + } + } + private string materialName = string.Empty; + public string MaterialName + { + get => materialName; + set => OnPropertyChanged(value, ref materialName); + } + public bool ParamsPanelVisibilty + { + get => paramsPanelVisibilty; + set => OnPropertyChanged(value, ref paramsPanelVisibilty); + } + public bool PopupCanBeClosed + { + get => popupCanBeClosed; + set => OnPropertyChanged(value, ref popupCanBeClosed); + } + public double ShowedOpacity + { + get => showedOpacity; + set + { + Opacity = (100 - value) / 100; + OnPropertyChanged(nameof(Opacity)); + OnPropertyChanged(value, ref showedOpacity); + } + } + public double Opacity + { + get => opacity; + set => OnPropertyChanged(value, ref opacity); + } + public int ShowedZIndex + { + get => showedZIndex; + set + { + ZIndex = value - 1; + OnPropertyChanged(nameof(ZIndex)); + OnPropertyChanged(value, ref showedZIndex); + } + } + public int ZIndex + { + get => zIndex; + set => OnPropertyChanged(value, ref zIndex); + } + public double X + { + get => x; + set => OnPropertyChanged(value, ref x); + } + public double Y + { + get => y; + set => OnPropertyChanged(value, ref y); + } + public double Xy1 + { + get => xY1; + set => OnPropertyChanged(value, ref xY1); + } + public double Yx1 + { + get => yX1; + set => OnPropertyChanged(value, ref yX1); + } + public double PrimitiveWidth + { + get => primitiveWidth; + set => OnPropertyChanged(value, ref primitiveWidth); + } + public double PrimitiveHeight + { + get => primitiveHeight; + set => OnPropertyChanged(value, ref primitiveHeight); + } + public double ShowedX + { + get => showedX; + set + { + UpdateCoordinatesX(value); + OnPropertyChanged(value, ref showedX); + OnPropertyChanged(nameof(X)); + } + } + public double ShowedY + { + get => showedY; + set + { + UpdateCoordinatesY(value); + OnPropertyChanged(value, ref showedY); + OnPropertyChanged(nameof(Y)); + } + } + public bool BorderCaptured + { + get => borderCaptured; + set => OnPropertyChanged(value, ref borderCaptured); + } + + #endregion + + #region Команды + public ICommand PrimitiveLeftButtonDown { get; } + public ICommand PrimitiveLeftButtonUp { get; } + public ICommand RectanglePreviewMouseMove { get; } + public ICommand EllipsePreviewMouseMove { get; } + public ICommand PrimitiveDoubleClick { get; } + + #endregion + + protected PrimitiveBase(PrimitiveType type, double rectX, double rectY, MainViewModel mainViewModel) + { + this.type = type; + Yx1 = rectX; + Xy1 = rectY; + var randomR = new Random().Next(150, 255); + var randomG = new Random().Next(0, 255); + var randomB = new Random().Next(30, 130); + var color = Color.FromRgb((byte)randomR, (byte)randomG, (byte)randomB); + Brush = new SolidColorBrush(color); + PrimitiveLeftButtonUp = new RelayCommand(o => Captured = false); + PrimitiveLeftButtonDown = new RelayCommand(o => Captured = true); + RectanglePreviewMouseMove = new RelayCommand(o => + { + if (!(o is Rectangle rect)) return; + if (Captured && !rect.BorderCaptured && !ElementLock) + { + var deltaX = PrimitiveWidth / 2; + var deltaY = PrimitiveHeight / 2; + + if (rect.ShowedX % 10 <= delta || rect.ShowedX % 10 >= 10 - delta) + rect.ShowedX = Math.Round((mainViewModel.PanelX - deltaX - Yx1) / 10) * 10; + else + rect.ShowedX = mainViewModel.PanelX - deltaX - Yx1; + + if (rect.ShowedY % 10 <= delta || rect.ShowedY % 10 >= 10 - delta) + rect.ShowedY = -(Math.Round((mainViewModel.PanelY - deltaY - Xy1 + rect.PrimitiveHeight) / 10) * 10); + else + rect.ShowedY = -(mainViewModel.PanelY - deltaY - Xy1 + rect.PrimitiveHeight); + } + if (ParameterCaptured) + { + //RectParameterX = rect.ShowedX; + //RectParameterY = rect.ShowedY; + //RectParameterWidth = rect.PrimitiveWidth; + //RectParameterHeight = rect.PrimitiveHeight; + //ParameterOpacity = rect.ShowedOpacity; + //ElementLock = rect.ElementLock; + } + }); + EllipsePreviewMouseMove = new RelayCommand(o => + { + if (!(o is Ellipse ellipse)) return; + if (ellipse.Captured && !ellipse.ElementLock) + { + var ellipseDelta = ellipse.PrimitiveWidth / 2; + + if (ellipse.ShowedX % 10 <= ellipseDelta || ellipse.ShowedX % 10 >= 10 - ellipseDelta) + ellipse.ShowedX = Math.Round((mainViewModel.PanelX - Yx1) / 10) * 10; + else + ellipse.ShowedX = mainViewModel.PanelX - ellipseDelta - Yx1; + + if (ellipse.ShowedY % 10 <= ellipseDelta || ellipse.ShowedY % 10 >= 10 - ellipseDelta) + ellipse.ShowedY = -(Math.Round((mainViewModel.PanelY - Xy1) / 10) * 10); + else + ellipse.ShowedY = -(mainViewModel.PanelY - ellipseDelta - Xy1); + } + if (ParameterCaptured) + { + //EllipseParameterX = ellipse.ShowedX; + //EllipseParameterY = ellipse.ShowedY; + //EllipseParameterSquare = ellipse.Square; + //ParameterOpacity = ellipse.ShowedOpacity; + //ElementLock = ellipse.ElementLock; + } + }); + PrimitiveDoubleClick = new RelayCommand(o => + { + PopupCanBeClosed = false; + Captured = false; + ParamsPanelVisibilty = true; + + //if (primitive is Rectangle rect) + // rect.BorderCaptured = false; + }); + + } + + private void UpdateCoordinatesX(double showedX) + { + if (Type == PrimitiveType.Rectangle) X = showedX + Yx1; + if (Type == PrimitiveType.Ellipse) X = showedX + Yx1 - PrimitiveWidth / 2; + } + private void UpdateCoordinatesY(double showedY) + { + if (Type == PrimitiveType.Rectangle) Y = -showedY + Xy1 - PrimitiveHeight; + if (Type == PrimitiveType.Ellipse) Y = -showedY + Xy1 - PrimitiveWidth / 2; + } + } +} diff --git a/Infrastructure/UI/DataContexts/Rectangle.cs b/Infrastructure/UI/DataContexts/Rectangle.cs new file mode 100644 index 0000000..3847e3a --- /dev/null +++ b/Infrastructure/UI/DataContexts/Rectangle.cs @@ -0,0 +1,19 @@ +using System; +using System.Windows.Input; +using System.Windows.Media; +using StructureHelper.Infrastructure.Enums; +using StructureHelper.Windows.MainWindow; + +namespace StructureHelper.Infrastructure.UI.DataContexts +{ + public class Rectangle : PrimitiveBase + { + public Rectangle(double primitiveWidth, double primitiveHeight, double rectX, double rectY, MainViewModel mainViewModel) : base(PrimitiveType.Rectangle, rectX, rectY, mainViewModel) + { + PrimitiveWidth = primitiveWidth; + PrimitiveHeight = primitiveHeight; + ShowedX = 0; + ShowedY = 0; + } + } +} diff --git a/Infrastructure/UI/DataTemplates/EllipseTemplate.xaml b/Infrastructure/UI/DataTemplates/EllipseTemplate.xaml new file mode 100644 index 0000000..66dfa6e --- /dev/null +++ b/Infrastructure/UI/DataTemplates/EllipseTemplate.xaml @@ -0,0 +1,15 @@ + + + + + diff --git a/Infrastructure/UI/DataTemplates/EllipseTemplate.xaml.cs b/Infrastructure/UI/DataTemplates/EllipseTemplate.xaml.cs new file mode 100644 index 0000000..c406719 --- /dev/null +++ b/Infrastructure/UI/DataTemplates/EllipseTemplate.xaml.cs @@ -0,0 +1,16 @@ +using System.Windows; +using System.Windows.Controls; + +namespace StructureHelper.Infrastructure.UI.DataTemplates +{ + /// + /// Логика взаимодействия для EllipseTemplate.xaml + /// + public partial class EllipseTemplate : UserControl + { + public EllipseTemplate() + { + InitializeComponent(); + } + } +} diff --git a/Infrastructure/UI/DataTemplates/RectangleTemplate.xaml b/Infrastructure/UI/DataTemplates/RectangleTemplate.xaml new file mode 100644 index 0000000..2230f86 --- /dev/null +++ b/Infrastructure/UI/DataTemplates/RectangleTemplate.xaml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + diff --git a/Infrastructure/UI/DataTemplates/RectangleTemplate.xaml.cs b/Infrastructure/UI/DataTemplates/RectangleTemplate.xaml.cs new file mode 100644 index 0000000..4a08178 --- /dev/null +++ b/Infrastructure/UI/DataTemplates/RectangleTemplate.xaml.cs @@ -0,0 +1,16 @@ +using System.Windows; +using System.Windows.Controls; + +namespace StructureHelper.Infrastructure.UI.DataTemplates +{ + /// + /// Логика взаимодействия для RectangleTemplate.xaml + /// + public partial class RectangleTemplate : UserControl + { + public RectangleTemplate() + { + InitializeComponent(); + } + } +} diff --git a/Infrastructure/UI/Styles.xaml b/Infrastructure/UI/Styles.xaml new file mode 100644 index 0000000..a502d4c --- /dev/null +++ b/Infrastructure/UI/Styles.xaml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Infrastructure/MouseEventTriggers/DoubleClickEventTrigger.cs b/Infrastructure/UI/Triggers/MouseEventTriggers/DoubleClickEventTrigger.cs similarity index 74% rename from Infrastructure/MouseEventTriggers/DoubleClickEventTrigger.cs rename to Infrastructure/UI/Triggers/MouseEventTriggers/DoubleClickEventTrigger.cs index 0219791..317b21d 100644 --- a/Infrastructure/MouseEventTriggers/DoubleClickEventTrigger.cs +++ b/Infrastructure/UI/Triggers/MouseEventTriggers/DoubleClickEventTrigger.cs @@ -1,6 +1,6 @@ using System.Windows.Input; -namespace StructureHelper.Infrastructure +namespace StructureHelper.Infrastructure.UI.Triggers.MouseEventTriggers { public class DoubleClickEventTrigger : EventTriggerBase { diff --git a/Infrastructure/MouseEventTriggers/MouseWheelDownEventTrigger.cs b/Infrastructure/UI/Triggers/MouseEventTriggers/MouseWheelDownEventTrigger.cs similarity index 74% rename from Infrastructure/MouseEventTriggers/MouseWheelDownEventTrigger.cs rename to Infrastructure/UI/Triggers/MouseEventTriggers/MouseWheelDownEventTrigger.cs index bea8b3b..9639310 100644 --- a/Infrastructure/MouseEventTriggers/MouseWheelDownEventTrigger.cs +++ b/Infrastructure/UI/Triggers/MouseEventTriggers/MouseWheelDownEventTrigger.cs @@ -1,6 +1,6 @@ using System.Windows.Input; -namespace StructureHelper.Infrastructure +namespace StructureHelper.Infrastructure.UI.Triggers.MouseEventTriggers { public class MouseWheelDownEventTrigger : EventTriggerBase { diff --git a/Infrastructure/MouseEventTriggers/MouseWheelUpEventTrigger.cs b/Infrastructure/UI/Triggers/MouseEventTriggers/MouseWheelUpEventTrigger.cs similarity index 74% rename from Infrastructure/MouseEventTriggers/MouseWheelUpEventTrigger.cs rename to Infrastructure/UI/Triggers/MouseEventTriggers/MouseWheelUpEventTrigger.cs index 18195c8..c7203a2 100644 --- a/Infrastructure/MouseEventTriggers/MouseWheelUpEventTrigger.cs +++ b/Infrastructure/UI/Triggers/MouseEventTriggers/MouseWheelUpEventTrigger.cs @@ -1,6 +1,6 @@ using System.Windows.Input; -namespace StructureHelper.Infrastructure +namespace StructureHelper.Infrastructure.UI.Triggers.MouseEventTriggers { public class MouseWheelUpEventTrigger : EventTriggerBase { diff --git a/Infrastructure/UI/UserControls/PrimitivePopup.xaml b/Infrastructure/UI/UserControls/PrimitivePopup.xaml new file mode 100644 index 0000000..316963c --- /dev/null +++ b/Infrastructure/UI/UserControls/PrimitivePopup.xaml @@ -0,0 +1,142 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +