Files
LlamaCasty/ViewModels/MainViewModel.Scoreboard.cs
T
gramps 85a4a8a7d3 feat(scoreboard): the layer is available to ALL scenes (YT_ACTIVITY_TICKER)
Creator ruling: the Scoreboard layer exists in every scene rather than being added five times.
ScoreboardHealer.Ensure keeps exactly one per scene — healed on load like the Background and Brand
logo — so a scene can never end up with two, and 'off' is the ordinary per-scene eye toggle rather
than a delete.

It is appended as the TOPMOST layer on purpose: a full-width strip draws over the scene, and
inserting it above the background would break the Brand logo's documented invariant (directly above
the background). That choice is also why the layer-reorder test still passes untouched.

Also: a layer-list icon for the type, so the row is not blank.

Guard: ScoreboardHealerTests — one per scene, topmost, exact strip geometry, idempotent, and a
duplicate healed back to one.
2026-10-30 14:50:20 -07:00

166 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using ytLive.Helpers;
using ytLive.Models;
using ytLive.Services;
using ytLive.Services.Compositor;
namespace ytLive.ViewModels;
/// <summary>The scoreboard strip: session tallies rendered as a full-width bar, per scene.
///
/// <para>It is a <see cref="SourceType.Scoreboard"/> layer, so a creator gets the same eye toggle,
/// drag and per-scene presence as every other element — "prominent on BRB, dialled back on Live" is
/// geometry, not a second mechanism.</para></summary>
public partial class MainViewModel
{
private readonly ScoreboardRecorder _scoreboard = new();
private ScoreboardTiles _scoreboardTiles = ScoreboardTiles.Default;
private ScoreboardStyle _scoreboardStyle = new();
private ScoreboardPresenter? _scoreboardPresenter;
private WriteableBitmap? _scoreboardPreview;
private Source? _scoreboardPreviewSource;
internal ScoreboardRecorder ScoreboardRecorder => _scoreboard;
internal ScoreboardTiles ScoreboardTilesState => _scoreboardTiles;
/// <summary>Composited strip frame for the output path (cached by the presenter).</summary>
internal VideoFrame? ScoreboardFrame => _scoreboardPresenter?.Frame;
internal void StartScoreboardSession()
{
_scoreboard.StartSession();
RefreshScoreboardPreviews();
}
internal void EndScoreboardSession()
{
_scoreboard.EndSession();
RefreshScoreboardPreviews();
}
/// <summary>One Scoreboard layer in every scene, healed on load — "available to all scenes"
/// (creator ruling 2026-10-30). Per-scene visibility is the ordinary eye toggle.</summary>
internal void EnsureScoreboards()
{
if (ScoreboardHealer.Ensure(Scenes))
{
foreach (var scene in Scenes) _sceneGraph.InvalidateBake(scene);
RefreshScoreboardPreviews();
OnPropertyChanged(nameof(LayerCount));
}
}
private void InitScoreboard()
{
_scoreboardPresenter = new ScoreboardPresenter(_scoreboard, _scoreboardStyle)
{
// placeholder so SetTiles below drives the first render
};
_scoreboardPresenter.SetTiles(_scoreboardTiles);
_scoreboardPresenter.SetStyle(_scoreboardStyle);
_scoreboard.Changed += RefreshScoreboardPreviews;
}
/// <summary>Snapshot of the strip presenters as themed properties, for the properties panel
/// (unit 7) — kept here so the panel binds one object instead of six primitives.</summary>
internal ScoreboardStyle ScoreboardStyleState => _scoreboardStyle;
// ─── Properties panel surface (per layout: one scoreboard look) ────────────
public string ScoreboardTextColor
{
get => _scoreboardStyle.TextColor;
set { _scoreboardStyle = _scoreboardStyle with { TextColor = value }; PushStyle(); }
}
public bool ScoreboardBackgroundEnabled
{
get => _scoreboardStyle.BackgroundEnabled;
set { _scoreboardStyle = _scoreboardStyle with { BackgroundEnabled = value }; PushStyle(); }
}
public string ScoreboardBackgroundColor
{
get => _scoreboardStyle.BackgroundColor;
set { _scoreboardStyle = _scoreboardStyle with { BackgroundColor = value }; PushStyle(); }
}
public double ScoreboardBackgroundOpacity
{
get => _scoreboardStyle.BackgroundOpacity;
set
{
_scoreboardStyle = _scoreboardStyle with { BackgroundOpacity = Math.Clamp(value, 0, 1) };
PushStyle();
}
}
public double ScoreboardFontSize
{
get => _scoreboardStyle.FontSize;
set
{
_scoreboardStyle = _scoreboardStyle with
{
FontSize = Math.Clamp(value, ScoreboardLayout.FloorFontSize, ScoreboardLayout.MaxFontSize),
};
PushStyle();
}
}
/// <summary>The colours offered for text and plate: the app's one palette.</summary>
public IReadOnlyList<NamedColor> ScoreboardColorOptions => BorderColorPalette.All;
public bool ScoreboardShowSubscribers { get => _scoreboardTiles.Subscribers; set => SetTile(t => t.Subscribers = value); }
public bool ScoreboardShowWatchHours { get => _scoreboardTiles.WatchHours; set => SetTile(t => t.WatchHours = value); }
public bool ScoreboardShowSuperChats { get => _scoreboardTiles.SuperChats; set => SetTile(t => t.SuperChats = value); }
public bool ScoreboardShowNewMembers { get => _scoreboardTiles.NewMembers; set => SetTile(t => t.NewMembers = value); }
public bool ScoreboardShowMilestones { get => _scoreboardTiles.Milestones; set => SetTile(t => t.Milestones = value); }
public bool ScoreboardShowLastMember { get => _scoreboardTiles.LastMember; set => SetTile(t => t.LastMember = value); }
public bool ScoreboardShowLastSuperChat { get => _scoreboardTiles.LastSuperChat; set => SetTile(t => t.LastSuperChat = value); }
public bool ScoreboardShowLastGift { get => _scoreboardTiles.LastGift; set => SetTile(t => t.LastGift = value); }
private void SetTile(Action<ScoreboardTiles> change)
{
change(_scoreboardTiles);
_scoreboardPresenter?.SetTiles(_scoreboardTiles);
RefreshScoreboardPreviews();
OnPropertyChanged(nameof(ScoreboardTilesState));
}
private void PushStyle()
{
_scoreboardPresenter?.SetStyle(_scoreboardStyle);
RefreshScoreboardPreviews();
OnPropertyChanged(nameof(ScoreboardStyleState));
}
/// <summary>Publishes the strip to every scoreboard layer's preview element. The pump thread
/// must never touch a WriteableBitmap, so this runs on the UI thread only.</summary>
private void RefreshScoreboardPreviews()
{
var frame = _scoreboardPresenter?.Frame;
foreach (var scene in Scenes)
foreach (var source in scene.Elements.OfType<Source>().Where(s => s.IsScoreboard))
{
if (frame is null) { source.VideoImageSource = null; continue; }
if (_scoreboardPreview is null || _scoreboardPreview.PixelWidth != frame.Width ||
_scoreboardPreview.PixelHeight != frame.Height)
{
_scoreboardPreview = new WriteableBitmap(frame.Width, frame.Height, 96, 96, PixelFormats.Bgra32, null);
}
_scoreboardPreview.WritePixels(new System.Windows.Int32Rect(0, 0, frame.Width, frame.Height),
frame.BgraPixels, frame.Width * 4, 0);
source.VideoImageSource = _scoreboardPreview;
_scoreboardPreviewSource = source;
}
}
}