219 lines
11 KiB
C#
219 lines
11 KiB
C#
using System.Text.Json.Nodes;
|
||
using Microsoft.UI.Xaml;
|
||
using Microsoft.UI.Xaml.Controls;
|
||
using Microsoft.UI.Xaml.Controls.Primitives;
|
||
using Microsoft.UI.Xaml.Media;
|
||
using Pokerfreak.Cockpit.Kern;
|
||
|
||
namespace Pokerfreak.Cockpit.Seiten;
|
||
|
||
/// <summary>
|
||
/// Der Reiter Protokoll (EntscheidungenGeld.swift `ProtokollAnsicht`): Gedanken und Ereignisse als
|
||
/// Strom in Festbreitenschrift, anhaltbar zum Lesen; Geld als Liste der Geldbewegungen mit Art-Filter.
|
||
/// </summary>
|
||
public sealed class ProtokollSeite : UserControl, ISeite
|
||
{
|
||
private static readonly FontFamily Fest = new("Cascadia Mono, Consolas");
|
||
|
||
private readonly CockpitZustand _z;
|
||
private readonly Segmente _teil = new(new[] { "Gedanken", "Ereignisse", "Geld" }, "protokoll.teil");
|
||
private readonly ToggleButton _pause = new()
|
||
{
|
||
Content = Ui.Zeichen("", 12),
|
||
Padding = new Thickness(8, 5, 8, 5),
|
||
CornerRadius = new CornerRadius(8),
|
||
};
|
||
private readonly Auswahl _art = new("Art");
|
||
private readonly TextBox _suche = new() { PlaceholderText = "suchen", FontSize = 12, Width = 150 };
|
||
private readonly Button _filter = new() { Content = Ui.Zeichen("", 12), Padding = new Thickness(8, 5, 8, 5) };
|
||
private readonly HashSet<string> _versteckt = new();
|
||
private readonly ToggleButton _nurSitzung = new() { Content = Ui.Zeichen("", 12), Padding = new Thickness(8, 5, 8, 5), CornerRadius = new CornerRadius(8) };
|
||
private double _sitzungsStart = double.NegativeInfinity;
|
||
|
||
/// <summary>Beginn der laufenden Sitzung: das letzte `zeitplan`-Ereignis mit phase "spielen".</summary>
|
||
private void SitzungMerken(JsonObject t)
|
||
{
|
||
_sitzungsStart = double.NegativeInfinity;
|
||
foreach (var z in t.Objekte("zeilen"))
|
||
if (z.Text("art") == "zeitplan" && (z.Text("text") ?? "").Contains("phase=spielen"))
|
||
_sitzungsStart = z.Zahl("zeit") ?? _sitzungsStart;
|
||
}
|
||
private static readonly string[] Farben = { "blau", "gruen", "orange", "lila", "teal", "gelb" };
|
||
|
||
/// <summary>Gleiche Art, gleiche Farbe - ein Hash auf die Palette, ueber Sitzungen stabil.</summary>
|
||
private static string Farbe(string? art)
|
||
{
|
||
if (string.IsNullOrEmpty(art)) return "grau";
|
||
var h = 0;
|
||
foreach (var b in art) h = (h * 31 + b) & 0x7fffffff;
|
||
return Farben[h % Farben.Length];
|
||
}
|
||
|
||
private bool Sichtbar(JsonObject z)
|
||
{
|
||
if (_nurSitzung.IsChecked == true && (z.Zahl("zeit") ?? double.NegativeInfinity) < _sitzungsStart) return false;
|
||
if (z.Text("art") is string a && _versteckt.Contains(a)) return false;
|
||
var suche = _suche.Text.Trim().ToLowerInvariant();
|
||
if (suche.Length == 0) return true;
|
||
return new[] { z.Text("art"), z.Text("raum"), z.Text("text") }
|
||
.Any(s => s != null && s.ToLowerInvariant().Contains(suche));
|
||
}
|
||
private readonly Border _uebung = Ui.Abzeichen("ÜBUNG", "blau");
|
||
private readonly StackPanel _liste = Ui.Spalte(6);
|
||
private readonly ScrollViewer _rollen;
|
||
private JsonObject? _angehalten;
|
||
private string _abdruck = "";
|
||
|
||
public ProtokollSeite(CockpitZustand z)
|
||
{
|
||
_z = z;
|
||
ToolTipService.SetToolTip(_pause, "Anhalten zum Lesen");
|
||
_teil.Gewaehlt += _ => { _abdruck = ""; Zeichnen(); };
|
||
_pause.Click += (_, _) => { _angehalten = _pause.IsChecked == true ? _z.Ansicht.Ticker : null; _abdruck = ""; Zeichnen(); };
|
||
_art.Geaendert += () => { _abdruck = ""; Zeichnen(); };
|
||
_suche.TextChanged += (_, _) => { _abdruck = ""; Zeichnen(); };
|
||
ToolTipService.SetToolTip(_nurSitzung, "Nur die laufende Sitzung zeigen");
|
||
_nurSitzung.Click += (_, _) => { _abdruck = ""; Zeichnen(); };
|
||
_filter.Padding = new Thickness(8, 5, 8, 5);
|
||
_filter.Flyout = new Flyout { Content = new StackPanel() };
|
||
ToolTipService.SetToolTip(_filter, "Arten ein-/ausblenden");
|
||
|
||
var kopf = new Grid { ColumnSpacing = 8, Padding = new Thickness(12) };
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition());
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.Children.Add(_teil);
|
||
kopf.Children.Add(_pause.Spalte(1).Mit(p => p.VerticalAlignment = VerticalAlignment.Center));
|
||
kopf.Children.Add(_art.Spalte(2).Mit(a => a.VerticalAlignment = VerticalAlignment.Center));
|
||
kopf.Children.Add(_uebung.Spalte(3));
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.Children.Add(_nurSitzung.Spalte(kopf.ColumnDefinitions.Count - 1).Mit(x => x.VerticalAlignment = VerticalAlignment.Center));
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.Children.Add(_suche.Spalte(kopf.ColumnDefinitions.Count - 1).Mit(x => x.VerticalAlignment = VerticalAlignment.Center));
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||
kopf.Children.Add(_filter.Spalte(5).Mit(x => x.VerticalAlignment = VerticalAlignment.Center));
|
||
|
||
_liste.Padding = new Thickness(12);
|
||
_rollen = new ScrollViewer { Content = _liste, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled };
|
||
var g = new Grid();
|
||
g.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
||
g.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
||
g.RowDefinitions.Add(new RowDefinition());
|
||
g.Children.Add(kopf);
|
||
g.Children.Add(new Border { Height = 1, Background = Ui.Weiss(0.08) }.Zeile(1));
|
||
g.Children.Add(_rollen.Zeile(2));
|
||
Content = g;
|
||
}
|
||
|
||
public void Zeichnen()
|
||
{
|
||
var geld = _teil.Index == 2;
|
||
_pause.Visibility = geld ? Visibility.Collapsed : Visibility.Visible;
|
||
_suche.Visibility = _teil.Index == 1 ? Visibility.Visible : Visibility.Collapsed;
|
||
_filter.Visibility = _teil.Index == 1 ? Visibility.Visible : Visibility.Collapsed;
|
||
_nurSitzung.Visibility = _teil.Index == 1 ? Visibility.Visible : Visibility.Collapsed;
|
||
_art.Visibility = geld ? Visibility.Visible : Visibility.Collapsed;
|
||
_uebung.Visibility = geld && _z.Ansicht.Geld.Wahr("uebung") ? Visibility.Visible : Visibility.Collapsed;
|
||
if (geld) GeldZeichnen();
|
||
else StromZeichnen(_teil.Index == 0);
|
||
}
|
||
|
||
private void StromZeichnen(bool gedanken)
|
||
{
|
||
var t = _angehalten ?? _z.Ansicht.Ticker;
|
||
var abdruck = (gedanken ? "g" : "e") + (gedanken ? t?["gedanken"] : t?["zeilen"]).Abdruck();
|
||
if (abdruck == _abdruck) return;
|
||
_abdruck = abdruck;
|
||
_liste.Children.Clear();
|
||
if (gedanken)
|
||
{
|
||
foreach (var g in t.Texte("gedanken"))
|
||
_liste.Children.Add(FestText(g));
|
||
}
|
||
else
|
||
{
|
||
SitzungMerken(t);
|
||
var arten = t.Objekte("zeilen").Select(z => z.Text("art")).Where(a => !string.IsNullOrEmpty(a))
|
||
.Select(a => a!).Distinct().OrderBy(a => a).ToList();
|
||
var panel = new StackPanel { Spacing = 4 };
|
||
foreach (var a in arten)
|
||
{
|
||
var cb = new CheckBox { Content = a, IsChecked = !_versteckt.Contains(a) };
|
||
var wert = a;
|
||
cb.Click += (_, _) => { if (cb.IsChecked == true) _versteckt.Remove(wert); else _versteckt.Add(wert); _abdruck = ""; Zeichnen(); };
|
||
panel.Children.Add(cb);
|
||
}
|
||
if (panel.Children.Count == 0) panel.Children.Add(Ui.Klein("noch keine Arten"));
|
||
(_filter.Flyout as Flyout)!.Content = panel;
|
||
|
||
foreach (var z in t.Objekte("zeilen").Where(Sichtbar))
|
||
{
|
||
var tb = FestText("");
|
||
tb.Inlines.Add(new Microsoft.UI.Xaml.Documents.Run { Text = Format.Uhr(z.Zahl("zeit")) + " ", Foreground = Ui.Zweit });
|
||
if (z.Text("art") is string art)
|
||
tb.Inlines.Add(new Microsoft.UI.Xaml.Documents.Run { Text = art + " ", Foreground = Ui.Pinsel(Farbe(art)), FontWeight = Microsoft.UI.Text.FontWeights.Bold });
|
||
tb.Inlines.Add(new Microsoft.UI.Xaml.Documents.Run
|
||
{
|
||
Text = string.Join(" · ", new[] { z.Text("raum"), z.Text("text") }.Where(s => !string.IsNullOrEmpty(s))),
|
||
});
|
||
_liste.Children.Add(tb);
|
||
}
|
||
}
|
||
if (_liste.Children.Count == 0)
|
||
_liste.Children.Add(Teile.Leer("Noch nichts", t.Text("leer") ?? "Der Strom läuft, sobald der Läufer arbeitet.", ""));
|
||
}
|
||
|
||
private static TextBlock FestText(string text) => new()
|
||
{
|
||
Text = text,
|
||
FontFamily = Fest,
|
||
FontSize = 11.5,
|
||
TextWrapping = TextWrapping.Wrap,
|
||
IsTextSelectionEnabled = true,
|
||
};
|
||
|
||
private void GeldZeichnen()
|
||
{
|
||
var g = _z.Ansicht.Geld;
|
||
var alle = g.Objekte("zeilen").ToList();
|
||
_art.Werte(alle.Select(z => z.Text("art")).OfType<string>().Distinct().OrderBy(a => a));
|
||
var abdruck = "geld" + g?["zeilen"].Abdruck() + _art.Wahl;
|
||
if (abdruck == _abdruck) return;
|
||
_abdruck = abdruck;
|
||
_liste.Children.Clear();
|
||
var zeilen = alle.Where(z => _art.Wahl == null || z.Text("art") == _art.Wahl).ToList();
|
||
if (zeilen.Count == 0)
|
||
{
|
||
_liste.Children.Add(Teile.Leer("Keine Geldbewegungen", g.Text("leer") ?? "Buy-in, Nachkauf und Aufstehen erscheinen hier.", ""));
|
||
return;
|
||
}
|
||
foreach (var z in zeilen)
|
||
{
|
||
var kopf = new Grid { ColumnSpacing = 6 };
|
||
foreach (var w in new[] { GridLength.Auto, GridLength.Auto, GridLength.Auto, new GridLength(1, GridUnitType.Star), GridLength.Auto, GridLength.Auto })
|
||
kopf.ColumnDefinitions.Add(new ColumnDefinition { Width = w });
|
||
kopf.Children.Add(Ui.Text(Format.Uhr(z.Zahl("zeit")), 12, pinsel: Ui.Zweit, zahlen: true).Mit(t => t.VerticalAlignment = VerticalAlignment.Center));
|
||
kopf.Children.Add(Ui.Text(z.Text("art") ?? "–", 14, fett: true).Spalte(1));
|
||
if (z.WahrOderNull("uebung") == true) kopf.Children.Add(Ui.Abzeichen("Übung", "blau").Spalte(2));
|
||
if (z.Zahl("betrag") is double b) kopf.Children.Add(Ui.Text(Format.Zahl(b, 2), 14, zahlen: true).Spalte(4));
|
||
if (z.Zahl("betrag_bb") is double bb)
|
||
kopf.Children.Add(Ui.Text(Format.Auto(bb) + " bb", 12, pinsel: Ui.Zweit).Spalte(5).Mit(t => t.VerticalAlignment = VerticalAlignment.Center));
|
||
var unter = new TextBlock
|
||
{
|
||
Text = string.Join(" · ", new[] { z.Text("raum"), z.Text("grund") }.Where(s => !string.IsNullOrEmpty(s))),
|
||
FontSize = 12,
|
||
Foreground = Ui.Zweit,
|
||
TextWrapping = TextWrapping.Wrap,
|
||
IsTextSelectionEnabled = true,
|
||
};
|
||
var s = Ui.Spalte(3, kopf, unter);
|
||
s.Padding = new Thickness(4, 6, 4, 6);
|
||
s.Opacity = z.WahrOderNull("uebung") == true ? 0.75 : 1;
|
||
if (_liste.Children.Count > 0) _liste.Children.Add(new Border { Height = 1, Background = Ui.Weiss(0.06) });
|
||
_liste.Children.Add(s);
|
||
}
|
||
}
|
||
}
|