From 9e3425182882af529c192094893f2f8bbb4341c6 Mon Sep 17 00:00:00 2001 From: netquick Date: Tue, 16 Jul 2024 20:16:38 +0200 Subject: [PATCH] Changed to datamodel with additional information --- DeFRaG_Helper/App.xaml | 1 + DeFRaG_Helper/App.xaml.cs | 1 + DeFRaG_Helper/Config/AppConfig.cs | 37 ++++- .../Converters/DynamicSvgConverter.cs | 42 +++-- .../Converters/SvgPathAndColorConverter.cs | 106 +++++++++++++ DeFRaG_Helper/Objects/Map.cs | 32 +++- DeFRaG_Helper/Objects/MapIcon.cs | 15 ++ DeFRaG_Helper/UserControls/MapCard.xaml | 146 ++++++++++++++++++ DeFRaG_Helper/UserControls/MapCard.xaml.cs | 50 ++++++ DeFRaG_Helper/ViewModels/ServerViewModel.cs | 2 + DeFRaG_Helper/Views/Maps.xaml | 140 ++--------------- DeFRaG_Helper/Views/Settings.xaml | 3 + DeFRaG_Helper/Views/Settings.xaml.cs | 36 ++++- 13 files changed, 459 insertions(+), 152 deletions(-) create mode 100644 DeFRaG_Helper/Converters/SvgPathAndColorConverter.cs create mode 100644 DeFRaG_Helper/Objects/MapIcon.cs create mode 100644 DeFRaG_Helper/UserControls/MapCard.xaml create mode 100644 DeFRaG_Helper/UserControls/MapCard.xaml.cs diff --git a/DeFRaG_Helper/App.xaml b/DeFRaG_Helper/App.xaml index 9a740f0..7daa918 100644 --- a/DeFRaG_Helper/App.xaml +++ b/DeFRaG_Helper/App.xaml @@ -18,6 +18,7 @@ + diff --git a/DeFRaG_Helper/App.xaml.cs b/DeFRaG_Helper/App.xaml.cs index 546350f..6741e1f 100644 --- a/DeFRaG_Helper/App.xaml.cs +++ b/DeFRaG_Helper/App.xaml.cs @@ -50,6 +50,7 @@ private async Task LoadConfigurationAndStartAsync() await AppConfig.LoadConfigurationAsync(); await MessageHelper.LogAsync($"Configuration loaded: {AppConfig.GameDirectoryPath}"); + AppConfig.UpdateThemeColor(); ApplyThemeColor(); // Create an instance of MapHistoryManager diff --git a/DeFRaG_Helper/Config/AppConfig.cs b/DeFRaG_Helper/Config/AppConfig.cs index 8f0434f..9274570 100644 --- a/DeFRaG_Helper/Config/AppConfig.cs +++ b/DeFRaG_Helper/Config/AppConfig.cs @@ -2,7 +2,9 @@ using System.IO; using System.Text.Json; using System.Threading.Tasks; - +using System.Windows.Media; +using System.Windows; +using System.Diagnostics; namespace DeFRaG_Helper { public static class AppConfig @@ -18,7 +20,7 @@ public static class AppConfig public static string? ConnectionString { get; set; } public delegate Task RequestGameDirectoryDelegate(); - public static event RequestGameDirectoryDelegate OnRequestGameDirectory; + public static event RequestGameDirectoryDelegate? OnRequestGameDirectory; static AppConfig() { string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); @@ -60,6 +62,8 @@ public static async Task LoadConfigurationAsync() await MessageHelper.LogAsync($"PhysicsSetting: {PhysicsSetting}"); await MessageHelper.LogAsync($"DatabasePath: {DatabasePath}"); await MessageHelper.LogAsync($"DatabaseUrl: {DatabaseUrl}"); + + } else { @@ -77,6 +81,35 @@ public static async Task LoadConfigurationAsync() } } + public static void UpdateThemeColor() + { + try + { + // Attempt to convert SelectedColor to a SolidColorBrush + if (!string.IsNullOrEmpty(AppConfig.SelectedColor)) + { + var color = (Color)ColorConverter.ConvertFromString(AppConfig.SelectedColor); + var brush = new SolidColorBrush(color); + Application.Current.Resources["ThemeColor"] = brush; + } + else + { + throw new FormatException("SelectedColor is null or empty."); + } + } + catch (Exception ex) + { + // Log the exception or handle it as needed + Debug.WriteLine($"Failed to update theme color, applying default color. Error: {ex.Message}"); + + // Apply a default color + var defaultColor = Colors.Yellow; // Example default color + Application.Current.Resources["ThemeColor"] = new SolidColorBrush(defaultColor); + } + } + + + public static async Task SaveConfigurationAsync() { diff --git a/DeFRaG_Helper/Converters/DynamicSvgConverter.cs b/DeFRaG_Helper/Converters/DynamicSvgConverter.cs index 6c8a58f..c369cf4 100644 --- a/DeFRaG_Helper/Converters/DynamicSvgConverter.cs +++ b/DeFRaG_Helper/Converters/DynamicSvgConverter.cs @@ -1,11 +1,10 @@ using System; -//using SharpVectors.Renderers.Wpf; -//using SharpVectors.Converters; using System.Diagnostics; using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Media; +using System.Windows.Resources; using System.Xml.Linq; namespace DeFRaG_Helper.Converters @@ -16,19 +15,30 @@ public object Convert(object value, Type targetType, object parameter, CultureIn { var path = value as string; if (string.IsNullOrEmpty(path)) - return null; + return DependencyProperty.UnsetValue; // Fix for Problem 4 + + Brush colorBrush = Brushes.White; // Default color + if (parameter is string colorParam) + { + var tempBrush = new BrushConverter().ConvertFromString(colorParam); + if (tempBrush != null) // Fix for Problem 5 + { + colorBrush = (Brush)tempBrush; + } + } + + StreamResourceInfo streamResourceInfo = null; // Declare outside try block for wider scope - // First, try to construct the pack URI for the embedded resource. var resourcePath = $"pack://application:,,,/DeFRaG_Helper;component/{path}"; try { var uri = new Uri(resourcePath, UriKind.RelativeOrAbsolute); - var streamResourceInfo = Application.GetResourceStream(uri); + streamResourceInfo = Application.GetResourceStream(uri); if (streamResourceInfo != null) { using (var stream = streamResourceInfo.Stream) { - return LoadSvgFromStream(stream); + return LoadSvgFromStream(stream, colorBrush); // Fix for Problem 1 } } } @@ -37,7 +47,6 @@ public object Convert(object value, Type targetType, object parameter, CultureIn Debug.WriteLine($"Error loading SVG as resource: {ex.Message}"); } - // If loading as a resource failed, try loading from the file system. var basePath = AppDomain.CurrentDomain.BaseDirectory; var filePath = System.IO.Path.Combine(basePath, path); if (System.IO.File.Exists(filePath)) @@ -46,7 +55,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn { using (var stream = System.IO.File.OpenRead(filePath)) { - return LoadSvgFromStream(stream); + return LoadSvgFromStream(stream, colorBrush); // Fix for Problem 2 } } catch (Exception ex) @@ -55,12 +64,19 @@ public object Convert(object value, Type targetType, object parameter, CultureIn } } - return null; + if (streamResourceInfo != null) // Check for null to fix Problem 3 + { + return LoadSvgFromStream(streamResourceInfo.Stream, colorBrush); + } + + return DependencyProperty.UnsetValue; // Return a non-null default value if all else fails } - private object LoadSvgFromStream(System.IO.Stream stream) + private object LoadSvgFromStream(System.IO.Stream stream, Brush colorBrush) { var svgDocument = XDocument.Load(stream); + if (svgDocument.Root == null) return DependencyProperty.UnsetValue; // Fix for Problem 6 + var ns = svgDocument.Root.GetDefaultNamespace(); var paths = svgDocument.Descendants(ns + "path"); @@ -78,18 +94,16 @@ private object LoadSvgFromStream(System.IO.Stream stream) var drawing = new GeometryDrawing { Geometry = geometryGroup, - Brush = Brushes.White, // Set to your desired color - Pen = new Pen(Brushes.White, 1) // Set to your desired pen + Brush = colorBrush, + Pen = new Pen(colorBrush, 1) }; return new DrawingImage(drawing); } - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } } - diff --git a/DeFRaG_Helper/Converters/SvgPathAndColorConverter.cs b/DeFRaG_Helper/Converters/SvgPathAndColorConverter.cs new file mode 100644 index 0000000..ffda63c --- /dev/null +++ b/DeFRaG_Helper/Converters/SvgPathAndColorConverter.cs @@ -0,0 +1,106 @@ +using System; +using System.Diagnostics; +using System.Globalization; +using System.Windows; +using System.Windows.Data; +using System.Windows.Media; +using System.Windows.Resources; +using System.Xml.Linq; + +namespace DeFRaG_Helper.Converters +{ + public class SvgPathAndColorConverter : IMultiValueConverter + { + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + { + if (values.Length != 2 || !(values[0] is string path) || !(values[1] is string colorParam)) + return DependencyProperty.UnsetValue; + + Brush colorBrush = Brushes.White; // Default color + if (!string.IsNullOrEmpty(colorParam)) + { + var tempBrush = new BrushConverter().ConvertFromString(colorParam); + if (tempBrush != null) + { + colorBrush = (Brush)tempBrush; + } + } + + // The rest of the method follows the same logic as in DynamicSvgConverter's Convert method, + // but uses the path and colorBrush determined from the values array. + + StreamResourceInfo streamResourceInfo = null; + var resourcePath = $"pack://application:,,,/DeFRaG_Helper;component/{path}"; + try + { + var uri = new Uri(resourcePath, UriKind.RelativeOrAbsolute); + streamResourceInfo = Application.GetResourceStream(uri); + if (streamResourceInfo != null) + { + using (var stream = streamResourceInfo.Stream) + { + return LoadSvgFromStream(stream, colorBrush); + } + } + } + catch (Exception ex) + { + Debug.WriteLine($"Error loading SVG as resource: {ex.Message}"); + } + + var basePath = AppDomain.CurrentDomain.BaseDirectory; + var filePath = System.IO.Path.Combine(basePath, path); + if (System.IO.File.Exists(filePath)) + { + try + { + using (var stream = System.IO.File.OpenRead(filePath)) + { + return LoadSvgFromStream(stream, colorBrush); + } + } + catch (Exception ex) + { + Debug.WriteLine($"Error loading SVG from file: {ex.Message}"); + } + } + + return DependencyProperty.UnsetValue; + } + + private object LoadSvgFromStream(System.IO.Stream stream, Brush colorBrush) + { + var svgDocument = XDocument.Load(stream); + if (svgDocument.Root == null) return DependencyProperty.UnsetValue; // Fix for Problem 6 + + var ns = svgDocument.Root.GetDefaultNamespace(); + var paths = svgDocument.Descendants(ns + "path"); + + var geometryGroup = new GeometryGroup(); + foreach (var pathElement in paths) + { + var dataAttribute = pathElement.Attribute("d"); + if (dataAttribute != null) + { + var geometry = Geometry.Parse(dataAttribute.Value); + geometryGroup.Children.Add(geometry); + } + } + + var drawing = new GeometryDrawing + { + Geometry = geometryGroup, + Brush = colorBrush, + Pen = new Pen(colorBrush, 1) + }; + + return new DrawingImage(drawing); + } + + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/DeFRaG_Helper/Objects/Map.cs b/DeFRaG_Helper/Objects/Map.cs index 5db4b5e..aa16cb5 100644 --- a/DeFRaG_Helper/Objects/Map.cs +++ b/DeFRaG_Helper/Objects/Map.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using DeFRaG_Helper.Objects; +using System.ComponentModel; namespace DeFRaG_Helper { @@ -393,6 +394,33 @@ public List Functions OnPropertyChanged(nameof(Functions)); } } - } + } + + public List GenerateIcons() + { + List icons = new List(); + + // Example mapping for demonstration purposes + Dictionary iconMapping = new Dictionary + { + {"Rocket Launcher", ("Icons/Weapons/iconw_rocket.svg", "Red")}, + {"Plasmagun", ("Icons/Weapons/iconw_plasma.svg", "Blue")}, + // Add mappings for other weapons, items, and functions + }; + + foreach (var weapon in Weapons) + { + if (iconMapping.TryGetValue(weapon, out var iconInfo)) + { + icons.Add(new MapIcon { SvgPath = iconInfo.path, Color = iconInfo.color }); + } + } + + // Repeat for Items and Functions if necessary + + return icons; + } + + } } diff --git a/DeFRaG_Helper/Objects/MapIcon.cs b/DeFRaG_Helper/Objects/MapIcon.cs new file mode 100644 index 0000000..a8d6c6f --- /dev/null +++ b/DeFRaG_Helper/Objects/MapIcon.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DeFRaG_Helper.Objects +{ + public class MapIcon + { + public string SvgPath { get; set; } + public string Color { get; set; } + } + +} diff --git a/DeFRaG_Helper/UserControls/MapCard.xaml b/DeFRaG_Helper/UserControls/MapCard.xaml new file mode 100644 index 0000000..adf9711 --- /dev/null +++ b/DeFRaG_Helper/UserControls/MapCard.xaml @@ -0,0 +1,146 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/DeFRaG_Helper/UserControls/MapCard.xaml.cs b/DeFRaG_Helper/UserControls/MapCard.xaml.cs new file mode 100644 index 0000000..69d1e54 --- /dev/null +++ b/DeFRaG_Helper/UserControls/MapCard.xaml.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace DeFRaG_Helper.UserControls +{ + /// + /// Interaction logic for MapCard.xaml + /// + public partial class MapCard : UserControl + { + public Map CurrentMap { get; set; } + public static readonly DependencyProperty IconsProperty = DependencyProperty.Register( + "Icons", typeof(IEnumerable), typeof(MapCard), new PropertyMetadata(null)); + + public IEnumerable Icons + { + get { return (IEnumerable)GetValue(IconsProperty); } + set { SetValue(IconsProperty, value); } + } + + public MapCard() + { + InitializeComponent(); + } + private void FavoriteCheckBox_Checked(object sender, RoutedEventArgs e) + { + // Add map to favorites + } + + private void FavoriteCheckBox_Unchecked(object sender, RoutedEventArgs e) + { + // Remove map from favorites + } + } + + +} diff --git a/DeFRaG_Helper/ViewModels/ServerViewModel.cs b/DeFRaG_Helper/ViewModels/ServerViewModel.cs index 1d8bd91..cdc15f4 100644 --- a/DeFRaG_Helper/ViewModels/ServerViewModel.cs +++ b/DeFRaG_Helper/ViewModels/ServerViewModel.cs @@ -133,6 +133,8 @@ private async void UpdateServerData(object sender, EventArgs e) await Task.WhenAll(tasks); + + //TODO: Make sure the mainview is updated after all servers have been updated // Refresh the SortedServersView on the UI thread after all updates App.Current.Dispatcher.Invoke(() => { diff --git a/DeFRaG_Helper/Views/Maps.xaml b/DeFRaG_Helper/Views/Maps.xaml index 7cac38a..bc419c0 100644 --- a/DeFRaG_Helper/Views/Maps.xaml +++ b/DeFRaG_Helper/Views/Maps.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:DeFRaG_Helper" xmlns:converters="clr-namespace:DeFRaG_Helper.Converters" xmlns:sys="clr-namespace:System;assembly=mscorlib" - + xmlns:uc="clr-namespace:DeFRaG_Helper.UserControls" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="MapsList" @@ -36,130 +36,18 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + diff --git a/DeFRaG_Helper/Views/Settings.xaml b/DeFRaG_Helper/Views/Settings.xaml index bbd8d9f..96780d6 100644 --- a/DeFRaG_Helper/Views/Settings.xaml +++ b/DeFRaG_Helper/Views/Settings.xaml @@ -67,6 +67,9 @@ + + + \ No newline at end of file diff --git a/DeFRaG_Helper/Views/Settings.xaml.cs b/DeFRaG_Helper/Views/Settings.xaml.cs index 2e15a34..2531085 100644 --- a/DeFRaG_Helper/Views/Settings.xaml.cs +++ b/DeFRaG_Helper/Views/Settings.xaml.cs @@ -49,17 +49,18 @@ private async void ColorComboBox_SelectionChanged(object sender, SelectionChange // Retrieve the SolidColorBrush from the selected ComboBoxItem var selectedBrush = selectedItem.Background as SolidColorBrush; - // Create a new SolidColorBrush instance with the selected color - var newBrush = new SolidColorBrush(selectedBrush.Color); + // Convert the Color to a hexadecimal string + var color = selectedBrush.Color; + string hexColor = $"#{color.R:X2}{color.G:X2}{color.B:X2}"; // Replace the ThemeColor resource with the new SolidColorBrush instance - Application.Current.Resources["ThemeColor"] = newBrush; - AppConfig.SelectedColor = selectedItem.Content.ToString(); + Application.Current.Resources["ThemeColor"] = new SolidColorBrush(color); + AppConfig.SelectedColor = hexColor; // Save the hexadecimal color string await AppConfig.SaveConfigurationAsync(); - } } + private async void btnSync_Click(object sender, RoutedEventArgs e) { // Get the singleton instance of MapViewModel @@ -85,15 +86,34 @@ private async void btnSync_Click(object sender, RoutedEventArgs e) private void Page_Loaded(object sender, RoutedEventArgs e) { - // Load the selected color from AppConfig and update the ComboBox selection - var selectedItem = ColorComboBox.Items.OfType().FirstOrDefault(item => item.Content.ToString() == AppConfig.SelectedColor); + // Assuming AppConfig.SelectedColor is in the format "#RRGGBB" + var hexColor = AppConfig.SelectedColor; + + // Find the ComboBoxItem whose background color matches the hexColor + ComboBoxItem selectedItem = null; + foreach (var item in ColorComboBox.Items.OfType()) + { + if (item.Background is SolidColorBrush brush) + { + var color = brush.Color; + string itemHexColor = $"#{color.R:X2}{color.G:X2}{color.B:X2}"; + if (itemHexColor.Equals(hexColor, StringComparison.OrdinalIgnoreCase)) + { + selectedItem = item; + break; + } + } + } + + // Update the ComboBox selection and other settings if (selectedItem != null) { ColorComboBox.SelectedItem = selectedItem; - txtGamePath.Text = AppConfig.GameDirectoryPath; } + txtGamePath.Text = AppConfig.GameDirectoryPath; } + private void btnUpdate_Click(object sender, RoutedEventArgs e) { Task.Run(async () =>