Skip to content

Commit

Permalink
Fix edit mode bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrabbers committed Mar 5, 2024
1 parent 86e89f3 commit a711752
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .idea/.idea.BnbnavNetClient/.idea/avalonia.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion BnbnavNetClient/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:local="using:BnbnavNetClient"
xmlns:controls="clr-namespace:BnbnavNetClient.Controls"
x:Class="BnbnavNetClient.App"
Name="bnbnav">
Name="bnbnav"
RequestedThemeVariant="Light">
<NativeMenu.Menu>
<NativeMenu>
<NativeMenuItem Header="About bnbnav"></NativeMenuItem>
Expand Down Expand Up @@ -82,6 +83,13 @@
<Style Selector="TextBlock.roadname">
<Setter Property="FontSize" Value="25"/>
</Style>
<Style Selector="TextBlock.title">
<Setter Property="FontSize" Value="16"/>
</Style>
<Style Selector="TabControl TabItem">
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontWeight" Value="Normal"/>
</Style>
<StyleInclude Source="/Controls/DayNightButton.axaml"/>
<StyleInclude Source="/Controls/LandmarkSearchControl.axaml" />
<StyleInclude Source="/Controls/InstructionDisplayControl.axaml" />
Expand Down
2 changes: 0 additions & 2 deletions BnbnavNetClient/Services/BnbnavWebsocketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ internal abstract record NodeMessage : BnbnavMessage
public required int Y { get; init; }
public required int Z { get; init; }
public required string World { get; init; }

public required string Player { get; init; }
}

internal sealed record NodeCreated : NodeMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public override void Render(MapView mapView, DrawingContext context)
//Make the shape into a circle
if (geo.Points.Count == 1) geo.Points.Add(geo.Points.First());

var pen = (Pen)mapView.FindResource("RoadGhostPen")!;
var pen = (Pen)mapView.ThemeDict["RoadGhostPen"]!;
pen.Thickness = mapView.ThicknessForRoadType(RoadType.Local) * mapView.MapViewModel.Scale;
context.DrawGeometry(null, pen, geo);
}
var nodeBorder = (Pen)mapView.FindResource("NodeBorder")!;
var selNodeBrush = (Brush)mapView.FindResource("SelectedNodeFill")!;
var nodeBorder = (Pen)mapView.ThemeDict["NodeBorder"]!;
var selNodeBrush = (Brush)mapView.ThemeDict["SelectedNodeFill"]!;
foreach (var (rect, _) in mapView.DrawnNodes.Where(x => ghosts.Contains(x.Item2)))
{
context.DrawRectangle(selNodeBrush, nodeBorder, rect);
Expand Down
19 changes: 15 additions & 4 deletions BnbnavNetClient/Services/MapEditorService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.ObjectModel;
using BnbnavNetClient.Models;
using BnbnavNetClient.Services.EditControllers;
using BnbnavNetClient.Services.NetworkOperations;
Expand All @@ -8,10 +9,11 @@ namespace BnbnavNetClient.Services;

public class MapEditorService : ReactiveObject
{
List<NetworkOperation> _networkOperations = [];
readonly List<NetworkOperation> _networkOperations = [];

public MapEditorService()
{
OngoingNetworkOperations = _networkOperations.AsReadOnly();
EditController = new SelectEditController(this);
this.ObservableForProperty(x => x.CurrentEditMode).Subscribe(x =>
{
Expand All @@ -29,10 +31,18 @@ public MapEditorService()

public void TrackNetworkOperation(NetworkOperation operation)
{
_networkOperations.Add(operation);
lock (OngoingNetworkOperationsMutex)
{
_networkOperations.Add(operation);
}

operation.PerformOperation().ContinueWith(_ =>
{
_networkOperations.Remove(operation);
lock (OngoingNetworkOperationsMutex)
{
_networkOperations.Remove(operation);
}

this.RaisePropertyChanged(nameof(OngoingNetworkOperations));
});
this.RaisePropertyChanged(nameof(OngoingNetworkOperations));
Expand All @@ -48,5 +58,6 @@ public void TrackNetworkOperation(NetworkOperation operation)

public MapService? MapService { get; set; }

public IReadOnlyList<NetworkOperation> OngoingNetworkOperations => _networkOperations.AsReadOnly();
public object OngoingNetworkOperationsMutex { get; } = new();
public ReadOnlyCollection<NetworkOperation> OngoingNetworkOperations { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public override async Task PerformOperation()

public override void Render(MapView mapView, DrawingContext context)
{
var nodeBorder = (Pen)mapView.FindResource("NodeBorder")!;
var nodeBrush = (Brush)mapView.FindResource("NodeFill")!;
var nodeBorder = (Pen)mapView.ThemeDict["NodeBorder"]!;
var nodeBrush = (Brush)mapView.ThemeDict["NodeFill"]!;
var rect = node.BoundingRect(mapView);
using (context.PushOpacity(0.5))
context.DrawRectangle(nodeBrush, nodeBorder, rect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public override async Task PerformOperation()

public override void Render(MapView mapView, DrawingContext context)
{
var nodeBorder = (Pen)mapView.FindResource("NodeBorder")!;
var selNodeBrush = (Brush)mapView.FindResource("SelectedNodeFill")!;
var nodeBorder = (Pen)mapView.ThemeDict["NodeBorder"]!;
var selNodeBrush = (Brush)mapView.ThemeDict["SelectedNodeFill"]!;

var movingRect = toUpdate.BoundingRect(mapView);
var movedRect = updateTo.BoundingRect(mapView);
Expand All @@ -53,7 +53,7 @@ public override void Render(MapView mapView, DrawingContext context)
geo.Points.Add(arrowhead1.Point1);
geo.Points.Add(arrowhead2.Point2);

var pen = (Pen)mapView.FindResource("EditMovePen")!;
var pen = (Pen)mapView.ThemeDict["EditMovePen"]!;
context.DrawLine(pen, lineBetween.Point1, lineBetween.Point2);
context.DrawGeometry(null, pen, geo);

Expand Down
2 changes: 1 addition & 1 deletion BnbnavNetClient/Views/LandmarkFlyoutView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
xmlns:viewModels="clr-namespace:BnbnavNetClient.ViewModels">

<StackPanel>
<TextBlock Text="{str:Tr Key=EDIT_LANDMARK}" />
<TextBlock Text="{str:Tr Key=EDIT_LANDMARK}" Classes="title" />
<TabControl SelectedIndex="{Binding CurrentTabIndex}">
<TabItem Header="{str:Tr Key=LANDMARK}">
<StackPanel>
Expand Down
2 changes: 1 addition & 1 deletion BnbnavNetClient/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<Grid Name="MainUiGrid" RowDefinitions="* Auto Auto Auto" Classes="">
<Grid.Styles>
<Style Selector="Grid.blur">
<Setter Property="Effect" Value="blur(10)"/>
<Setter Property="Width" Value="blur(10)"/>
</Style>
</Grid.Styles>
<!-- Map -->
Expand Down
16 changes: 10 additions & 6 deletions BnbnavNetClient/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public async void ViewLoaded(object sender, RoutedEventArgs e)
WorldSelectComboBox.IsVisible = true;
vm.RaisePropertyChanged(nameof(MainViewModel.PanText));

vm.WhenAnyValue<MainViewModel, ViewModel?>(x => x.Popup).Subscribe(p =>
// c.f. issue #32 for why we disable the blur effect on windows
if (!OperatingSystem.IsWindows())
{
if (p is null)
MainUiGrid.Classes.Clear();
else
MainUiGrid.Classes.Add("blur");
});
vm.WhenAnyValue<MainViewModel, ViewModel?>(x => x.Popup).Subscribe(p =>
{
if (p is null)
MainUiGrid.Classes.Clear();
else
MainUiGrid.Classes.Add("blur");
});
}
}

public async void ColorModeSwitch(object? _, RoutedEventArgs? __)
Expand Down
72 changes: 40 additions & 32 deletions BnbnavNetClient/Views/MapView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public partial class MapView : UserControl

// For some reason, using the proper method, (i.e. ResourceDictionary.ThemeDictionaries) does not seem to work here.
// This is a pretty crap solution, so if we find a better way it would probably be worthwhile implementing it
IResourceDictionary _themeDict = default!;
public IResourceDictionary ThemeDict { get; private set; }= default!;

Matrix _toScreenMtx = Matrix.Identity;
Matrix _toWorldMtx = Matrix.Identity;
Expand Down Expand Up @@ -230,17 +230,16 @@ protected override void OnInitialized()
{
foreach (var (name, player) in prop.Value)
{
if (((MapViewModel.FollowMeEnabled) && name == MapViewModel.LoggedInUsername) ||
(player.World == MapViewModel.ChosenWorld &&
Bounds.Intersects(GeometryHelper.SquareCenteredOn(ToScreen(player.Point), PlayerSize))))
if (player.World == MapViewModel.ChosenWorld &&
Bounds.Intersects(GeometryHelper.SquareCenteredOn(ToScreen(player.Point), PlayerSize)))
{
if (!player.Moved)
continue;

shouldUpdateVisuals = true;
player.StartCalculateSnappedEdge(); // only do so if the player is on screen!
}
else if (MapViewModel.CurrentUi == AvailableUi.Go &&
else if ((MapViewModel.CurrentUi == AvailableUi.Go || MapViewModel.FollowMeEnabled) &&
name == MapViewModel.LoggedInUsername)
{
shouldUpdateVisuals = true;
Expand Down Expand Up @@ -466,20 +465,20 @@ void UpdateDrawnItems(Rect? boundsRect = null)

Pen PenForRoadType(RoadType type) => (Pen)(type switch
{
RoadType.Local => _themeDict["LocalRoadPen"]!,
RoadType.Main => _themeDict["MainRoadPen"]!,
RoadType.Highway => _themeDict["HighwayRoadPen"]!,
RoadType.Expressway => _themeDict["ExpresswayRoadPen"]!,
RoadType.Motorway => _themeDict["MotorwayRoadPen"]!,
RoadType.Footpath => _themeDict["FootpathRoadPen"]!,
RoadType.Waterway => _themeDict["WaterwayRoadPen"]!,
RoadType.Private => _themeDict["PrivateRoadPen"]!,
RoadType.Roundabout => _themeDict["RoundaboutRoadPen"]!,
RoadType.DuongWarp => _themeDict["DuongWarpRoadPen"]!,
_ => _themeDict["UnknownRoadPen"]!,
RoadType.Local => ThemeDict["LocalRoadPen"]!,
RoadType.Main => ThemeDict["MainRoadPen"]!,
RoadType.Highway => ThemeDict["HighwayRoadPen"]!,
RoadType.Expressway => ThemeDict["ExpresswayRoadPen"]!,
RoadType.Motorway => ThemeDict["MotorwayRoadPen"]!,
RoadType.Footpath => ThemeDict["FootpathRoadPen"]!,
RoadType.Waterway => ThemeDict["WaterwayRoadPen"]!,
RoadType.Private => ThemeDict["PrivateRoadPen"]!,
RoadType.Roundabout => ThemeDict["RoundaboutRoadPen"]!,
RoadType.DuongWarp => ThemeDict["DuongWarpRoadPen"]!,
_ => ThemeDict["UnknownRoadPen"]!,
});

public double ThicknessForRoadType(RoadType type) => (double)(type == RoadType.Motorway ? _themeDict["MotorwayThickness"]! : _themeDict["RoadThickness"]!);
public double ThicknessForRoadType(RoadType type) => (double)(type == RoadType.Motorway ? ThemeDict["MotorwayThickness"]! : ThemeDict["RoadThickness"]!);

public void DrawEdge(DrawingContext context, RoadType roadType, Point from, Point to, bool drawGhost = false, bool drawRoute = false)
{
Expand Down Expand Up @@ -521,7 +520,7 @@ public void DrawLandmark(DrawingContext context, Landmark landmark, Rect rect)
if (scale < lowerScaleBound || scale > higherScaleBound) return;

var text = new FormattedText(landmark.Name, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(FontFamily), size * scale, (Brush)_themeDict["ForegroundBrush"]!);
new Typeface(FontFamily), size * scale, (Brush)ThemeDict["ForegroundBrush"]!);

context.DrawText(text, rect.Center - new Point(text.Width / 2, text.Height / 2));
return;
Expand All @@ -538,16 +537,30 @@ public void DrawLandmark(DrawingContext context, Landmark landmark, Rect rect)

public override void Render(DrawingContext context)
{
_themeDict = (IResourceDictionary)this.FindResource(ActualThemeVariant.ToString())!;
ThemeDict = (IResourceDictionary)this.FindResource(ActualThemeVariant.ToString())!;

context.FillRectangle((Brush)_themeDict["BackgroundBrush"]!, Bounds);
context.FillRectangle((Brush)ThemeDict["BackgroundBrush"]!, Bounds);

var noRender = new List<MapItem>();
noRender.AddRange(MapViewModel.MapEditorService.EditController.ItemsNotToRender);
foreach (var operation in MapViewModel.MapEditorService.OngoingNetworkOperations)

try
{
noRender.AddRange(operation.ItemsNotToRender);
if (Monitor.TryEnter(MapViewModel.MapEditorService.OngoingNetworkOperationsMutex))
{
foreach (var operation in MapViewModel.MapEditorService.OngoingNetworkOperations)
{
noRender.AddRange(operation.ItemsNotToRender);
operation.Render(this, context);
}
}
}
finally
{
if (Monitor.IsEntered(MapViewModel.MapEditorService.OngoingNetworkOperationsMutex))
Monitor.Exit(MapViewModel.MapEditorService.OngoingNetworkOperationsMutex);
}


foreach (var (from, to, edge) in DrawnEdges)
{
Expand Down Expand Up @@ -584,10 +597,10 @@ public override void Render(DrawingContext context)

if (MapViewModel.IsInEditMode)
{
var nodeBorder = (Pen)_themeDict["NodeBorder"]!;
var nodeBrush = (Brush)_themeDict["NodeFill"]!;
var spiedBorder = (Pen)_themeDict["SpiedNodeBorder"]!;
var spiedBrush = (Brush)_themeDict["SpiedNodeFill"]!;
var nodeBorder = (Pen)ThemeDict["NodeBorder"]!;
var nodeBrush = (Brush)ThemeDict["NodeFill"]!;
var spiedBorder = (Pen)ThemeDict["SpiedNodeBorder"]!;
var spiedBrush = (Brush)ThemeDict["SpiedNodeFill"]!;
foreach (var (rect, node) in DrawnNodes)
{
if (noRender.Contains(node)) continue;
Expand All @@ -605,11 +618,6 @@ public override void Render(DrawingContext context)
MapViewModel.MapEditorService.EditController.Render(this, context);
}

foreach (var operation in MapViewModel.MapEditorService.OngoingNetworkOperations)
{
operation.Render(this, context);
}

foreach (var player in MapViewModel.MapService.Players.Values
.Where(player => player.World == MapViewModel.ChosenWorld && Bounds.Contains(ToScreen(player.Point))))
{
Expand All @@ -618,7 +626,7 @@ public override void Render(DrawingContext context)
context.DrawSvgUrl(uriString, rect, -player.MarkerAngle + MapViewModel.Rotation);

//Draw the player name
var textBrush = (Brush)_themeDict["ForegroundBrush"]!;
var textBrush = (Brush)ThemeDict["ForegroundBrush"]!;

if (player.PlayerText is null)
{
Expand Down
2 changes: 1 addition & 1 deletion BnbnavNetClient/Views/NewEdgeFlyoutView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
xmlns:str="using:BnbnavNetClient.I18Next"
>
<StackPanel>
<TextBlock Text="{str:Tr Key=NEW_EDGE}" />
<TextBlock Text="{str:Tr Key=NEW_EDGE}" Classes="title"/>
<CheckBox IsChecked="{Binding Bidirectional}" Content="{str:Tr Key=MAKE_BIDIRECTIONAL}" />
<TabControl SelectedIndex="{Binding CurrentTabIndex}">
<TabItem Header="{str:Tr Key=NEW}">
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<PropertyGroup>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AvaloniaVersion>11.0.9</AvaloniaVersion>
<AvaloniaVersion>11.0.7</AvaloniaVersion>
</PropertyGroup>
</Project>

0 comments on commit a711752

Please sign in to comment.