Commit WIP changes.
This commit is contained in:
parent
1c3c730e82
commit
2690c5bec0
86
Dashboard.Common/Measure.cs
Normal file
86
Dashboard.Common/Measure.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
namespace Dashboard
|
||||||
|
{
|
||||||
|
public enum MeasurementUnit
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The default unit. A size of a single picture element.
|
||||||
|
/// </summary>
|
||||||
|
Pixel,
|
||||||
|
/// <summary>
|
||||||
|
/// 1/72th of an inch traditional in graphics design.
|
||||||
|
/// </summary>
|
||||||
|
Point,
|
||||||
|
/// <summary>
|
||||||
|
/// The universal length unit for small distances.
|
||||||
|
/// </summary>
|
||||||
|
Millimeter,
|
||||||
|
/// <summary>
|
||||||
|
/// An inverse proportional unit with respect to the container size.
|
||||||
|
/// </summary>
|
||||||
|
Star,
|
||||||
|
/// <summary>
|
||||||
|
/// A directly proportional unit with respect to the container size.
|
||||||
|
/// </summary>
|
||||||
|
Percent,
|
||||||
|
}
|
||||||
|
|
||||||
|
public record struct AdvancedMetric(MeasurementUnit Unit, float Value)
|
||||||
|
{
|
||||||
|
public AdvancedMetric Convert(MeasurementUnit target, float dpi, float rel, int stars)
|
||||||
|
{
|
||||||
|
if (Unit == target)
|
||||||
|
return this;
|
||||||
|
|
||||||
|
float pixels = Unit switch {
|
||||||
|
MeasurementUnit.Pixel => Value,
|
||||||
|
MeasurementUnit.Point => Value * (72f / dpi),
|
||||||
|
MeasurementUnit.Millimeter => Value * (28.3464566929f / dpi),
|
||||||
|
MeasurementUnit.Star => Value * rel / stars,
|
||||||
|
MeasurementUnit.Percent => Value * rel / 100,
|
||||||
|
_ => throw new Exception(),
|
||||||
|
};
|
||||||
|
|
||||||
|
float value = target switch {
|
||||||
|
MeasurementUnit.Pixel => pixels,
|
||||||
|
MeasurementUnit.Point => Value * (dpi / 72f),
|
||||||
|
// MeasurementUnit.Millimeter =>
|
||||||
|
};
|
||||||
|
|
||||||
|
return new AdvancedMetric(target, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{Value} {Unit.ToShortString()}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool TryParse(ReadOnlySpan<char> str, out AdvancedMetric metric)
|
||||||
|
{
|
||||||
|
metric = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AdvancedMetric Parse(ReadOnlySpan<char> str) =>
|
||||||
|
TryParse(str, out AdvancedMetric metric)
|
||||||
|
? metric
|
||||||
|
: throw new Exception($"Could not parse the value '{str}'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MeasurementExtensions
|
||||||
|
{
|
||||||
|
public static bool IsRelative(this MeasurementUnit unit) => unit switch {
|
||||||
|
MeasurementUnit.Star or MeasurementUnit.Percent => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
public static bool IsAbsolute(this MeasurementUnit unit) => !IsRelative(unit);
|
||||||
|
|
||||||
|
public static string ToShortString(this MeasurementUnit unit) => unit switch {
|
||||||
|
MeasurementUnit.Pixel => "px",
|
||||||
|
MeasurementUnit.Point => "pt",
|
||||||
|
MeasurementUnit.Millimeter => "mm",
|
||||||
|
MeasurementUnit.Star => "*",
|
||||||
|
MeasurementUnit.Percent => "%",
|
||||||
|
_ => throw new Exception("Unknown unit."),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -67,6 +67,7 @@ namespace Dashboard
|
|||||||
public virtual void Leave()
|
public virtual void Leave()
|
||||||
{
|
{
|
||||||
Backend.Leave();
|
Backend.Leave();
|
||||||
|
Leaving?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Run() => Run(true, CancellationToken.None);
|
public void Run() => Run(true, CancellationToken.None);
|
||||||
|
@ -4,7 +4,6 @@ using System.Text;
|
|||||||
using Dashboard.Drawing.OpenGL;
|
using Dashboard.Drawing.OpenGL;
|
||||||
using Dashboard.ImmediateUI;
|
using Dashboard.ImmediateUI;
|
||||||
using Dashboard.OpenTK.PAL2;
|
using Dashboard.OpenTK.PAL2;
|
||||||
using OpenTK.Graphics;
|
|
||||||
using OpenTK.Platform;
|
using OpenTK.Platform;
|
||||||
using OpenTK.Graphics.OpenGL;
|
using OpenTK.Graphics.OpenGL;
|
||||||
using OpenTK.Mathematics;
|
using OpenTK.Mathematics;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user