cb20d7056c
The title box is an unbordered, centred EditBox (black, no shadow) in vanilla. Horizon kept Ctrl/Cmd+A's selection but never drew it, and centred the title together with its cursor. Draw it with ui::append_field (now with a text shadow flag): the value centred alone, the cursor after it, and the inverted highlight. horizon-ctl gets the super (Cmd) and F6 key names, and A/C/V/X with the edit modifier held act on the open book. Part of #275 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1274 lines
40 KiB
Rust
1274 lines
40 KiB
Rust
//! Shared GUI widgets drawn as HUD quads: nine-slice sprites (gui.scaling
|
|
//! metadata), buttons, sliders, tiled menu backgrounds, separators and the
|
|
//! EditBox text field with vanilla cursor, selection and scrolling.
|
|
use crate::{
|
|
hud::{HudAtlas, HudBatch, Rect, rgba},
|
|
text::Text,
|
|
};
|
|
use std::{collections::HashMap, path::Path, sync::RwLock, time::Instant};
|
|
|
|
/// Nine-slice borders (left, top, right, bottom) from `.png.mcmeta` files.
|
|
static NINE_SLICE: RwLock<Option<HashMap<String, [u32; 4]>>> = RwLock::new(None);
|
|
|
|
/// Read `gui.scaling` metadata for every GUI sprite under `root`.
|
|
pub fn load_metadata(root: &Path) {
|
|
let mut map = HashMap::new();
|
|
let base = root.join("assets/minecraft/textures");
|
|
let mut stack = vec![base.join("gui/sprites")];
|
|
while let Some(dir) = stack.pop() {
|
|
let Ok(entries) = std::fs::read_dir(&dir) else {
|
|
continue;
|
|
};
|
|
for e in entries.flatten() {
|
|
let p = e.path();
|
|
if p.is_dir() {
|
|
stack.push(p);
|
|
continue;
|
|
}
|
|
let name = p.to_string_lossy().into_owned();
|
|
let Some(stripped) = name.strip_suffix(".png.mcmeta") else {
|
|
continue;
|
|
};
|
|
let Ok(text) = std::fs::read_to_string(&p) else {
|
|
continue;
|
|
};
|
|
let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) else {
|
|
continue;
|
|
};
|
|
let s = &v["gui"]["scaling"];
|
|
if s["type"] != "nine_slice" {
|
|
continue;
|
|
}
|
|
let border = &s["border"];
|
|
let b = if let Some(n) = border.as_u64() {
|
|
[n as u32; 4]
|
|
} else {
|
|
let g = |k: &str| border[k].as_u64().unwrap_or(0) as u32;
|
|
[g("left"), g("top"), g("right"), g("bottom")]
|
|
};
|
|
let key = Path::new(stripped)
|
|
.strip_prefix(&base)
|
|
.map(|r| r.to_string_lossy().replace('\\', "/"))
|
|
.unwrap_or_default();
|
|
map.insert(key, b);
|
|
}
|
|
}
|
|
*NINE_SLICE.write().unwrap_or_else(|e| e.into_inner()) = Some(map);
|
|
}
|
|
fn border(sprite: &str) -> [u32; 4] {
|
|
NINE_SLICE
|
|
.read()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.as_ref()
|
|
.and_then(|m| m.get(sprite).copied())
|
|
.unwrap_or(match sprite {
|
|
"gui/sprites/widget/button"
|
|
| "gui/sprites/widget/button_highlighted"
|
|
| "gui/sprites/widget/button_disabled" => [3; 4],
|
|
"gui/sprites/widget/slider_handle" | "gui/sprites/widget/slider_handle_highlighted" => {
|
|
[2, 2, 2, 3]
|
|
}
|
|
_ => [1; 4],
|
|
})
|
|
}
|
|
fn sub(r: Rect, x: f32, y: f32, w: f32, h: f32) -> Rect {
|
|
let (du, dv) = ((r.u1 - r.u0) / r.w as f32, (r.v1 - r.v0) / r.h as f32);
|
|
Rect {
|
|
u0: r.u0 + x * du,
|
|
v0: r.v0 + y * dv,
|
|
u1: r.u0 + (x + w) * du,
|
|
v1: r.v0 + (y + h) * dv,
|
|
w: w as u32,
|
|
h: h as u32,
|
|
}
|
|
}
|
|
/// Tile `src` (a sub-rectangle of a sprite, in its pixels) over a target area.
|
|
fn tile(batch: &mut HudBatch, r: Rect, src: [f32; 4], dst: [f32; 4], color: u32) {
|
|
let [sx, sy, sw, sh] = src;
|
|
let [dx, dy, dw, dh] = dst;
|
|
if sw <= 0.0 || sh <= 0.0 || dw <= 0.0 || dh <= 0.0 {
|
|
return;
|
|
}
|
|
let mut y = 0.0;
|
|
while y < dh {
|
|
let h = sh.min(dh - y);
|
|
let mut x = 0.0;
|
|
while x < dw {
|
|
let w = sw.min(dw - x);
|
|
batch.rect_uv(dx + x, dy + y, w, h, sub(r, sx, sy, w, h), color);
|
|
x += sw;
|
|
}
|
|
y += sh;
|
|
}
|
|
}
|
|
/// GuiGraphics.blitSprite with nine-slice scaling (inner parts tiled).
|
|
pub fn nine_slice(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
sprite: &str,
|
|
x: f32,
|
|
y: f32,
|
|
w: f32,
|
|
h: f32,
|
|
color: u32,
|
|
) {
|
|
let Some(r) = atlas.sprites.get(sprite).copied() else {
|
|
return;
|
|
};
|
|
let [l, t, rr, b] = border(sprite).map(|v| v as f32);
|
|
let (sw, sh) = (r.w as f32, r.h as f32);
|
|
let l = l.min(w / 2.0);
|
|
let rr = rr.min(w / 2.0);
|
|
let t = t.min(h / 2.0);
|
|
let b = b.min(h / 2.0);
|
|
let (cw, ch) = (w - l - rr, h - t - b);
|
|
let (scw, sch) = (sw - l - rr, sh - t - b);
|
|
// Corners.
|
|
batch.rect_uv(x, y, l, t, sub(r, 0.0, 0.0, l, t), color);
|
|
batch.rect_uv(x + w - rr, y, rr, t, sub(r, sw - rr, 0.0, rr, t), color);
|
|
batch.rect_uv(x, y + h - b, l, b, sub(r, 0.0, sh - b, l, b), color);
|
|
batch.rect_uv(
|
|
x + w - rr,
|
|
y + h - b,
|
|
rr,
|
|
b,
|
|
sub(r, sw - rr, sh - b, rr, b),
|
|
color,
|
|
);
|
|
// Edges and centre.
|
|
tile(batch, r, [l, 0.0, scw, t], [x + l, y, cw, t], color);
|
|
tile(
|
|
batch,
|
|
r,
|
|
[l, sh - b, scw, b],
|
|
[x + l, y + h - b, cw, b],
|
|
color,
|
|
);
|
|
tile(batch, r, [0.0, t, l, sch], [x, y + t, l, ch], color);
|
|
tile(
|
|
batch,
|
|
r,
|
|
[sw - rr, t, rr, sch],
|
|
[x + w - rr, y + t, rr, ch],
|
|
color,
|
|
);
|
|
tile(batch, r, [l, t, scw, sch], [x + l, y + t, cw, ch], color);
|
|
}
|
|
/// Repeat a whole texture with each copy drawn `size` GUI pixels square.
|
|
pub fn tiled(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
sprite: &str,
|
|
x: f32,
|
|
y: f32,
|
|
w: f32,
|
|
h: f32,
|
|
size: f32,
|
|
color: u32,
|
|
) {
|
|
let Some(r) = atlas.sprites.get(sprite).copied() else {
|
|
return;
|
|
};
|
|
let scale = r.w as f32 / size;
|
|
let mut yy = 0.0;
|
|
while yy < h {
|
|
let th = size.min(h - yy);
|
|
let mut xx = 0.0;
|
|
while xx < w {
|
|
let tw = size.min(w - xx);
|
|
batch.rect_uv(
|
|
x + xx,
|
|
y + yy,
|
|
tw,
|
|
th,
|
|
sub(r, 0.0, 0.0, tw * scale, th * scale),
|
|
color,
|
|
);
|
|
xx += size;
|
|
}
|
|
yy += size;
|
|
}
|
|
}
|
|
/// Text drawn at `scale` times its size (a scaled pose).
|
|
pub fn text_scaled(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
text: &Text,
|
|
x: f32,
|
|
y: f32,
|
|
scale: f32,
|
|
color: u32,
|
|
) {
|
|
let start = batch.vertices.len();
|
|
batch.text_styled(atlas, text, 0.0, 0.0, color, true);
|
|
for v in &mut batch.vertices[start..] {
|
|
v.pos = [x + v.pos[0] * scale, y + v.pos[1] * scale];
|
|
}
|
|
}
|
|
pub fn hovered(cursor: (f32, f32), x: f32, y: f32, w: f32, h: f32) -> bool {
|
|
cursor.0 >= x && cursor.0 < x + w && cursor.1 >= y && cursor.1 < y + h
|
|
}
|
|
/// Centered text with a shadow, scrolled when wider than `w` (AbstractWidget.renderScrollingString).
|
|
pub fn centered_text(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
text: &Text,
|
|
x: f32,
|
|
y: f32,
|
|
w: f32,
|
|
color: u32,
|
|
) {
|
|
let width = atlas.font.width_text(text);
|
|
if width <= w {
|
|
batch.text_styled(atlas, text, x + ((w - width) / 2.0).floor(), y, color, true);
|
|
} else {
|
|
// Clip by characters (vanilla scrolls; we show the part that fits).
|
|
let mut shown = Text::empty();
|
|
let mut used = 0.0;
|
|
for (i, (c, s)) in text.chars().enumerate() {
|
|
used += atlas.font.advance(c, s);
|
|
if used > w - 2.0 {
|
|
break;
|
|
}
|
|
shown.push(text.slice(i, i + 1));
|
|
}
|
|
batch.text_styled(atlas, &shown, x + 1.0, y, color, true);
|
|
}
|
|
}
|
|
/// Button (AbstractButton): nine-slice sprite plus the centred label.
|
|
pub fn button(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
x: f32,
|
|
y: f32,
|
|
w: f32,
|
|
h: f32,
|
|
label: &Text,
|
|
active: bool,
|
|
hover: bool,
|
|
) {
|
|
crate::widgets::record(&label.plain(), [x, y, w, h], active);
|
|
let sprite = if !active {
|
|
"gui/sprites/widget/button_disabled"
|
|
} else if hover {
|
|
"gui/sprites/widget/button_highlighted"
|
|
} else {
|
|
"gui/sprites/widget/button"
|
|
};
|
|
nine_slice(batch, atlas, sprite, x, y, w, h, u32::MAX);
|
|
let color = if active {
|
|
rgba(255, 255, 255, 255)
|
|
} else {
|
|
rgba(160, 160, 160, 255)
|
|
};
|
|
centered_text(
|
|
batch,
|
|
atlas,
|
|
label,
|
|
x + 2.0,
|
|
y + ((h - 8.0) / 2.0).floor(),
|
|
w - 4.0,
|
|
color,
|
|
);
|
|
}
|
|
/// AbstractSliderButton: track, 8px handle at `value` (0..1) and the label.
|
|
pub fn slider(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
x: f32,
|
|
y: f32,
|
|
w: f32,
|
|
label: &Text,
|
|
value: f32,
|
|
hover: bool,
|
|
active: bool,
|
|
) {
|
|
let kind = crate::widgets::Kind::Slider {
|
|
value,
|
|
track: [x + 4.0, w - 8.0],
|
|
};
|
|
// The control socket's widget tree (`ui`): its label and value.
|
|
crate::widgets::record_as(&label.plain(), [x, y, w, 20.0], active, kind);
|
|
if active {
|
|
// A controller's D-pad can focus it (Horizon's controller support).
|
|
crate::widgets::target_as([x, y, w, 20.0], kind);
|
|
}
|
|
let track = if hover && active {
|
|
"gui/sprites/widget/slider_highlighted"
|
|
} else {
|
|
"gui/sprites/widget/slider"
|
|
};
|
|
nine_slice(batch, atlas, track, x, y, w, 20.0, u32::MAX);
|
|
let handle = if hover && active {
|
|
"gui/sprites/widget/slider_handle_highlighted"
|
|
} else {
|
|
"gui/sprites/widget/slider_handle"
|
|
};
|
|
nine_slice(
|
|
batch,
|
|
atlas,
|
|
handle,
|
|
x + (value.clamp(0.0, 1.0) * (w - 8.0)).floor(),
|
|
y,
|
|
8.0,
|
|
20.0,
|
|
u32::MAX,
|
|
);
|
|
let color = if active {
|
|
u32::MAX
|
|
} else {
|
|
rgba(160, 160, 160, 255)
|
|
};
|
|
centered_text(batch, atlas, label, x + 2.0, y + 6.0, w - 4.0, color);
|
|
}
|
|
/// Slider value for a click at `cx` (AbstractSliderButton.setValueFromMouse).
|
|
pub fn slider_value(x: f32, w: f32, cx: f32) -> f32 {
|
|
((cx - (x + 4.0)) / (w - 8.0)).clamp(0.0, 1.0)
|
|
}
|
|
/// Screen.renderMenuBackground: in a world the translucent dark texture,
|
|
/// otherwise the dirt-like menu texture, tiled at 32 pixels.
|
|
pub fn menu_background(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
x: f32,
|
|
y: f32,
|
|
w: f32,
|
|
h: f32,
|
|
in_world: bool,
|
|
) {
|
|
let sprite = if in_world {
|
|
"gui/inworld_menu_background"
|
|
} else {
|
|
"gui/menu_background"
|
|
};
|
|
if atlas.sprites.contains_key(sprite) {
|
|
tiled(batch, atlas, sprite, x, y, w, h, 32.0, u32::MAX);
|
|
} else {
|
|
batch.fill(atlas, x, y, w, h, rgba(0, 0, 0, 160));
|
|
}
|
|
}
|
|
/// Header and footer separators of list screens.
|
|
pub fn separators(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
header_bottom: f32,
|
|
footer_top: f32,
|
|
in_world: bool,
|
|
) {
|
|
let (header, footer) = if in_world {
|
|
(
|
|
"gui/inworld_header_separator",
|
|
"gui/inworld_footer_separator",
|
|
)
|
|
} else {
|
|
("gui/header_separator", "gui/footer_separator")
|
|
};
|
|
let w = batch.width;
|
|
tiled(
|
|
batch,
|
|
atlas,
|
|
header,
|
|
0.0,
|
|
header_bottom - 2.0,
|
|
w,
|
|
2.0,
|
|
32.0,
|
|
u32::MAX,
|
|
);
|
|
tiled(
|
|
batch,
|
|
atlas,
|
|
footer,
|
|
0.0,
|
|
footer_top,
|
|
w,
|
|
2.0,
|
|
32.0,
|
|
u32::MAX,
|
|
);
|
|
}
|
|
/// List background between the separators.
|
|
pub fn list_background(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
top: f32,
|
|
bottom: f32,
|
|
in_world: bool,
|
|
) {
|
|
let sprite = if in_world {
|
|
"gui/inworld_menu_list_background"
|
|
} else {
|
|
"gui/menu_list_background"
|
|
};
|
|
tiled(
|
|
batch,
|
|
atlas,
|
|
sprite,
|
|
0.0,
|
|
top,
|
|
batch.width,
|
|
bottom - top,
|
|
32.0,
|
|
u32::MAX,
|
|
);
|
|
}
|
|
/// The visible scrollbar thumb, shared by painting and pointer input.
|
|
pub(crate) fn scrollbar_thumb(
|
|
top: f32,
|
|
bottom: f32,
|
|
content: f32,
|
|
scroll: f32,
|
|
) -> Option<(f32, f32)> {
|
|
let height = bottom - top;
|
|
if content <= height {
|
|
return None;
|
|
}
|
|
let bar = (height * height / content)
|
|
.clamp(32.0, height - 8.0)
|
|
.floor();
|
|
let y = top + ((scroll / (content - height)) * (height - bar)).floor();
|
|
Some((y, bar))
|
|
}
|
|
|
|
/// `AbstractScrollArea`'s scrolling of a list `top..bottom` holding `content`
|
|
/// pixels: the amount, the mouse wheel and the held scrollbar.
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct Scroll {
|
|
pub amount: f32,
|
|
/// The pointer's last y while the scrollbar is held (`scrolling`).
|
|
held: Option<f32>,
|
|
}
|
|
impl Scroll {
|
|
fn max(top: f32, bottom: f32, content: f32) -> f32 {
|
|
(content - (bottom - top)).max(0.0)
|
|
}
|
|
/// `setScrollAmount`: kept within the content.
|
|
pub fn clamp(&mut self, top: f32, bottom: f32, content: f32) {
|
|
self.amount = self.amount.clamp(0.0, Self::max(top, bottom, content));
|
|
}
|
|
/// `mouseScrolled`: `rate` pixels per wheel step.
|
|
pub fn wheel(&mut self, delta: f32, rate: f32, top: f32, bottom: f32, content: f32) {
|
|
self.amount -= delta * rate;
|
|
self.clamp(top, bottom, content);
|
|
}
|
|
/// `updateScrolling`: a press on the scrollbar column at `bar_x` (when
|
|
/// there is something to scroll) holds it.
|
|
pub fn press(
|
|
&mut self,
|
|
(x, y): (f32, f32),
|
|
bar_x: f32,
|
|
top: f32,
|
|
bottom: f32,
|
|
content: f32,
|
|
) -> bool {
|
|
let over = x >= bar_x && x <= bar_x + 6.0 && y >= top && y < bottom;
|
|
self.held = (over && Self::max(top, bottom, content) > 0.0).then_some(y);
|
|
self.held.is_some()
|
|
}
|
|
pub fn held(&self) -> bool {
|
|
self.held.is_some()
|
|
}
|
|
/// `mouseDragged` while held: above or below the list jumps to an end,
|
|
/// otherwise the thumb follows the pointer.
|
|
pub fn drag(&mut self, y: f32, top: f32, bottom: f32, content: f32) {
|
|
let Some(last) = self.held else { return };
|
|
let max = Self::max(top, bottom, content);
|
|
if y < top {
|
|
self.amount = 0.0;
|
|
} else if y > bottom {
|
|
self.amount = max;
|
|
} else {
|
|
let height = bottom - top;
|
|
let bar = ((height * height / content).trunc()).clamp(32.0, height - 8.0);
|
|
self.amount += (y - last) * (max.max(1.0) / (height - bar)).max(1.0);
|
|
}
|
|
self.clamp(top, bottom, content);
|
|
self.held = Some(y);
|
|
}
|
|
/// `onRelease`.
|
|
pub fn release(&mut self) {
|
|
self.held = None;
|
|
}
|
|
}
|
|
|
|
/// Vertical scroll bar (AbstractScrollArea): background and scroller sprites.
|
|
pub fn scrollbar(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
x: f32,
|
|
top: f32,
|
|
bottom: f32,
|
|
content: f32,
|
|
scroll: f32,
|
|
) {
|
|
let Some((y, bar)) = scrollbar_thumb(top, bottom, content, scroll) else {
|
|
return;
|
|
};
|
|
let height = bottom - top;
|
|
nine_slice(
|
|
batch,
|
|
atlas,
|
|
"gui/sprites/widget/scroller_background",
|
|
x,
|
|
top,
|
|
6.0,
|
|
height,
|
|
u32::MAX,
|
|
);
|
|
nine_slice(
|
|
batch,
|
|
atlas,
|
|
"gui/sprites/widget/scroller",
|
|
x,
|
|
y,
|
|
6.0,
|
|
bar,
|
|
u32::MAX,
|
|
);
|
|
}
|
|
|
|
/// Screen tooltip (DefaultTooltipPositioner + TooltipRenderUtil): the
|
|
/// background and frame sprites around the lines, beside the cursor.
|
|
pub fn tooltip(batch: &mut HudBatch, atlas: &HudAtlas, lines: &[Text], cursor: (f32, f32)) {
|
|
if lines.is_empty() {
|
|
return;
|
|
}
|
|
let w = lines
|
|
.iter()
|
|
.map(|l| atlas.font.width_text(l))
|
|
.fold(0.0, f32::max);
|
|
let h = if lines.len() == 1 {
|
|
8.0
|
|
} else {
|
|
10.0 * lines.len() as f32
|
|
};
|
|
let mut x = cursor.0 + 12.0;
|
|
let mut y = cursor.1 - 12.0;
|
|
if x + w > batch.width {
|
|
x = (cursor.0 - 24.0 - w).max(4.0);
|
|
}
|
|
if y + h + 3.0 > batch.height {
|
|
y = batch.height - h - 3.0;
|
|
}
|
|
y = y.max(4.0);
|
|
nine_slice(
|
|
batch,
|
|
atlas,
|
|
"gui/sprites/tooltip/background",
|
|
x - 12.0,
|
|
y - 12.0,
|
|
w + 24.0,
|
|
h + 24.0,
|
|
u32::MAX,
|
|
);
|
|
nine_slice(
|
|
batch,
|
|
atlas,
|
|
"gui/sprites/tooltip/frame",
|
|
x - 12.0,
|
|
y - 12.0,
|
|
w + 24.0,
|
|
h + 24.0,
|
|
u32::MAX,
|
|
);
|
|
let mut ly = y;
|
|
for (i, line) in lines.iter().enumerate() {
|
|
batch.text_styled(atlas, line, x, ly, u32::MAX, true);
|
|
ly += if i == 0 { 12.0 } else { 10.0 };
|
|
}
|
|
}
|
|
|
|
/// The EditBox text field.
|
|
#[derive(Clone, Debug)]
|
|
pub struct EditBox {
|
|
pub value: String,
|
|
/// Cursor and selection anchor, in characters.
|
|
pub cursor: usize,
|
|
pub highlight: usize,
|
|
display: usize,
|
|
pub max_length: usize,
|
|
pub bordered: bool,
|
|
pub x: f32,
|
|
pub y: f32,
|
|
pub width: f32,
|
|
pub height: f32,
|
|
pub focused: bool,
|
|
focused_at: Instant,
|
|
/// Grey completion shown after the text (EditBox.setSuggestion).
|
|
pub suggestion: Option<String>,
|
|
pub text_color: u32,
|
|
}
|
|
/// The focused text field of a screen that keeps its values as `String`s
|
|
/// (the world list's search, world names, seeds): the `EditBox` state that
|
|
/// outlives a frame, its cursor, selection and scroll.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Caret {
|
|
field: Option<crate::clipboard::FieldId>,
|
|
edit: EditBox,
|
|
/// The last click's time and field, for double clicks.
|
|
last_click: Option<(Instant, crate::clipboard::FieldId)>,
|
|
/// The button is held since a press on the field (`onDrag`).
|
|
held: bool,
|
|
}
|
|
impl Default for Caret {
|
|
fn default() -> Self {
|
|
Self {
|
|
field: None,
|
|
edit: EditBox::new(0.0, 0.0, 200.0, 20.0),
|
|
last_click: None,
|
|
held: false,
|
|
}
|
|
}
|
|
}
|
|
impl Caret {
|
|
/// The box for field `id` holding `value`. A field newly focused, or
|
|
/// whose value changed elsewhere, starts with its cursor at the end.
|
|
fn sync(&mut self, id: crate::clipboard::FieldId, value: &str, max: usize) -> &mut EditBox {
|
|
self.edit.max_length = max;
|
|
if self.field != Some(id) || self.edit.value != value {
|
|
self.edit.value = value.to_owned();
|
|
self.edit.move_cursor_to_end(false);
|
|
if self.field != Some(id) {
|
|
self.edit.display = 0;
|
|
self.field = Some(id);
|
|
}
|
|
}
|
|
&mut self.edit
|
|
}
|
|
/// Edit field `id`'s `value` through its box; the value is written back.
|
|
pub fn edit<R>(
|
|
&mut self,
|
|
id: crate::clipboard::FieldId,
|
|
value: &mut String,
|
|
max: usize,
|
|
f: impl FnOnce(&mut EditBox) -> R,
|
|
) -> R {
|
|
let edit = self.sync(id, value, max);
|
|
let result = f(edit);
|
|
value.clone_from(&edit.value);
|
|
result
|
|
}
|
|
/// A press on field `id` at GUI `x` (`EditBox.onClick`): a second press
|
|
/// within 250 ms is a double click (`MouseHandler`).
|
|
pub fn press(
|
|
&mut self,
|
|
atlas: &HudAtlas,
|
|
(id, value, max): (crate::clipboard::FieldId, &mut String, usize),
|
|
rect: [f32; 4],
|
|
x: f32,
|
|
shift: bool,
|
|
) {
|
|
let now = Instant::now();
|
|
let double = self
|
|
.last_click
|
|
.is_some_and(|(at, field)| field == id && now.duration_since(at).as_millis() < 250);
|
|
self.last_click = Some((now, id));
|
|
self.held = true;
|
|
self.edit(id, value, max, |edit| {
|
|
[edit.x, edit.y, edit.width, edit.height] = rect;
|
|
edit.click(atlas, x, shift, double);
|
|
});
|
|
}
|
|
/// The pointer moved with the button held after a press on field `id`.
|
|
pub fn drag(
|
|
&mut self,
|
|
atlas: &HudAtlas,
|
|
(id, value, max): (crate::clipboard::FieldId, &mut String, usize),
|
|
x: f32,
|
|
) {
|
|
if self.held && self.last_click.is_some_and(|(_, field)| field == id) {
|
|
self.edit(id, value, max, |edit| edit.drag_to(atlas, x));
|
|
}
|
|
}
|
|
/// The button was released: drags no longer select.
|
|
pub fn release(&mut self) {
|
|
self.held = false;
|
|
}
|
|
/// Draw field `id` at `rect`: the focused one with its caret, the others
|
|
/// showing the end of their text.
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn draw(
|
|
&mut self,
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
(id, value, max): (crate::clipboard::FieldId, &str, usize),
|
|
rect: [f32; 4],
|
|
focused: bool,
|
|
focused_at: Instant,
|
|
) {
|
|
let mut idle;
|
|
let edit = if focused {
|
|
self.sync(id, value, max)
|
|
} else {
|
|
idle = EditBox::new(0.0, 0.0, 0.0, 0.0);
|
|
idle.max_length = max;
|
|
idle.set_value(value);
|
|
&mut idle
|
|
};
|
|
[edit.x, edit.y, edit.width, edit.height] = rect;
|
|
edit.focused = focused;
|
|
if edit.focused_at < focused_at {
|
|
edit.focused_at = focused_at;
|
|
}
|
|
edit.draw(batch, atlas, None);
|
|
}
|
|
}
|
|
|
|
/// `EditBox.extractWidgetRenderState` for Horizon's append-only fields,
|
|
/// unbordered and with the cursor at the end: the text from (x, y), the
|
|
/// highlight when Select All covers the value (`invert_text` as
|
|
/// `setInvertHighlightedTextColor`), and the cursor, blinking from when the
|
|
/// field took the focus (`TextCursorUtils.isCursorVisible`): an underscore,
|
|
/// or a bar once the value is `max_chars` long. `shadow` is `setTextShadow`.
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn append_field(
|
|
batch: &mut HudBatch,
|
|
atlas: &HudAtlas,
|
|
value: &str,
|
|
(x, y, width): (f32, f32, f32),
|
|
(color, shadow): (u32, bool),
|
|
max_chars: usize,
|
|
selected: bool,
|
|
invert_text: bool,
|
|
focused_ms: u128,
|
|
) {
|
|
let mut cursor_x = x;
|
|
if !value.is_empty() {
|
|
batch.text(atlas, value, x, y, color, shadow);
|
|
cursor_x += HudBatch::text_width(atlas, value) + 1.0;
|
|
}
|
|
let insert = char_len(value) >= max_chars;
|
|
if insert {
|
|
cursor_x -= 1.0;
|
|
}
|
|
if selected && !value.is_empty() {
|
|
let right = x + width;
|
|
let (x0, x1) = (cursor_x.min(right), (x - 1.0).min(right));
|
|
batch.text_highlight(atlas, (x1, y - 1.0, x0 - x1, 11.0), invert_text);
|
|
}
|
|
if (focused_ms / 300).is_multiple_of(2) {
|
|
if insert {
|
|
batch.fill(atlas, cursor_x, y - 1.0, 1.0, 11.0, color);
|
|
} else {
|
|
batch.text(atlas, "_", cursor_x, y, color, shadow);
|
|
}
|
|
}
|
|
}
|
|
fn byte_index(s: &str, chars: usize) -> usize {
|
|
s.char_indices().nth(chars).map_or(s.len(), |(i, _)| i)
|
|
}
|
|
pub fn char_len(s: &str) -> usize {
|
|
s.chars().count()
|
|
}
|
|
impl crate::clipboard::Editable for EditBox {
|
|
fn select_all(&mut self) {
|
|
self.move_cursor_to_end(false);
|
|
self.highlight = 0;
|
|
}
|
|
fn selected_text(&self) -> String {
|
|
EditBox::selected_text(self)
|
|
}
|
|
fn insert(&mut self, text: &str) -> bool {
|
|
let before = self.value.clone();
|
|
self.insert_text(text);
|
|
self.value != before
|
|
}
|
|
}
|
|
impl EditBox {
|
|
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
|
|
Self {
|
|
value: String::new(),
|
|
cursor: 0,
|
|
highlight: 0,
|
|
display: 0,
|
|
max_length: 32,
|
|
bordered: true,
|
|
x,
|
|
y,
|
|
width,
|
|
height,
|
|
focused: true,
|
|
focused_at: Instant::now(),
|
|
suggestion: None,
|
|
text_color: rgba(224, 224, 224, 255),
|
|
}
|
|
}
|
|
/// When the field gained focus, for the cursor's blink.
|
|
pub fn focused_since(&mut self, at: Instant) {
|
|
self.focused_at = at;
|
|
}
|
|
pub fn inner_width(&self) -> f32 {
|
|
if self.bordered {
|
|
self.width - 8.0
|
|
} else {
|
|
self.width
|
|
}
|
|
}
|
|
pub fn text_x(&self) -> f32 {
|
|
if self.bordered { self.x + 4.0 } else { self.x }
|
|
}
|
|
pub fn text_y(&self) -> f32 {
|
|
if self.bordered {
|
|
self.y + ((self.height - 8.0) / 2.0).floor()
|
|
} else {
|
|
self.y
|
|
}
|
|
}
|
|
pub fn reset_blink(&mut self) {
|
|
self.focused_at = Instant::now();
|
|
}
|
|
pub fn set_value(&mut self, value: &str) {
|
|
self.value = value.chars().take(self.max_length).collect();
|
|
self.move_cursor_to_end(false);
|
|
}
|
|
fn selected(&self) -> (usize, usize) {
|
|
(
|
|
self.cursor.min(self.highlight),
|
|
self.cursor.max(self.highlight),
|
|
)
|
|
}
|
|
pub fn selected_text(&self) -> String {
|
|
let (a, b) = self.selected();
|
|
self.value.chars().skip(a).take(b - a).collect()
|
|
}
|
|
/// EditBox.insertText: replace the selection, respecting the maximum length.
|
|
pub fn insert_text(&mut self, text: &str) {
|
|
let (a, b) = self.selected();
|
|
let filtered = crate::clipboard::filter_text(text, false);
|
|
let room = self
|
|
.max_length
|
|
.saturating_sub(char_len(&self.value) - (b - a));
|
|
let insert: String = filtered.chars().take(room).collect();
|
|
let (ba, bb) = (byte_index(&self.value, a), byte_index(&self.value, b));
|
|
self.value.replace_range(ba..bb, &insert);
|
|
let pos = a + char_len(&insert);
|
|
self.set_cursor(pos, false);
|
|
}
|
|
/// Delete `n` characters (negative: backwards) or the selection.
|
|
pub fn delete_chars(&mut self, n: isize) {
|
|
if self.cursor != self.highlight {
|
|
self.insert_text("");
|
|
return;
|
|
}
|
|
let target = (self.cursor as isize + n).clamp(0, char_len(&self.value) as isize) as usize;
|
|
let (a, b) = (self.cursor.min(target), self.cursor.max(target));
|
|
let (ba, bb) = (byte_index(&self.value, a), byte_index(&self.value, b));
|
|
self.value.replace_range(ba..bb, "");
|
|
self.set_cursor(a, false);
|
|
}
|
|
/// EditBox.getWordPosition from the cursor.
|
|
pub fn word_position(&self, dir: isize) -> usize {
|
|
self.word_position_from(dir, self.cursor)
|
|
}
|
|
/// EditBox.getWordPosition(dir, from).
|
|
fn word_position_from(&self, dir: isize, from: usize) -> usize {
|
|
let chars: Vec<char> = self.value.chars().collect();
|
|
let mut pos = from.min(chars.len());
|
|
if dir < 0 {
|
|
while pos > 0 && chars[pos - 1] == ' ' {
|
|
pos -= 1;
|
|
}
|
|
while pos > 0 && chars[pos - 1] != ' ' {
|
|
pos -= 1;
|
|
}
|
|
} else {
|
|
let len = chars.len();
|
|
while pos < len && chars[pos] != ' ' {
|
|
pos += 1;
|
|
}
|
|
while pos < len && chars[pos] == ' ' {
|
|
pos += 1;
|
|
}
|
|
}
|
|
pos
|
|
}
|
|
pub fn set_cursor(&mut self, pos: usize, select: bool) {
|
|
self.cursor = pos.min(char_len(&self.value));
|
|
if !select {
|
|
self.highlight = self.cursor;
|
|
}
|
|
self.scroll_to(self.cursor);
|
|
self.reset_blink();
|
|
}
|
|
pub fn move_cursor_to_end(&mut self, select: bool) {
|
|
self.set_cursor(char_len(&self.value), select);
|
|
}
|
|
/// EditBox.findClickedPositionInText: the character boundary under GUI `x`.
|
|
pub fn position_at(&mut self, atlas: &HudAtlas, x: f32) -> usize {
|
|
self.layout(atlas);
|
|
let room = (x.floor() - self.text_x()).min(self.inner_width());
|
|
let rest: String = self.value.chars().skip(self.display).collect();
|
|
self.display + atlas.font.fit(&rest, room)
|
|
}
|
|
/// EditBox.onClick: the cursor to the pointer, extending the selection
|
|
/// with Shift; a double click selects the word under it (`selectWord`).
|
|
pub fn click(&mut self, atlas: &HudAtlas, x: f32, shift: bool, double: bool) {
|
|
let pos = self.position_at(atlas, x);
|
|
if double {
|
|
let (start, end) = (
|
|
self.word_position_from(-1, pos),
|
|
self.word_position_from(1, pos),
|
|
);
|
|
self.set_cursor(start, false);
|
|
self.set_cursor(end, true);
|
|
} else {
|
|
self.set_cursor(pos, shift);
|
|
}
|
|
}
|
|
/// EditBox.onDrag: the selection follows the pointer.
|
|
pub fn drag_to(&mut self, atlas: &HudAtlas, x: f32) {
|
|
let pos = self.position_at(atlas, x);
|
|
self.set_cursor(pos, true);
|
|
}
|
|
fn scroll_to(&mut self, pos: usize) {
|
|
// EditBox.scrollTo without font access is completed in `layout`.
|
|
self.display = self.display.min(char_len(&self.value));
|
|
if pos < self.display {
|
|
self.display = pos;
|
|
}
|
|
}
|
|
fn layout(&mut self, atlas: &HudAtlas) {
|
|
let len = char_len(&self.value);
|
|
self.display = self.display.min(len);
|
|
let (value, inner) = (self.value.clone(), self.inner_width());
|
|
let visible = |d: usize| -> usize {
|
|
let rest: String = value.chars().skip(d).collect();
|
|
atlas.font.fit(&rest, inner)
|
|
};
|
|
while self.cursor > self.display + visible(self.display) && self.display < len {
|
|
self.display += 1;
|
|
}
|
|
if self.cursor < self.display {
|
|
self.display = self.cursor;
|
|
}
|
|
}
|
|
/// Keyboard editing shared by every text field. Returns true if handled.
|
|
pub fn key(&mut self, key: winit::keyboard::KeyCode, ctrl: bool, shift: bool) -> bool {
|
|
use winit::keyboard::KeyCode as K;
|
|
match key {
|
|
K::Backspace => {
|
|
let n = if ctrl {
|
|
self.word_position(-1) as isize - self.cursor as isize
|
|
} else {
|
|
-1
|
|
};
|
|
self.delete_chars(n);
|
|
}
|
|
K::Delete => {
|
|
let n = if ctrl {
|
|
self.word_position(1) as isize - self.cursor as isize
|
|
} else {
|
|
1
|
|
};
|
|
self.delete_chars(n);
|
|
}
|
|
K::ArrowLeft => {
|
|
let p = if ctrl {
|
|
self.word_position(-1)
|
|
} else {
|
|
self.cursor.saturating_sub(1)
|
|
};
|
|
self.set_cursor(p, shift);
|
|
}
|
|
K::ArrowRight => {
|
|
let p = if ctrl {
|
|
self.word_position(1)
|
|
} else {
|
|
self.cursor + 1
|
|
};
|
|
self.set_cursor(p, shift);
|
|
}
|
|
K::Home => self.set_cursor(0, shift),
|
|
K::End => self.move_cursor_to_end(shift),
|
|
K::KeyA if ctrl => {
|
|
self.move_cursor_to_end(false);
|
|
self.highlight = 0;
|
|
}
|
|
_ => return false,
|
|
}
|
|
true
|
|
}
|
|
/// Ctrl+A/C/V/X (`EditBox.keyPressed`). True when the value changed.
|
|
pub fn shortcut(
|
|
&mut self,
|
|
shortcut: crate::clipboard::EditShortcut,
|
|
clipboard: &mut dyn crate::clipboard::Clipboard,
|
|
) -> bool {
|
|
let before = self.value.clone();
|
|
crate::clipboard::apply(self, shortcut, clipboard);
|
|
self.reset_blink();
|
|
self.value != before
|
|
}
|
|
/// The first shown character after scrolling to the cursor, and the
|
|
/// milliseconds since the field was focused (the cursor's blink).
|
|
pub fn view_state(&mut self, atlas: &HudAtlas) -> (usize, u64) {
|
|
self.layout(atlas);
|
|
(self.display, self.focused_at.elapsed().as_millis() as u64)
|
|
}
|
|
/// Draw the field; `format` styles the visible text (command colouring).
|
|
pub fn draw(&mut self, batch: &mut HudBatch, atlas: &HudAtlas, format: Option<&Text>) {
|
|
self.layout(atlas);
|
|
crate::widgets::target_as(
|
|
[self.x, self.y, self.width, self.height],
|
|
crate::widgets::Kind::Field,
|
|
);
|
|
if self.bordered {
|
|
let sprite = if self.focused {
|
|
"gui/sprites/widget/text_field_highlighted"
|
|
} else {
|
|
"gui/sprites/widget/text_field"
|
|
};
|
|
nine_slice(
|
|
batch,
|
|
atlas,
|
|
sprite,
|
|
self.x,
|
|
self.y,
|
|
self.width,
|
|
self.height,
|
|
u32::MAX,
|
|
);
|
|
}
|
|
let color = self.text_color;
|
|
let rest: String = self.value.chars().skip(self.display).collect();
|
|
let shown_len = atlas.font.fit(&rest, self.inner_width());
|
|
let displayed: String = rest.chars().take(shown_len).collect();
|
|
let rel_cursor = self.cursor as isize - self.display as isize;
|
|
let on_screen = rel_cursor >= 0 && rel_cursor as usize <= shown_len;
|
|
let show_cursor = self.focused
|
|
&& (self.focused_at.elapsed().as_millis() / 300).is_multiple_of(2)
|
|
&& on_screen;
|
|
let styled = |from: usize, to: usize| -> Text {
|
|
match format {
|
|
Some(t) => t.slice(self.display + from, self.display + to),
|
|
None => Text::literal(
|
|
displayed
|
|
.chars()
|
|
.skip(from)
|
|
.take(to - from)
|
|
.collect::<String>(),
|
|
),
|
|
}
|
|
};
|
|
let (tx, ty) = (self.text_x(), self.text_y());
|
|
let mut draw_x = tx;
|
|
if !displayed.is_empty() {
|
|
let half = if on_screen {
|
|
rel_cursor as usize
|
|
} else {
|
|
shown_len
|
|
};
|
|
let t = styled(0, half);
|
|
batch.text_styled(atlas, &t, draw_x, ty, color, true);
|
|
draw_x += atlas.font.width_text(&t) + 1.0;
|
|
}
|
|
let insert =
|
|
self.cursor < char_len(&self.value) || char_len(&self.value) >= self.max_length;
|
|
let mut cursor_x = draw_x;
|
|
if !on_screen {
|
|
cursor_x = if rel_cursor > 0 { tx + self.width } else { tx };
|
|
} else if insert {
|
|
cursor_x -= 1.0;
|
|
draw_x -= 1.0;
|
|
}
|
|
if !displayed.is_empty() && on_screen && (rel_cursor as usize) < shown_len {
|
|
batch.text_styled(
|
|
atlas,
|
|
&styled(rel_cursor as usize, shown_len),
|
|
draw_x,
|
|
ty,
|
|
color,
|
|
true,
|
|
);
|
|
}
|
|
if !insert && let Some(s) = &self.suggestion {
|
|
batch.text(atlas, s, cursor_x - 1.0, ty, rgba(128, 128, 128, 255), true);
|
|
}
|
|
let rel_highlight =
|
|
(self.highlight as isize - self.display as isize).clamp(0, shown_len as isize) as usize;
|
|
if rel_highlight as isize != rel_cursor {
|
|
let hx = tx
|
|
+ atlas
|
|
.font
|
|
.width(&displayed.chars().take(rel_highlight).collect::<String>());
|
|
// textHighlight between the cursor and the anchor, clipped at the right edge.
|
|
let right = self.x + self.width;
|
|
let (x0, x1) = (cursor_x.min(right), (hx - 1.0).min(right));
|
|
let (a, b) = (x0.min(x1), x0.max(x1));
|
|
batch.text_highlight(atlas, (a, ty - 1.0, b - a, 11.0), true);
|
|
}
|
|
if show_cursor {
|
|
if insert {
|
|
batch.fill(atlas, cursor_x, ty - 1.0, 1.0, 11.0, color);
|
|
} else {
|
|
batch.text(atlas, "_", cursor_x, ty, color, true);
|
|
}
|
|
}
|
|
}
|
|
/// Character index under GUI x (mouse click).
|
|
pub fn index_at(&self, atlas: &HudAtlas, x: f32) -> usize {
|
|
let rest: String = self.value.chars().skip(self.display).collect();
|
|
self.display + atlas.font.fit(&rest, (x - self.text_x()).max(0.0))
|
|
}
|
|
/// GUI x of a character index (EditBox.getScreenX).
|
|
pub fn screen_x(&self, atlas: &HudAtlas, index: usize) -> f32 {
|
|
if index > char_len(&self.value) {
|
|
return self.x;
|
|
}
|
|
let shown: String = self
|
|
.value
|
|
.chars()
|
|
.skip(self.display)
|
|
.take(index.saturating_sub(self.display))
|
|
.collect();
|
|
self.text_x() + atlas.font.width(&shown)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn append_fields_highlight_the_whole_value_and_blink_their_cursor() {
|
|
use crate::hud::Blend;
|
|
let atlas = HudAtlas::build(Path::new("/nonexistent/horizon-test-assets")).unwrap();
|
|
let end = 10.0 + HudBatch::text_width(&atlas, "abc") + 1.0;
|
|
let span = |b: &HudBatch, r: &std::ops::Range<u32>| {
|
|
let v = &b.vertices[r.start as usize..r.end as usize];
|
|
let xs = v.iter().map(|v| v.pos[0]);
|
|
let ys = v.iter().map(|v| v.pos[1]);
|
|
(
|
|
xs.clone().fold(f32::MAX, f32::min),
|
|
xs.fold(f32::MIN, f32::max),
|
|
ys.clone().fold(f32::MAX, f32::min),
|
|
ys.fold(f32::MIN, f32::max),
|
|
)
|
|
};
|
|
// Select All in the creative search: inverted white, then blue added,
|
|
// from one pixel before the text to the cursor, 11 high.
|
|
let mut b = HudBatch::new([854, 480]);
|
|
append_field(
|
|
&mut b,
|
|
&atlas,
|
|
"abc",
|
|
(10.0, 20.0, 80.0),
|
|
(u32::MAX, true),
|
|
50,
|
|
true,
|
|
true,
|
|
0,
|
|
);
|
|
let blends: Vec<_> = b.blends.iter().map(|(_, blend)| *blend).collect();
|
|
assert_eq!(blends, vec![Blend::Invert, Blend::Additive]);
|
|
for (r, _) in &b.blends {
|
|
assert_eq!(span(&b, r), (9.0, end, 19.0, 30.0));
|
|
}
|
|
// The anvil keeps its text colour; 300 ms later the cursor is hidden.
|
|
let mut b = HudBatch::new([854, 480]);
|
|
append_field(
|
|
&mut b,
|
|
&atlas,
|
|
"abc",
|
|
(10.0, 20.0, 103.0),
|
|
(u32::MAX, true),
|
|
50,
|
|
true,
|
|
false,
|
|
300,
|
|
);
|
|
let blends: Vec<_> = b.blends.iter().map(|(_, blend)| *blend).collect();
|
|
assert_eq!(blends, vec![Blend::Additive]);
|
|
let quads = |ms| {
|
|
let mut b = HudBatch::new([854, 480]);
|
|
append_field(
|
|
&mut b,
|
|
&atlas,
|
|
"abc",
|
|
(10.0, 20.0, 103.0),
|
|
(0, true),
|
|
50,
|
|
false,
|
|
false,
|
|
ms,
|
|
);
|
|
b.vertices.len()
|
|
};
|
|
assert!(quads(300) < quads(600));
|
|
// A full field shows a bar on its last pixel instead of an underscore.
|
|
let mut b = HudBatch::new([854, 480]);
|
|
append_field(
|
|
&mut b,
|
|
&atlas,
|
|
"abc",
|
|
(10.0, 20.0, 80.0),
|
|
(u32::MAX, true),
|
|
3,
|
|
false,
|
|
true,
|
|
0,
|
|
);
|
|
let bar = b.vertices.len() as u32 - 6..b.vertices.len() as u32;
|
|
assert_eq!(span(&b, &bar), (end - 1.0, end, 19.0, 30.0));
|
|
}
|
|
#[test]
|
|
fn held_scrollbars_follow_the_pointer_and_stop_at_the_ends() {
|
|
// A 100-pixel list holding 400: the thumb is 25 -> 32 high, so a
|
|
// pixel of drag scrolls 300 / 68 pixels (AbstractScrollArea.mouseDragged).
|
|
let (top, bottom, content) = (0.0, 100.0, 400.0);
|
|
let mut s = Scroll::default();
|
|
assert!(!s.press((50.0, 10.0), 100.0, top, bottom, content));
|
|
assert!(s.press((103.0, 10.0), 100.0, top, bottom, content));
|
|
s.drag(20.0, top, bottom, content);
|
|
assert!((s.amount - 10.0 * 300.0 / 68.0).abs() < 1e-3);
|
|
s.drag(150.0, top, bottom, content);
|
|
assert_eq!(s.amount, 300.0);
|
|
s.drag(-5.0, top, bottom, content);
|
|
assert_eq!(s.amount, 0.0);
|
|
s.release();
|
|
s.drag(50.0, top, bottom, content);
|
|
assert_eq!(s.amount, 0.0);
|
|
// Nothing to scroll: the column doesn't take the press.
|
|
assert!(!s.press((103.0, 10.0), 100.0, top, bottom, 90.0));
|
|
s.wheel(-2.0, 18.0, top, bottom, content);
|
|
assert_eq!(s.amount, 36.0);
|
|
}
|
|
#[test]
|
|
fn edit_box_clicks_place_the_cursor_and_select() {
|
|
let atlas = HudAtlas::build(Path::new("/nonexistent/horizon-test-assets")).unwrap();
|
|
let mut e = EditBox::new(10.0, 0.0, 200.0, 20.0);
|
|
e.set_value("hello world");
|
|
let at = |e: &EditBox, s: &str| e.text_x() + atlas.font.width(s);
|
|
// findClickedPositionInText: the characters left of the pointer.
|
|
e.click(&atlas, at(&e, "hello "), false, false);
|
|
assert_eq!((e.cursor, e.highlight), (6, 6));
|
|
// Shift extends the selection from the anchor.
|
|
e.click(&atlas, at(&e, "hello wor"), true, false);
|
|
assert_eq!(e.selected_text(), "wor");
|
|
// selectWord: the word and the spaces after it.
|
|
e.click(&atlas, at(&e, "he"), false, true);
|
|
assert_eq!(e.selected_text(), "hello ");
|
|
// onDrag from a click: left of the text is its start.
|
|
e.click(&atlas, at(&e, "hello "), false, false);
|
|
e.drag_to(&atlas, e.text_x() - 5.0);
|
|
assert_eq!((e.cursor, e.highlight), (0, 6));
|
|
}
|
|
#[test]
|
|
fn edit_box_editing() {
|
|
let mut e = EditBox::new(0.0, 0.0, 100.0, 20.0);
|
|
e.max_length = 10;
|
|
e.insert_text("hello world");
|
|
assert_eq!(e.value, "hello worl");
|
|
e.set_cursor(5, false);
|
|
assert_eq!(e.word_position(-1), 0);
|
|
assert_eq!(e.word_position(1), 6);
|
|
e.delete_chars(-2);
|
|
assert_eq!(e.value, "hel worl");
|
|
e.set_cursor(0, false);
|
|
e.set_cursor(3, true);
|
|
assert_eq!(e.selected_text(), "hel");
|
|
e.insert_text("é§x");
|
|
assert_eq!(e.value, "éx worl");
|
|
assert_eq!(e.cursor, 2);
|
|
}
|
|
}
|