Files
TuneBossPC/TuneBoss.Core/Models/OneDimDisplay.cs
T
2026-09-28 16:55:31 +08:00

40 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
namespace TuneBoss.Core.Models
{
/// <summary>
/// Display scaling for 1D tables: displayed value = raw × multiplier.
/// A multiplier of 0 or 1 means unscaled (×1). Editing is done in displayed units
/// and must land on a multiple of the multiplier.
///
/// This is the single source of truth for the rules the 1D view-model applies
/// (OneDimTablesViewModel can delegate to it so the UI and tests can't drift).
/// </summary>
public static class OneDimDisplay
{
/// <summary>0/1 => 1; otherwise the multiplier itself.</summary>
public static int EffectiveMultiplier(int multiplier) => multiplier < 1 ? 1 : multiplier;
/// <summary>Raw byte value → displayed value.</summary>
public static long ToDisplay(int raw, int multiplier) =>
(long)raw * EffectiveMultiplier(multiplier);
/// <summary>
/// Displayed value → raw. Returns false if the displayed value is not an exact
/// multiple of the (effective) multiplier, so the caller can reject the edit.
/// </summary>
public static bool TryFromDisplay(double display, int multiplier, out int raw)
{
int m = EffectiveMultiplier(multiplier);
double q = display / m;
if (Math.Abs(q - Math.Round(q)) > 1e-6)
{
raw = 0;
return false;
}
raw = (int)Math.Round(q);
return true;
}
}
}