Skip to main content

dear_imgui_rs/render/draw_data/
vertex.rs

1use crate::sys;
2
3/// Vertex format used by Dear ImGui
4#[repr(C)]
5#[derive(Copy, Clone, Debug, PartialEq)]
6pub struct DrawVert {
7    /// Position (2D)
8    pub pos: [f32; 2],
9    /// UV coordinates
10    pub uv: [f32; 2],
11    /// Color (packed RGBA)
12    pub col: u32,
13}
14
15// Ensure our Rust-side vertex/index types stay layout-compatible with the raw sys bindings.
16const _: [(); std::mem::size_of::<sys::ImDrawVert>()] = [(); std::mem::size_of::<DrawVert>()];
17const _: [(); std::mem::align_of::<sys::ImDrawVert>()] = [(); std::mem::align_of::<DrawVert>()];
18
19impl DrawVert {
20    /// Creates a new draw vertex with u32 color
21    pub fn new(pos: [f32; 2], uv: [f32; 2], col: u32) -> Self {
22        Self { pos, uv, col }
23    }
24
25    /// Creates a new draw vertex from RGBA bytes
26    pub fn from_rgba(pos: [f32; 2], uv: [f32; 2], rgba: [u8; 4]) -> Self {
27        let col = ((rgba[3] as u32) << 24)
28            | ((rgba[2] as u32) << 16)
29            | ((rgba[1] as u32) << 8)
30            | (rgba[0] as u32);
31        Self { pos, uv, col }
32    }
33
34    /// Extracts RGBA bytes from the packed color
35    pub fn rgba(&self) -> [u8; 4] {
36        [
37            (self.col & 0xFF) as u8,
38            ((self.col >> 8) & 0xFF) as u8,
39            ((self.col >> 16) & 0xFF) as u8,
40            ((self.col >> 24) & 0xFF) as u8,
41        ]
42    }
43}
44
45/// Index type used by Dear ImGui
46pub type DrawIdx = u16;
47
48const _: [(); std::mem::size_of::<sys::ImDrawIdx>()] = [(); std::mem::size_of::<DrawIdx>()];
49const _: [(); std::mem::align_of::<sys::ImDrawIdx>()] = [(); std::mem::align_of::<DrawIdx>()];