Skip to main content

dear_imgui_rs/render/draw_data/
cmd.rs

1use super::callbacks::{StandardDrawCallback, classify_standard_draw_callback};
2use crate::sys;
3use crate::texture::TextureId;
4use std::slice;
5
6/// Iterator over draw commands
7pub struct DrawCmdIterator<'a> {
8    iter: slice::Iter<'a, sys::ImDrawCmd>,
9}
10
11impl<'a> DrawCmdIterator<'a> {
12    pub(super) fn new(iter: slice::Iter<'a, sys::ImDrawCmd>) -> Self {
13        Self { iter }
14    }
15}
16
17impl<'a> Iterator for DrawCmdIterator<'a> {
18    type Item = DrawCmd;
19
20    fn next(&mut self) -> Option<Self::Item> {
21        self.iter.next().map(|cmd| {
22            let cmd_params = DrawCmdParams {
23                clip_rect: [
24                    cmd.ClipRect.x,
25                    cmd.ClipRect.y,
26                    cmd.ClipRect.z,
27                    cmd.ClipRect.w,
28                ],
29                // Use raw field; backends may resolve effective TexID later
30                texture_id: TextureId::from(cmd.TexRef._TexID),
31                vtx_offset: cmd.VtxOffset as usize,
32                idx_offset: cmd.IdxOffset as usize,
33            };
34
35            match classify_standard_draw_callback(cmd.UserCallback) {
36                Some(StandardDrawCallback::ResetRenderState) => DrawCmd::ResetRenderState,
37                Some(StandardDrawCallback::SetSamplerLinear) => DrawCmd::SetSamplerLinear,
38                Some(StandardDrawCallback::SetSamplerNearest) => DrawCmd::SetSamplerNearest,
39                None => match cmd.UserCallback {
40                    Some(raw_callback) => DrawCmd::RawCallback {
41                        callback: raw_callback,
42                        raw_cmd: cmd,
43                    },
44                    None => DrawCmd::Elements {
45                        count: cmd.ElemCount as usize,
46                        cmd_params,
47                        raw_cmd: cmd as *const sys::ImDrawCmd,
48                    },
49                },
50            }
51        })
52    }
53}
54
55/// Parameters for a draw command
56#[derive(Copy, Clone, Debug, PartialEq)]
57pub struct DrawCmdParams {
58    /// Clipping rectangle [left, top, right, bottom]
59    pub clip_rect: [f32; 4],
60    /// Texture ID to use for rendering
61    ///
62    /// Notes:
63    /// - For legacy paths (plain `TextureId`), this is the effective id.
64    /// - With the modern texture system (ImTextureRef/ImTextureData), this may be 0.
65    ///   Renderer backends should resolve the effective id at bind time using
66    ///   `ImDrawCmd_GetTexID` with the `raw_cmd` pointer and the backend render state.
67    pub texture_id: TextureId,
68    /// Vertex buffer offset
69    pub vtx_offset: usize,
70    /// Index buffer offset
71    pub idx_offset: usize,
72}
73
74/// A draw command
75#[derive(Clone, Debug)]
76pub enum DrawCmd {
77    /// Elements to draw
78    Elements {
79        /// The number of indices used for this draw command
80        count: usize,
81        cmd_params: DrawCmdParams,
82        /// Raw command pointer for backends
83        ///
84        /// Backend note: when using the modern texture system, resolve the effective
85        /// texture id at bind time via `ImDrawCmd_GetTexID(raw_cmd)` together with your
86        /// renderer state. This pointer is only valid during the `render_draw_data()`
87        /// call that produced it; do not store it.
88        raw_cmd: *const sys::ImDrawCmd,
89    },
90    /// Reset render state
91    ResetRenderState,
92    /// Switch texture sampling to linear/filtering.
93    SetSamplerLinear,
94    /// Switch texture sampling to nearest/point.
95    SetSamplerNearest,
96    /// Raw callback
97    RawCallback {
98        callback: unsafe extern "C" fn(*const sys::ImDrawList, cmd: *const sys::ImDrawCmd),
99        raw_cmd: *const sys::ImDrawCmd,
100    },
101}