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 {
17 Self(ctx)
18 }
19
20 /// Draw a text string at GUI position `(x, y)`.
21 /// `color` is `0xAARRGGBB`. `shadow` adds a drop-shadow.
22 pub fn text(&self, text: &str, x: f32, y: f32, color: u32, shadow: bool) {
23 let a = self.0.api();
24 unsafe { (a.draw2d_text)(YogStr::from_str(text), x, y, color, shadow) }
25 }
26
27 /// Fill a rectangle with a flat color (`0xAARRGGBB`).
28 pub fn rect(&self, x1: f32, y1: f32, x2: f32, y2: f32, color: u32) {
29 let a = self.0.api();
30 unsafe { (a.draw2d_rect)(x1, y1, x2, y2, color) }
31 }
32
33 /// Fill a rectangle with a vertical gradient (top → bottom, `0xAARRGGBB`).
34 pub fn gradient(&self, x1: f32, y1: f32, x2: f32, y2: f32, top: u32, bottom: u32) {
35 let a = self.0.api();
36 unsafe { (a.draw2d_gradient)(x1, y1, x2, y2, top, bottom) }
37 }
38
39 /// Blit a region from a Minecraft texture identified by namespace + path.
40 ///
41 /// - `id`: e.g. `"minecraft:textures/gui/icons.png"`
42 /// - `(x, y)`: screen position
43 /// - `(u0, v0)`: top-left UV in texels
44 /// - `(w, h)`: region size in pixels
45 /// - `(tw, th)`: full texture size in pixels
46 pub fn mc_texture(
47 &self,
48 id: &str,
49 x: f32,
50 y: f32,
51 u0: f32,
52 v0: f32,
53 w: f32,
54 h: f32,
55 tw: f32,
56 th: f32,
57 ) {
58 let a = self.0.api();
59 unsafe { (a.draw2d_mc_tex)(YogStr::from_str(id), x, y, u0, v0, w, h, tw, th) }
60 }
61
62 /// Render an item stack (3D block models included) via Minecraft's item
63 /// renderer — like inventory slots or Patchouli's item icons.
64 ///
65 /// - `id`: item registry id, e.g. `"minecraft:crafting_table"`
66 /// - `(x, y)`: screen position in GUI pixels
67 /// - `size`: on-screen size in GUI pixels (16 = inventory icon size)
68 pub fn item(&self, id: &str, x: f32, y: f32, size: f32) {
69 let a = self.0.api();
70 unsafe { (a.draw2d_item)(YogStr::from_str(id), x, y, size) }
71 }
72}