feat: add map drawing

This commit is contained in:
k0t9i 2023-02-02 21:54:08 +04:00
parent c35f4e317a
commit d03f37fbf7
275 changed files with 469 additions and 10 deletions

View File

@ -0,0 +1,58 @@
<ContentControl x:Class="Client.Application.Components.Map"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client.Application.Components"
xmlns:services="clr-namespace:Client.Application.Services"
mc:Ignorable="d"
services:SizeObserver.Observe="True"
services:SizeObserver.ObservedWidth="{Binding ViewportWidth, Mode=OneWayToSource}"
services:SizeObserver.ObservedHeight="{Binding ViewportHeight, Mode=OneWayToSource}"
>
<Grid>
<ItemsControl ItemsSource="{Binding Path=Blocks}">
<ItemsControl.Resources>
<services:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image
Source="{Binding ImageSource,Mode=OneWay}"
Width="{Binding Size,Mode=OneWay}"
Height="{Binding Size,Mode=OneWay}"
Visibility="{Binding Visible,Converter={StaticResource BooleanToVisibilityConverter}}"
>
<Image.RenderTransform>
<TranslateTransform X="{Binding DeltaX}" Y="{Binding DeltaY}"/>
</Image.RenderTransform>
</Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="200" Height="20" Margin="0 0 5 5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="White" CornerRadius="3" Margin="0 0 5 0">
<TextBlock Text="{Binding Scale, Mode=OneWay}" FontSize="14" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
<Slider
TickFrequency="1"
IsSnapToTickEnabled="True"
Value="{Binding Scale}"
Maximum="32"
Minimum="1"
Grid.Column="1"
VerticalAlignment="Center"
/>
</Grid>
<Ellipse Width="10" Height="10" Fill="White" Stroke="Black" StrokeThickness="2" VerticalAlignment="Center" HorizontalAlignment="Center"></Ellipse>
</Grid>
</ContentControl>

View File

@ -0,0 +1,28 @@
using Client.Application.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Client.Application.Components
{
/// <summary>
/// Interaction logic for Map.xaml
/// </summary>
public partial class Map : ContentControl
{
public Map()
{
InitializeComponent();
}
}
}

View File

@ -4,14 +4,14 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client.Application.Components"
xmlns:converters="clr-namespace:Client.Application.Converters"
xmlns:services="clr-namespace:Client.Application.Services"
mc:Ignorable="d"
IsEnabled="{Binding Path=IsButtonActive,ElementName=root,Mode=OneWay}"
ToolTipService.ShowOnDisabled="True"
x:Name="root">
<Button.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<services:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<services:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<DataTemplate x:Key="ToolTipContent">
<StackPanel MaxWidth="300">
<TextBlock TextWrapping="Wrap" FontWeight="Bold" FontSize="14" Margin="0,0,0,5">

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace Client.Application.Converters
namespace Client.Application.Services
{
public class BooleanToVisibilityConverter : IValueConverter
{

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace Client.Application.Converters
namespace Client.Application.Services
{
public class NullToVisibilityConverter : IValueConverter
{

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Client.Application.Services
{
public static class SizeObserver
{
public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached(
"Observe",
typeof(bool),
typeof(SizeObserver),
new FrameworkPropertyMetadata(OnObserveChanged));
public static readonly DependencyProperty ObservedWidthProperty = DependencyProperty.RegisterAttached(
"ObservedWidth",
typeof(double),
typeof(SizeObserver));
public static readonly DependencyProperty ObservedHeightProperty = DependencyProperty.RegisterAttached(
"ObservedHeight",
typeof(double),
typeof(SizeObserver));
public static bool GetObserve(FrameworkElement frameworkElement)
{
return (bool)frameworkElement.GetValue(ObserveProperty);
}
public static void SetObserve(FrameworkElement frameworkElement, bool observe)
{
frameworkElement.SetValue(ObserveProperty, observe);
}
public static double GetObservedWidth(FrameworkElement frameworkElement)
{
return (double)frameworkElement.GetValue(ObservedWidthProperty);
}
public static void SetObservedWidth(FrameworkElement frameworkElement, double observedWidth)
{
frameworkElement.SetValue(ObservedWidthProperty, observedWidth);
}
public static double GetObservedHeight(FrameworkElement frameworkElement)
{
return (double)frameworkElement.GetValue(ObservedHeightProperty);
}
public static void SetObservedHeight(FrameworkElement frameworkElement, double observedHeight)
{
frameworkElement.SetValue(ObservedHeightProperty, observedHeight);
}
private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var frameworkElement = (FrameworkElement)dependencyObject;
if ((bool)e.NewValue)
{
frameworkElement.Loaded += OnFrameworkElementLoaded;
}
else
{
frameworkElement.SizeChanged -= OnFrameworkElementSizeChanged;
}
}
private static void OnFrameworkElementLoaded(object sender, RoutedEventArgs e)
{
var frameworkElement = (FrameworkElement)sender;
frameworkElement.SizeChanged += OnFrameworkElementSizeChanged;
frameworkElement.Loaded -= OnFrameworkElementLoaded;
UpdateObservedSizesForFrameworkElement(frameworkElement);
}
private static void OnFrameworkElementSizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateObservedSizesForFrameworkElement((FrameworkElement)sender);
}
private static void UpdateObservedSizesForFrameworkElement(FrameworkElement frameworkElement)
{
frameworkElement.SetCurrentValue(ObservedWidthProperty, frameworkElement.ActualWidth);
frameworkElement.SetCurrentValue(ObservedHeightProperty, frameworkElement.ActualHeight);
}
}
}

View File

@ -34,14 +34,18 @@ namespace Client.Application.ViewModels
{
Hero = new HeroSummaryInfoViewModel(@event.Hero);
hero = @event.Hero;
Map.Hero = hero;
OnPropertyChanged("Hero");
OnPropertyChanged("Map");
}
public void Handle(HeroDeletedEvent @event)
{
Hero = null;
hero = null;
Map.Hero = null;
OnPropertyChanged("Hero");
OnPropertyChanged("Map");
}
public void Handle(CreatureCreatedEvent @event)
@ -116,6 +120,7 @@ namespace Client.Application.ViewModels
public ObservableCollection<SkillListViewModel> PassiveSkills { get; } = new ObservableCollection<SkillListViewModel>();
public ObservableCollection<BaseItem> Items { get; } = new ObservableCollection<BaseItem>();
public HeroSummaryInfoViewModel? Hero { get; private set; }
public MapViewModel Map { get; private set; } = new MapViewModel();
public Hero? hero;
}
}

View File

@ -0,0 +1,58 @@
using Client.Domain.Common;
using Client.Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace Client.Application.ViewModels
{
public class MapBlockViewModel : ObservableObject
{
public string ImageSource => "/Assets/maps/" + mapBlock.BlockX + "_" + mapBlock.BlockY + ".jpg";
public float DeltaX => mapBlock.DeltaX;
public float DeltaY => mapBlock.DeltaY;
public float Size => mapBlock.Size;
public bool Visible
{
get => visible;
set
{
if (visible != value)
{
visible = value;
OnPropertyChanged();
}
}
}
public MapBlock MapBlock => mapBlock;
public MapBlockViewModel(MapBlock mapBlock)
{
this.mapBlock = mapBlock;
mapBlock.PropertyChanged += MapBlock_PropertyChanged;
}
private void MapBlock_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "DeltaX")
{
OnPropertyChanged("DeltaX");
}
if (e.PropertyName == "DeltaY")
{
OnPropertyChanged("DeltaY");
}
if (e.PropertyName == "Size")
{
OnPropertyChanged("Size");
}
}
private readonly MapBlock mapBlock;
private bool visible = true;
}
}

View File

@ -0,0 +1,122 @@
using Client.Domain.Common;
using Client.Domain.Entities;
using Client.Domain.Service;
using Client.Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
namespace Client.Application.ViewModels
{
public class MapViewModel : ObservableObject
{
public Hero? Hero
{
get => hero;
set
{
if (hero != value)
{
if (hero != null)
{
hero.Transform.Position.PropertyChanged -= HeroPosition_PropertyChanged;
}
hero = value;
if (hero != null)
{
hero.Transform.Position.PropertyChanged += HeroPosition_PropertyChanged;
}
UpdateMap();
}
}
}
private void HeroPosition_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
UpdateMap();
}
private void UpdateMap()
{
foreach (var block in Blocks)
{
block.Visible = false;
}
if (hero != null)
{
var blocks = selector.SelectImages((float)ViewportWidth, (float)ViewportHeight, hero.Transform.Position, Scale);
foreach (var block in blocks)
{
if (this.blocks.ContainsKey(block.Id))
{
this.blocks[block.Id].MapBlock.DeltaX = block.DeltaX;
this.blocks[block.Id].MapBlock.DeltaY = block.DeltaY;
this.blocks[block.Id].MapBlock.Size = block.Size;
}
else
{
var model = new MapBlockViewModel(block);
this.blocks.Add(block.Id, model);
Blocks.Add(model);
}
this.blocks[block.Id].Visible = true;
}
}
}
public ObservableCollection<MapBlockViewModel> Blocks { get; } = new ObservableCollection<MapBlockViewModel>();
public double ViewportWidth
{
get => viewportWidth;
set
{
if (viewportWidth != value)
{
viewportWidth = value;
UpdateMap();
}
}
}
public double ViewportHeight
{
get => viewportHeight;
set
{
if (viewportHeight != value)
{
viewportHeight = value;
UpdateMap();
}
}
}
public float Scale
{
get => scale;
set
{
if (scale != value)
{
scale = value;
UpdateMap();
OnPropertyChanged();
}
}
}
MapImageSelector selector = new MapImageSelector();
Dictionary<uint, MapBlockViewModel> blocks = new Dictionary<uint, MapBlockViewModel>();
private Hero? hero;
private float scale = 8;
private double viewportWidth = 50;
private double viewportHeight = 50;
}
}

View File

@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:converters="clr-namespace:Client.Application.Converters"
xmlns:services="clr-namespace:Client.Application.Services"
xmlns:components="clr-namespace:Client.Application.Components"
xmlns:views="clr-namespace:Client.Application.Views"
mc:Ignorable="d"
@ -21,7 +21,7 @@
<scm:SortDescription PropertyName="Distance" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<services:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
@ -39,6 +39,7 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<components:Map DataContext="{Binding Map}" />
<TabControl Grid.Row="0" Grid.Column="1">
<TabItem>
<TabItem.Header>Environment</TabItem.Header>
@ -128,8 +129,8 @@
<DataTemplate>
<Button>
<Button.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<services:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<services:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<DataTemplate x:Key="ToolTipContent">
<StackPanel MaxWidth="300">
<TextBlock TextWrapping="Wrap" FontWeight="Bold" FontSize="14" Margin="0,0,0,5">

View File

@ -1,4 +1,5 @@
using Client.Application.ViewModels;
using Client.Application.Components;
using Client.Application.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
@ -21,11 +22,14 @@ namespace Client.Application.Views
/// </summary>
public partial class MainWindow : Window
{
private readonly MainViewModel mainViewModel;
public MainWindow(MainViewModel mainViewModel)
{
InitializeComponent();
DataContext = mainViewModel;
this.mainViewModel = mainViewModel;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 596 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 519 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 734 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 666 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 531 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 634 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 601 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 565 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 672 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 542 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 576 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 589 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 628 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 550 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Some files were not shown because too many files have changed in this diff Show More