Skip to content

Commit

Permalink
implemented custom tags (missing new tags and tagbar, state backup)
Browse files Browse the repository at this point in the history
  • Loading branch information
netquick committed Aug 16, 2024
1 parent cdf02b5 commit c5f7f54
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 41 deletions.
47 changes: 47 additions & 0 deletions DeFRaG_Helper/Behaviors/ListBoxBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace DeFRaG_Helper.Behaviors
{
public static class ListBoxBehavior
{
public static readonly DependencyProperty BindableSelectedItemsProperty =
DependencyProperty.RegisterAttached("BindableSelectedItems", typeof(IList), typeof(ListBoxBehavior), new PropertyMetadata(null, OnBindableSelectedItemsChanged));

public static IList GetBindableSelectedItems(DependencyObject obj)
{
return (IList)obj.GetValue(BindableSelectedItemsProperty);
}

public static void SetBindableSelectedItems(DependencyObject obj, IList value)
{
obj.SetValue(BindableSelectedItemsProperty, value);
}

private static void OnBindableSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ListBox listBox)
{
listBox.SelectionChanged -= ListBox_SelectionChanged;
listBox.SelectionChanged += ListBox_SelectionChanged;
}
}

private static void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ListBox listBox)
{
IList selectedItems = GetBindableSelectedItems(listBox);
if (selectedItems != null)
{
selectedItems.Clear();
foreach (var item in listBox.SelectedItems)
{
selectedItems.Add(item);
}
}
}
}
}
}
4 changes: 4 additions & 0 deletions DeFRaG_Helper/Helpers/DbActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace DeFRaG_Helper
{
/// <summary>
/// This file is meant to do the complex database action when loading maps from the web
/// </summary>
internal class DbActions
{

//initialize dbqueue
private static DbActions _instance;
private static readonly object _lock = new object();
Expand Down
16 changes: 16 additions & 0 deletions DeFRaG_Helper/Objects/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,22 @@ public ObservableCollection<MapIcon> GenerateFunctionIcons()
return newFunctionIcons;
}


private List<string> tags = new List<string>();
public List<string> Tags
{
get => tags;
set
{
if (tags != value)
{
tags = value;
OnPropertyChanged(nameof(Tags));
}
}
}


}


Expand Down
6 changes: 4 additions & 2 deletions DeFRaG_Helper/UserControls/MapCardBig.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@
Width="60" Height="60" Style="{StaticResource FavoriteCheckBoxStyle}" VerticalAlignment="Top" Margin="0,0,240,0"
Checked="FavoriteCheckBox_Checked" Unchecked="FavoriteCheckBox_Unchecked"/>
<Button Width="30" Height="30" Margin="0,35,0,0" HorizontalAlignment="Right" VerticalAlignment="Top"
Command="{Binding DataContext.TagsCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding}" Background="Transparent">
Command="{Binding OpenTagManagerCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}" Background="Transparent">
<Image Width="20" Height="20">
<Image.Source>
<Binding>
Expand All @@ -263,6 +263,8 @@
</Image.Source>
</Image>
</Button>


</Grid>
</Border>
</UserControl>
56 changes: 56 additions & 0 deletions DeFRaG_Helper/UserControls/MapCardBig.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
Expand All @@ -25,11 +26,66 @@ public partial class MapCardBig : UserControl
public Map CurrentMap { get; set; }
public static readonly DependencyProperty IconsProperty = DependencyProperty.Register(
"Icons", typeof(IEnumerable), typeof(MapCardBig), new PropertyMetadata(null));

public ICommand OpenTagManagerCommand { get; }


public MapCardBig()
{
InitializeComponent();
//DataContext = MapViewModel.GetInstanceAsync().Result;
OpenTagManagerCommand = new RelayCommand(OpenTagManager);

}



private void OpenTagManager(object parameter)
{
if (parameter is Button triggerButton)
{
var map = triggerButton.DataContext as Map;
if (map != null)
{
var viewModel = new TagManagerViewModel(map);
var tagManager = new TagManager(viewModel);

var window = new Window
{
Content = tagManager,
Width = 400,
Height = 300,
Title = "Manage Tags",
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = new SolidColorBrush(Color.FromArgb(204, 17, 17, 17)) // 80% alpha and color #111
};

// Set the owner of the window to the main application window
window.Owner = Application.Current.MainWindow;

// Get the position of the button that triggered the popup
Point buttonPosition = triggerButton.PointToScreen(new Point(0, 0));
window.Left = buttonPosition.X;
window.Top = buttonPosition.Y;

// Handle Deactivated event to close the window
window.Deactivated += (s, e) => window.Close();

// Add fade-in animation
var fadeInAnimation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(0.3));
window.BeginAnimation(Window.OpacityProperty, fadeInAnimation);

window.Show();
}
}
}






public IEnumerable Icons
{
get { return (IEnumerable)GetValue(IconsProperty); }
Expand Down
29 changes: 29 additions & 0 deletions DeFRaG_Helper/UserControls/TagManager.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<UserControl x:Class="DeFRaG_Helper.UserControls.TagManager"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Border Background="Transparent" Padding="10" CornerRadius="5">
<StackPanel>
<TextBlock Text="Manage Tags" FontSize="16" Margin="0,0,0,10" Foreground="White"/>
<ListBox ItemsSource="{Binding Tags}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Command="{Binding DataContext.TagCheckedCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox x:Name="NewTagTextBox" Width="200" Margin="0,10,0,0" Text="{Binding NewTagName, UpdateSourceTrigger=PropertyChanged}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button Command="{Binding CancelCommand}" Content="Cancel"/>
<Button Command="{Binding RemoveTagCommand}" Content="Remove Tag"/>
<Button Command="{Binding AddTagCommand}" Content="Add Tag" CommandParameter="{Binding Text, ElementName=NewTagTextBox}"/>
</StackPanel>
</StackPanel>
</Border>
</UserControl>
24 changes: 24 additions & 0 deletions DeFRaG_Helper/UserControls/TagManager.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Windows.Controls;
using System.Windows;
using DeFRaG_Helper.ViewModels;

namespace DeFRaG_Helper.UserControls
{
public partial class TagManager : UserControl
{
public TagManager(TagManagerViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;

viewModel.RequestClose += (sender, e) =>
{
var window = Window.GetWindow(this);
if (window != null)
{
window.Close();
}
};
}
}
}
14 changes: 14 additions & 0 deletions DeFRaG_Helper/ViewModels/BaseViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel;

namespace DeFRaG_Helper.ViewModels
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

Check warning on line 7 in DeFRaG_Helper/ViewModels/BaseViewModel.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Nullability of reference types in type of 'event PropertyChangedEventHandler BaseViewModel.PropertyChanged' doesn't match implicitly implemented member 'event PropertyChangedEventHandler? INotifyPropertyChanged.PropertyChanged'.

Check warning on line 7 in DeFRaG_Helper/ViewModels/BaseViewModel.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Nullability of reference types in type of 'event PropertyChangedEventHandler BaseViewModel.PropertyChanged' doesn't match implicitly implemented member 'event PropertyChangedEventHandler? INotifyPropertyChanged.PropertyChanged'.

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
87 changes: 48 additions & 39 deletions DeFRaG_Helper/ViewModels/MapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,13 @@ private async Task<List<Map>> GetLastPlayedMaps()
return lastPlayedMaps;
}

public async Task<Map> GetMapByIdAsync(int mapId)
{
Map map = null;
DbQueue.Instance.Enqueue(async connection =>
{
using (var command = new SqliteCommand(@"SELECT
public async Task<Map> GetMapByIdAsync(int mapId)
{
Map map = null;
DbQueue.Instance.Enqueue(async connection =>
{
using (var command = new SqliteCommand(@"
SELECT
Maps.ID,
Maps.Name,
Maps.Mapname,
Expand All @@ -523,7 +524,8 @@ public async Task<Map> GetMapByIdAsync(int mapId)
Maps.isFavorite,
GROUP_CONCAT(DISTINCT Weapon.Weapon) AS Weapons,
GROUP_CONCAT(DISTINCT Item.Item) AS Items,
GROUP_CONCAT(DISTINCT Function.Function) AS Functions
GROUP_CONCAT(DISTINCT Function.Function) AS Functions,
GROUP_CONCAT(DISTINCT Tag.Tag) AS Tags
FROM
Maps
LEFT JOIN
Expand All @@ -538,6 +540,10 @@ LEFT JOIN
MapFunction ON Maps.ID = MapFunction.MapID
LEFT JOIN
Function ON MapFunction.FunctionID = Function.FunctionID
LEFT JOIN
MapTag ON Maps.ID = MapTag.MapID
LEFT JOIN
Tag ON MapTag.TagID = Tag.TagID
WHERE
Maps.ID = @mapId
GROUP BY
Expand All @@ -556,43 +562,46 @@ GROUP BY
Maps.isDownloaded,
Maps.isInstalled,
Maps.isFavorite", connection))
{
command.Parameters.AddWithValue("@mapId", mapId);
using (var reader = await command.ExecuteReaderAsync())
{
if (await reader.ReadAsync())
{
command.Parameters.AddWithValue("@mapId", mapId);
using (var reader = await command.ExecuteReaderAsync())
map = new Map
{
if (await reader.ReadAsync())
{
map = new Map
{
Id = reader.GetInt32(reader.GetOrdinal("ID")),
Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? null : reader.GetString(reader.GetOrdinal("Name")),
Mapname = reader.IsDBNull(reader.GetOrdinal("Mapname")) ? null : reader.GetString(reader.GetOrdinal("Mapname")),
Filename = reader.IsDBNull(reader.GetOrdinal("Filename")) ? null : reader.GetString(reader.GetOrdinal("Filename")),
Releasedate = reader.IsDBNull(reader.GetOrdinal("Releasedate")) ? null : reader.GetString(reader.GetOrdinal("Releasedate")),
Author = reader.IsDBNull(reader.GetOrdinal("Author")) ? null : reader.GetString(reader.GetOrdinal("Author")),
GameType = reader.IsDBNull(reader.GetOrdinal("Mod")) ? null : reader.GetString(reader.GetOrdinal("Mod")),
Size = reader.IsDBNull(reader.GetOrdinal("Size")) ? 0 : reader.GetInt64(reader.GetOrdinal("Size")),
Physics = reader.IsDBNull(reader.GetOrdinal("Physics")) ? 0 : reader.GetInt32(reader.GetOrdinal("Physics")),
Hits = reader.IsDBNull(reader.GetOrdinal("Hits")) ? 0 : reader.GetInt32(reader.GetOrdinal("Hits")),
LinkDetailpage = reader.IsDBNull(reader.GetOrdinal("LinkDetailpage")) ? null : reader.GetString(reader.GetOrdinal("LinkDetailpage")),
Style = reader.IsDBNull(reader.GetOrdinal("Style")) ? null : reader.GetString(reader.GetOrdinal("Style")),
IsDownloaded = reader.IsDBNull(reader.GetOrdinal("isDownloaded")) ? 0 : reader.GetInt32(reader.GetOrdinal("isDownloaded")),
IsInstalled = reader.IsDBNull(reader.GetOrdinal("isInstalled")) ? 0 : reader.GetInt32(reader.GetOrdinal("isInstalled")),
IsFavorite = reader.IsDBNull(reader.GetOrdinal("isFavorite")) ? 0 : reader.GetInt32(reader.GetOrdinal("isFavorite")),
Weapons = reader.IsDBNull(reader.GetOrdinal("Weapons")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Weapons")).Split(',').ToList(),
Items = reader.IsDBNull(reader.GetOrdinal("Items")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Items")).Split(',').ToList(),
Functions = reader.IsDBNull(reader.GetOrdinal("Functions")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Functions")).Split(',').ToList(),
};
}
}
Id = reader.GetInt32(reader.GetOrdinal("ID")),
Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? null : reader.GetString(reader.GetOrdinal("Name")),
Mapname = reader.IsDBNull(reader.GetOrdinal("Mapname")) ? null : reader.GetString(reader.GetOrdinal("Mapname")),
Filename = reader.IsDBNull(reader.GetOrdinal("Filename")) ? null : reader.GetString(reader.GetOrdinal("Filename")),
Releasedate = reader.IsDBNull(reader.GetOrdinal("Releasedate")) ? null : reader.GetString(reader.GetOrdinal("Releasedate")),
Author = reader.IsDBNull(reader.GetOrdinal("Author")) ? null : reader.GetString(reader.GetOrdinal("Author")),
GameType = reader.IsDBNull(reader.GetOrdinal("Mod")) ? null : reader.GetString(reader.GetOrdinal("Mod")),
Size = reader.IsDBNull(reader.GetOrdinal("Size")) ? 0 : reader.GetInt64(reader.GetOrdinal("Size")),
Physics = reader.IsDBNull(reader.GetOrdinal("Physics")) ? 0 : reader.GetInt32(reader.GetOrdinal("Physics")),
Hits = reader.IsDBNull(reader.GetOrdinal("Hits")) ? 0 : reader.GetInt32(reader.GetOrdinal("Hits")),
LinkDetailpage = reader.IsDBNull(reader.GetOrdinal("LinkDetailpage")) ? null : reader.GetString(reader.GetOrdinal("LinkDetailpage")),
Style = reader.IsDBNull(reader.GetOrdinal("Style")) ? null : reader.GetString(reader.GetOrdinal("Style")),
IsDownloaded = reader.IsDBNull(reader.GetOrdinal("isDownloaded")) ? 0 : reader.GetInt32(reader.GetOrdinal("isDownloaded")),
IsInstalled = reader.IsDBNull(reader.GetOrdinal("isInstalled")) ? 0 : reader.GetInt32(reader.GetOrdinal("isInstalled")),
IsFavorite = reader.IsDBNull(reader.GetOrdinal("isFavorite")) ? 0 : reader.GetInt32(reader.GetOrdinal("isFavorite")),
Weapons = reader.IsDBNull(reader.GetOrdinal("Weapons")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Weapons")).Split(',').ToList(),
Items = reader.IsDBNull(reader.GetOrdinal("Items")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Items")).Split(',').ToList(),
Functions = reader.IsDBNull(reader.GetOrdinal("Functions")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Functions")).Split(',').ToList(),
Tags = reader.IsDBNull(reader.GetOrdinal("Tags")) ? new List<string>() : reader.GetString(reader.GetOrdinal("Tags")).Split(',').ToList(),
};
}
});
}
}
});

// Await the completion of all enqueued operations, including the one just added
await DbQueue.Instance.WhenAllCompleted();

return map;
}

// Await the completion of all enqueued operations, including the one just added
await DbQueue.Instance.WhenAllCompleted();

return map;
}

private async Task PerformPostDataLoadActions()
{
Expand Down
Loading

0 comments on commit c5f7f54

Please sign in to comment.