Files
pokerfreak/winui/Seiten/Ui.cs
T

346 lines
15 KiB
C#

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;
/// <summary>
/// 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.
/// </summary>
public static class Ui
{
private static Brush Res(string k) => (Brush)Application.Current.Resources[k];
/// <summary>Farbname der Ansicht ("gruen", "rot", ...; "" = Text) -> Farbe.</summary>
public static Color Farbe(string? ton) => ton switch
{
"gruen" => Color.FromArgb(255, 0x6C, 0xCB, 0x5F),
"rot" => Color.FromArgb(255, 0xFF, 0x6B, 0x63),
"orange" => Color.FromArgb(255, 0xFC, 0xB0, 0x40),
"gelb" => Color.FromArgb(255, 0xFF, 0xD6, 0x0A),
"blau" => Color.FromArgb(255, 0x4C, 0xA6, 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(0xFF, 0xA8, 0xAB, 0xB2), // zweite Textfarbe
_ => Color.FromArgb(255, 0xFF, 0xFF, 0xFF),
};
public static Brush Pinsel(string? ton) => new SolidColorBrush(Farbe(ton));
/// <summary>Die Farbe mit Deckkraft (wie `.opacity(0.14)`).</summary>
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(0xFF, 0x78, 0x7C, 0x85)); // dritte Textfarbe
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;
}
/// <summary>.caption in .secondary</summary>
public static TextBlock Klein(string text, bool umbruch = false) =>
Text(text, 12, pinsel: Zweit, umbruch: umbruch);
/// <summary>.caption2</summary>
public static TextBlock Winzig(string text, Brush? pinsel = null, bool fett = false) =>
Text(text, 11, pinsel: pinsel ?? Zweit, fett: fett);
/// <summary>Gerundete Zahlenschrift wie `.design(.rounded)` - unter Windows die Variable-Schrift.</summary>
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;
}
/// <summary>Ein Symbol; ohne Ton erbt es die Textfarbe (kein gesetztes null).</summary>
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;
}
/// <summary>Eine Flaeche wie `.background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 14))`.</summary>
public static Border Flaeche(UIElement? inhalt, double radius = 14, double weiss = 0.06, double polster = 10) => new()
{
Background = Weiss(Math.Min(weiss, 0.05)),
BorderBrush = Weiss(0.07),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(Math.Min(radius, 8)), // eine Rundung fuer alles (Windows 11)
Padding = new Thickness(polster),
Child = inhalt,
};
/// <summary>Eine getoente Flaeche: `.background(farbe.opacity(0.12), in: RoundedRectangle(10))`.</summary>
public static Border Getoent(UIElement? inhalt, string ton, double deckkraft = 0.12, double radius = 10, double polster = 10) => new()
{
Background = Pinsel(ton, deckkraft),
BorderBrush = Pinsel(ton, 0.35),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(Math.Min(radius, 8)),
Padding = new Thickness(polster),
Child = inhalt,
};
/// <summary>Abzeichen: kleiner Text in einer getoenten Kapsel.</summary>
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(4),
Padding = new Thickness(7, 1, 7, 2),
VerticalAlignment = VerticalAlignment.Center,
Child = inhalt,
};
}
/// <summary>Abschnittskopf: "Tische" halbfett in .secondary, daneben ein leiser Hinweis.</summary>
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;
}
/// <summary>Eine Spielkarte: Rang und Farbe, rot oder hell, auf dunklem Grund mit Rand (KarteAnsicht).</summary>
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,
};
}
/// <summary>Eine kleine Karte fuer Tischzeilen.</summary>
public static Border Minikarte(string? roh)
{
var b = Spielkarte(roh);
b.MinWidth = 24;
b.Height = 22;
b.CornerRadius = new CornerRadius(4);
((TextBlock)b.Child).FontSize = 11.5;
return b;
}
/// <summary>Feine Trennlinie in Karten und Listen.</summary>
public static Border Linie() => new() { Height = 1, Background = Weiss(0.06) };
/// <summary>Titel ueber einer Karte.</summary>
public static TextBlock Ueberschrift(string text) => Text(text, 13, fett: true).Mit(t => t.Margin = new Thickness(2, 4, 0, 0));
/// <summary>Ein leerer Kartenplatz (gestrichelt wie im Minitisch).</summary>
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;
}
/// <summary>Links etwas, rechts etwas, dazwischen Platz (HStack mit Spacer).</summary>
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,
};
/// <summary>Langer Text hinter einem kleinen Zeichen: beim Zeigen als Tooltip, auf Klick als Blase.</summary>
public sealed class Info : Grid
{
private readonly TextBlock _blase = Ui.Text("", 12, umbruch: true), _tipp = Ui.Text("", 12, umbruch: true);
private readonly FontIcon _zeichen;
private bool _warn;
public Info(string text = "", bool warn = false)
{
_zeichen = Zeichen("\uE946", 12, "grau");
_blase.MaxWidth = _tipp.MaxWidth = 300;
var b = new Button
{
Content = _zeichen, Padding = new Thickness(4), MinWidth = 0, MinHeight = 0,
Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)), BorderThickness = new Thickness(0),
Flyout = new Flyout { Content = _blase },
};
ToolTipService.SetToolTip(b, new ToolTip { Content = _tipp });
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(b, "Mehr dazu");
VerticalAlignment = VerticalAlignment.Center;
Children.Add(b);
Setzen(text, warn);
}
/// <summary>Ohne Text ist das Zeichen weg; `warn` macht es zum orangen Dreieck.</summary>
public void Setzen(string? text, bool warn = false)
{
if (_blase.Text != (text ?? "")) _blase.Text = _tipp.Text = text ?? "";
if (warn != _warn) { _warn = warn; _zeichen.Glyph = warn ? "\uE7BA" : "\uE946"; _zeichen.Foreground = Pinsel(warn ? "orange" : "grau"); }
Visibility = string.IsNullOrEmpty(text) ? Visibility.Collapsed : Visibility.Visible;
}
}
/// <summary>Ein schlichter Symbolknopf ohne Rahmen (`.buttonStyle(.borderless)`).</summary>
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),
// wie die kleinen Zahlknoepfe: kein Fokus - sonst springt er beim Neuzeichnen ins naechste Feld
IsTabStop = false,
};
ToolTipService.SetToolTip(b, hilfe);
b.Click += (_, _) => tun();
return b;
}
private static class Colors
{
public static Color Transparent => Color.FromArgb(0, 0, 0, 0);
}
}
/// <summary>
/// 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.
/// </summary>
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; try { Getippt?.Invoke(); } catch (Exception x) { Merker.Absturz(x, behandelt: true); } } };
}
public bool An
{
get => _an;
set { _an = value; Opacity = value ? 1 : 0.4; }
}
}
public static class UiErweiterung
{
public static T Spalte<T>(this T e, int spalte) where T : FrameworkElement
{
Grid.SetColumn(e, spalte);
return e;
}
public static T Zeile<T>(this T e, int zeile) where T : FrameworkElement
{
Grid.SetRow(e, zeile);
return e;
}
public static T Mit<T>(this T e, Action<T> 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;
}
}