using Microsoft.UI.Input;
using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Windows.UI;
using Windows.UI.Text;
namespace Pokerfreak.Cockpit.Seiten;
///
/// Bausteine fuer Oberflaeche aus Code, optisch nach der Mac-App (mac/Sources/Cockpit/Bausteine.swift):
/// dunkles Glas, Flaechen in Weiss mit wenig Deckkraft, Kapseln, abgerundete Kacheln, Systemfarben.
///
public static class Ui
{
private static Brush Res(string k) => (Brush)Application.Current.Resources[k];
/// Farbname der Ansicht ("gruen", "rot", ...; "" = Text) -> Farbe.
public static Color Farbe(string? ton) => ton switch
{
"gruen" => Color.FromArgb(255, 0x30, 0xD1, 0x58),
"rot" => Color.FromArgb(255, 0xFF, 0x45, 0x3A),
"orange" => Color.FromArgb(255, 0xFF, 0x9F, 0x0A),
"gelb" => Color.FromArgb(255, 0xFF, 0xD6, 0x0A),
"blau" => Color.FromArgb(255, 0x0A, 0x84, 0xFF),
"indigo" => Color.FromArgb(255, 0x5E, 0x5C, 0xE6),
"teal" => Color.FromArgb(255, 0x40, 0xC8, 0xE0),
"lila" => Color.FromArgb(255, 0xBF, 0x5A, 0xF2),
"grau" => Color.FromArgb(0x99, 0xEB, 0xEB, 0xF5), // .secondary auf dunkel
_ => Color.FromArgb(255, 0xFF, 0xFF, 0xFF),
};
public static Brush Pinsel(string? ton) => new SolidColorBrush(Farbe(ton));
/// Die Farbe mit Deckkraft (wie `.opacity(0.14)`).
public static Brush Pinsel(string? ton, double deckkraft)
{
var c = Farbe(ton);
return new SolidColorBrush(Color.FromArgb((byte)(c.A * deckkraft), c.R, c.G, c.B));
}
public static Brush Weiss(double deckkraft) => new SolidColorBrush(Color.FromArgb((byte)(255 * deckkraft), 255, 255, 255));
public static Brush Schwarz(double deckkraft) => new SolidColorBrush(Color.FromArgb((byte)(255 * deckkraft), 0, 0, 0));
public static Brush Zweit => Pinsel("grau");
public static Brush Dritt => new SolidColorBrush(Color.FromArgb(0x4D, 0xEB, 0xEB, 0xF5)); // .tertiary
public static Brush Akzent => Pinsel("blau");
public static TextBlock Text(string text, double groesse = 14, string? ton = null, bool fett = false,
bool umbruch = false, Brush? pinsel = null, bool zahlen = false)
{
var t = new TextBlock
{
Text = text,
FontSize = groesse,
FontWeight = fett ? FontWeights.SemiBold : FontWeights.Normal,
TextWrapping = umbruch ? TextWrapping.WrapWholeWords : TextWrapping.NoWrap,
TextTrimming = umbruch ? TextTrimming.None : TextTrimming.CharacterEllipsis,
};
if (zahlen) Microsoft.UI.Xaml.Documents.Typography.SetNumeralAlignment(t, FontNumeralAlignment.Tabular);
if (pinsel != null) t.Foreground = pinsel;
else if (ton != null) t.Foreground = Pinsel(ton);
return t;
}
/// .caption in .secondary
public static TextBlock Klein(string text, bool umbruch = false) =>
Text(text, 12, pinsel: Zweit, umbruch: umbruch);
/// .caption2
public static TextBlock Winzig(string text, Brush? pinsel = null, bool fett = false) =>
Text(text, 11, pinsel: pinsel ?? Zweit, fett: fett);
/// Gerundete Zahlenschrift wie `.design(.rounded)` - unter Windows die Variable-Schrift.
public static TextBlock Gross(string text, double groesse, string? ton = null, bool fett = true)
{
var t = Text(text, groesse, ton, fett, zahlen: true);
t.FontFamily = new FontFamily("Segoe UI Variable Display");
return t;
}
/// Ein Symbol; ohne Ton erbt es die Textfarbe (kein gesetztes null).
public static FontIcon Zeichen(string glyph, double groesse = 16, string? ton = null)
{
var z = new FontIcon { Glyph = glyph, FontSize = groesse };
if (ton != null) z.Foreground = Pinsel(ton);
return z;
}
/// Eine Flaeche wie `.background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 14))`.
public static Border Flaeche(UIElement? inhalt, double radius = 14, double weiss = 0.06, double polster = 10) => new()
{
Background = Weiss(weiss),
CornerRadius = new CornerRadius(radius),
Padding = new Thickness(polster),
Child = inhalt,
};
/// Eine getoente Flaeche: `.background(farbe.opacity(0.12), in: RoundedRectangle(10))`.
public static Border Getoent(UIElement? inhalt, string ton, double deckkraft = 0.12, double radius = 10, double polster = 10) => new()
{
Background = Pinsel(ton, deckkraft),
CornerRadius = new CornerRadius(radius),
Padding = new Thickness(polster),
Child = inhalt,
};
/// Abzeichen: kleiner Text in einer getoenten Kapsel.
public static Border Abzeichen(string text, string ton, string? glyph = null)
{
var inhalt = Reihe(4);
if (glyph != null) inhalt.Children.Add(Zeichen(glyph, 9, ton));
inhalt.Children.Add(Text(text, 11, ton, fett: true));
return new Border
{
Background = Pinsel(ton, 0.14),
CornerRadius = new CornerRadius(10),
Padding = new Thickness(7, 1, 7, 2),
VerticalAlignment = VerticalAlignment.Center,
Child = inhalt,
};
}
/// Abschnittskopf: "Tische" halbfett in .secondary, daneben ein leiser Hinweis.
public static Grid Abschnitt(string titel, string? hinweis = null, UIElement? rechts = null)
{
var g = new Grid { ColumnSpacing = 6, Margin = new Thickness(0, 4, 0, 0) };
g.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
g.ColumnDefinitions.Add(new ColumnDefinition());
g.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
g.Children.Add(Text(titel, 12, pinsel: Zweit, fett: true));
if (hinweis != null) g.Children.Add(Winzig(hinweis, Dritt).Spalte(1).Mit(t => t.VerticalAlignment = VerticalAlignment.Bottom));
if (rechts is FrameworkElement r) g.Children.Add(r.Spalte(2));
return g;
}
/// Eine Spielkarte: Rang und Farbe, rot oder hell, auf dunklem Grund mit Rand (KarteAnsicht).
public static Border Spielkarte(string? roh, bool gross = false)
{
var k = Kern.Format.Karte(roh);
var t = new TextBlock
{
Text = k?.Text ?? "?",
FontSize = gross ? 17 : 13,
FontWeight = FontWeights.Bold,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Foreground = k?.Rot == true ? Pinsel("rot") : Pinsel(""),
};
return new Border
{
MinWidth = gross ? 34 : 26,
Height = gross ? 44 : 32,
Padding = new Thickness(2, 0, 2, 0),
CornerRadius = new CornerRadius(5),
BorderThickness = new Thickness(1),
BorderBrush = Weiss(0.18),
Background = new SolidColorBrush(Color.FromArgb(255, 0x1C, 0x1C, 0x1E)),
Child = t,
};
}
/// Ein leerer Kartenplatz (gestrichelt wie im Minitisch).
public static Border Kartenplatz(double b = 24, double h = 29) => new()
{
Width = b,
Height = h,
CornerRadius = new CornerRadius(5),
BorderThickness = new Thickness(1),
BorderBrush = Weiss(0.18),
};
public static StackPanel Reihe(double abstand = 8, params UIElement[] kinder)
{
var s = new StackPanel { Orientation = Orientation.Horizontal, Spacing = abstand };
foreach (var k in kinder) s.Children.Add(k);
return s;
}
public static StackPanel Spalte(double abstand = 4, params UIElement[] kinder)
{
var s = new StackPanel { Spacing = abstand };
foreach (var k in kinder) s.Children.Add(k);
return s;
}
/// Links etwas, rechts etwas, dazwischen Platz (HStack mit Spacer).
public static Grid Zeile(UIElement links, UIElement? rechts, double abstand = 8)
{
var g = new Grid { ColumnSpacing = abstand };
g.ColumnDefinitions.Add(new ColumnDefinition());
g.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
g.Children.Add(links);
if (rechts is FrameworkElement r) g.Children.Add(r.Spalte(1));
return g;
}
public static Microsoft.UI.Xaml.Shapes.Ellipse Punkt(string ton, double d = 8) => new()
{
Width = d,
Height = d,
Fill = Pinsel(ton),
VerticalAlignment = VerticalAlignment.Center,
};
/// Ein schlichter Symbolknopf ohne Rahmen (`.buttonStyle(.borderless)`).
public static Button Symbolknopf(string glyph, string hilfe, Action tun, double groesse = 12, string? ton = "grau")
{
var b = new Button
{
Content = Zeichen(glyph, groesse, ton),
Padding = new Thickness(5),
MinWidth = 0,
MinHeight = 0,
Background = new SolidColorBrush(Colors.Transparent),
BorderThickness = new Thickness(0),
};
ToolTipService.SetToolTip(b, hilfe);
b.Click += (_, _) => tun();
return b;
}
private static class Colors
{
public static Color Transparent => Color.FromArgb(0, 0, 0, 0);
}
}
///
/// Eine Flaeche, die man antippen kann: Hover hellt auf, Druck gibt nach (KartenDruck), deaktiviert
/// halb durchsichtig. Ohne die Knopf-Optik von WinUI - so sehen Kacheln und Kapseln aus wie am Mac.
///
public sealed class Tippflaeche : Grid
{
private readonly Border _schleier = new() { IsHitTestVisible = false };
private bool _an = true;
public event Action? Getippt;
public Tippflaeche(UIElement inhalt, double radius = 10)
{
Children.Add(inhalt);
_schleier.CornerRadius = new CornerRadius(radius);
Children.Add(_schleier);
Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
PointerEntered += (_, _) => { if (_an) _schleier.Background = Ui.Weiss(0.05); ProtectedCursor = InputSystemCursor.Create(InputSystemCursorShape.Hand); };
PointerExited += (_, _) => { _schleier.Background = null; Scale = new System.Numerics.Vector3(1); };
PointerPressed += (_, _) => { if (_an) { _schleier.Background = Ui.Weiss(0.10); Scale = new System.Numerics.Vector3(0.98f); } };
PointerReleased += (_, _) => { _schleier.Background = _an ? Ui.Weiss(0.05) : null; Scale = new System.Numerics.Vector3(1); };
Loaded += (_, _) => CenterPoint = new System.Numerics.Vector3((float)ActualWidth / 2, (float)ActualHeight / 2, 0);
SizeChanged += (_, _) => CenterPoint = new System.Numerics.Vector3((float)ActualWidth / 2, (float)ActualHeight / 2, 0);
Tapped += (_, e) => { if (_an) { e.Handled = true; Getippt?.Invoke(); } };
}
public bool An
{
get => _an;
set { _an = value; Opacity = value ? 1 : 0.4; }
}
}
public static class UiErweiterung
{
public static T Spalte(this T e, int spalte) where T : FrameworkElement
{
Grid.SetColumn(e, spalte);
return e;
}
public static T Zeile(this T e, int zeile) where T : FrameworkElement
{
Grid.SetRow(e, zeile);
return e;
}
public static T Mit(this T e, Action tun)
{
tun(e);
return e;
}
public static Tippflaeche Tippen(this UIElement e, Action tun, double radius = 10, string? hilfe = null)
{
var t = new Tippflaeche(e, radius);
t.Getippt += tun;
if (hilfe != null) ToolTipService.SetToolTip(t, hilfe);
return t;
}
}