704 lines
34 KiB
C#
704 lines
34 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using KernelPanic.Data;
|
|
using KernelPanic.Meta;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace KernelPanic.UI
|
|
{
|
|
/// <summary>A read-only receipt viewer. Revealing, selecting and skipping never grant rewards.</summary>
|
|
public sealed class GachaRevealView : IDisposable
|
|
{
|
|
private readonly VisualElement _root;
|
|
private readonly CompletedGachaReward[] _rewards;
|
|
private readonly List<Button> _tiles = new();
|
|
private readonly List<Label> _selectionMarkers = new();
|
|
private readonly ScrollView _gridScroll;
|
|
private readonly ScrollView _detailScroll;
|
|
private readonly VisualElement _grid;
|
|
private readonly Label _progress;
|
|
private readonly Label _event;
|
|
private readonly Label _help;
|
|
private readonly VisualElement _summary;
|
|
private readonly Button _next;
|
|
private readonly Button _all;
|
|
private readonly Button _continue;
|
|
private readonly Action _onContinue;
|
|
private IVisualElementScheduledItem _auto;
|
|
private float _nextRevealAt;
|
|
private int _columns = 1;
|
|
private bool _begun;
|
|
private bool _disposed;
|
|
private bool _leaving;
|
|
private bool _followReveal = true;
|
|
private bool _escapeHeld;
|
|
private bool _enterHeld;
|
|
private int _lastSubmitFrame = -1;
|
|
private int _signatureIndex = -1;
|
|
private float _signatureStartedAt;
|
|
private int _signaturePhase;
|
|
private VisualElement _signature;
|
|
private Label _signatureHeading;
|
|
private readonly List<IVisualElementScheduledItem> _transitions = new();
|
|
|
|
public int RevealedCount { get; private set; }
|
|
public int SelectedIndex { get; private set; } = -1;
|
|
public bool IsComplete => RevealedCount == _rewards.Length && !IsSignatureRevealing;
|
|
public bool IsSignatureRevealing => _signatureIndex >= 0;
|
|
|
|
public GachaRevealView(VisualElement root, CompletedGachaPull receipt, Action onContinue)
|
|
{
|
|
_root = root;
|
|
_root.focusable = true;
|
|
_onContinue = onContinue;
|
|
_rewards = new CompletedGachaReward[receipt.Rewards.Count];
|
|
for (int i = 0; i < _rewards.Length; i++) _rewards[i] = receipt.Rewards[i];
|
|
var shell = Box("reveal-shell");
|
|
shell.EnableInClassList("reveal-single", _rewards.Length == 1);
|
|
root.Add(shell);
|
|
var header = Box("reveal-header");
|
|
var heading = Box("reveal-heading");
|
|
heading.Add(Text(_rewards.Length > 1 ? "$ git pull --ten" : "$ git pull --single", "reveal-command"));
|
|
heading.Add(Text(receipt.HeaderText, "reveal-payment"));
|
|
header.Add(heading);
|
|
_progress = Text($"0 / {_rewards.Length:00} unpacked", "reveal-progress");
|
|
header.Add(_progress);
|
|
shell.Add(header);
|
|
_event = Text(_rewards.Length == 0 ? "No rewards to display." : $"Receiving objects: {_rewards.Length}. Ready to unpack.", "reveal-event");
|
|
shell.Add(_event);
|
|
|
|
var workspace = Box("reveal-workspace");
|
|
var board = Box("reveal-board");
|
|
_gridScroll = new ScrollView(ScrollViewMode.Vertical) { name = "PullRewardScroll" };
|
|
_gridScroll.AddToClassList("reveal-grid-scroll");
|
|
_grid = Box("reveal-grid");
|
|
_gridScroll.Add(_grid);
|
|
board.Add(_gridScroll);
|
|
_summary = Box("reveal-summary");
|
|
_summary.name = "PullSummaryTotals";
|
|
_summary.AddToClassList("hidden");
|
|
board.Add(_summary);
|
|
workspace.Add(board);
|
|
_detailScroll = new ScrollView(ScrollViewMode.Vertical) { name = "PullRewardDetail" };
|
|
_detailScroll.AddToClassList("reveal-detail-scroll");
|
|
_detailScroll.Add(Text("Select an unpacked object to inspect it.", "reveal-empty"));
|
|
workspace.Add(_detailScroll);
|
|
shell.Add(workspace);
|
|
|
|
for (int i = 0; i < _rewards.Length; i++)
|
|
{
|
|
int index = i;
|
|
var tile = new Button(() => Activate(index)) { name = $"PullReward-{i + 1:00}" };
|
|
tile.AddToClassList("reveal-tile");
|
|
tile.AddToClassList("reveal-sealed");
|
|
tile.Add(Text($"{_rewards[i].Index:00} / sealed object", "reveal-kind"));
|
|
tile.Add(Text("[ unpack ]", "reveal-sealed-label"));
|
|
tile.RegisterCallback<FocusInEvent>(_ =>
|
|
{
|
|
if (index < RevealedCount) Select(index);
|
|
});
|
|
_tiles.Add(tile);
|
|
_selectionMarkers.Add(null);
|
|
_grid.Add(tile);
|
|
}
|
|
_gridScroll.contentViewport.RegisterCallback<GeometryChangedEvent>(ResizeGrid);
|
|
_root.EnableInClassList("reveal-phone", _root.layout.width < 480f);
|
|
_root.EnableInClassList("reveal-landscape", _root.layout.width >= 480f && _root.layout.width < 760f && _root.layout.height < 540f);
|
|
_root.RegisterCallback<GeometryChangedEvent>(ResizeShell);
|
|
var footer = Box("reveal-footer");
|
|
_help = Text("[Tab] focus [arrows] inspect [Esc] reveal all", "reveal-help");
|
|
footer.Add(_help);
|
|
var actions = Box("reveal-actions");
|
|
_next = ActionButton("[Space] unpack next", () => { _followReveal = true; RevealNext(); });
|
|
_all = ActionButton("[R] reveal all", RevealAll);
|
|
_continue = ActionButton("[Enter] continue", Continue);
|
|
_continue.name = "PullContinue";
|
|
_continue.AddToClassList("reveal-continue");
|
|
_continue.SetEnabled(false);
|
|
actions.Add(_next);
|
|
actions.Add(_all);
|
|
actions.Add(_continue);
|
|
footer.Add(actions);
|
|
shell.Add(footer);
|
|
root.RegisterCallback<KeyDownEvent>(HandleKeyDown, TrickleDown.TrickleDown);
|
|
root.RegisterCallback<KeyUpEvent>(HandleKeyUp, TrickleDown.TrickleDown);
|
|
root.RegisterCallback<NavigationMoveEvent>(HandleNavigationMove, TrickleDown.TrickleDown);
|
|
root.RegisterCallback<NavigationSubmitEvent>(HandleNavigationSubmit, TrickleDown.TrickleDown);
|
|
}
|
|
|
|
public void Begin()
|
|
{
|
|
if (_begun || _disposed) return;
|
|
_begun = true;
|
|
_root.Focus();
|
|
if (_rewards.Length == 0) { Complete(); return; }
|
|
if (UIPreferences.ReducedMotion) { RevealAll(); return; }
|
|
_nextRevealAt = Time.realtimeSinceStartup + .25f;
|
|
EnsureTimeline();
|
|
}
|
|
|
|
private void EnsureTimeline() => _auto ??= _root.schedule.Execute(Tick).Every(50);
|
|
|
|
private void Tick()
|
|
{
|
|
if (_disposed) return;
|
|
if (UIPreferences.ReducedMotion) { RevealAll(); return; }
|
|
if (IsSignatureRevealing)
|
|
{
|
|
float elapsed = Time.realtimeSinceStartup - _signatureStartedAt;
|
|
int phase = elapsed < .22f ? 0 : elapsed < .55f ? 1 : 2;
|
|
if (phase != _signaturePhase)
|
|
{
|
|
_signaturePhase = phase;
|
|
_signature?.EnableInClassList("signature-verified", phase >= 1);
|
|
_signature?.EnableInClassList("signature-resolved", phase >= 2);
|
|
if (_signatureHeading != null) _signatureHeading.text = "[ VERIFIED ]";
|
|
}
|
|
if (elapsed >= 1.15f) FinishSignature();
|
|
}
|
|
else if (Time.realtimeSinceStartup >= _nextRevealAt) RevealNext();
|
|
}
|
|
|
|
public void RevealNext()
|
|
{
|
|
if (_disposed || IsComplete) return;
|
|
_root.EnableInClassList("reduced-motion", UIPreferences.ReducedMotion);
|
|
if (IsSignatureRevealing) { FinishSignature(); return; }
|
|
int index = RevealedCount++;
|
|
PopulateTile(index, !UIPreferences.ReducedMotion);
|
|
CompletedGachaReward reward = _rewards[index];
|
|
_event.text = $"[{reward.Index:00}/{_rewards.Length:00}] " +
|
|
(reward.IsCharacter ? "DISTRIBUTION SIGNATURE FOUND: " : reward.Stars >= 5 ? "5-STAR PACKAGE MANIFEST: " : "package unpacked: ") + reward.DisplayName;
|
|
_event.EnableInClassList("distribution-event", reward.IsCharacter);
|
|
_progress.text = $"{RevealedCount:00} / {_rewards.Length:00} unpacked";
|
|
if (_followReveal) SelectCore(index);
|
|
if (!UIPreferences.ReducedMotion && reward.IsCharacter && reward.Stars >= 5 &&
|
|
reward.DisplayData?.Distro != null && reward.OutcomeKind != PullOutcomeKind.Invalid)
|
|
{
|
|
StartSignature(index);
|
|
return;
|
|
}
|
|
_nextRevealAt = Time.realtimeSinceStartup + (reward.IsCharacter ? .7f : reward.Stars >= 4 ? .45f : .3f);
|
|
if (IsComplete) Complete();
|
|
}
|
|
|
|
public void RevealAll()
|
|
{
|
|
if (_disposed) return;
|
|
_root.EnableInClassList("reduced-motion", UIPreferences.ReducedMotion);
|
|
CancelTransitions();
|
|
ClearSignature();
|
|
if (IsComplete && _summary.childCount > 0) return;
|
|
while (RevealedCount < _rewards.Length) PopulateTile(RevealedCount++, false);
|
|
Complete();
|
|
}
|
|
|
|
public void Select(int index)
|
|
{
|
|
if (_disposed || index < 0 || index >= RevealedCount) return;
|
|
_followReveal = false;
|
|
if (IsSignatureRevealing) FinishSignature();
|
|
SelectCore(index);
|
|
_gridScroll.schedule.Execute(() =>
|
|
{
|
|
if (!_disposed && _tiles[index].panel != null && SelectedIndex == index)
|
|
_gridScroll.ScrollTo(_tiles[index]);
|
|
});
|
|
}
|
|
|
|
private void StartSignature(int index)
|
|
{
|
|
_signatureIndex = index;
|
|
_signatureStartedAt = Time.realtimeSinceStartup;
|
|
_signaturePhase = 0;
|
|
_tiles[index].AddToClassList("signature-active");
|
|
SelectCore(index);
|
|
var reward = _rewards[index];
|
|
_event.text = $"[{reward.Index:00}/{_rewards.Length:00}] 5-STAR DISTRIBUTION / signature verification";
|
|
_next.text = "[Space] skip reveal";
|
|
_detailScroll.Clear();
|
|
_detailScroll.scrollOffset = Vector2.zero;
|
|
_signature = Box("reveal-signature");
|
|
_signature.name = "DistributionSignature";
|
|
_signatureHeading = Text("[ SIGNATURE VERIFICATION ]", "signature-heading");
|
|
_signature.Add(_signatureHeading);
|
|
_signature.Add(Text("Distribution image detected.", "reveal-body"));
|
|
var identity = Box("signature-identity");
|
|
AddArt(identity, reward.DisplayData.Distro, false);
|
|
var name = Text(reward.DisplayName, "reveal-distro-title");
|
|
name.style.color = reward.DisplayData.Distro.AccentColor;
|
|
identity.Add(name);
|
|
identity.Add(Stars(reward));
|
|
identity.Add(Text(Release(reward) + " / " + DistroPresentation.FormatLanguages(reward.DisplayData.Distro), "reveal-detail-languages"));
|
|
_signature.Add(identity);
|
|
var outcome = BuildOutcome(reward);
|
|
outcome.AddToClassList("signature-outcome");
|
|
_signature.Add(outcome);
|
|
_signature.Add(Text("[Space] skip reveal [R] reveal all", "reveal-note"));
|
|
_detailScroll.Add(_signature);
|
|
EnsureTimeline();
|
|
}
|
|
|
|
private void ClearSignature()
|
|
{
|
|
if (!IsSignatureRevealing) return;
|
|
_tiles[_signatureIndex].RemoveFromClassList("signature-active");
|
|
_signatureIndex = -1;
|
|
_signature = null;
|
|
_signatureHeading = null;
|
|
_next.text = "[Space] unpack next";
|
|
// Rebuild the normal inspector even if the selected index is unchanged.
|
|
int selected = SelectedIndex;
|
|
SelectedIndex = -1;
|
|
SelectCore(selected);
|
|
}
|
|
|
|
private void FinishSignature()
|
|
{
|
|
ClearSignature();
|
|
_nextRevealAt = Time.realtimeSinceStartup + .2f;
|
|
if (IsComplete) Complete();
|
|
}
|
|
|
|
private void SelectCore(int index)
|
|
{
|
|
if (index < 0 || index >= RevealedCount) return;
|
|
if (SelectedIndex == index) return;
|
|
SelectedIndex = index;
|
|
for (int i = 0; i < _tiles.Count; i++)
|
|
{
|
|
_tiles[i].EnableInClassList("selected", i == index);
|
|
if (_selectionMarkers[i] != null) _selectionMarkers[i].text = i == index ? ">" : " ";
|
|
}
|
|
_detailScroll.Clear();
|
|
_detailScroll.scrollOffset = Vector2.zero;
|
|
var reward = _rewards[index];
|
|
_detailScroll.Add(reward.IsCharacter ? BuildDistributionDetail(reward) : BuildPackageDetail(reward));
|
|
}
|
|
|
|
private void PopulateTile(int index, bool animate)
|
|
{
|
|
Button tile = _tiles[index];
|
|
var reward = _rewards[index];
|
|
tile.Clear();
|
|
tile.RemoveFromClassList("reveal-sealed");
|
|
tile.AddToClassList(RarityPresentation.ForStars(reward.Stars).ClassName);
|
|
tile.AddToClassList(reward.IsCharacter ? "reveal-distribution" : "reveal-package");
|
|
tile.AddToClassList("reveal-revealed");
|
|
tile.EnableInClassList("acquisition-new", reward.OutcomeKind == PullOutcomeKind.Granted);
|
|
tile.EnableInClassList("acquisition-duplicate", IsDuplicate(reward));
|
|
var top = Box("reveal-tile-top");
|
|
var marker = Text(" ", "reveal-selection-marker");
|
|
_selectionMarkers[index] = marker;
|
|
top.Add(marker);
|
|
top.Add(Text($"{reward.Index:00}", "reveal-index"));
|
|
var rarity = Stars(reward);
|
|
if (reward.Stars >= 5) rarity.text = "[" + rarity.text + "]";
|
|
top.Add(rarity);
|
|
tile.Add(top);
|
|
tile.Add(Text(reward.IsCharacter ? "DISTRIBUTION" : PackageKind(reward).Replace(" / ", " /\n"), "reveal-kind"));
|
|
var name = Text(reward.DisplayName, "reveal-name");
|
|
tile.Add(name);
|
|
if (reward.IsCharacter)
|
|
{
|
|
var distro = reward.DisplayData?.Distro;
|
|
if (distro != null)
|
|
{
|
|
name.style.color = distro.AccentColor;
|
|
AddArt(tile, distro, true);
|
|
tile.Add(Text(Release(reward), "reveal-release"));
|
|
}
|
|
}
|
|
else if (reward.DisplayData?.Package != null)
|
|
{
|
|
var package = reward.DisplayData.Package;
|
|
string effect = string.IsNullOrWhiteSpace(package.Description) ? CategoryRole(package.Slot) : package.Description.Trim();
|
|
// Measure a word-boundary preview; the tooltip and inspector retain the exact rules.
|
|
var preview = Text(effect, "reveal-package-role");
|
|
preview.tooltip = effect;
|
|
preview.RegisterCallback<GeometryChangedEvent>(_ => FitPackagePreview(preview, effect));
|
|
tile.Add(preview);
|
|
}
|
|
var status = Text(Outcome(reward, false), "reveal-outcome");
|
|
status.EnableInClassList("is-new", reward.OutcomeKind == PullOutcomeKind.Granted);
|
|
tile.Add(status);
|
|
tile.tooltip = $"{reward.Index:00}: {reward.DisplayName} / {reward.Stars}-star " +
|
|
(reward.IsCharacter ? "distribution" : "package") + ". " + Outcome(reward, true);
|
|
if (animate)
|
|
{
|
|
tile.AddToClassList("just-unpacked");
|
|
_transitions.Add(tile.schedule.Execute(() => tile.RemoveFromClassList("just-unpacked"))
|
|
.StartingIn(reward.Stars >= 5 ? 380 : reward.Stars == 4 ? 260 : 160));
|
|
}
|
|
}
|
|
|
|
private void CancelTransitions()
|
|
{
|
|
foreach (var transition in _transitions) transition.Pause();
|
|
_transitions.Clear();
|
|
foreach (var tile in _tiles) tile.RemoveFromClassList("just-unpacked");
|
|
}
|
|
|
|
private static void FitPackagePreview(Label label, string description)
|
|
{
|
|
float width = label.contentRect.width - 1f;
|
|
if (label.panel == null || width <= 0) return;
|
|
float lineHeight = label.MeasureTextSize("Ag", 0, VisualElement.MeasureMode.Undefined, 0, VisualElement.MeasureMode.Undefined).y;
|
|
if (lineHeight <= 0) return;
|
|
float height = Mathf.Ceil(lineHeight * 3f) + 1f;
|
|
label.style.height = height;
|
|
bool Fits(string text) => label.MeasureTextSize(text, width, VisualElement.MeasureMode.AtMost, 0, VisualElement.MeasureMode.Undefined).y <= height;
|
|
string normalized = string.Join(" ", description.Split((char[])null, StringSplitOptions.RemoveEmptyEntries));
|
|
string result = normalized;
|
|
if (!Fits(result))
|
|
{
|
|
result = "…";
|
|
int boundary = normalized.IndexOf(' ');
|
|
while (boundary > 0)
|
|
{
|
|
string candidate = normalized.Substring(0, boundary) + " …";
|
|
if (!Fits(candidate)) break;
|
|
result = candidate;
|
|
boundary = normalized.IndexOf(' ', boundary + 1);
|
|
}
|
|
}
|
|
if (label.text != result) label.text = result;
|
|
label.EnableInClassList("preview-truncated", result != normalized);
|
|
}
|
|
|
|
private VisualElement BuildDistributionDetail(CompletedGachaReward reward)
|
|
{
|
|
var detail = Box("distribution-detail");
|
|
detail.AddToClassList("reveal-detail");
|
|
detail.AddToClassList(RarityPresentation.ForStars(reward.Stars).ClassName);
|
|
detail.Add(Text(reward.OutcomeKind == PullOutcomeKind.Granted ? "NEW DISTRIBUTION" :
|
|
IsDuplicate(reward) ? "DISTRIBUTION DUPLICATE" : "DISTRIBUTION", "reveal-detail-kind"));
|
|
detail.Add(Stars(reward));
|
|
var name = Text(reward.DisplayName, "reveal-distro-title");
|
|
detail.Add(name);
|
|
DistroDefinition distro = reward.DisplayData?.Distro;
|
|
if (distro != null)
|
|
{
|
|
name.style.color = distro.AccentColor;
|
|
AddArt(detail, distro, false);
|
|
detail.Add(Text($"{Release(reward)} / V{reward.DisplayData.Version}", "reveal-release"));
|
|
detail.Add(Text(DistroPresentation.FormatLanguages(distro), "reveal-detail-languages"));
|
|
if (distro.Passive != null)
|
|
{
|
|
detail.Add(Text(distro.Passive.Name, "reveal-passive-name"));
|
|
detail.Add(Text(distro.Passive.RulesText, "reveal-body"));
|
|
if (reward.DisplayData.Version > 1)
|
|
{
|
|
detail.Add(Text("INSTALLED RELEASE / V" + reward.DisplayData.Version, "reveal-section"));
|
|
detail.Add(Text(DistroVersionCatalog.GetEffectSummary(distro.Id, reward.DisplayData.Version), "reveal-body"));
|
|
}
|
|
}
|
|
}
|
|
else detail.Add(Text("Distribution metadata unavailable.", "reveal-body"));
|
|
detail.Add(BuildOutcome(reward));
|
|
AddFlags(detail, reward);
|
|
return detail;
|
|
}
|
|
|
|
private VisualElement BuildPackageDetail(CompletedGachaReward reward)
|
|
{
|
|
var detail = Box("package-detail");
|
|
detail.AddToClassList("reveal-detail");
|
|
detail.AddToClassList(RarityPresentation.ForStars(reward.Stars).ClassName);
|
|
detail.Add(Text(PackageKind(reward), "reveal-detail-kind"));
|
|
detail.Add(Stars(reward));
|
|
detail.Add(Text(reward.DisplayName, "reveal-package-title"));
|
|
var package = reward.DisplayData?.Package;
|
|
if (package != null)
|
|
{
|
|
var record = Box("reveal-package-record");
|
|
record.Add(Text("slot " + package.Slot.DisplayName(), "reveal-record-line"));
|
|
record.Add(Text(package.MaxUpgradeLevel == 0 ? "owned fixed effect"
|
|
: "owned Lv " + reward.DisplayData.PackageLevel + "/" + package.MaxUpgradeLevel, "reveal-record-line"));
|
|
record.Add(Text("id " + package.Id, "reveal-record-id"));
|
|
if (!string.IsNullOrWhiteSpace(package.IntendedDistroId))
|
|
record.Add(Text("tuned for " + package.IntendedDistroId, "reveal-record-line"));
|
|
detail.Add(record);
|
|
detail.Add(Text("BASE PACKAGE EFFECTS", "reveal-section"));
|
|
detail.Add(Text(string.IsNullOrWhiteSpace(package.Description) ? "Effect description unavailable." : package.Description, "reveal-body"));
|
|
if (IsDuplicate(reward)) detail.Add(Text("Existing package and upgrade level retained.", "reveal-body"));
|
|
}
|
|
else detail.Add(Text("Package metadata unavailable.", "reveal-body"));
|
|
detail.Add(BuildOutcome(reward));
|
|
AddFlags(detail, reward);
|
|
return detail;
|
|
}
|
|
|
|
private static VisualElement BuildOutcome(CompletedGachaReward reward)
|
|
{
|
|
var result = Box("reveal-conversion");
|
|
result.EnableInClassList("is-new", reward.OutcomeKind == PullOutcomeKind.Granted);
|
|
result.Add(Text(reward.OutcomeKind == PullOutcomeKind.Granted ?
|
|
(reward.IsCharacter ? "UNIT INSTALLED" : "PACKAGE ACQUIRED") :
|
|
IsDuplicate(reward) ? (reward.IsCharacter ? "DISTRIBUTION ALREADY INSTALLED\nDUPLICATE CONVERTED" : "PACKAGE DUPLICATE CONVERTED") : "NOT GRANTED", "reveal-conversion-title"));
|
|
result.Add(Text(Outcome(reward, true), "reveal-conversion-value"));
|
|
return result;
|
|
}
|
|
|
|
private static void AddFlags(VisualElement detail, CompletedGachaReward reward)
|
|
{
|
|
if (reward.OutcomeKind == PullOutcomeKind.DupeOverflow)
|
|
detail.Add(Text("Maximum release already installed.", "reveal-note"));
|
|
if (!string.IsNullOrWhiteSpace(reward.LanguageUnlockText))
|
|
detail.Add(Text(reward.LanguageUnlockText, "reveal-note"));
|
|
if (reward.Guaranteed) detail.Add(Text("Guaranteed reward", "reveal-note"));
|
|
if (reward.PityTriggered) detail.Add(Text("Pity triggered", "reveal-note"));
|
|
}
|
|
|
|
private static void AddArt(VisualElement target, DistroDefinition distro, bool compact)
|
|
{
|
|
if (distro.AsciiArt == null || string.IsNullOrWhiteSpace(distro.AsciiArt.text)) return;
|
|
var label = Text(distro.AsciiArt.text, compact ? "reveal-mini-art" : "reveal-art");
|
|
var font = TerminalFontResolver.Resolve(null);
|
|
DistroArtPresenter.ConfigureArtLabel(label, font);
|
|
label.style.color = distro.AccentColor;
|
|
target.Add(label);
|
|
new AsciiArtFitter(label, font).SetArt(distro.AsciiArt.text);
|
|
}
|
|
|
|
private void Complete()
|
|
{
|
|
_auto?.Pause();
|
|
var focused = _root.focusController?.focusedElement;
|
|
bool restoreFocus = focused == _next || focused == _all;
|
|
_next.SetEnabled(false);
|
|
_all.SetEnabled(false);
|
|
_next.AddToClassList("hidden");
|
|
_all.AddToClassList("hidden");
|
|
_continue.SetEnabled(true);
|
|
_help.text = "[Tab] focus [arrows] inspect [Esc] back";
|
|
// Do not leave focus on hidden buttons or move it to Continue during held Space.
|
|
if (restoreFocus) _root.Focus();
|
|
_progress.text = $"{RevealedCount:00} / {_rewards.Length:00} unpacked";
|
|
_event.text = _rewards.Length == 0 ? "No rewards were granted." :
|
|
"Unpacking complete. Select an object for its full rules.";
|
|
_event.RemoveFromClassList("distribution-event");
|
|
Summary();
|
|
_summary.RemoveFromClassList("hidden");
|
|
if (_followReveal || SelectedIndex < 0) SelectCore(FeaturedIndex());
|
|
if (_followReveal)
|
|
_gridScroll.schedule.Execute(() =>
|
|
{
|
|
if (_disposed) return;
|
|
if (_root.ClassListContains("narrow") && SelectedIndex >= 0) _gridScroll.ScrollTo(_tiles[SelectedIndex]);
|
|
else _gridScroll.scrollOffset = Vector2.zero;
|
|
});
|
|
}
|
|
|
|
private int FeaturedIndex()
|
|
{
|
|
int best = -1, score = -1;
|
|
for (int i = 0; i < _rewards.Length; i++)
|
|
{
|
|
int candidate = (_rewards[i].IsCharacter ? 10 : 0) + _rewards[i].Stars;
|
|
if (candidate > score) { score = candidate; best = i; }
|
|
}
|
|
return best;
|
|
}
|
|
|
|
private void Summary()
|
|
{
|
|
int units = 0, unitDupes = 0, packages = 0, packageDupes = 0, cache = 0, merges = 0, highest = 0, invalid = 0;
|
|
foreach (var reward in _rewards)
|
|
{
|
|
highest = Math.Max(highest, reward.Stars);
|
|
if (reward.OutcomeKind == PullOutcomeKind.Granted)
|
|
{
|
|
if (reward.IsCharacter) units++; else packages++;
|
|
}
|
|
else if (IsDuplicate(reward))
|
|
{
|
|
if (reward.IsCharacter) { unitDupes++; merges += reward.MergesAwarded; }
|
|
else { packageDupes++; cache += reward.MergesAwarded; }
|
|
}
|
|
else invalid++;
|
|
}
|
|
_summary.Clear();
|
|
var heading = Box("reveal-receipt-heading");
|
|
heading.Add(Text(_rewards.Length == 0 ? "No completed rewards." :
|
|
$"PULL RECEIPT / {_rewards.Length} {(_rewards.Length == 1 ? "pull" : "pulls")}", "reveal-receipt-title"));
|
|
if (_rewards.Length > 0) heading.Add(Text($"highest {highest}★", "reveal-receipt-highest"));
|
|
_summary.Add(heading);
|
|
if (_rewards.Length == 0) return;
|
|
var totals = Box("reveal-totals");
|
|
AddTotal(totals, "Distributions", $"{units} new / {unitDupes} duplicate");
|
|
AddTotal(totals, "Packages", $"{packages} new / {packageDupes} duplicate");
|
|
AddTotal(totals, "Merges gained", $"+{merges} Merges");
|
|
AddTotal(totals, "Cache gained", $"+{cache} Cache");
|
|
_summary.Add(totals);
|
|
if (invalid > 0) _summary.Add(Text($"{invalid} not granted", "reveal-note"));
|
|
}
|
|
|
|
private static void AddTotal(VisualElement target, string label, string value)
|
|
{
|
|
var row = Box("reveal-total");
|
|
row.EnableInClassList("reveal-total-resource", value.StartsWith("+", StringComparison.Ordinal));
|
|
row.Add(Text(label, "reveal-total-label"));
|
|
row.Add(Text(value, "reveal-total-value"));
|
|
target.Add(row);
|
|
}
|
|
|
|
private void Activate(int index)
|
|
{
|
|
if (index < RevealedCount) Select(index);
|
|
else { _followReveal = true; RevealNext(); }
|
|
}
|
|
|
|
private void ResizeGrid(GeometryChangedEvent evt)
|
|
{
|
|
float width = evt.newRect.width;
|
|
if (width < 1) return;
|
|
bool narrow = _root.ClassListContains("narrow");
|
|
var mode = narrow ? ScrollViewMode.Horizontal : ScrollViewMode.Vertical;
|
|
if (_gridScroll.mode != mode) _gridScroll.mode = mode;
|
|
_grid.style.flexWrap = narrow ? Wrap.NoWrap : Wrap.Wrap;
|
|
_grid.style.width = narrow ? new StyleLength(_rewards.Length * 168f) : new StyleLength(StyleKeyword.Auto);
|
|
_columns = narrow ? 1 : Mathf.Clamp(Mathf.FloorToInt(width / 140f), 1, 5);
|
|
float tileWidth = narrow ? 160f : Mathf.Max(0, (width - 8f * _columns - 4f) / _columns);
|
|
foreach (var tile in _tiles) tile.style.width = tileWidth;
|
|
}
|
|
|
|
private void ResizeShell(GeometryChangedEvent evt)
|
|
{
|
|
_root.EnableInClassList("reveal-phone", evt.newRect.width < 480f);
|
|
_root.EnableInClassList("reveal-landscape", evt.newRect.width >= 480f && evt.newRect.width < 760f && evt.newRect.height < 540f);
|
|
}
|
|
|
|
private void MoveSelection(int delta)
|
|
{
|
|
if (RevealedCount == 0) return;
|
|
int index = Mathf.Clamp(SelectedIndex + delta, 0, RevealedCount - 1);
|
|
Select(index);
|
|
if (_rewards.Length > 1) _tiles[index].Focus();
|
|
}
|
|
|
|
private void HandleKeyDown(KeyDownEvent evt)
|
|
{
|
|
bool handled = true;
|
|
switch (evt.keyCode)
|
|
{
|
|
case KeyCode.R: if (!IsComplete) RevealAll(); break;
|
|
case KeyCode.Escape:
|
|
if (!_escapeHeld)
|
|
{
|
|
_escapeHeld = true;
|
|
if (IsComplete) Continue(); else RevealAll();
|
|
}
|
|
break;
|
|
case KeyCode.Space:
|
|
if (!IsComplete) { _followReveal = true; RevealNext(); }
|
|
else handled = false;
|
|
break;
|
|
case KeyCode.Return:
|
|
case KeyCode.KeypadEnter:
|
|
if (!_enterHeld)
|
|
{
|
|
_enterHeld = true;
|
|
Submit();
|
|
}
|
|
break;
|
|
case KeyCode.LeftArrow: MoveSelection(-1); break;
|
|
case KeyCode.RightArrow: MoveSelection(1); break;
|
|
case KeyCode.UpArrow: MoveSelection(-_columns); break;
|
|
case KeyCode.DownArrow: MoveSelection(_columns); break;
|
|
default: handled = false; break;
|
|
}
|
|
if (!handled) return;
|
|
_root.panel?.focusController.IgnoreEvent(evt);
|
|
evt.StopImmediatePropagation();
|
|
}
|
|
|
|
private void HandleKeyUp(KeyUpEvent evt)
|
|
{
|
|
if (evt.keyCode == KeyCode.Escape) _escapeHeld = false;
|
|
if (evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.KeypadEnter) _enterHeld = false;
|
|
}
|
|
|
|
private void HandleNavigationMove(NavigationMoveEvent evt)
|
|
{
|
|
if (evt.direction == NavigationMoveEvent.Direction.Next || evt.direction == NavigationMoveEvent.Direction.Previous) return;
|
|
if (!(_root.focusController?.focusedElement is Button focused) || !_tiles.Contains(focused)) return;
|
|
int delta = evt.direction switch
|
|
{
|
|
NavigationMoveEvent.Direction.Left => -1,
|
|
NavigationMoveEvent.Direction.Right => 1,
|
|
NavigationMoveEvent.Direction.Up => -_columns,
|
|
NavigationMoveEvent.Direction.Down => _columns,
|
|
_ => 0
|
|
};
|
|
if (delta == 0) return;
|
|
MoveSelection(delta);
|
|
_root.panel?.focusController.IgnoreEvent(evt);
|
|
evt.StopImmediatePropagation();
|
|
}
|
|
|
|
private void HandleNavigationSubmit(NavigationSubmitEvent evt)
|
|
{
|
|
if (!_enterHeld) Submit();
|
|
evt.StopImmediatePropagation();
|
|
}
|
|
|
|
private void Submit()
|
|
{
|
|
// Input systems can emit both KeyDown and NavigationSubmit for one physical press.
|
|
if (_lastSubmitFrame == Time.frameCount) return;
|
|
_lastSubmitFrame = Time.frameCount;
|
|
var focused = _root.focusController?.focusedElement as Button;
|
|
if (focused == _all) { RevealAll(); return; }
|
|
if (focused == _next) { _followReveal = true; RevealNext(); return; }
|
|
int index = _tiles.IndexOf(focused);
|
|
if (index >= 0) { Activate(index); return; }
|
|
if (IsComplete) Continue(); else RevealNext();
|
|
}
|
|
|
|
private void Continue()
|
|
{
|
|
if (!IsComplete || _leaving || _disposed) return;
|
|
_leaving = true;
|
|
_onContinue?.Invoke();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
_auto?.Pause();
|
|
CancelTransitions();
|
|
_gridScroll.contentViewport.UnregisterCallback<GeometryChangedEvent>(ResizeGrid);
|
|
_root.UnregisterCallback<GeometryChangedEvent>(ResizeShell);
|
|
_root.UnregisterCallback<KeyDownEvent>(HandleKeyDown, TrickleDown.TrickleDown);
|
|
_root.UnregisterCallback<KeyUpEvent>(HandleKeyUp, TrickleDown.TrickleDown);
|
|
_root.UnregisterCallback<NavigationMoveEvent>(HandleNavigationMove, TrickleDown.TrickleDown);
|
|
_root.UnregisterCallback<NavigationSubmitEvent>(HandleNavigationSubmit, TrickleDown.TrickleDown);
|
|
}
|
|
|
|
private static bool IsDuplicate(CompletedGachaReward reward) => reward.OutcomeKind == PullOutcomeKind.Dupe || reward.OutcomeKind == PullOutcomeKind.DupeOverflow;
|
|
private static string Release(CompletedGachaReward reward) => DistroVersionCatalog.GetReleaseLabel(reward.DisplayData.Distro.Id, reward.DisplayData.Version);
|
|
private static string PackageKind(CompletedGachaReward reward) => reward.DisplayData?.Package == null ? "PACKAGE" : "PACKAGE / " + reward.DisplayData.Package.Slot.DisplayName().ToUpperInvariant();
|
|
private static string CategoryRole(PackageSlot slot) => slot switch { PackageSlot.Kernel => "[sys] system equipment", PackageSlot.Runtime => "[tool] tools equipment", _ => "[bg] services equipment" };
|
|
private static string Outcome(CompletedGachaReward reward, bool detail)
|
|
{
|
|
if (reward.OutcomeKind == PullOutcomeKind.Granted) return detail ? "Added to your collection." :
|
|
reward.IsCharacter ? "+ NEW DISTRO\nUnit installed" : "+ NEW PACKAGE\nAcquired";
|
|
if (IsDuplicate(reward)) return (detail ? "+" : "DUPLICATE\n+") + reward.MergesAwarded + (reward.IsCharacter ? " Merges" : " Cache");
|
|
return "Not granted / unavailable reward";
|
|
}
|
|
private static VisualElement Box(string className) { var box = new VisualElement(); box.AddToClassList(className); return box; }
|
|
private static Label Text(string text, string className) { var label = new Label(text) { enableRichText = false }; label.AddToClassList(className); return label; }
|
|
private static Label Stars(CompletedGachaReward reward)
|
|
{
|
|
var label = Text(RarityPresentation.ForStars(reward.Stars).Stars, "reveal-stars");
|
|
label.AddToClassList(RarityPresentation.ForStars(reward.Stars).ClassName);
|
|
label.tooltip = $"{reward.Stars}-star reward";
|
|
return label;
|
|
}
|
|
private static Button ActionButton(string text, Action action) { var button = new Button(action) { text = text }; button.AddToClassList("reveal-action"); return button; }
|
|
}
|
|
}
|