use super::callbacks::{StandardDrawCallback, classify_standard_draw_callback};
use crate::sys;
use crate::texture::TextureId;
use std::slice;
pub struct DrawCmdIterator<'a> {
iter: slice::Iter<'a, sys::ImDrawCmd>,
}
impl<'a> DrawCmdIterator<'a> {
pub(super) fn new(iter: slice::Iter<'a, sys::ImDrawCmd>) -> Self {
Self { iter }
}
}
impl<'a> Iterator for DrawCmdIterator<'a> {
type Item = DrawCmd;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|cmd| {
let cmd_params = DrawCmdParams {
clip_rect: [
cmd.ClipRect.x,
cmd.ClipRect.y,
cmd.ClipRect.z,
cmd.ClipRect.w,
],
texture_id: TextureId::from(cmd.TexRef._TexID),
vtx_offset: cmd.VtxOffset as usize,
idx_offset: cmd.IdxOffset as usize,
};
match classify_standard_draw_callback(cmd.UserCallback) {
Some(StandardDrawCallback::ResetRenderState) => DrawCmd::ResetRenderState,
Some(StandardDrawCallback::SetSamplerLinear) => DrawCmd::SetSamplerLinear,
Some(StandardDrawCallback::SetSamplerNearest) => DrawCmd::SetSamplerNearest,
None => match cmd.UserCallback {
Some(raw_callback) => DrawCmd::RawCallback {
callback: raw_callback,
raw_cmd: cmd,
},
None => DrawCmd::Elements {
count: cmd.ElemCount as usize,
cmd_params,
raw_cmd: cmd as *const sys::ImDrawCmd,
},
},
}
})
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct DrawCmdParams {
pub clip_rect: [f32; 4],
pub texture_id: TextureId,
pub vtx_offset: usize,
pub idx_offset: usize,
}
#[derive(Clone, Debug)]
pub enum DrawCmd {
Elements {
count: usize,
cmd_params: DrawCmdParams,
raw_cmd: *const sys::ImDrawCmd,
},
ResetRenderState,
SetSamplerLinear,
SetSamplerNearest,
RawCallback {
callback: unsafe extern "C" fn(*const sys::ImDrawList, cmd: *const sys::ImDrawCmd),
raw_cmd: *const sys::ImDrawCmd,
},
}