Projekt_SS25/Project_Periodensystem.View/PeriodicTablePage.axaml.cs

147 lines
5.3 KiB
C#

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<Grid>("PeriodicGrid") ?? throw new Exception("PeriodicGrid not found");
InitializePeriodicTable();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void InitializePeriodicTable()
{
try
{
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<MainWindow>();
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<MainWindow>();
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}");
}
}
}
}