Skip to content

Commit

Permalink
Changed to datamodel with additional information
Browse files Browse the repository at this point in the history
  • Loading branch information
netquick committed Jul 16, 2024
1 parent 5bb921c commit 9e34251
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 152 deletions.
1 change: 1 addition & 0 deletions DeFRaG_Helper/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<converters:PhysicsModeConverter x:Key="PhysicsModeConverter"/>
<converters:SizeFormatConverter x:Key="SizeFormatConverter"/>
<converters:ProgressBarWidthConverter x:Key="ProgressBarWidthConverter"/>
<converters:SvgPathAndColorConverter x:Key="SvgPathAndColorConverter" />

<converters:DynamicSvgConverter x:Key="DynamicSvgConverter"/>
<converters:SidebarWidthConverter x:Key="SidebarWidthConverter"/>
Expand Down
1 change: 1 addition & 0 deletions DeFRaG_Helper/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 35 additions & 2 deletions DeFRaG_Helper/Config/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +20,7 @@ public static class AppConfig
public static string? ConnectionString { get; set; }

public delegate Task<string> RequestGameDirectoryDelegate();
public static event RequestGameDirectoryDelegate OnRequestGameDirectory;
public static event RequestGameDirectoryDelegate? OnRequestGameDirectory;
static AppConfig()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Expand Down Expand Up @@ -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
{
Expand All @@ -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()
{
Expand Down
42 changes: 28 additions & 14 deletions DeFRaG_Helper/Converters/DynamicSvgConverter.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
}
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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");

Expand All @@ -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();
}
}
}

106 changes: 106 additions & 0 deletions DeFRaG_Helper/Converters/SvgPathAndColorConverter.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
32 changes: 30 additions & 2 deletions DeFRaG_Helper/Objects/Map.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using DeFRaG_Helper.Objects;
using System.ComponentModel;

namespace DeFRaG_Helper
{
Expand Down Expand Up @@ -393,6 +394,33 @@ public List<string> Functions
OnPropertyChanged(nameof(Functions));
}
}
}
}

public List<MapIcon> GenerateIcons()
{
List<MapIcon> icons = new List<MapIcon>();

// Example mapping for demonstration purposes
Dictionary<string, (string path, string color)> iconMapping = new Dictionary<string, (string, string)>
{
{"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;
}


}
}
15 changes: 15 additions & 0 deletions DeFRaG_Helper/Objects/MapIcon.cs
Original file line number Diff line number Diff line change
@@ -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; }
}

}
Loading

0 comments on commit 9e34251

Please sign in to comment.