From aa2e3c5985147ec8bdd38c48962736d385abd5ff Mon Sep 17 00:00:00 2001 From: Ed Kolis Date: Sun, 20 Oct 2024 14:09:24 -0400 Subject: [PATCH] Add ViewBase class to Blazor UI; add code behind to GalaxyMap.razor; delete placeholder Component1.razor. (#325) --- FrEee.UI.Blazor/Component1.razor | 3 - FrEee.UI.Blazor/Component1.razor.css | 6 -- FrEee.UI.Blazor/Views/GalaxyMap.razor | 61 +------------------ FrEee.UI.Blazor/Views/GalaxyMap.razor.cs | 44 +++++++++++++ FrEee.UI.Blazor/Views/ImageDisplay.razor | 31 ++-------- FrEee.UI.Blazor/Views/PieChart.razor | 25 +------- FrEee.UI.Blazor/Views/ProgressBar.razor | 26 +------- FrEee.UI.Blazor/Views/ResourceDisplay.razor | 26 +------- .../Views/ResourceQuantityDisplay.razor | 26 +------- FrEee.UI.Blazor/Views/ViewBase.cs | 44 +++++++++++++ 10 files changed, 100 insertions(+), 192 deletions(-) delete mode 100644 FrEee.UI.Blazor/Component1.razor delete mode 100644 FrEee.UI.Blazor/Component1.razor.css create mode 100644 FrEee.UI.Blazor/Views/GalaxyMap.razor.cs create mode 100644 FrEee.UI.Blazor/Views/ViewBase.cs diff --git a/FrEee.UI.Blazor/Component1.razor b/FrEee.UI.Blazor/Component1.razor deleted file mode 100644 index b9822b7e..00000000 --- a/FrEee.UI.Blazor/Component1.razor +++ /dev/null @@ -1,3 +0,0 @@ -
- This component is defined in the FrEee.UI.Blazor library. -
diff --git a/FrEee.UI.Blazor/Component1.razor.css b/FrEee.UI.Blazor/Component1.razor.css deleted file mode 100644 index c6afca40..00000000 --- a/FrEee.UI.Blazor/Component1.razor.css +++ /dev/null @@ -1,6 +0,0 @@ -.my-component { - border: 2px dashed red; - padding: 1em; - margin: 1em 0; - background-image: url('background.png'); -} diff --git a/FrEee.UI.Blazor/Views/GalaxyMap.razor b/FrEee.UI.Blazor/Views/GalaxyMap.razor index dad62317..33b0b249 100644 --- a/FrEee.UI.Blazor/Views/GalaxyMap.razor +++ b/FrEee.UI.Blazor/Views/GalaxyMap.razor @@ -3,63 +3,7 @@ @using System.Numerics @using FrEee.Extensions @using Excubo.Blazor.Canvas - -@code { - [Parameter] - public GalaxyMapViewModel VM { get; set; } = new(); - - /// - /// When the view model's properties change, update the UI. - /// - /// - /// - private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - StateHasChanged(); - } - - protected override void OnInitialized() - { - VM.PropertyChanged += ViewModelPropertyChanged; - } - - public void Dispose() - { - VM.PropertyChanged -= ViewModelPropertyChanged; - } - - private Canvas helper_canvas; - private ElementReference normal_canvas; - protected override async Task OnAfterRenderAsync(bool firstRender) - { - var size = VM.Scale + 1; - var xoffset = VM.Width / 2d; - var yoffset = VM.Height / 2d; - - await using (var ctx = await helper_canvas.GetContext2DAsync()) - { - await ctx.ClearRectAsync(0, 0, VM.Width, VM.Height); - await ctx.SetTransformAsync(size, 0, 0, size, (xoffset + 0.5) * size, (yoffset + 0.5) * size); - await ctx.RestoreAsync(); - await ctx.SaveAsync(); - await ctx.StrokeStyleAsync("white"); - await ctx.LineWidthAsync(0.1); - foreach (var connections in VM.WarpGraph.Connections) - { - var src = connections.Key; - foreach (var dest in connections.Value) - { - // TODO: display one way warps differently (arrows, incomplete lines, gradients?) - await ctx.MoveToAsync(src.Location.X, src.Location.Y); - await ctx.LineToAsync(dest.Location.X, dest.Location.Y); - await ctx.StrokeAsync(); - } - } - } - - await base.OnAfterRenderAsync(firstRender); - } -} +@inherits ViewBase
@@ -73,7 +17,8 @@ { for (var y = VM.MinY; y <= VM.MaxY; y++) { - var sysloc = VM.StarSystemLocations.SingleOrDefault(q => q.Location.X == x && q.Location.Y == y); + var sysloc = VM.StarSystemLocations + .SingleOrDefault(q => q.Location.X == x && q.Location.Y == y); var row = y - VM.MinY + 1; var col = x - VM.MinX + 1; if (sysloc is not null) diff --git a/FrEee.UI.Blazor/Views/GalaxyMap.razor.cs b/FrEee.UI.Blazor/Views/GalaxyMap.razor.cs new file mode 100644 index 00000000..68811681 --- /dev/null +++ b/FrEee.UI.Blazor/Views/GalaxyMap.razor.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Excubo.Blazor.Canvas; +using Microsoft.AspNetCore.Components; + +namespace FrEee.UI.Blazor.Views; + +public partial class GalaxyMap +{ + private Canvas helper_canvas; + private ElementReference normal_canvas; + protected override async Task OnAfterRenderAsync(bool firstRender) + { + var size = VM.Scale + 1; + var xoffset = VM.Width / 2d; + var yoffset = VM.Height / 2d; + + await using (var ctx = await helper_canvas.GetContext2DAsync()) + { + await ctx.ClearRectAsync(0, 0, VM.Width, VM.Height); + await ctx.SetTransformAsync(size, 0, 0, size, (xoffset + 0.5) * size, (yoffset + 0.5) * size); + await ctx.RestoreAsync(); + await ctx.SaveAsync(); + await ctx.StrokeStyleAsync("white"); + await ctx.LineWidthAsync(0.1); + foreach (var connections in VM.WarpGraph.Connections) + { + var src = connections.Key; + foreach (var dest in connections.Value) + { + // TODO: display one way warps differently (arrows, incomplete lines, gradients?) + await ctx.MoveToAsync(src.Location.X, src.Location.Y); + await ctx.LineToAsync(dest.Location.X, dest.Location.Y); + await ctx.StrokeAsync(); + } + } + } + + await base.OnAfterRenderAsync(firstRender); + } +} diff --git a/FrEee.UI.Blazor/Views/ImageDisplay.razor b/FrEee.UI.Blazor/Views/ImageDisplay.razor index 7921983a..9afc4d12 100644 --- a/FrEee.UI.Blazor/Views/ImageDisplay.razor +++ b/FrEee.UI.Blazor/Views/ImageDisplay.razor @@ -1,29 +1,8 @@ @using System.Drawing @using System.ComponentModel +@inherits ViewBase -@code { - [Parameter] - public ImageDisplayViewModel VM { get; set; } = new(); - - /// - /// When the view model's properties change, update the UI. - /// - /// - /// - private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - StateHasChanged(); - } - - protected override void OnInitialized() - { - VM.PropertyChanged += ViewModelPropertyChanged; - } - - public void Dispose() - { - VM.PropertyChanged -= ViewModelPropertyChanged; - } -} - - \ No newline at end of file + \ No newline at end of file diff --git a/FrEee.UI.Blazor/Views/PieChart.razor b/FrEee.UI.Blazor/Views/PieChart.razor index 7cf012b3..284998f4 100644 --- a/FrEee.UI.Blazor/Views/PieChart.razor +++ b/FrEee.UI.Blazor/Views/PieChart.razor @@ -5,30 +5,7 @@ @typeparam T where T : INumber, IMultiplyOperators, IDivisionOperators -@code { - [Parameter] - public PieChartViewModel VM { get; set; } = new(); - - /// - /// When the view model's properties change, update the UI. - /// - /// - /// - private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - StateHasChanged(); - } - - protected override void OnInitialized() - { - VM.PropertyChanged += ViewModelPropertyChanged; - } - - public void Dispose() - { - VM.PropertyChanged -= ViewModelPropertyChanged; - } -} +@inherits ViewBase>
- /// - private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - StateHasChanged(); - } - - protected override void OnInitialized() - { - VM.PropertyChanged += ViewModelPropertyChanged; - } - - public void Dispose() - { - VM.PropertyChanged -= ViewModelPropertyChanged; - } -} +@inherits ViewBase
diff --git a/FrEee.UI.Blazor/Views/ResourceDisplay.razor b/FrEee.UI.Blazor/Views/ResourceDisplay.razor index dd4193e7..36ba8b32 100644 --- a/FrEee.UI.Blazor/Views/ResourceDisplay.razor +++ b/FrEee.UI.Blazor/Views/ResourceDisplay.razor @@ -1,30 +1,6 @@ @using System.Drawing @using System.ComponentModel - -@code { - [Parameter] - public ResourceDisplayViewModel VM { get; set; } = new(); - - /// - /// When the view model's properties change, update the UI. - /// - /// - /// - private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - StateHasChanged(); - } - - protected override void OnInitialized() - { - VM.PropertyChanged += ViewModelPropertyChanged; - } - - public void Dispose() - { - VM.PropertyChanged -= ViewModelPropertyChanged; - } -} +@inherits ViewBase
diff --git a/FrEee.UI.Blazor/Views/ResourceQuantityDisplay.razor b/FrEee.UI.Blazor/Views/ResourceQuantityDisplay.razor index fa7db72a..cf097847 100644 --- a/FrEee.UI.Blazor/Views/ResourceQuantityDisplay.razor +++ b/FrEee.UI.Blazor/Views/ResourceQuantityDisplay.razor @@ -1,30 +1,6 @@ @using System.Drawing @using System.ComponentModel - -@code { - [Parameter] - public ResourceQuantityDisplayViewModel VM { get; set; } = new(); - - /// - /// When the view model's properties change, update the UI. - /// - /// - /// - private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - StateHasChanged(); - } - - protected override void OnInitialized() - { - VM.PropertyChanged += ViewModelPropertyChanged; - } - - public void Dispose() - { - VM.PropertyChanged -= ViewModelPropertyChanged; - } -} +@inherits ViewBase
@foreach (var resourceVM in VM.ResourceViewModels) diff --git a/FrEee.UI.Blazor/Views/ViewBase.cs b/FrEee.UI.Blazor/Views/ViewBase.cs new file mode 100644 index 00000000..f54e05f4 --- /dev/null +++ b/FrEee.UI.Blazor/Views/ViewBase.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; + +namespace FrEee.UI.Blazor.Views; + +/// +/// Base class for Razor views in FrEee. +/// +/// +public class ViewBase + : ComponentBase + where TVM : INotifyPropertyChanged, new() +{ + /// + /// The view model for this view. + /// + [Parameter] + public TVM VM { get; set; } = new(); + + /// + /// When the view model's properties change, update the UI. + /// + /// + /// + private void ViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e) + { + StateHasChanged(); + } + + protected override void OnInitialized() + { + VM.PropertyChanged += ViewModelPropertyChanged; + } + + public void Dispose() + { + VM.PropertyChanged -= ViewModelPropertyChanged; + } +}