diff --git a/Mappy/Models/FeatureViewViewModel.cs b/Mappy/Models/FeatureViewViewModel.cs index 1531593..a720ec0 100644 --- a/Mappy/Models/FeatureViewViewModel.cs +++ b/Mappy/Models/FeatureViewViewModel.cs @@ -27,7 +27,9 @@ public sealed class FeatureViewViewModel : ISectionViewViewModel, IDisposable private readonly FeatureService featureService; - public FeatureViewViewModel(FeatureService featureService) + private readonly Dispatcher dispatcher; + + public FeatureViewViewModel(FeatureService featureService, Dispatcher dispatcher) { featureService.FeaturesChanged += this.OnFeaturesChanged; @@ -58,6 +60,7 @@ public FeatureViewViewModel(FeatureService featureService) .Subscribe(_ => this.UpdateFeatures()); this.featureService = featureService; + this.dispatcher = dispatcher; } public IObservable ComboBox1Model => this.worlds; @@ -76,6 +79,11 @@ public void SelectComboBox2Item(int index) this.selectCategoryEvent.OnNext(index); } + public void SetSelectedItem(string featureName) + { + this.dispatcher.SetSelectedFeature(featureName); + } + public void Dispose() { this.worlds.Dispose(); diff --git a/Mappy/Models/IMapViewViewModel.cs b/Mappy/Models/IMapViewViewModel.cs index 74f90de..43d42d0 100644 --- a/Mappy/Models/IMapViewViewModel.cs +++ b/Mappy/Models/IMapViewViewModel.cs @@ -20,7 +20,9 @@ public interface IMapViewViewModel IObservable ViewportLocation { get; } - void MouseDown(Point location); + void MouseLeftDown(Point location); + + void MouseRightDown(Point location); void MouseMove(Point locattion); diff --git a/Mappy/Models/ISectionViewViewModel.cs b/Mappy/Models/ISectionViewViewModel.cs index d804bb8..9237930 100644 --- a/Mappy/Models/ISectionViewViewModel.cs +++ b/Mappy/Models/ISectionViewViewModel.cs @@ -14,5 +14,7 @@ public interface ISectionViewViewModel void SelectComboBox1Item(int index); void SelectComboBox2Item(int index); + + void SetSelectedItem(string selectedItemName); } } \ No newline at end of file diff --git a/Mappy/Models/MapViewViewModel.cs b/Mappy/Models/MapViewViewModel.cs index 5c365e7..7738cad 100644 --- a/Mappy/Models/MapViewViewModel.cs +++ b/Mappy/Models/MapViewViewModel.cs @@ -186,7 +186,7 @@ public void ScrollPositionChanged(Point position) this.dispatcher.SetViewportLocation(position); } - public void MouseDown(Point location) + public void MouseLeftDown(Point location) { this.mouseDown = true; this.lastMousePos = location; @@ -207,6 +207,22 @@ public void MouseDown(Point location) } } + public void MouseRightDown(Point location) + { + this.mouseDown = true; + this.lastMousePos = location; + + if (!this.itemsLayer.Value.IsInSelection(location.X, location.Y) && + this.featureService.SelectedFeature != null) + { + this.dispatcher.DragDropFeature( + this.featureService.SelectedFeature.Name, + location.X, + location.Y + ); + } + } + public void MouseMove(Point location) { this.dispatcher.UpdateMousePosition(Maybe.Return(location)); diff --git a/Mappy/Models/SectionViewViewModel.cs b/Mappy/Models/SectionViewViewModel.cs index 1725652..6eb0729 100644 --- a/Mappy/Models/SectionViewViewModel.cs +++ b/Mappy/Models/SectionViewViewModel.cs @@ -22,7 +22,9 @@ public sealed class SectionViewViewModel : ISectionViewViewModel, IDisposable private readonly SectionService sectionService; - public SectionViewViewModel(SectionService sectionService) + private readonly Dispatcher dispatcher; + + public SectionViewViewModel(SectionService sectionService, Dispatcher dispatcher) { sectionService.SectionsChanged += this.OnSectionsChanged; @@ -44,6 +46,7 @@ public SectionViewViewModel(SectionService sectionService) }); this.sectionService = sectionService; + this.dispatcher = dispatcher; } public IObservable ComboBox1Model => this.worlds; @@ -62,6 +65,11 @@ public void SelectComboBox2Item(int index) this.selectCategoryEvent.OnNext(index); } + public void SetSelectedItem(string sectionName) + { + // this.dispatcher.SetSelectedSection(sectionName); + } + public void Dispose() { this.worlds.Dispose(); diff --git a/Mappy/Program.cs b/Mappy/Program.cs index 662246a..3839b2d 100644 --- a/Mappy/Program.cs +++ b/Mappy/Program.cs @@ -67,8 +67,8 @@ public static void Main() tileCache); mainForm.SetModel(new MainFormViewModel(model, dispatcher, featureService)); - mainForm.SectionView.SetModel(new SectionViewViewModel(sectionsService)); - mainForm.FeatureView.SetModel(new FeatureViewViewModel(featureService)); + mainForm.SectionView.SetModel(new SectionViewViewModel(sectionsService, dispatcher)); + mainForm.FeatureView.SetModel(new FeatureViewViewModel(featureService, dispatcher)); mainForm.MapViewPanel.SetModel(new MapViewViewModel(model, dispatcher, featureService)); diff --git a/Mappy/Services/Dispatcher.cs b/Mappy/Services/Dispatcher.cs index 71c8bd2..5477882 100644 --- a/Mappy/Services/Dispatcher.cs +++ b/Mappy/Services/Dispatcher.cs @@ -545,6 +545,15 @@ public void SelectStartPosition(int index) this.model.Map.IfSome(x => x.SelectStartPosition(index)); } + public void SetSelectedFeature(string featureName) + { + var featureFromTag = this.featureService.TryGetFeature(featureName); + if (featureFromTag.HasValue) + { + this.featureService.SelectedFeature = featureFromTag.UnsafeValue; + } + } + private static IEnumerable GetMapNames(HpiArchive hpi) { return hpi.GetFiles("maps") diff --git a/Mappy/Services/FeatureService.cs b/Mappy/Services/FeatureService.cs index f8c6bbc..2ba8ab8 100644 --- a/Mappy/Services/FeatureService.cs +++ b/Mappy/Services/FeatureService.cs @@ -30,6 +30,8 @@ public FeatureService() public event EventHandler FeaturesChanged; + public Feature SelectedFeature { get; set; } + public void AddFeatures(IEnumerable features) { foreach (var f in features) @@ -88,31 +90,31 @@ private static OffsetBitmap LoadRenderFile(HpiArchive archive, HpiArchive.FileIn private static OffsetBitmap LoadBitmap(GafEntry[] gaf, string sequenceName) { - var entry = gaf.FirstOrDefault( - x => string.Equals(x.Name, sequenceName, StringComparison.OrdinalIgnoreCase)); - if (entry == null) - { - // skip if the sequence is not in this gaf file - return null; - } + var entry = gaf.FirstOrDefault( + x => string.Equals(x.Name, sequenceName, StringComparison.OrdinalIgnoreCase)); + if (entry == null) + { + // skip if the sequence is not in this gaf file + return null; + } - var frame = entry.Frames[0]; + var frame = entry.Frames[0]; - Bitmap bmp; - if (frame.Data == null || frame.Width == 0 || frame.Height == 0) - { - bmp = new Bitmap(50, 50); - } - else - { - bmp = BitmapConvert.ToBitmap( - frame.Data, - frame.Width, - frame.Height, - frame.TransparencyIndex); - } + Bitmap bmp; + if (frame.Data == null || frame.Width == 0 || frame.Height == 0) + { + bmp = new Bitmap(50, 50); + } + else + { + bmp = BitmapConvert.ToBitmap( + frame.Data, + frame.Width, + frame.Height, + frame.TransparencyIndex); + } - return new OffsetBitmap(-frame.OffsetX, -frame.OffsetY, bmp); + return new OffsetBitmap(-frame.OffsetX, -frame.OffsetY, bmp); } private static GafEntry[] LoadGafEntries(HpiArchive.FileInfo fileInfo, HpiArchive archive) @@ -193,16 +195,16 @@ private IEnumerable EnumerateFeaturesFromInternal(IEnumerable EnumerateFeaturesFromInternal(IEnumerable this.comboBox1; @@ -14,5 +22,34 @@ public DoubleComboListView() public ComboBox ComboBox2 => this.comboBox2; public ListView ListView => this.listView1; + + private void ListView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) + { + e.DrawDefault = true; + } + + private void ListView1_DrawItem(object sender, DrawListViewItemEventArgs e) + { + e.DrawDefault = true; + if (e.Item.Selected) + { + e.Graphics.DrawRectangle(this.selectionPen, e.Bounds); + this.previousSelection = e.Item; + } + else if (sender is ListView && ((ListView)sender).SelectedItems.Count <= 0 && + this.previousSelection != null && this.previousSelection.Index == e.Item.Index) + { + e.Graphics.DrawRectangle(this.selectionPen, this.previousSelection.Bounds); + } + else + { + e.Graphics.DrawRectangle(this.defaultPen, e.Bounds); + } + } + + private void ListView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) + { + this.ListView.Refresh(); + } } } diff --git a/Mappy/UI/Controls/MapViewPanel.cs b/Mappy/UI/Controls/MapViewPanel.cs index d9fa18f..f0894f6 100644 --- a/Mappy/UI/Controls/MapViewPanel.cs +++ b/Mappy/UI/Controls/MapViewPanel.cs @@ -44,7 +44,14 @@ private void MapViewDragDrop(object sender, DragEventArgs e) private void MapViewMouseDown(object sender, MouseEventArgs e) { var loc = this.mapView.ToVirtualPoint(e.Location); - this.model.MouseDown(loc); + if (e.Button == MouseButtons.Left) + { + this.model.MouseLeftDown(loc); + } + else if (e.Button == MouseButtons.Right) + { + this.model.MouseRightDown(loc); + } } private void MapViewMouseMove(object sender, MouseEventArgs e) diff --git a/Mappy/UI/Controls/SectionView.cs b/Mappy/UI/Controls/SectionView.cs index b9a1229..4c17ba1 100644 --- a/Mappy/UI/Controls/SectionView.cs +++ b/Mappy/UI/Controls/SectionView.cs @@ -18,6 +18,8 @@ public partial class SectionView : UserControl private bool suppressCombo1SelectedItemEvents; private bool suppressCombo2SelectedItemEvents; + private ListViewItem previousSelection; + public SectionView() { this.InitializeComponent(); @@ -25,6 +27,7 @@ public SectionView() this.control.ComboBox1.SelectedIndexChanged += this.ComboBox1SelectedIndexChanged; this.control.ComboBox2.SelectedIndexChanged += this.ComboBox2SelectedIndexChanged; this.control.ListView.ItemDrag += this.ListViewItemDrag; + this.control.ListView.SelectedIndexChanged += this.ListViewItemSelectionChanged; } public Size ImageSize { get; set; } = new Size(128, 128); @@ -134,5 +137,36 @@ private void ListViewItemDrag(object sender, ItemDragEventArgs e) var data = view.SelectedItems[0].Tag; view.DoDragDrop(data, DragDropEffects.Copy); } + + private void ListViewItemSelectionChanged(object sender, EventArgs e) + { + var view = (ListView)sender; + if (view.SelectedItems.Count == 0) + { + return; + } + + view.ItemSelectionChanged -= this.ListViewItemSelectionChanged; + var selItem = view.SelectedItems[0]; + + if (this.previousSelection == null || + this.previousSelection.Index < 0 || + view.Items[this.previousSelection.Index] == null) + { + this.previousSelection = selItem; + view.Items[selItem.Index].Selected = true; + this.model.SetSelectedItem(selItem.Text); + } + + if (selItem != this.previousSelection) + { + view.Items[this.previousSelection.Index].Selected = false; + view.Items[selItem.Index].Selected = true; + this.previousSelection = selItem; + this.model.SetSelectedItem(selItem.Text); + } + + view.ItemSelectionChanged += this.ListViewItemSelectionChanged; + } } }