Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Incari's Keystrokes (features/keystrokes) for the Rust build (ADR 0068, 0069). Hooks by name only
|
||||
# (native-hooks.json). Its settings and HUD elements are the client's (rust/mods/incari): the module
|
||||
# menu shows them and the HUD editor places them.
|
||||
[mod]
|
||||
id = "incari_keystrokes"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_keystrokes"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[injection]]
|
||||
id = "hud"
|
||||
hook = "hud"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "hud.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "batch", ty = "&mut horizon_client::hud::HudBatch", value = "&mut *batch" },
|
||||
{ name = "corners", ty = "&mut horizon_client::extras_hud::Corners", value = "&mut *corners" },
|
||||
]
|
||||
@@ -0,0 +1,78 @@
|
||||
// The keys on the glass, where the player placed them, lit while held (the Java module's Keys).
|
||||
fn incari_keystrokes_hud(
|
||||
game: &mut crate::game::Game<'_>,
|
||||
batch: &mut horizon_client::hud::HudBatch,
|
||||
corners: &mut horizon_client::extras_hud::Corners,
|
||||
) {
|
||||
use crate::game::{incari_client as c, incari_keystrokes as m};
|
||||
use incari_core::theme::Tone;
|
||||
let _ = corners;
|
||||
let Some(shown) = c::settings(m::ID, |s| m::Shown {
|
||||
size: s.int("key-size"),
|
||||
mouse: s.bool("show-mouse"),
|
||||
clicks: s.bool("show-cps"),
|
||||
sneak_sprint: s.bool("show-sneak-sprint"),
|
||||
space: s.bool("show-space"),
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
let layout = m::layout(shown);
|
||||
let (left, right) = game.native_clicks();
|
||||
// Each shown key's state and name, read before drawing: native_mapping needs the game mutably.
|
||||
// It makes the name each call (Horizon's helper), a few short strings a frame.
|
||||
let mut keys: [(bool, String); m::MAX_CELLS] = Default::default();
|
||||
for cell in layout.cells() {
|
||||
if let Some((down, name)) = game.native_mapping(m::MAPPINGS[cell.key]) {
|
||||
keys[cell.key] = (down, m::short_name(&name).unwrap_or(name));
|
||||
}
|
||||
}
|
||||
c::element(game, batch, m::ID, m::ELEMENT, |canvas| {
|
||||
let s = canvas.module;
|
||||
let custom = s.bool("custom-color").then(|| 0xFF00_0000 | s.color("pressed-color"));
|
||||
for cell in layout.cells() {
|
||||
let (down, name) = (keys[cell.key].0, keys[cell.key].1.as_str());
|
||||
let [x, y, w, h] = cell.rect.map(|v| v as f32);
|
||||
match (down, custom) {
|
||||
(true, Some(argb)) => canvas.fill_argb(x, y, w, h, argb),
|
||||
(true, None) => canvas.fill(x, y, w, h, Tone::Accent),
|
||||
(false, _) => canvas.fill(x, y, w, h, Tone::Background),
|
||||
}
|
||||
let tone = |muted: bool| match (down, muted) {
|
||||
(true, _) => Tone::OnAccent,
|
||||
(false, true) => Tone::Muted,
|
||||
(false, false) => Tone::Text,
|
||||
};
|
||||
// Centred, and shrunk if it's still too wide: text never spills out of its key.
|
||||
let label = |canvas: &mut incari_core::hud::HudCanvas<'_, '_>, text: &str, ty: f32, muted: bool| {
|
||||
let width = canvas.text_width(text);
|
||||
let scale = m::fit_scale(width, w);
|
||||
if scale >= 1.0 {
|
||||
let tx = x + ((w - width) / 2.0).trunc();
|
||||
canvas.text(text, tx, ty, tone(muted));
|
||||
} else {
|
||||
let argb = canvas.color(tone(muted));
|
||||
let ly = ty + m::LINE as f32 * (1.0 - scale) / 2.0;
|
||||
canvas.text_scaled(text, x + (w - width * scale) / 2.0, ly, argb, scale);
|
||||
}
|
||||
};
|
||||
match cell.face {
|
||||
m::Face::Name => label(&mut *canvas, name, m::label_y(cell.rect, false) as f32, false),
|
||||
m::Face::Clicks(button) => {
|
||||
let ty = m::label_y(cell.rect, true) as f32;
|
||||
label(&mut *canvas, name, ty, false);
|
||||
let count = if button == 0 { left } else { right };
|
||||
m::with_clicks(button, count, |text| {
|
||||
// "12 CPS" too wide for the button: "12".
|
||||
let text = if canvas.text_width(text) > w { m::count_only(text) } else { text };
|
||||
label(&mut *canvas, text, ty + m::LINE as f32, true);
|
||||
});
|
||||
}
|
||||
m::Face::Bar => {
|
||||
let [bx, by, bw, bh] = m::bar(cell.rect).map(|v| v as f32);
|
||||
canvas.fill(bx, by, bw, bh, if down { Tone::OnAccent } else { Tone::Text });
|
||||
}
|
||||
}
|
||||
}
|
||||
Some([layout.size[0] as f32, layout.size[1] as f32])
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
// Incari's Keystrokes for the Rust build (ADR 0068, 0069), as features/keystrokes does it: the
|
||||
// movement keys, the mouse buttons with their clicks a second, sneak and sprint, and the space bar,
|
||||
// as the player bound them, lit while held. Its settings and HUD element are the module's in
|
||||
// `incari_core::modules` (the Java ids). No game types: rust/lib.rs tests it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "keystrokes";
|
||||
/// Its HUD element's id.
|
||||
pub const ELEMENT: &str = "keys";
|
||||
|
||||
/// The space between keys.
|
||||
pub const GAP: i32 = 2;
|
||||
/// A row of sneak and sprint, and the space bar, are this much of a key's height.
|
||||
const SHORT_ROW_PERCENT: i32 = 60;
|
||||
/// A line of text's height (`incari_core::hud::LINE`).
|
||||
pub const LINE: i32 = 9;
|
||||
|
||||
/// The vanilla mappings a key can show, in `Cell::key`'s order.
|
||||
pub const MAPPINGS: [&str; 9] = [
|
||||
"key.forward",
|
||||
"key.left",
|
||||
"key.back",
|
||||
"key.right",
|
||||
"key.attack",
|
||||
"key.use",
|
||||
"key.sneak",
|
||||
"key.sprint",
|
||||
"key.jump",
|
||||
];
|
||||
|
||||
/// What a key shows.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Face {
|
||||
/// Its key's name, as Controls shows it.
|
||||
Name,
|
||||
/// Its name over its clicks a second: 0 attack, 1 use.
|
||||
Clicks(usize),
|
||||
/// A bar, not the key's name: "Space" doesn't fit a short row at small sizes.
|
||||
Bar,
|
||||
}
|
||||
|
||||
/// A key: which of `MAPPINGS` it shows, where (from the element's top left) and what it shows.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Cell {
|
||||
pub key: usize,
|
||||
pub rect: [i32; 4],
|
||||
pub face: Face,
|
||||
}
|
||||
|
||||
pub const MAX_CELLS: usize = MAPPINGS.len();
|
||||
|
||||
/// What the settings ask for.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Shown {
|
||||
pub size: i32,
|
||||
pub mouse: bool,
|
||||
pub clicks: bool,
|
||||
pub sneak_sprint: bool,
|
||||
pub space: bool,
|
||||
}
|
||||
|
||||
/// The keys and the element's size.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Layout {
|
||||
pub cells: [Cell; MAX_CELLS],
|
||||
pub len: usize,
|
||||
pub size: [i32; 2],
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
pub fn cells(&self) -> &[Cell] {
|
||||
&self.cells[..self.len]
|
||||
}
|
||||
}
|
||||
|
||||
/// The short rows' height.
|
||||
pub fn short_row(size: i32) -> i32 {
|
||||
(size * SHORT_ROW_PERCENT / 100).max(8)
|
||||
}
|
||||
|
||||
/// The keys, as the Java module lays them out: W over A S D, the mouse buttons (tall enough for
|
||||
/// two lines when they write clicks a second), sneak and sprint, then the space bar.
|
||||
pub fn layout(shown: Shown) -> Layout {
|
||||
let s = shown.size;
|
||||
let width = s * 3 + GAP * 2;
|
||||
let half = (width - GAP) / 2;
|
||||
let short = short_row(s);
|
||||
let mut cells = [Cell {
|
||||
key: 0,
|
||||
rect: [0; 4],
|
||||
face: Face::Name,
|
||||
}; MAX_CELLS];
|
||||
let mut len = 0;
|
||||
let mut push = |key, rect, face| {
|
||||
cells[len] = Cell { key, rect, face };
|
||||
len += 1;
|
||||
};
|
||||
push(0, [s + GAP, 0, s, s], Face::Name);
|
||||
let mut y = s + GAP;
|
||||
push(1, [0, y, s, s], Face::Name);
|
||||
push(2, [s + GAP, y, s, s], Face::Name);
|
||||
push(3, [(s + GAP) * 2, y, s, s], Face::Name);
|
||||
y += s + GAP;
|
||||
if shown.mouse {
|
||||
let (h, left, right) = if shown.clicks {
|
||||
(s.max(LINE * 2 + 3), Face::Clicks(0), Face::Clicks(1))
|
||||
} else {
|
||||
(s, Face::Name, Face::Name)
|
||||
};
|
||||
push(4, [0, y, half, h], left);
|
||||
push(5, [half + GAP, y, width - half - GAP, h], right);
|
||||
y += h + GAP;
|
||||
}
|
||||
if shown.sneak_sprint {
|
||||
push(6, [0, y, half, short], Face::Name);
|
||||
push(7, [half + GAP, y, width - half - GAP, short], Face::Name);
|
||||
y += short + GAP;
|
||||
}
|
||||
if shown.space {
|
||||
push(8, [0, y, width, short], Face::Bar);
|
||||
y += short + GAP;
|
||||
}
|
||||
Layout {
|
||||
cells,
|
||||
len,
|
||||
size: [width, y - GAP],
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a key's label (and the line below it, when it has one) starts down: centred in the key.
|
||||
pub fn label_y(rect: [i32; 4], two_lines: bool) -> i32 {
|
||||
let text = if two_lines { LINE * 2 - 1 } else { LINE - 1 };
|
||||
rect[1] + (rect[3] - text + 1) / 2
|
||||
}
|
||||
|
||||
/// The space bar's line: a third of the bar wide, centred, half way down.
|
||||
pub fn bar(rect: [i32; 4]) -> [i32; 4] {
|
||||
let [x, y, w, h] = rect;
|
||||
let width = w / 3;
|
||||
[x + (w - width) / 2, y + h / 2, width, 1]
|
||||
}
|
||||
|
||||
// Hot path, don't allocate: each button's "12 CPS", made again only when its count changes.
|
||||
thread_local! {
|
||||
static CLICKS: std::cell::RefCell<[(u32, String); 2]> =
|
||||
const { std::cell::RefCell::new([(u32::MAX, String::new()), (u32::MAX, String::new())]) };
|
||||
}
|
||||
|
||||
/// Calls `f` with "12 CPS" for button `button` (0 attack, 1 use) at `count`.
|
||||
pub fn with_clicks<R>(button: usize, count: u32, f: impl FnOnce(&str) -> R) -> R {
|
||||
CLICKS.with(|cell| {
|
||||
let mut cached = cell.borrow_mut();
|
||||
let entry = &mut cached[button.min(1)];
|
||||
if entry.0 != count {
|
||||
*entry = (count, format!("{count} CPS"));
|
||||
}
|
||||
f(&entry.1)
|
||||
})
|
||||
}
|
||||
|
||||
/// The count alone of a "12 CPS": what a button too narrow for the whole shows.
|
||||
pub fn count_only(clicks: &str) -> &str {
|
||||
clicks.split(' ').next().unwrap_or(clicks)
|
||||
}
|
||||
|
||||
/// How much to shrink text `width` wide to fit a key `room` wide: 1 if it fits (never grows it).
|
||||
pub fn fit_scale(width: f32, room: f32) -> f32 {
|
||||
if width <= room || room <= 0.0 {
|
||||
1.0
|
||||
} else {
|
||||
room / width
|
||||
}
|
||||
}
|
||||
|
||||
/// "LMB" for "Left Button": the mouse's names are too long for a key's box (ModuleGame.shortName).
|
||||
pub fn short_name(name: &str) -> Option<String> {
|
||||
Some(
|
||||
match name {
|
||||
"Left Button" => "LMB",
|
||||
"Right Button" => "RMB",
|
||||
"Middle Button" => "MMB",
|
||||
"Left Shift" => "Shift",
|
||||
"Left Control" => "Ctrl",
|
||||
"Left Alt" => "Alt",
|
||||
_ => return None,
|
||||
}
|
||||
.to_owned(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const ALL: Shown = Shown {
|
||||
size: 20,
|
||||
mouse: true,
|
||||
clicks: true,
|
||||
sneak_sprint: true,
|
||||
space: true,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn lays_the_keys_out_as_the_java_module_does() {
|
||||
let all = layout(ALL);
|
||||
assert_eq!(all.len, 9);
|
||||
assert_eq!(all.size[0], 64);
|
||||
assert_eq!(all.cells[0].rect, [22, 0, 20, 20]);
|
||||
assert_eq!(all.cells[4].face, Face::Clicks(0));
|
||||
assert_eq!(all.cells[4].rect, [0, 44, 31, 21]);
|
||||
assert_eq!(all.cells[5].rect, [33, 44, 31, 21]);
|
||||
// Movement, mouse (two lines and 3 high), sneak and sprint, space.
|
||||
assert_eq!(all.size[1], 20 + 2 + 20 + 2 + 21 + 2 + 12 + 2 + 12);
|
||||
let keys = layout(Shown {
|
||||
size: 14,
|
||||
mouse: false,
|
||||
sneak_sprint: false,
|
||||
space: false,
|
||||
..ALL
|
||||
});
|
||||
assert_eq!(keys.cells().len(), 4);
|
||||
assert_eq!(keys.size, [46, 30]);
|
||||
let mouse = layout(Shown {
|
||||
size: 30,
|
||||
clicks: false,
|
||||
sneak_sprint: false,
|
||||
..ALL
|
||||
});
|
||||
assert_eq!(mouse.cells()[4].rect[3], 30);
|
||||
assert_eq!(mouse.cells()[6].face, Face::Bar);
|
||||
assert_eq!(MAPPINGS[mouse.cells()[6].key], "key.jump");
|
||||
assert_eq!(short_row(14), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn centres_labels_as_the_java_module_does() {
|
||||
assert_eq!(label_y([0, 0, 20, 20], false), 6);
|
||||
assert_eq!(label_y([0, 44, 31, 21], true), 46);
|
||||
assert_eq!(bar([0, 90, 64, 12]), [21, 96, 21, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_too_wide_drops_its_unit_then_shrinks() {
|
||||
assert_eq!(count_only("12 CPS"), "12");
|
||||
assert_eq!(fit_scale(30.0, 31.0), 1.0);
|
||||
assert_eq!(fit_scale(40.0, 20.0), 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn counts_each_button_apart() {
|
||||
assert_eq!(with_clicks(0, 12, str::to_owned), "12 CPS");
|
||||
assert_eq!(with_clicks(1, 3, str::to_owned), "3 CPS");
|
||||
let first = with_clicks(0, 12, |t| t.as_ptr());
|
||||
assert_eq!(with_clicks(0, 12, |t| t.as_ptr()), first);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user