158 lines
4.2 KiB
C#
158 lines
4.2 KiB
C#
|
using System;
|
|||
|
using System.Diagnostics.CodeAnalysis;
|
|||
|
using System.Drawing;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Numerics;
|
|||
|
using System.Text;
|
|||
|
using Dashboard.Drawing;
|
|||
|
|
|||
|
namespace Dashboard.ImmediateUI
|
|||
|
{
|
|||
|
public class DimUIConfig
|
|||
|
{
|
|||
|
public float Margin = 4f;
|
|||
|
public float Padding = 4f;
|
|||
|
|
|||
|
public required IFont Font { get; init; }
|
|||
|
public IBrush TextBrush = new SolidBrush(Color.Black);
|
|||
|
|
|||
|
public float ButtonBorderSize = 2f;
|
|||
|
public IBrush ButtonBorderBrush = new SolidBrush(Color.DarkSlateGray);
|
|||
|
public IBrush ButtonFillBrush = new SolidBrush(Color.SlateGray);
|
|||
|
public IBrush? ButtonShadowBrush = new SolidBrush(Color.FromArgb(32, Color.LightSteelBlue));
|
|||
|
public float ButtonShadowOffset = 4f;
|
|||
|
|
|||
|
public float InputBorderSize = 2f;
|
|||
|
public IBrush InputPlaceholderTextBrush = new SolidBrush(Color.SlateGray);
|
|||
|
public IBrush InputBorderBrush = new SolidBrush(Color.SlateGray);
|
|||
|
public IBrush InputFillBrush = new SolidBrush(Color.LightGray);
|
|||
|
public IBrush? InputShadowBrush = new SolidBrush(Color.FromArgb(32, Color.LightSteelBlue));
|
|||
|
public float InputShadowOffset = -4f;
|
|||
|
|
|||
|
public float MenuBorderSize = 2f;
|
|||
|
public IBrush MenuBorderBrush = new SolidBrush(Color.DarkSlateGray);
|
|||
|
public IBrush MenuFillBrush = new SolidBrush(Color.SlateGray);
|
|||
|
public IBrush? MenuShadowBrush = new SolidBrush(Color.FromArgb(32, Color.LightSteelBlue));
|
|||
|
public float MenuShadowOffset = 6f;
|
|||
|
}
|
|||
|
|
|||
|
public class DimUI
|
|||
|
{
|
|||
|
private readonly DimUIConfig _config;
|
|||
|
private Vector2 _pen;
|
|||
|
private Box2d _bounds;
|
|||
|
private bool _firstLine = false;
|
|||
|
private bool _sameLine = false;
|
|||
|
private float _z = -1;
|
|||
|
private float _lineHeight;
|
|||
|
private DrawQueue _queue;
|
|||
|
|
|||
|
public DimUI(DimUIConfig config)
|
|||
|
{
|
|||
|
_config = config;
|
|||
|
}
|
|||
|
|
|||
|
[MemberNotNull(nameof(_queue))]
|
|||
|
public void Begin(Box2d bounds, DrawQueue queue)
|
|||
|
{
|
|||
|
_bounds = bounds;
|
|||
|
_pen = _bounds.Min;
|
|||
|
_queue = queue;
|
|||
|
_firstLine = true;
|
|||
|
_lineHeight = 0;
|
|||
|
_z = -1;
|
|||
|
}
|
|||
|
|
|||
|
public void SameLine()
|
|||
|
{
|
|||
|
_sameLine = true;
|
|||
|
}
|
|||
|
|
|||
|
private void Line()
|
|||
|
{
|
|||
|
if (!_firstLine && !_sameLine)
|
|||
|
{
|
|||
|
_pen = new Vector2(_bounds.Left, _pen.Y + _lineHeight);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_firstLine = false;
|
|||
|
_sameLine = false;
|
|||
|
}
|
|||
|
|
|||
|
_pen.X += _config.Margin;
|
|||
|
_lineHeight = 0;
|
|||
|
}
|
|||
|
|
|||
|
private float Z()
|
|||
|
{
|
|||
|
return _z += 0.001f;
|
|||
|
}
|
|||
|
|
|||
|
public void Text(string text)
|
|||
|
{
|
|||
|
Line();
|
|||
|
|
|||
|
SizeF sz = Typesetter.MeasureString(_config.Font, text);
|
|||
|
float z = Z();
|
|||
|
float h = _config.Margin * 2 + sz.Height;
|
|||
|
_queue.Text(new Vector3(_pen + new Vector2(0, _config.Margin), z), _config.TextBrush, text, _config.Font);
|
|||
|
|
|||
|
_lineHeight = Math.Max(_lineHeight, h);
|
|||
|
_pen.X += sz.Width;
|
|||
|
}
|
|||
|
|
|||
|
public bool Button(string label)
|
|||
|
{
|
|||
|
Line();
|
|||
|
|
|||
|
SizeF sz = Typesetter.MeasureString(_config.Font, label);
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public bool Input(string placeholder, StringBuilder value)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public void BeginMenu()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public bool MenuItem(string name)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public void EndMenu()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public int Id(ReadOnlySpan<char> str)
|
|||
|
{
|
|||
|
// Uses the FVN-1A algorithm in 32-bit mode.
|
|||
|
const int PRIME = 0x01000193;
|
|||
|
const int BASIS = unchecked((int)0x811c9dc5);
|
|||
|
|
|||
|
int hash = BASIS;
|
|||
|
for (int i = 0; i < str.Length; i++)
|
|||
|
{
|
|||
|
hash ^= str[i] & 0xFF;
|
|||
|
hash *= PRIME;
|
|||
|
hash ^= str[i] >> 8;
|
|||
|
hash *= PRIME;
|
|||
|
}
|
|||
|
|
|||
|
return hash;
|
|||
|
}
|
|||
|
|
|||
|
public void Finish()
|
|||
|
{
|
|||
|
// TODO:
|
|||
|
}
|
|||
|
}
|
|||
|
}
|