yog_gfx/draw2d.rs
1//! 2D drawing helpers — thin wrappers over the `draw2d_*` ABI functions.
2//!
3//! These use Minecraft's own rendering infrastructure (text renderer, texture
4//! manager) and are therefore only valid during `on_hud_render`.
5
6use yog_abi::YogStr;
7
8use crate::GfxContext;
9
10/// 2D drawing helpers, valid only within `on_hud_render`.
11///
12/// Obtain via [`GfxContext::draw2d`].
13pub struct Draw2D<'ctx>(&'ctx GfxContext);
14
15impl<'ctx> Draw2D<'ctx> {
16 pub(crate) fn new(ctx: &'ctx GfxContext) -> Self { Self(ctx) }
17
18 /// Draw a text string at GUI position `(x, y)`.
19 /// `color` is `0xAARRGGBB`. `shadow` adds a drop-shadow.
20 pub fn text(&self, text: &str, x: f32, y: f32, color: u32, shadow: bool) {
21 let a = self.0.api();
22 unsafe { (a.draw2d_text)(YogStr::from_str(text), x, y, color, shadow) }
23 }
24
25 /// Fill a rectangle with a flat color (`0xAARRGGBB`).
26 pub fn rect(&self, x1: f32, y1: f32, x2: f32, y2: f32, color: u32) {
27 let a = self.0.api();
28 unsafe { (a.draw2d_rect)(x1, y1, x2, y2, color) }
29 }
30
31 /// Fill a rectangle with a vertical gradient (top → bottom, `0xAARRGGBB`).
32 pub fn gradient(&self, x1: f32, y1: f32, x2: f32, y2: f32, top: u32, bottom: u32) {
33 let a = self.0.api();
34 unsafe { (a.draw2d_gradient)(x1, y1, x2, y2, top, bottom) }
35 }
36
37 /// Blit a region from a Minecraft texture identified by namespace + path.
38 ///
39 /// - `id`: e.g. `"minecraft:textures/gui/icons.png"`
40 /// - `(x, y)`: screen position
41 /// - `(u0, v0)`: top-left UV in texels
42 /// - `(w, h)`: region size in pixels
43 /// - `(tw, th)`: full texture size in pixels
44 pub fn mc_texture(
45 &self, id: &str,
46 x: f32, y: f32,
47 u0: f32, v0: f32,
48 w: f32, h: f32,
49 tw: f32, th: f32,
50 ) {
51 let a = self.0.api();
52 unsafe { (a.draw2d_mc_tex)(YogStr::from_str(id), x, y, u0, v0, w, h, tw, th) }
53 }
54}