Skip to content

Commit

Permalink
Fix drag-reordering of search filters
Browse files Browse the repository at this point in the history
Includes minor clean-ups.

Closes #10
  • Loading branch information
coldino committed Jan 27, 2018
1 parent 53cdf62 commit 6e1c3e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
22 changes: 7 additions & 15 deletions LarkatorGUI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
xmlns:sysCol="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
x:Name="root"
SourceInitialized="Window_SourceInitialized" Loaded="Window_Loaded"
Title="{Binding WindowTitle}" Background="{DynamicResource WindowBackgroundBrush}"
MaxHeight="1000" MinHeight="240"
MaxHeight="1016" MinHeight="240"
Left="{Binding Source={StaticResource Settings}, Path=Default.MainWindowLeft, Mode=TwoWay}"
Top="{Binding Source={StaticResource Settings}, Path=Default.MainWindowTop, Mode=TwoWay}"
Width="{Binding Source={StaticResource Settings}, Path=Default.MainWindowWidth, Mode=TwoWay}"
Expand All @@ -28,16 +29,6 @@
<local:GenderToImageConverter x:Key="GenderToImageConverter"/>
<local:GenderToLetterConverter x:Key="GenderToLetterConverter"/>
<SolidColorBrush x:Key="MaleSymbolBrush" Color="#FF050C93"/>
<sysCol:ArrayList x:Key="DummyData">
<common:Dino Name="Dummy" BaseLevel="100" Female="False" Id="213412341234" Type="Allosaurus">
<common:Dino.Location>
<common:Position Lat="10" Lon="20"/>
</common:Dino.Location>
<common:Dino.WildLevels>
<common:StatPoints Health="1" Melee="2"/>
</common:Dino.WildLevels>
</common:Dino>
</sysCol:ArrayList>
<DataTemplate x:Key="SearchCriteriaTemplate" DataType="{x:Type common:SearchCriteria}">
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -142,7 +133,7 @@
<Grid/>
</DataTemplate>
</Window.Resources>
<DockPanel x:Name="root">
<DockPanel>
<Border x:Name="statusPanel" DockPanel.Dock="Bottom" Background="#33000000" Padding="8,6,8,3">
<Grid Height="16" TextBlock.Foreground="AntiqueWhite">
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -192,7 +183,8 @@
Background="Transparent" BorderThickness="0"
HorizontalAlignment="Stretch" HorizontalContentAlignment="Center"
SelectionChanged="Searches_SelectionChanged" SelectionMode="Single"
lvl:ListViewLayoutManager.Enabled="True" Padding="-1" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding ElementName=root}">
lvl:ListViewLayoutManager.Enabled="True" Padding="-1"
dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding ElementName=root}">
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed" />
Expand Down Expand Up @@ -356,7 +348,7 @@
<Grid>
<Image x:Name="image" Source="{Binding MapImage}" Width="1000" Height="1000" />
<Canvas>
<ItemsControl x:Name="map" ItemTemplate="{DynamicResource MapPinTemplate}" ItemsSource="{Binding ListResults}" d:DataContext="{StaticResource DummyData}"/>
<ItemsControl x:Name="map" ItemTemplate="{DynamicResource MapPinTemplate}" ItemsSource="{Binding ListResults}"/>
</Canvas>
<fa:ImageAwesome Visibility="{Binding IsLoading, Converter={StaticResource BoolToVisibilityConverter}}"
Icon="cog" Spin="True" SpinDuration="3" Width="360" Height="360" Foreground="#88ffffff"/>
Expand Down Expand Up @@ -398,7 +390,7 @@
<TextBlock Grid.Column="7" Text="Sp" TextAlignment="Center"/>
</Grid>

<ItemsControl x:Name="resultsList" TextBlock.FontSize="16" ItemTemplate="{DynamicResource DinoTemplate}" ItemsSource="{Binding ListResults}" d:DataContext="{StaticResource DummyData}"/>
<ItemsControl x:Name="resultsList" TextBlock.FontSize="16" ItemTemplate="{DynamicResource DinoTemplate}" ItemsSource="{Binding ListResults}"/>

</StackPanel>
</DockPanel>
Expand Down
39 changes: 22 additions & 17 deletions LarkatorGUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,7 @@ private void UpdateCurrentSearch()

void IDropTarget.DragOver(IDropInfo dropInfo)
{
SearchCriteria sourceItem = dropInfo.Data as SearchCriteria;
SearchCriteria targetItem = dropInfo.TargetItem as SearchCriteria;

if (sourceItem != null && targetItem != null)
if (dropInfo.TargetItem is SearchCriteria targetItem && dropInfo.Data is SearchCriteria sourceItem)
{
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
dropInfo.Effects = DragDropEffects.Move;
Expand All @@ -639,23 +636,31 @@ void IDropTarget.Drop(IDropInfo dropInfo)

// Try to figure out the other item to insert between, or pick a boundary
var options = ListSearches
.OrderBy(sc => sc.Group).ThenBy(sc => sc.Order).ThenBy(sc => sc.Species).ThenBy(sc => sc.MinLevel).ThenBy(sc => sc.MaxLevel)
.Where(sc => (ip == RelativeInsertPosition.BeforeTargetItem) ? sc.Order < targetItem.Order : sc.Order > targetItem.Order).ToArray();
.Where(sc => sc != sourceItem)
.Where(sc => sc.Group == targetItem.Group)
.OrderBy(sc => sc.Order)
.ToArray();

double bound;
if (options.Length > 0)
{
var otherItem = (ip == RelativeInsertPosition.BeforeTargetItem) ? options.Last() : options.First();
bound = otherItem.Order;
}
else
var above = options.Where(sc => sc.Order < targetItem.Order).OrderByDescending(sc => sc.Order).FirstOrDefault();
var below = options.Where(sc => sc.Order > targetItem.Order).OrderBy(sc => sc.Order).FirstOrDefault();

var aboveOrder = (above == null) ? options.Min(sc => sc.Order) - 1 : above.Order;
var belowOrder = (below == null) ? options.Max(sc => sc.Order) + 1 : below.Order;

// Update the order to be mid-way between either above or below, based on drag insert position
sourceItem.Order = (targetItem.Order + (ip.HasFlag(RelativeInsertPosition.AfterTargetItem) ? belowOrder : aboveOrder)) / 2;

// Renumber the results
var orderedSearches = ListSearches
.OrderBy(sc => sc.Group)
.ThenBy(sc => sc.Order)
.ToArray();

for (var i = 0; i < orderedSearches.Length; i++)
{
bound = targetItem.Order + ((ip == RelativeInsertPosition.BeforeTargetItem) ? -100 : 100);
orderedSearches[i].Order = i;
}

// Update the order to be mid-way between the two
sourceItem.Order = (targetItem.Order + bound) / 2;

// Force binding update
CollectionViewSource.GetDefaultView(searchesList.ItemsSource).Refresh();

Expand Down

0 comments on commit 6e1c3e6

Please sign in to comment.