UI: anti-aliased Segoe UI/Consolas F1 menu and slim F3 stats panel

This commit is contained in:
ArnoX5
2026-12-01 05:39:16 +05:30
parent 7d93ee0759
commit 3bde492368
3 changed files with 1168 additions and 2501 deletions

View File

@ -1,11 +1,421 @@
//! The dot letters the stats panel (`panel.rs`, diagnostics builds) and the settings menu
//! (`menu.rs`, every build) are written with, and the pixels they are painted in.
//! Smooth anti-aliased GDI canvas renderer (Vice City theme) for the F1 settings
//! menu (`menu.rs`), F2 advisor, and F3 stats panel (`panel.rs`), plus the legacy
//! 5x7 glyph table kept for compatibility with upstream helpers and tests.
use std::ffi::c_void;
use windows::Win32::Graphics::Dxgi::Common::*;
/// Seven rows of five dots, the leftmost the highest bit.
/// RGB tuple (0..=255).
pub(super) type Rgb = (u8, u8, u8);
/// Refined Vice City color palette.
pub(super) mod theme {
use super::Rgb;
pub const BG: Rgb = (17, 13, 28); // #110D1C deep twilight plum
pub const BORDER: Rgb = (58, 43, 82); // #3A2B52 subtle card border
pub const DIVIDER: Rgb = (42, 32, 61); // #2A203D section divider
pub const ROW_HI: Rgb = (35, 26, 56); // #231A38 active row highlight
pub const GRAPH_BG: Rgb = (24, 18, 40); // #181228 sparkline track
pub const PINK: Rgb = (255, 79, 163); // #FF4FA3 Vice neon pink
pub const CYAN: Rgb = (92, 235, 255); // #5CEBFF Vice electric cyan
pub const MINT: Rgb = (92, 255, 184); // #5CFFB8 active / ON / FG
pub const AMBER: Rgb = (255, 184, 92); // #FFB85C changed (*) / notice
pub const CORAL: Rgb = (255, 110, 140); // #FF6E8C warning / dropped > 0
pub const TEXT: Rgb = (232, 226, 244); // #E8E2F4 crisp off-white
pub const LABEL: Rgb = (198, 190, 218); // #C6BEDA secondary label
pub const MUTED: Rgb = (146, 136, 172); // #9288AC hints / OFF / footer
pub const SHADOW: Rgb = (5, 3, 10); // #05030A 1px drop shadow
}
#[repr(C)]
struct BitmapInfoHeader {
bi_size: u32,
bi_width: i32,
bi_height: i32,
bi_planes: u16,
bi_bit_count: u16,
bi_compression: u32,
bi_size_image: u32,
bi_x_pels_per_meter: i32,
bi_y_pels_per_meter: i32,
bi_clr_used: u32,
bi_clr_important: u32,
}
#[repr(C)]
struct BitmapInfo {
header: BitmapInfoHeader,
colors: [u32; 1],
}
#[repr(C)]
struct GdiSize {
cx: i32,
cy: i32,
}
#[link(name = "gdi32")]
unsafe extern "system" {
fn CreateCompatibleDC(hdc: *mut c_void) -> *mut c_void;
fn DeleteDC(hdc: *mut c_void) -> i32;
fn CreateDIBSection(
hdc: *mut c_void,
pbmi: *const BitmapInfo,
usage: u32,
ppv_bits: *mut *mut c_void,
hsection: *mut c_void,
offset: u32,
) -> *mut c_void;
fn SelectObject(hdc: *mut c_void, h: *mut c_void) -> *mut c_void;
fn DeleteObject(ho: *mut c_void) -> i32;
fn CreateFontW(
height: i32,
width: i32,
escapement: i32,
orientation: i32,
weight: i32,
italic: u32,
underline: u32,
strikeout: u32,
charset: u32,
out_prec: u32,
clip_prec: u32,
quality: u32,
pitch_family: u32,
face: *const u16,
) -> *mut c_void;
fn SetBkMode(hdc: *mut c_void, mode: i32) -> i32;
fn SetTextColor(hdc: *mut c_void, color: u32) -> u32;
fn TextOutW(hdc: *mut c_void, x: i32, y: i32, lpstring: *const u16, c: i32) -> i32;
fn GetTextExtentPoint32W(hdc: *mut c_void, lpstring: *const u16, c: i32, psizl: *mut GdiSize) -> i32;
fn GdiFlush() -> i32;
}
const TRANSPARENT: i32 = 1;
const ANTIALIASED_QUALITY: u32 = 4;
const FW_NORMAL: i32 = 400;
const FW_BOLD: i32 = 700;
fn colorref((r, g, b): Rgb) -> u32 {
u32::from(r) | (u32::from(g) << 8) | (u32::from(b) << 16)
}
fn wide_z(s: &str) -> Vec<u16> {
s.encode_utf16().chain(Some(0)).collect()
}
pub(super) struct Canvas {
hdc: *mut c_void,
bmp: *mut c_void,
old_bmp: *mut c_void,
bits: *mut u8,
pub(super) width: u32,
pub(super) height: u32,
}
impl Canvas {
pub(super) fn new(width: u32, height: u32) -> Option<Canvas> {
if width == 0 || height == 0 {
return None;
}
unsafe {
let hdc = CreateCompatibleDC(std::ptr::null_mut());
if hdc.is_null() {
return None;
}
let bmi = BitmapInfo {
header: BitmapInfoHeader {
bi_size: std::mem::size_of::<BitmapInfoHeader>() as u32,
bi_width: width as i32,
bi_height: -(height as i32),
bi_planes: 1,
bi_bit_count: 32,
bi_compression: 0,
bi_size_image: 0,
bi_x_pels_per_meter: 0,
bi_y_pels_per_meter: 0,
bi_clr_used: 0,
bi_clr_important: 0,
},
colors: [0],
};
let mut bits: *mut c_void = std::ptr::null_mut();
let bmp = CreateDIBSection(hdc, &bmi, 0, &mut bits, std::ptr::null_mut(), 0);
if bmp.is_null() || bits.is_null() {
DeleteDC(hdc);
return None;
}
let old_bmp = SelectObject(hdc, bmp);
SetBkMode(hdc, TRANSPARENT);
Some(Canvas { hdc, bmp, old_bmp, bits: bits.cast::<u8>(), width, height })
}
}
fn pixels_mut(&mut self) -> &mut [u8] {
let len = (self.width as usize) * (self.height as usize) * 4;
unsafe { std::slice::from_raw_parts_mut(self.bits, len) }
}
pub(super) fn clear(&mut self, (r, g, b): Rgb) {
let px = [b, g, r, 0xFF];
for chunk in self.pixels_mut().chunks_exact_mut(4) {
chunk.copy_from_slice(&px);
}
}
pub(super) fn fill_rect(&mut self, x0: u32, y0: u32, x1: u32, y1: u32, (r, g, b): Rgb) {
let x0 = x0.min(self.width);
let x1 = x1.min(self.width);
let y0 = y0.min(self.height);
let y1 = y1.min(self.height);
if x0 >= x1 || y0 >= y1 {
return;
}
let w = self.width as usize;
let px = [b, g, r, 0xFF];
let buf = self.pixels_mut();
for y in y0..y1 {
let row = (y as usize) * w * 4;
for x in x0..x1 {
let at = row + (x as usize) * 4;
buf[at..at + 4].copy_from_slice(&px);
}
}
}
pub(super) fn stroke_border(&mut self, thickness: u32, col: Rgb) {
let t = thickness.max(1);
let (w, h) = (self.width, self.height);
self.fill_rect(0, 0, w, t, col);
self.fill_rect(0, h.saturating_sub(t), w, h, col);
self.fill_rect(0, 0, t, h, col);
self.fill_rect(w.saturating_sub(t), 0, w, h, col);
}
pub(super) fn top_gradient_bar(&mut self, bar_h: u32, left: Rgb, right: Rgb) {
let h = bar_h.min(self.height);
let w = self.width.max(1);
let span = (w - 1).max(1) as f32;
let buf = self.pixels_mut();
for x in 0..w {
let t = x as f32 / span;
let r = (f32::from(left.0) + (f32::from(right.0) - f32::from(left.0)) * t).round() as u8;
let g = (f32::from(left.1) + (f32::from(right.1) - f32::from(left.1)) * t).round() as u8;
let b = (f32::from(left.2) + (f32::from(right.2) - f32::from(left.2)) * t).round() as u8;
let px = [b, g, r, 0xFF];
for y in 0..h {
let at = ((y as usize) * (w as usize) + (x as usize)) * 4;
buf[at..at + 4].copy_from_slice(&px);
}
}
}
pub(super) fn text(
&mut self,
x: i32,
y: i32,
text: &str,
face: &str,
px_height: i32,
bold: bool,
color: Rgb,
shadow_px: i32,
) {
if text.is_empty() {
return;
}
let utf16: Vec<u16> = text.encode_utf16().collect();
let face_w = wide_z(face);
unsafe {
let font = CreateFontW(
-px_height.max(8),
0,
0,
0,
if bold { FW_BOLD } else { FW_NORMAL },
0,
0,
0,
0,
0,
0,
ANTIALIASED_QUALITY,
0,
face_w.as_ptr(),
);
let old_font = if !font.is_null() { SelectObject(self.hdc, font) } else { std::ptr::null_mut() };
if shadow_px > 0 {
SetTextColor(self.hdc, colorref(theme::SHADOW));
TextOutW(self.hdc, x + shadow_px, y + shadow_px, utf16.as_ptr(), utf16.len() as i32);
}
SetTextColor(self.hdc, colorref(color));
TextOutW(self.hdc, x, y, utf16.as_ptr(), utf16.len() as i32);
if !font.is_null() {
SelectObject(self.hdc, old_font);
DeleteObject(font);
}
}
}
pub(super) fn text_right(
&mut self,
right_x: i32,
y: i32,
text: &str,
face: &str,
px_height: i32,
bold: bool,
color: Rgb,
shadow_px: i32,
) {
if text.is_empty() {
return;
}
let utf16: Vec<u16> = text.encode_utf16().collect();
let face_w = wide_z(face);
unsafe {
let font = CreateFontW(
-px_height.max(8),
0,
0,
0,
if bold { FW_BOLD } else { FW_NORMAL },
0,
0,
0,
0,
0,
0,
ANTIALIASED_QUALITY,
0,
face_w.as_ptr(),
);
let old_font = if !font.is_null() { SelectObject(self.hdc, font) } else { std::ptr::null_mut() };
let mut sz = GdiSize { cx: 0, cy: 0 };
GetTextExtentPoint32W(self.hdc, utf16.as_ptr(), utf16.len() as i32, &mut sz);
let x = right_x - sz.cx;
if shadow_px > 0 {
SetTextColor(self.hdc, colorref(theme::SHADOW));
TextOutW(self.hdc, x + shadow_px, y + shadow_px, utf16.as_ptr(), utf16.len() as i32);
}
SetTextColor(self.hdc, colorref(color));
TextOutW(self.hdc, x, y, utf16.as_ptr(), utf16.len() as i32);
if !font.is_null() {
SelectObject(self.hdc, old_font);
DeleteObject(font);
}
}
}
pub(super) fn pack(&self, format: DXGI_FORMAT, pitch: usize) -> Option<Vec<u8>> {
unsafe { GdiFlush() };
let w = self.width as usize;
let h = self.height as usize;
let src = unsafe { std::slice::from_raw_parts(self.bits, w * h * 4) };
let mut out = vec![0u8; pitch * h];
match format {
DXGI_FORMAT_B8G8R8A8_UNORM => {
for y in 0..h {
let srow = y * w * 4;
let drow = y * pitch;
for x in 0..w {
let s = srow + x * 4;
let d = drow + x * 4;
out[d] = src[s];
out[d + 1] = src[s + 1];
out[d + 2] = src[s + 2];
out[d + 3] = 0xFF;
}
}
}
DXGI_FORMAT_R8G8B8A8_UNORM => {
for y in 0..h {
let srow = y * w * 4;
let drow = y * pitch;
for x in 0..w {
let s = srow + x * 4;
let d = drow + x * 4;
out[d] = src[s + 2];
out[d + 1] = src[s + 1];
out[d + 2] = src[s];
out[d + 3] = 0xFF;
}
}
}
DXGI_FORMAT_R10G10B10A2_UNORM => {
for y in 0..h {
let srow = y * w * 4;
let drow = y * pitch;
for x in 0..w {
let s = srow + x * 4;
let r10 = (u32::from(src[s + 2]) * 1023 + 127) / 255;
let g10 = (u32::from(src[s + 1]) * 1023 + 127) / 255;
let b10 = (u32::from(src[s]) * 1023 + 127) / 255;
let packed = r10 | (g10 << 10) | (b10 << 20) | (3u32 << 30);
let d = drow + x * 4;
out[d..d + 4].copy_from_slice(&packed.to_le_bytes());
}
}
}
DXGI_FORMAT_R16G16B16A16_FLOAT => {
for y in 0..h {
let srow = y * w * 4;
let drow = y * pitch;
for x in 0..w {
let s = srow + x * 4;
let r = f32_to_f16(srgb_to_lin(src[s + 2]));
let g = f32_to_f16(srgb_to_lin(src[s + 1]));
let b = f32_to_f16(srgb_to_lin(src[s]));
let d = drow + x * 8;
out[d..d + 2].copy_from_slice(&r.to_le_bytes());
out[d + 2..d + 4].copy_from_slice(&g.to_le_bytes());
out[d + 4..d + 6].copy_from_slice(&b.to_le_bytes());
out[d + 6..d + 8].copy_from_slice(&0x3C00u16.to_le_bytes());
}
}
}
_ => return None,
}
Some(out)
}
}
impl Drop for Canvas {
fn drop(&mut self) {
unsafe {
SelectObject(self.hdc, self.old_bmp);
DeleteObject(self.bmp);
DeleteDC(self.hdc);
}
}
}
#[inline]
fn srgb_to_lin(c: u8) -> f32 {
let x = f32::from(c) / 255.0;
x * x
}
#[inline]
fn f32_to_f16(v: f32) -> u16 {
let bits = v.to_bits();
let sign = ((bits >> 16) & 0x8000) as u16;
let exp = ((bits >> 23) & 0xFF) as i32 - 127 + 15;
let mant = bits & 0x007F_FFFF;
if exp <= 0 {
sign
} else if exp >= 31 {
sign | 0x7C00
} else {
sign | ((exp as u16) << 10) | ((mant >> 13) as u16)
}
}
pub(super) fn bpp(format: DXGI_FORMAT) -> Option<u32> {
Some(colours(format)?[0].len() as u32)
}
#[rustfmt::skip]
#[allow(unreachable_patterns, clippy::match_overlapping_arm)]
pub(super) const fn glyph(c: u8) -> [u8; 7] {
match c {
b'0' => [0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110],
@ -48,7 +458,6 @@ pub(super) const fn glyph(c: u8) -> [u8; 7] {
b'/' => [0b00001, 0b00010, 0b00010, 0b00100, 0b01000, 0b01000, 0b10000],
b'%' => [0b11000, 0b11001, 0b00010, 0b00100, 0b01000, 0b10011, 0b00011],
b'-' => [0, 0, 0, 0b11111, 0, 0, 0],
// For the settings menu (menu.rs).
b'>' => [0b01000, 0b00100, 0b00010, 0b00001, 0b00010, 0b00100, 0b01000],
b'<' => [0b00010, 0b00100, 0b01000, 0b10000, 0b01000, 0b00100, 0b00010],
b'*' => [0, 0b00100, 0b10101, 0b01110, 0b10101, 0b00100, 0],
@ -66,16 +475,14 @@ pub(super) const fn glyph(c: u8) -> [u8; 7] {
}
}
/// The bytes of a black and a white pixel of a swap chain's format.
pub(super) fn colours(format: DXGI_FORMAT) -> Option<[&'static [u8]; 2]> {
Some(match format {
DXGI_FORMAT_R8G8B8A8_UNORM | DXGI_FORMAT_B8G8R8A8_UNORM => [&[0, 0, 0, 0xFF], &[0xFF; 4]],
DXGI_FORMAT_R10G10B10A2_UNORM => [&[0, 0, 0, 0xC0], &[0xFF; 4]],
// Halves of 0 and 1.
DXGI_FORMAT_R16G16B16A16_FLOAT => [
&[0, 0, 0, 0, 0, 0, 0, 0x3C],
&[0, 0x3C, 0, 0x3C, 0, 0x3C, 0, 0x3C],
],
_ => return None,
})
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff