From b856ab9230acbbd10f78f7eb5dc52d1f7d878f8e Mon Sep 17 00:00:00 2001 From: OliverT87 Date: Tue, 27 May 2025 15:35:07 +0200 Subject: [PATCH] navigation geht, grid ist da, farben passen noch nicht --- .../AboutPage.axaml.cs | 19 ++- Project_Periodensystem.View/LandingPage.axaml | 32 ++--- .../LandingPage.axaml.cs | 54 +++++++- Project_Periodensystem.View/MainWindow.axaml | 6 +- .../MainWindow.axaml.cs | 104 ++++++++++++--- .../PeriodicTablePage.axaml | 69 +++++++++- .../PeriodicTablePage.axaml.cs | 125 +++++++++++++++++- Project_Periodensystem.View/logger.cs | 54 ++++++++ 8 files changed, 404 insertions(+), 59 deletions(-) create mode 100644 Project_Periodensystem.View/logger.cs diff --git a/Project_Periodensystem.View/AboutPage.axaml.cs b/Project_Periodensystem.View/AboutPage.axaml.cs index 5ae8acc..418e7a7 100644 --- a/Project_Periodensystem.View/AboutPage.axaml.cs +++ b/Project_Periodensystem.View/AboutPage.axaml.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; @@ -17,10 +18,22 @@ namespace Project_Periodensystem.View private void BackButton_Click(object sender, RoutedEventArgs e) { - var window = this.GetVisualRoot() as MainWindow; - if (window != null) + try { - window.ShowLanding(); + var mainWindow = this.FindAncestorOfType(); + if (mainWindow != null) + { + mainWindow.ShowLanding(); + } + else + { + Console.WriteLine("MainWindow nicht gefunden!"); + } + } + catch (Exception ex) + { + Console.WriteLine($"Fehler in BackButton_Click: {ex.Message}"); + Console.WriteLine($"StackTrace: {ex.StackTrace}"); } } } diff --git a/Project_Periodensystem.View/LandingPage.axaml b/Project_Periodensystem.View/LandingPage.axaml index 8de217f..a782c3b 100644 --- a/Project_Periodensystem.View/LandingPage.axaml +++ b/Project_Periodensystem.View/LandingPage.axaml @@ -1,22 +1,18 @@ - - - + diff --git a/Project_Periodensystem.View/LandingPage.axaml.cs b/Project_Periodensystem.View/LandingPage.axaml.cs index d16c698..a78ce8c 100644 --- a/Project_Periodensystem.View/LandingPage.axaml.cs +++ b/Project_Periodensystem.View/LandingPage.axaml.cs @@ -1,15 +1,34 @@ +using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.VisualTree; +using System; namespace Project_Periodensystem.View { public partial class LandingPage : UserControl { + private Button? startButton; + private DateTime lastClickTime = DateTime.MinValue; + private const int DEBOUNCE_MS = 500; + public LandingPage() { + Logger.Log("=== LandingPage wird initialisiert ==="); InitializeComponent(); + startButton = this.Find - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Project_Periodensystem.View/PeriodicTablePage.axaml.cs b/Project_Periodensystem.View/PeriodicTablePage.axaml.cs index 54a3d89..e23a2b6 100644 --- a/Project_Periodensystem.View/PeriodicTablePage.axaml.cs +++ b/Project_Periodensystem.View/PeriodicTablePage.axaml.cs @@ -1,15 +1,52 @@ +using System; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.Media; +using Avalonia; using Avalonia.VisualTree; +using Project_Periodensystem.Persistence; namespace Project_Periodensystem.View { + // Converter to map element series to a color + public class SeriesToColorConverter + { + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + // Example: Map series string to color + if (value == null) return new SolidColorBrush(Colors.Gray); + switch (value.ToString()) + { + case "Alkali Metal": + return new SolidColorBrush(Colors.OrangeRed); + case "Alkaline Earth Metal": + return new SolidColorBrush(Colors.Gold); + case "Transition Metal": + return new SolidColorBrush(Colors.LightBlue); + case "Metalloid": + return new SolidColorBrush(Colors.LightGreen); + case "Nonmetal": + return new SolidColorBrush(Colors.LightGray); + case "Halogen": + return new SolidColorBrush(Colors.Violet); + case "Noble Gas": + return new SolidColorBrush(Colors.Cyan); + default: + return new SolidColorBrush(Colors.Gray); + } + } + } + public partial class PeriodicTablePage : UserControl { + private readonly Grid periodicGrid; + public PeriodicTablePage() { InitializeComponent(); + periodicGrid = this.Find("PeriodicGrid") ?? throw new Exception("PeriodicGrid not found"); + InitializePeriodicTable(); } private void InitializeComponent() @@ -17,12 +54,92 @@ namespace Project_Periodensystem.View AvaloniaXamlLoader.Load(this); } - private void AboutButton_Click(object sender, RoutedEventArgs e) + private void InitializePeriodicTable() { - var window = this.GetVisualRoot() as MainWindow; - if (window != null) + try { - window.ShowAbout(); + Logger.Log("Initialisiere PeriodicTable..."); + foreach (var element in PeriodicTableData.Elements) + { + var border = new Border + { + Width = 58, + Height = 58, + Margin = new Thickness(1), + CornerRadius = new CornerRadius(4), + BorderThickness = new Thickness(1), + BorderBrush = new SolidColorBrush(Colors.Gray) + }; + + var stackPanel = new StackPanel(); + + // Symbol + stackPanel.Children.Add(new TextBlock + { + Text = element.Symbol, + FontSize = 20, + Foreground = new SolidColorBrush(Colors.White), + HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center + }); + + // Atomic Number + stackPanel.Children.Add(new TextBlock + { + Text = element.AtomicNumber.ToString(), + FontSize = 12, + Foreground = new SolidColorBrush(Colors.White), + HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center + }); + + border.Child = stackPanel; + + // Set background color based on element series + var converter = new SeriesToColorConverter(); + border.Background = converter.Convert(element.Series, typeof(IBrush), null!, System.Globalization.CultureInfo.CurrentCulture) as IBrush; + + // Position the element - adjust for 0-based indexing + Grid.SetRow(border, Math.Max(0, element.Row - 1)); + Grid.SetColumn(border, Math.Max(0, element.Column - 1)); + + periodicGrid.Children.Add(border); + } + Logger.Log("PeriodicTable wurde initialisiert"); + } + catch (Exception ex) + { + var mainWindow = this.FindAncestorOfType(); + if (mainWindow != null) + { + Logger.Log("MainWindow gefunden, navigiere zu About..."); + mainWindow.ShowAbout(); + } + else + { + Logger.Log("FEHLER: MainWindow nicht gefunden!"); + } + Logger.Log($"FEHLER im PeriodicTable-Initialisierer: {ex.Message}"); + } + } + + private void AboutButton_Click(object? sender, RoutedEventArgs e) + { + try + { + Logger.Log("AboutButton wurde geklickt"); + var mainWindow = this.FindAncestorOfType(); + if (mainWindow != null) + { + Logger.Log("Navigation zu About Page"); + mainWindow.ShowAbout(); + } + else + { + Logger.Log("FEHLER: MainWindow nicht gefunden"); + } + } + catch (Exception ex) + { + Logger.Log($"FEHLER im AboutButton_Click: {ex.Message}"); } } } diff --git a/Project_Periodensystem.View/logger.cs b/Project_Periodensystem.View/logger.cs new file mode 100644 index 0000000..4fa77e3 --- /dev/null +++ b/Project_Periodensystem.View/logger.cs @@ -0,0 +1,54 @@ +using System; +using System.IO; + +namespace Project_Periodensystem.View +{ + public static class Logger + { + private static readonly string LogDirectory = AppDomain.CurrentDomain.BaseDirectory; + private static readonly string LogFile = Path.Combine(LogDirectory, "app_log.txt"); + + public static void Log(string message) + { + try + { + // Ensure directory exists + Directory.CreateDirectory(LogDirectory); + + var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - {message}"; + // Write immediately to console for direct feedback + Console.WriteLine(logMessage); + + // Append to file with direct flush + using (StreamWriter sw = File.AppendText(LogFile)) + { + sw.WriteLine(logMessage); + sw.Flush(); + } + } + catch (Exception ex) + { + Console.WriteLine($"Logging failed: {ex.Message}"); + Console.WriteLine($"Attempted to write to: {LogFile}"); + Console.WriteLine($"Original message: {message}"); + } + } + + static Logger() + { + // Clear log on startup + try + { + if (File.Exists(LogFile)) + { + File.Delete(LogFile); + } + Log("=== Logging started ==="); + } + catch (Exception ex) + { + Console.WriteLine($"Failed to initialize log file: {ex.Message}"); + } + } + } +} \ No newline at end of file