|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Numerics; |
| 4 | +using System.Windows.Controls; |
| 5 | +using System.Windows.Media; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Shapes; |
| 8 | +using System.Windows.Media.Imaging; |
| 9 | +using AnnoMapEditor.Models; |
| 10 | + |
| 11 | +namespace AnnoMapEditor.Controls |
| 12 | +{ |
| 13 | + public partial class MapObject : UserControl |
| 14 | + { |
| 15 | + static readonly Dictionary<string, SolidColorBrush> MapObjectColors = new() |
| 16 | + { |
| 17 | + ["Normal"] = new(Color.FromArgb(255, 8, 172, 137)), |
| 18 | + ["Starter"] = new(Color.FromArgb(255, 130, 172, 8)), |
| 19 | + ["ThirdParty"] = new(Color.FromArgb(255, 189, 73, 228)), |
| 20 | + ["Decoration"] = new(Color.FromArgb(255, 151, 162, 125)), |
| 21 | + ["PirateIsland"] = new(Color.FromArgb(255, 186, 0, 36)), |
| 22 | + ["Cliff"] = new(Color.FromArgb(255, 103, 105, 114)) |
| 23 | + }; |
| 24 | + static readonly Dictionary<IslandType, int> ZIndex = new() |
| 25 | + { |
| 26 | + [IslandType.Normal] = 3, |
| 27 | + [IslandType.Starter] = 2, |
| 28 | + [IslandType.ThirdParty] = 4, |
| 29 | + [IslandType.Decoration] = 1, |
| 30 | + [IslandType.PirateIsland] = 4, |
| 31 | + [IslandType.Cliff] = 0 |
| 32 | + }; |
| 33 | + static readonly SolidColorBrush White = new(Color.FromArgb(255, 255, 255, 255)); |
| 34 | + static readonly SolidColorBrush Yellow = new(Color.FromArgb(255, 234, 224, 83)); |
| 35 | + readonly Session session; |
| 36 | + |
| 37 | + public MapObject(Session session) |
| 38 | + { |
| 39 | + InitializeComponent(); |
| 40 | + |
| 41 | + this.session = session; |
| 42 | + DataContextChanged += MapObject_DataContextChanged; |
| 43 | + } |
| 44 | + |
| 45 | + private async void MapObject_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) |
| 46 | + { |
| 47 | + if (DataContext is not Island island) |
| 48 | + return; |
| 49 | + |
| 50 | + if (island.ElementType == 2) |
| 51 | + { |
| 52 | + var circle = new Ellipse() |
| 53 | + { |
| 54 | + Width = 20, |
| 55 | + Height = 20, |
| 56 | + Fill = Yellow, |
| 57 | + }; |
| 58 | + |
| 59 | + Canvas.SetLeft(circle, -10); |
| 60 | + Canvas.SetTop(circle, -10); |
| 61 | + canvas.Children.Add(circle); |
| 62 | + |
| 63 | + Width = 1; |
| 64 | + Height = 1; |
| 65 | + Canvas.SetLeft(this, island.Position.X); |
| 66 | + Canvas.SetTop(this, session.Size.Y - island.Position.Y); |
| 67 | + Panel.SetZIndex(this, 100); |
| 68 | + return; |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + |
| 73 | + Width = island.SizeInTiles; |
| 74 | + Height = island.SizeInTiles; |
| 75 | + Canvas.SetLeft(this, island.Position.X); |
| 76 | + Canvas.SetTop(this, session.Size.Y - island.Position.Y - island.SizeInTiles); |
| 77 | + Panel.SetZIndex(this, ZIndex[island.Type]); |
| 78 | + |
| 79 | + Image? image = null; |
| 80 | + if (island.ImageFile != null) |
| 81 | + { |
| 82 | + image = new(); |
| 83 | + BitmapImage? png = null; |
| 84 | + await System.Threading.Tasks.Task.Run(() => |
| 85 | + { |
| 86 | + png = new(); |
| 87 | + try |
| 88 | + { |
| 89 | + using var stream = File.OpenRead(island.ImageFile); |
| 90 | + png.BeginInit(); |
| 91 | + png.StreamSource = stream; |
| 92 | + png.CacheOption = BitmapCacheOption.OnLoad; |
| 93 | + png.EndInit(); |
| 94 | + png.Freeze(); |
| 95 | + } |
| 96 | + catch |
| 97 | + { |
| 98 | + png = null; |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + image.Width = island.SizeInTiles; |
| 103 | + image.Height = island.SizeInTiles; |
| 104 | + image.RenderTransform = new RotateTransform(island.Rotation * -90); |
| 105 | + image.RenderTransformOrigin = new Point(0.5, 0.5); |
| 106 | + image.Source = png; |
| 107 | + canvas.Children.Add(image); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + Rectangle rect = new(); |
| 112 | + if (image is not null) |
| 113 | + { |
| 114 | + rect.Stroke = MapObjectColors[island.Type.ToString()]; |
| 115 | + rect.StrokeThickness = 5; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + rect.Fill = MapObjectColors[island.Type.ToString()]; |
| 120 | + } |
| 121 | + rect.Width = island.SizeInTiles; |
| 122 | + rect.Height = island.SizeInTiles; |
| 123 | + canvas.Children.Add(rect); |
| 124 | + |
| 125 | + var circle = new Ellipse() |
| 126 | + { |
| 127 | + Width = 10, |
| 128 | + Height = 10, |
| 129 | + Fill = White, |
| 130 | + }; |
| 131 | + |
| 132 | + Canvas.SetLeft(circle, 0); |
| 133 | + Canvas.SetTop(circle, island.SizeInTiles - 10); |
| 134 | + canvas.Children.Add(circle); |
| 135 | + |
| 136 | + if (!string.IsNullOrEmpty(island.Label)) |
| 137 | + title.Text = island.Label; |
| 138 | + else if (island.Type == IslandType.PirateIsland) |
| 139 | + title.Text = "Pirate"; |
| 140 | + else if (island.Type == IslandType.ThirdParty) |
| 141 | + title.Text = "3rd"; |
| 142 | + else if (island.IsPool) |
| 143 | + { |
| 144 | + title.Text = island.Size.ToString(); |
| 145 | + if (island.Type == IslandType.Starter) |
| 146 | + title.Text = "Starter\n" + title.Text; |
| 147 | + } |
| 148 | + else |
| 149 | + title.Text = ""; |
| 150 | + |
| 151 | + titleBackground.Visibility = title.Text == "" ? Visibility.Collapsed : Visibility.Visible; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments