yog_ui/slot_cache.rs
1//! Pre-fetched inventory slot data — populated by the JNI layer before each
2//! render frame, consumed by [`WidgetKind::InvSlot`] widgets.
3
4use std::sync::Mutex;
5
6/// One slot's render data, fetched once per frame.
7#[derive(Debug, Clone, Default)]
8pub struct SlotData {
9 /// `"namespace:item_id"` — empty string if slot is empty.
10 pub item_id: String,
11 /// Stack count (0 if empty).
12 pub count: u32,
13 /// Screen-space pixel position (x, y).
14 pub x: i32,
15 pub y: i32,
16}
17
18/// Global slot cache, populated by `yog-runtime` before each inventory render.
19static SLOTS: Mutex<Vec<SlotData>> = Mutex::new(Vec::new());
20
21/// Replace the cached slot data (called by the JNI bridge before rendering).
22pub fn set_slot_cache(data: Vec<SlotData>) {
23 *SLOTS.lock().unwrap() = data;
24}
25
26/// Read cached slot data for the given slot index. Returns `None` if out of range.
27pub fn get_slot(index: usize) -> Option<SlotData> {
28 SLOTS.lock().unwrap().get(index).cloned()
29}
30
31/// Number of cached slots.
32pub fn slot_count() -> usize {
33 SLOTS.lock().unwrap().len()
34}