Preview
Loading GPUI
Props
Install
cargo add evilbuttonsThe label wants a pixel font. The demos use Pixelify Sans (SIL Open Font License); load it into GPUI and pass its family name:
cx.text_system().add_fonts(vec![
std::borrow::Cow::Borrowed(include_bytes!("../assets/fonts/PixelifySans.ttf").as_slice()),
])?;Usage
use evilbuttons::MinecraftButton;
use gpui::{div, prelude::*, Context, IntoElement, Render, Window};
struct ButtonDemo;
impl Render for ButtonDemo {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().child(
MinecraftButton::new("singleplayer")
.font_family("Pixelify Sans")
.taps_per_stage(5)
.stages(4)
.on_crack(|stage, _, _| println!("Crack stage {stage}"))
.on_break(|_, _| println!("Broken.")),
)
}
}Props
MinecraftButton is a GPUI RenderOnce builder; its taps, cracks and dropped item are keyed by id.
| Method | Type | Default | Description |
|---|---|---|---|
new(id) | impl Into<ElementId> | - | Unique id for the button state. |
label(s) | impl Into<SharedString> | "Singleplayer" | Button label. |
taps_per_stage(n) | u32 | 5 | Taps needed to advance one crack stage. |
stages(n) | u32 | 4 | Stages until it breaks. The button breaks after taps_per_stage * stages taps. |
pickaxe(bool) | bool | true | Draw a pixel pickaxe at the pointer that winds up on press and strikes on release. |
respawn_after(d) | Duration | - | Respawn automatically after this long. Unset, it waits for the drop to be picked up. |
font_family(f) | impl Into<SharedString> | inherited | Font for the label and the item tooltip, e.g. a pixel font. |
on_click(f) | Fn(&ClickEvent, &mut Window, &mut App) | - | Fired on every tap, before it counts. |
on_crack(f) | Fn(u32, &mut Window, &mut App) | - | Fired when the crack deepens to a new stage. |
on_break(f) | Fn(&mut Window, &mut App) | - | Fired when the button breaks. |
on_respawn(f) | Fn(&mut Window, &mut App) | - | Fired when the button comes back. |
disabled(bool) | bool | false | Darkens the stone and ignores input. |
Good to know
- Everything snaps to a 3px texel grid, and the stone texture and crack pattern are seeded, so they match the React version pixel for pixel.
- GPUI can't hide the system cursor, so the pickaxe is drawn next to a crosshair cursor whose centre sits on the pickaxe's tip.
- There are no sounds: GPUI has no audio API, so the React version's
soundprop has no equivalent here.