RustToDust/client/ImperialChrome.cs

213 lines
8.2 KiB
C#

using Godot;
namespace RustToDust.Client;
/// <summary>
/// Shared Industrial Imperial palette and beveled <c>panel_chrome.gdshader</c> panel/control
/// helpers for pre-match UI layers (<see cref="MainMenu"/>, <see cref="ServerBrowser"/>,
/// <see cref="LobbyScreen"/>). Kept separate from <see cref="Hud"/> so <c>Hud.GetChromeMaterials()</c>
/// preserves its exact 6-panel contract.
/// </summary>
public static class ImperialChrome
{
public static readonly Color BgDark = Color.FromHtml("#131613");
public static readonly Color BgInner = Color.FromHtml("#1b1f1b");
public static readonly Color BgHover = Color.FromHtml("#242b23");
public static readonly Color BgPressed = Color.FromHtml("#2e372c");
public static readonly Color BorderCol = Color.FromHtml("#3e4638");
public static readonly Color OliveCol = Color.FromHtml("#9bb06a");
public static readonly Color OrangeCol = Color.FromHtml("#e08a4a");
public static readonly Color TextLight = Color.FromHtml("#e4dfd2");
public static readonly Color TextMuted = Color.FromHtml("#989487");
public static readonly Color CyanCol = Color.FromHtml("#44d9e8");
public static readonly Color GoldCol = Color.FromHtml("#e6b838");
public static readonly Color RedCol = Color.FromHtml("#d93829");
public static readonly Color ScrimCol = new(0.03f, 0.04f, 0.03f, 0.56f);
private static Shader? _sharedChromeShader;
private static ImageTexture? _sharedWhiteTex;
internal static Shader? ChromeShader => _sharedChromeShader ??= GD.Load<Shader>("res://panel_chrome.gdshader");
internal static ImageTexture WhiteTexture
{
get
{
if (_sharedWhiteTex is null)
{
var img = Image.CreateEmpty(1, 1, false, Image.Format.Rgba8);
img.SetPixel(0, 0, Colors.White);
_sharedWhiteTex = ImageTexture.CreateFromImage(img);
}
return _sharedWhiteTex;
}
}
public static ImperialChromePanel CreatePanel(
Color accentColor,
float cornerLen = 14f,
float borderPx = 1.5f,
float bgAlpha = 0.90f,
int padH = 14,
int padV = 12)
{
return new ImperialChromePanel(accentColor, cornerLen, borderPx, bgAlpha, padH, padV);
}
public static Label CreateLabel(string text, int fontSize, Color color, HorizontalAlignment align = HorizontalAlignment.Left)
{
var lbl = new Label
{
Text = text,
HorizontalAlignment = align
};
lbl.AddThemeFontSizeOverride("font_size", fontSize);
lbl.AddThemeColorOverride("font_color", color);
return lbl;
}
public static Button CreateButton(string text, Color accentColor, int fontSize = 13, float minHeight = 32f)
{
var btn = new Button
{
Text = text,
FocusMode = Control.FocusModeEnum.None,
CustomMinimumSize = new Vector2(0f, minHeight)
};
StyleButton(btn, accentColor, fontSize);
return btn;
}
public static void StyleButton(Button btn, Color accentColor, int fontSize = 13)
{
btn.AddThemeFontSizeOverride("font_size", fontSize);
btn.AddThemeColorOverride("font_color", TextLight);
btn.AddThemeColorOverride("font_hover_color", Colors.White);
btn.AddThemeColorOverride("font_pressed_color", accentColor);
btn.AddThemeColorOverride("font_disabled_color", new Color(TextMuted.R, TextMuted.G, TextMuted.B, 0.45f));
var normal = MakeBox(BgInner, new Color(accentColor.R, accentColor.G, accentColor.B, 0.55f), 1);
var hover = MakeBox(BgHover, accentColor, 1);
var pressed = MakeBox(BgPressed, accentColor, 2);
var disabled = MakeBox(new Color(BgDark.R, BgDark.G, BgDark.B, 0.65f), new Color(BorderCol.R, BorderCol.G, BorderCol.B, 0.4f), 1);
btn.AddThemeStyleboxOverride("normal", normal);
btn.AddThemeStyleboxOverride("hover", hover);
btn.AddThemeStyleboxOverride("pressed", pressed);
btn.AddThemeStyleboxOverride("disabled", disabled);
btn.AddThemeStyleboxOverride("focus", hover);
}
public static void StyleLineEdit(LineEdit edit, Color accentColor, int fontSize = 13)
{
edit.AddThemeFontSizeOverride("font_size", fontSize);
edit.AddThemeColorOverride("font_color", TextLight);
edit.AddThemeColorOverride("font_placeholder_color", new Color(TextMuted.R, TextMuted.G, TextMuted.B, 0.65f));
edit.AddThemeColorOverride("font_uneditable_color", TextMuted);
edit.AddThemeColorOverride("caret_color", accentColor);
var normal = MakeBox(BgInner, BorderCol, 1, padH: 8, padV: 4);
var focus = MakeBox(BgHover, accentColor, 1, padH: 8, padV: 4);
var readOnly = MakeBox(new Color(BgDark.R, BgDark.G, BgDark.B, 0.72f), new Color(BorderCol.R, BorderCol.G, BorderCol.B, 0.45f), 1, padH: 8, padV: 4);
edit.AddThemeStyleboxOverride("normal", normal);
edit.AddThemeStyleboxOverride("focus", focus);
edit.AddThemeStyleboxOverride("read_only", readOnly);
}
public static void StyleOptionButton(OptionButton opt, Color accentColor, int fontSize = 13)
{
StyleButton(opt, accentColor, fontSize);
}
public static void StyleCheckBox(CheckBox chk, Color accentColor, int fontSize = 12)
{
chk.FocusMode = Control.FocusModeEnum.None;
chk.AddThemeFontSizeOverride("font_size", fontSize);
chk.AddThemeColorOverride("font_color", TextLight);
chk.AddThemeColorOverride("font_hover_color", Colors.White);
chk.AddThemeColorOverride("font_pressed_color", accentColor);
chk.AddThemeColorOverride("font_disabled_color", TextMuted);
}
public static HSeparator MakeRule(Color? color = null)
{
var sep = new HSeparator();
var line = new StyleBoxLine
{
Color = color ?? new Color(BorderCol.R, BorderCol.G, BorderCol.B, 0.75f),
Thickness = 1
};
sep.AddThemeStyleboxOverride("separator", line);
return sep;
}
private static StyleBoxFlat MakeBox(Color bg, Color border, int borderW, int padH = 10, int padV = 6)
{
var box = new StyleBoxFlat
{
BgColor = bg,
BorderColor = border,
ContentMarginLeft = padH,
ContentMarginRight = padH,
ContentMarginTop = padV,
ContentMarginBottom = padV
};
box.SetBorderWidthAll(borderW);
return box;
}
}
public sealed partial class ImperialChromePanel : PanelContainer
{
private readonly ShaderMaterial _material;
public ImperialChromePanel(
Color accentColor,
float cornerLen = 14f,
float borderPx = 1.5f,
float bgAlpha = 0.90f,
int padH = 14,
int padV = 12)
{
_material = new ShaderMaterial { Shader = ImperialChrome.ChromeShader };
_material.SetShaderParameter("bg_color", new Color(ImperialChrome.BgDark.R, ImperialChrome.BgDark.G, ImperialChrome.BgDark.B, bgAlpha));
_material.SetShaderParameter("border_color", ImperialChrome.BorderCol);
_material.SetShaderParameter("accent_color", accentColor);
_material.SetShaderParameter("corner_len", cornerLen);
_material.SetShaderParameter("border_px", borderPx);
Material = _material;
var empty = new StyleBoxEmpty
{
ContentMarginLeft = padH,
ContentMarginRight = padH,
ContentMarginTop = padV,
ContentMarginBottom = padV
};
AddThemeStyleboxOverride("panel", empty);
}
public ShaderMaterial ChromeMaterial => _material;
public void SetAccentColor(Color accentColor)
{
_material.SetShaderParameter("accent_color", accentColor);
}
public override void _Notification(int what)
{
if (what == NotificationResized)
{
_material.SetShaderParameter("rect_size", Size);
QueueRedraw();
}
}
public override void _Draw()
{
if (Size.X < 2f || Size.Y < 2f) return;
_material.SetShaderParameter("rect_size", Size);
DrawTextureRect(ImperialChrome.WhiteTexture, new Rect2(Vector2.Zero, Size), false, Colors.White);
}
}