Skip to content

Commit

Permalink
Support searching for tamed creatures by name
Browse files Browse the repository at this point in the history
Added a search box when showing tames. Results are highlighted on the map and in the creature list.

Result list now requires items to be selected before they are highlighted on the map, but you can now select multiple creatures and have them stay highlighted.

ISSUE: With many creatures in the results list, sometimes highlights are not shown immediately. Scrolling the list seems to fix it. The map highlights correctly.
  • Loading branch information
coldino committed Feb 24, 2018
1 parent c230c9e commit 84ba9fc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
2 changes: 2 additions & 0 deletions LarkatorGUI/DummyMainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class DummyMainWindow
{
public string ApplicationVersion { get => "DUMMY"; }

public string SearchText { get; set; } = "search";

public Collection<SearchCriteria> ListSearches { get => searches; }
public Collection<DinoViewModel> ListResults { get => results; }

Expand Down
2 changes: 2 additions & 0 deletions LarkatorGUI/LarkatorGUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -55,6 +56,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>compass.ico</ApplicationIcon>
Expand Down
28 changes: 25 additions & 3 deletions LarkatorGUI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<Style x:Key="ClearTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="AntiqueWhite"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Style>

<Style x:Key="CleanRowStyle" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource SelectionBackgroundBrush}" />
<Setter Property="Background" Value="#4884" />
</Trigger>
</Style.Triggers>
</Style>
Expand Down Expand Up @@ -285,7 +292,22 @@
</DataGrid>

<StackPanel Grid.Row="2" Orientation="Vertical">
<Button Width="32" FontFamily="Arial Black" FontSize="28" FontWeight="Bold" Padding="-4" Margin="16" Content="+" Click="CreateSearch_Click" Visibility="{Binding CreateSearchAvailable, Converter={StaticResource BoolToVisibilityConverter}}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,8" Visibility="{Binding ShowTames, Converter={StaticResource BoolToVisibilityConverter}}">
<Border BorderThickness="0,0,0,1" BorderBrush="#4FFF">
<StackPanel Orientation="Horizontal" TextBlock.FontSize="{Binding Source={StaticResource Settings}, Path=Default.ListFontSize}">
<TextBox x:Name="searchText" Width="140" AcceptsReturn="False" Style="{StaticResource ClearTextBox}"
Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" PreviewKeyDown="SearchText_KeyDown"
VerticalContentAlignment="Bottom" Padding="2,0,2,2" local:FontSizeHelper.RelativeFontSize="+2"/>
<Button Click="ClearSearch_Click" BorderBrush="{x:Null}" BorderThickness="0" Background="{x:Null}"
Margin="4,4,8,3" Padding="3,0" ToolTip="Clear search term">
<fa:ImageAwesome Icon="Close" Width="10" Foreground="AntiqueWhite" Opacity="0.6"/>
</Button>
</StackPanel>
</Border>
<fa:ImageAwesome Icon="Search" Width="24" Foreground="AntiqueWhite" Margin="8,0,0,0" Opacity="0.6" MouseDown="Search_Click"/>
</StackPanel>
<Button Width="32" FontFamily="Arial Black" FontSize="28" FontWeight="Bold" Padding="-4" Margin="16" Content="+" Click="CreateSearch_Click"
Visibility="{Binding CreateSearchAvailable, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Grid Margin="16,0,16,16" Visibility="{Binding NewSearchActive, Converter={StaticResource BoolToVisibilityConverter}}" Background="#44ffffff">
<StackPanel Margin="8" Orientation="Vertical" HorizontalAlignment="Stretch" DataContext="{Binding NewSearch}">
<ComboBox x:Name="groupsCombo" Text="{Binding Group}" IsEditable="True"/>
Expand Down Expand Up @@ -335,7 +357,7 @@
<DataGrid Grid.Row="1" x:Name="resultsList" Margin="8" AutoGenerateColumns="False" IsReadOnly="True"
ItemsSource="{Binding Source={StaticResource OrderedResults}}"
TextBlock.FontSize="{Binding Default.ListFontSize, Source={StaticResource Settings}}"
SelectionMode="Single" SelectionUnit="FullRow" CanUserSortColumns="True" Sorting="ResultList_Sorting"
SelectionMode="Extended" SelectionUnit="FullRow" CanUserSortColumns="True" Sorting="ResultList_Sorting"
Style="{DynamicResource CleanDataGridStyle}" RowStyle="{DynamicResource CleanDinoRowStyle}"
ColumnHeaderStyle="{DynamicResource CleanColumnHeaderStyle}" CellStyle="{DynamicResource CleanCellStyle}"
>
Expand Down
75 changes: 73 additions & 2 deletions LarkatorGUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public ImageSource ImageSource
set { SetValue(ImageSourceProperty, value); }
}

public string SearchText
{
get { return (string)GetValue(SearchTextProperty); }
set { SetValue(SearchTextProperty, value); }
}

public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register("SearchText", typeof(string), typeof(MainWindow), new PropertyMetadata(""));

public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(MainWindow), new PropertyMetadata(null));

Expand Down Expand Up @@ -131,6 +140,9 @@ public ImageSource ImageSource
private List<MapCalibration> mapCalibrations;
private readonly string appVersion;
readonly List<bool?> nullableBoolValues = new List<bool?> { null, false, true };
DateTime nameSearchTime;
string nameSearchArg;
bool nameSearchRunning;

public MainWindow()
{
Expand Down Expand Up @@ -163,6 +175,8 @@ public MainWindow()
var cmdThrowExceptionAndExit = new RoutedCommand();
cmdThrowExceptionAndExit.InputGestures.Add(new KeyGesture(Key.F2, ModifierKeys.Control));
CommandBindings.Add(new CommandBinding(cmdThrowExceptionAndExit, (o, e) => Dev_GenerateException_Click(null, null)));

DependencyPropertyDescriptor.FromProperty(SearchTextProperty, typeof(MainWindow)).AddValueChanged(DataContext, (s,e) => TriggerNameSearch());
}

private static string CalculateApplicationVersion()
Expand Down Expand Up @@ -320,6 +334,24 @@ private void RemoveSearch_Click(object sender, RoutedEventArgs e)
MarkSearchesChanged();
}

private void ClearSearch_Click(object sender, RoutedEventArgs e)
{
SearchText = "";

TriggerNameSearch(true);
}

private void Search_Click(object sender, MouseButtonEventArgs e)
{
TriggerNameSearch(true);
}

private void SearchText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
TriggerNameSearch(true);
}

private void MapPin_Click(object sender, MouseButtonEventArgs ev)
{
if (sender is FrameworkElement e && e.DataContext is DinoViewModel dvm)
Expand Down Expand Up @@ -522,7 +554,7 @@ private void Result_MouseEnter(object sender, MouseEventArgs e)
if (fe == null) return;
var dino = fe.DataContext as DinoViewModel;
if (dino == null) return;
dino.Highlight = true;
//dino.Highlight = true;
}

private void Result_MouseLeave(object sender, MouseEventArgs e)
Expand All @@ -531,7 +563,7 @@ private void Result_MouseLeave(object sender, MouseEventArgs e)
if (fe == null) return;
var dino = fe.DataContext as DinoViewModel;
if (dino == null) return;
dino.ClearValue(DinoViewModel.HighlightProperty);
//dino.ClearValue(DinoViewModel.HighlightProperty);
}

private void ResultList_Sorting(object sender, DataGridSortingEventArgs e)
Expand Down Expand Up @@ -660,6 +692,8 @@ private void UpdateSearchResults(IList<SearchCriteria> searches)
}

((CollectionViewSource)Resources["OrderedResults"]).View.Refresh();

TriggerNameSearch(true);
}

private async Task PerformConversion(bool force)
Expand Down Expand Up @@ -769,6 +803,8 @@ private void SetupTamedSearches()
ListSearches.Clear();
foreach (var search in searches)
ListSearches.Add(search);

TriggerNameSearch(true);
}

private void ShowWildSearches()
Expand Down Expand Up @@ -806,5 +842,40 @@ private void RefreshDataGridColumnWidths(string resourceName, DataGrid dataGrid)
foreach (var o in dataGrid.Columns.Zip(widths, (col, width) => new { col, width }))
o.col.Width = o.width;
}

private void TriggerNameSearch(bool immediate=false)
{
nameSearchTime = DateTime.Now + TimeSpan.FromSeconds(immediate ? 0.01 : 0.5);
nameSearchArg = SearchText;

if (!nameSearchRunning)
{
nameSearchRunning = true;
Dispatcher.Invoke(WaitForNameSearch, DispatcherPriority.Background);
}
}

private async Task WaitForNameSearch()
{
while (nameSearchTime > DateTime.Now)
{
await Task.Delay(100);
}

nameSearchRunning = false;

var searchText = nameSearchArg.Trim();

if (String.IsNullOrWhiteSpace(searchText) || searchText.Length < 2)
{
foreach (var dvm in ListResults)
dvm.Highlight = false;
}
else
{
foreach (var dvm in ListResults)
dvm.Highlight = (dvm.Dino.Name != null) && dvm.Dino.Name.Contains(searchText);
}
}
}
}

0 comments on commit 84ba9fc

Please sign in to comment.