Skip to content

Commit

Permalink
Cleaned up grid and added opacity slider
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdoombox committed Mar 12, 2020
1 parent c762b6d commit a870d08
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 80 deletions.
60 changes: 39 additions & 21 deletions PostScriptumMortarCalculator/Controls/GridOverlayControl.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand Down Expand Up @@ -51,36 +52,53 @@ private static void PropertyChangedCallback(DependencyObject d, DependencyProper
private void DrawGrid()
{
Children.Clear();
// This value is initially set to 0 until it updates, which causes a deadlock
if (MapPixelsPerMeter <= 0) return;
// Draw major grid lines
for (double x = 0; x <= ActualWidth; x += 300 * MapPixelsPerMeter)
Children.Add(CreateLine(x,x,0,ActualHeight, MajorLineThickness));
for (double y = 0; y <= ActualHeight; y += 300 * MapPixelsPerMeter)
Children.Add(CreateLine(0, ActualWidth, y, y, MajorLineThickness));
// Draw minor grid lines
for (double x = 0; x <= ActualWidth; x += 100 * MapPixelsPerMeter)
Children.Add(CreateLine(x,x,0,ActualHeight, MinorLineThickness));
for (double y = 0; y <= ActualHeight; y += 100 * MapPixelsPerMeter)
Children.Add(CreateLine(0, ActualWidth, y, y, MinorLineThickness));
// Draw micro grid lines
for (double x = 0; x <= ActualWidth; x += 33.333 * MapPixelsPerMeter)
Children.Add(CreateLine(x,x,0,ActualHeight, MicroLineThickness));
for (double y = 0; y <= ActualHeight; y += 33.333 * MapPixelsPerMeter)
Children.Add(CreateLine(0, ActualWidth, y, y, MicroLineThickness));
// This value is initially set to 0 until the map fully loads.
if (MapPixelsPerMeter <= 0) return;

var microIncrement = 33.333333333d * MapPixelsPerMeter;

// Create divisions ahead of time so we can control what kind of line we draw where.
var divisions = new List<double>();
var currentCrawl = 0d;
while ((currentCrawl += microIncrement) <= ActualWidth)
{
divisions.Add(currentCrawl);
}

for (var i = 1; i <= divisions.Count; i++)
{
var div = divisions[i-1];
if (i % 9 == 0)
{
DrawLine(div,div,0,ActualHeight, MajorLineThickness, Brushes.Black, 1);
DrawLine(0, ActualWidth, div, div, MajorLineThickness, Brushes.Black, 1);
}
else if (i % 3 == 0)
{
DrawLine(div,div,0,ActualHeight, MinorLineThickness, Brushes.Black);
DrawLine(0, ActualWidth, div, div, MinorLineThickness, Brushes.Black);
}
else
{
DrawLine(div,div,0,ActualHeight, MicroLineThickness, Brushes.White, -1);
DrawLine(0, ActualWidth, div, div, MicroLineThickness, Brushes.White, -1);
}
}
}

private Line CreateLine(double x1, double x2, double y1, double y2, double thickness)
private void DrawLine(double x1, double x2, double y1, double y2, double thickness, Brush stroke, int zIndex = 0)
{
return new Line
var line = new Line
{
StrokeThickness = thickness,
Stroke = Brushes.Black,
Stroke = stroke,
X1 = x1,
X2 = x2,
Y1 = y1,
Y2 = y2
Y2 = y2,
};
SetZIndex(line, zIndex);
Children.Add(line);
}
}
}
9 changes: 4 additions & 5 deletions PostScriptumMortarCalculator/Models/UserConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ public class UserConfigModel
{
[JsonProperty] public string Theme { get; set; }
[JsonProperty] public string Accent { get; set; }

[JsonProperty] public double Opacity { get; set; }

[JsonProperty] public double IndicatorOpacity { get; set; }
[JsonProperty] public double GridOpacity { get; set; }
[JsonProperty] public string LastMapName { get; set; }

[JsonProperty] public string LastMortarName { get; set; }

public static UserConfigModel Default => new UserConfigModel
{
Theme = "Dark",
Accent = "Lime",
Opacity = 0.25d,
IndicatorOpacity = 0.25d,
GridOpacity = 0.25d
};
}
}
17 changes: 13 additions & 4 deletions PostScriptumMortarCalculator/ViewModels/MapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public class MapViewModel : Screen, IHandle<MortarChangedEvent>

public double MapPixelsPerMeter { get; set; }

public double Opacity { get; set; }
public double IndicatorOpacity { get; set; }

public double GridOpacity { get; set; }

public string MapImageSource => c_RESOURCE_PATH + SelectedMap.MapImagePath;

Expand Down Expand Up @@ -87,7 +89,8 @@ public MapViewModel(IReadOnlyList<MapDataModel> availableMaps,
SelectedMap = string.IsNullOrWhiteSpace(m_configModel.LastMapName)
? AvailableMaps.First()
: AvailableMaps.First(x => x.Name == m_configModel.LastMapName);
Opacity = m_configModel.Opacity;
IndicatorOpacity = m_configModel.IndicatorOpacity;
GridOpacity = m_configModel.GridOpacity;
}

public void OnSelectedMapChanged()
Expand All @@ -99,9 +102,15 @@ public void OnSelectedMapChanged()
m_configService.SerialiseUserConfig();
}

public void OnOpacityChanged()
public void OnIndicatorOpacityChanged()
{
m_configModel.IndicatorOpacity = IndicatorOpacity;
m_configService.SerialiseUserConfig();
}

public void OnGridOpacityChanged()
{
m_configModel.Opacity = Opacity;
m_configModel.GridOpacity = GridOpacity;
m_configService.SerialiseUserConfig();
}

Expand Down
48 changes: 16 additions & 32 deletions PostScriptumMortarCalculator/Views/CalculatorView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@
d:DataContext="{d:DesignInstance viewModels:CalculatorViewModel}"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>

<StackPanel Grid.Column="0" Margin="10 0 10 0" VerticalAlignment="Center">
<StackPanel.Resources>
<Thickness x:Key="ControlMargin" Top="10" />
<Style x:Key="BaseTextBoxStyle" TargetType="TextBox" BasedOn="{StaticResource MahApps.Styles.TextBox}">
<Setter Property="FontSize" Value="20" />
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="mah:TextBoxHelper.UseFloatingWatermark" Value="True"/>
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseTextBoxStyle}"/>
<Style x:Key="LessVisibleStyle" TargetType="TextBox" BasedOn="{StaticResource BaseTextBoxStyle}">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Gray5}"/>
<Setter Property="BorderBrush" Value="{DynamicResource MahApps.Brushes.Gray5}"/>
<Setter Property="Focusable" Value="False"/>
</Style>
<Style x:Key="BaseTextBoxStyle" TargetType="TextBox" BasedOn="{StaticResource MahApps.Styles.TextBox}">
<Setter Property="FontSize" Value="20" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="mah:TextBoxHelper.UseFloatingWatermark" Value="True" />
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseTextBoxStyle}" />
<Style x:Key="LessVisibleStyle" TargetType="TextBox" BasedOn="{StaticResource BaseTextBoxStyle}">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Gray5}" />
<Setter Property="BorderBrush" Value="{DynamicResource MahApps.Brushes.Gray5}" />
<Setter Property="Focusable" Value="False" />
</Style>
</StackPanel.Resources>
<ComboBox ItemsSource="{Binding AvailableMortars}"
SelectedItem="{Binding SelectedMortar}"
Expand All @@ -41,30 +40,15 @@
<TextBox Margin="{StaticResource ControlMargin}"
mah:TextBoxHelper.Watermark="Mortar Position"
Text="{Binding MortarPositionCoordsString, Mode=OneWay}"
Style="{StaticResource LessVisibleStyle}">
<!-- <TextBox.Text> -->
<!-- <MultiBinding StringFormat="{}{0}m | {1}m" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"> -->
<!-- <Binding Path="MortarPositionMeters.X" /> -->
<!-- <Binding Path="MortarPositionMeters.Y" /> -->
<!-- </MultiBinding> -->
<!-- </TextBox.Text> -->
</TextBox>
Style="{StaticResource LessVisibleStyle}" />
<TextBox Margin="{StaticResource ControlMargin}"
mah:TextBoxHelper.Watermark="Target Position X | Y"
mah:TextBoxHelper.Watermark="Target Position"
Text="{Binding TargetPositionCoordsString, Mode=OneWay}"
Style="{StaticResource LessVisibleStyle}">
<!-- <TextBox.Text> -->
<!-- <MultiBinding StringFormat="{}{0}m | {1}m" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"> -->
<!-- <Binding Path="TargetPositionMeters.X" /> -->
<!-- <Binding Path="TargetPositionMeters.Y" /> -->
<!-- </MultiBinding> -->
<!-- </TextBox.Text> -->
</TextBox>
Style="{StaticResource LessVisibleStyle}" />
<TextBox Margin="{StaticResource ControlMargin}"
mah:TextBoxHelper.Watermark="Distance To Target"
Style="{StaticResource LessVisibleStyle}"
Text="{Binding Distance, StringFormat={}{0}m, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="True" />
Text="{Binding Distance, StringFormat={}{0}m, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<Separator Margin="{StaticResource ControlMargin}"
Background="{DynamicResource MahApps.Brushes.Accent}" />
<TextBox Margin="{StaticResource ControlMargin}"
Expand All @@ -80,7 +64,7 @@
FontSize="20"
Command="{s:Action CopyToClipboardClicked}"
mah:ControlsHelper.ContentCharacterCasing="Normal"
Content="Copy to Clipboard"/>
Content="Copy to Clipboard" />
</StackPanel>
</Grid>
</UserControl>
66 changes: 48 additions & 18 deletions PostScriptumMortarCalculator/Views/MapView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,38 @@
xmlns:s="https://github.com/canton7/Stylet"
xmlns:panAndZoom="clr-namespace:Wpf.Controls.PanAndZoom;assembly=Wpf.Controls.PanAndZoom"
xmlns:controls="clr-namespace:PostScriptumMortarCalculator.Controls"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance viewModels:MapViewModel}">
<Grid>
<Grid.Resources>
<Style TargetType="Ellipse" x:Key="AreaStyleEllipse">
<Setter Property="Opacity" Value="{Binding Opacity, Mode=OneWay}" />
<Setter Property="Opacity" Value="{Binding IndicatorOpacity, Mode=OneWay}" />
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
<Style TargetType="DockPanel" x:Key="OpacitySliderDockPanel">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Style.Resources>
<Style TargetType="TextBox" BasedOn="{StaticResource MahApps.Styles.TextBox}">
<Setter Property="Width" Value="40" />
<Setter Property="DockPanel.Dock" Value="Right" />
</Style>
<Style TargetType="Slider" BasedOn="{StaticResource MahApps.Styles.Slider}">
<Setter Property="MinWidth" Value="100"/>
<Setter Property="TickFrequency" Value="0.01"/>
<Setter Property="TickPlacement" Value="None"/>
<Setter Property="IsSnapToTickEnabled" Value="True"/>
<Setter Property="ToolTipService.InitialShowDelay" Value="200"/>
<Setter Property="mah:SliderHelper.EnableMouseWheel" Value="MouseHover"/>
<Setter Property="mah:SliderHelper.ChangeValueBy" Value="LargeChange"/>
<Setter Property="SmallChange" Value="0.01"/>
<Setter Property="LargeChange" Value="0.1"/>
<Setter Property="Minimum" Value="0"/>
<Setter Property="Maximum" Value="1"/>
<Setter Property="Margin" Value="0,0,5,0"/>
</Style>
</Style.Resources>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -24,8 +48,10 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0"
Margin="0 0 20 0"
ItemsSource="{Binding AvailableMaps}"
SelectedItem="{Binding SelectedMap}">
<ComboBox.ItemTemplate>
Expand All @@ -34,18 +60,21 @@
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Label Content="Opacity"
Foreground="{DynamicResource MahApps.Brushes.Accent}" />
<Slider Value="{Binding Opacity}"
MinWidth="100"
Minimum="0.1"
Maximum="1"
SmallChange="0.01"
LargeChange="0.1"
Margin="0,0,5,0" />
<TextBox Text="{Binding Opacity}" Width="40" />
</StackPanel>
<DockPanel Grid.Column="1"
Style="{StaticResource OpacitySliderDockPanel}"
Margin="20 0 20 0">
<TextBox Text="{Binding GridOpacity}"/>
<Slider Value="{Binding GridOpacity}"
ToolTip="Grid Opacity"
Minimum="0" />
</DockPanel>
<DockPanel Grid.Column="2"
Style="{StaticResource OpacitySliderDockPanel}"
Margin="20 0 0 0">
<TextBox Text="{Binding IndicatorOpacity}"/>
<Slider Value="{Binding IndicatorOpacity}"
ToolTip="Indicator Opacity" />
</DockPanel>
</Grid>
<panAndZoom:ZoomBorder Grid.Row="1"
Margin="0 10 0 0"
Expand All @@ -67,9 +96,10 @@
<controls:GridOverlayControl Width="{Binding ElementName=Image, Path=ActualWidth}"
Height="{Binding ElementName=Image, Path=ActualHeight}"
MapPixelsPerMeter="{Binding MapPixelsPerMeter}"
MajorLineThickness="5"
MinorLineThickness="3"
MicroLineThickness="1"/>
MajorLineThickness="6"
MinorLineThickness="2"
MicroLineThickness="1"
Opacity="{Binding GridOpacity}" />
<Image x:Name="Image"
Canvas.ZIndex="-10"
Source="{Binding MapImageSource, IsAsync=True}"
Expand All @@ -90,7 +120,7 @@
</Ellipse.RenderTransform>
</Ellipse>
<Path Fill="Blue"
Opacity="{Binding Opacity}"
Opacity="{Binding IndicatorOpacity}"
Visibility="{Binding IsMortarPositionSet, Converter={StaticResource BooleanToVisibility}}"
IsHitTestVisible="False">
<Path.Data>
Expand Down Expand Up @@ -130,7 +160,7 @@
X2="{Binding TargetPositionPixels.X}"
Y2="{Binding TargetPositionPixels.Y}"
IsHitTestVisible="False"
Opacity="{Binding Opacity}"
Opacity="{Binding IndicatorOpacity}"
Visibility="{Binding IsMortarAndTargetSet, Converter={StaticResource BooleanToVisibility}}"
StrokeThickness="5"
Stroke="Red" />
Expand Down

0 comments on commit a870d08

Please sign in to comment.