Projekt_SS25/Project_Periodensystem.View/PeriodicTablePage.axaml.cs

161 lines
5.9 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Project_Periodensystem.Model; // ElementModel should be here
using Project_Periodensystem.Persistence;
using Project_Periodensystem.View.Converters;
using System;
using System.Globalization;
using System.Linq;
namespace Project_Periodensystem.View
{
public partial class PeriodicTablePage : UserControl
{
private readonly Grid? periodicGrid;
public PeriodicTablePage()
{
InitializeComponent();
periodicGrid = this.Find<Grid>("PeriodicGrid");
InitializePeriodicTable();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void InitializePeriodicTable()
{
try
{
Logger.Log("Initialisiere PeriodicTable...");
// Prüfen ob das Grid gefunden wurde
if (periodicGrid == null)
{
Logger.Log("FEHLER: PeriodicGrid nicht gefunden!");
return;
}
// Prüfen ob Elemente vorhanden sind
if (PeriodicTableData.Elements == null || !PeriodicTableData.Elements.Any())
{
Logger.Log("FEHLER: Keine Elemente in PeriodicTableData!");
return;
}
// Debug: Alle Elemente ausgeben
Logger.Log($"Anzahl Elemente: {PeriodicTableData.Elements.Count()}");
var firstFew = PeriodicTableData.Elements.Take(5);
foreach (var el in firstFew)
{
Logger.Log($"Element: {el.Symbol} - Row: {el.Row}, Column: {el.Column}");
}
// Speziell nach H und He suchen
var hydrogen = PeriodicTableData.Elements.FirstOrDefault(e => e.Symbol == "H");
var helium = PeriodicTableData.Elements.FirstOrDefault(e => e.Symbol == "He");
if (hydrogen != null)
Logger.Log($"Wasserstoff gefunden: Row {hydrogen.Row}, Column {hydrogen.Column}");
else
Logger.Log("Wasserstoff NICHT gefunden!");
if (helium != null)
Logger.Log($"Helium gefunden: Row {helium.Row}, Column {helium.Column}");
else
Logger.Log("Helium NICHT gefunden!");
// Elemente hinzufügen
foreach (var element in PeriodicTableData.Elements)
{
Logger.Log($"Füge Element hinzu: {element.Symbol} ({element.Row},{element.Column})");
CreateElementButton(element);
}
Logger.Log("PeriodicTable wurde initialisiert");
}
catch (Exception ex)
{
Logger.Log($"FEHLER beim Initialisieren des PeriodicTable: {ex.Message}");
Logger.Log($"StackTrace: {ex.StackTrace}");
}
}
private void CreateElementButton(Element element)
{
var button = new Button { Classes = { "ElementTile" } };
var panel = new StackPanel();
// Set background color based on element series
var backgroundColor = new SeriesToColorConverter().Convert(element.Series, typeof(Brush), null, CultureInfo.InvariantCulture) as Brush;
if (backgroundColor != null)
{
button.Background = backgroundColor;
}
var symbolText = new TextBlock
{
Text = element.Symbol,
Classes = { "Symbol" },
Foreground = new SolidColorBrush(Colors.White),
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center
};
var numberText = new TextBlock
{
Text = element.AtomicNumber.ToString(),
Classes = { "Number" },
Foreground = new SolidColorBrush(Colors.White),
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center
};
panel.Children.Add(numberText); // Number on top
panel.Children.Add(symbolText); // Symbol below
button.Content = panel;
// FIX: Korrekte Grid-Positionierung ohne Math.Max für Spalten!
int gridRow = element.Row; // Row direkt verwenden (0-basiert)
int gridColumn = element.Column; // Column direkt verwenden (0-basiert)
Logger.Log($"Element {element.Symbol}: Original({element.Row},{element.Column}) -> Grid({gridRow},{gridColumn})");
Grid.SetRow(button, gridRow);
Grid.SetColumn(button, gridColumn);
if (periodicGrid != null)
{
periodicGrid.Children.Add(button);
Logger.Log($"Element {element.Symbol} wurde zum Grid hinzugefügt an Position ({gridRow},{gridColumn})");
}
}
private void ThemeButton_Click(object? sender, RoutedEventArgs e)
{
var nextTheme = MainWindow.CurrentTheme switch
{
AppTheme.Dark => AppTheme.Light,
AppTheme.Light => AppTheme.Classic,
_ => AppTheme.Dark
};
if (this.Parent is ContentControl content && content.Parent is MainWindow mainWindow)
{
mainWindow.UpdateTheme(nextTheme);
}
}
private void AboutButton_Click(object? sender, RoutedEventArgs e)
{
if (this.Parent is ContentControl content && content.Parent is MainWindow mainWindow)
{
mainWindow.ShowAboutPage();
}
}
}
}