dear_imgui_rs/render/draw_data/
core.rs1use super::DrawListIterator;
2use crate::internal::{RawCast, RawWrapper};
3use crate::sys;
4use std::slice;
5
6#[repr(C)]
8pub struct DrawData {
9 pub(super) valid: bool,
11 pub(super) cmd_lists_count: i32,
13 pub(super) total_idx_count: i32,
15 pub(super) total_vtx_count: i32,
17 pub(super) cmd_lists: crate::internal::ImVector<*mut sys::ImDrawList>,
19 pub display_pos: [f32; 2],
23 pub display_size: [f32; 2],
27 pub framebuffer_scale: [f32; 2],
32
33 pub(super) owner_viewport: *mut sys::ImGuiViewport,
35 pub(super) textures: *mut crate::internal::ImVector<*mut sys::ImTextureData>,
37}
38
39const _: [(); std::mem::size_of::<sys::ImDrawData>()] = [(); std::mem::size_of::<DrawData>()];
41const _: [(); std::mem::align_of::<sys::ImDrawData>()] = [(); std::mem::align_of::<DrawData>()];
42
43unsafe impl RawCast<sys::ImDrawData> for DrawData {}
44
45pub(super) fn total_count_from_i32(caller: &str, raw: i32) -> usize {
46 usize::try_from(raw).unwrap_or_else(|_| panic!("{caller} returned a negative count"))
47}
48
49impl RawWrapper for DrawData {
50 type Raw = sys::ImDrawData;
51
52 unsafe fn raw(&self) -> &Self::Raw {
53 unsafe { <Self as RawCast<Self::Raw>>::raw(self) }
54 }
55
56 unsafe fn raw_mut(&mut self) -> &mut Self::Raw {
57 unsafe { <Self as RawCast<Self::Raw>>::raw_mut(self) }
58 }
59}
60
61impl DrawData {
62 #[inline]
67 pub fn valid(&self) -> bool {
68 self.valid
69 }
70
71 #[inline]
73 pub fn draw_lists(&self) -> DrawListIterator<'_> {
74 unsafe { DrawListIterator::new(self.cmd_lists().iter()) }
75 }
76 #[inline]
78 pub fn draw_lists_count(&self) -> usize {
79 unsafe { self.cmd_lists().len() }
80 }
81
82 #[inline]
84 pub fn total_idx_count(&self) -> usize {
85 total_count_from_i32("DrawData::total_idx_count()", self.total_idx_count)
86 }
87
88 #[inline]
90 pub fn total_vtx_count(&self) -> usize {
91 total_count_from_i32("DrawData::total_vtx_count()", self.total_vtx_count)
92 }
93
94 #[inline]
96 pub fn display_pos(&self) -> [f32; 2] {
97 self.display_pos
98 }
99
100 #[inline]
102 pub fn display_size(&self) -> [f32; 2] {
103 self.display_size
104 }
105
106 #[inline]
108 pub fn framebuffer_scale(&self) -> [f32; 2] {
109 self.framebuffer_scale
110 }
111
112 #[inline]
118 pub fn owner_viewport(&self) -> *mut sys::ImGuiViewport {
119 self.owner_viewport
120 }
121
122 #[inline]
123 pub(crate) unsafe fn cmd_lists(&self) -> &[*mut sys::ImDrawList] {
124 unsafe {
125 if self.cmd_lists_count <= 0 || self.cmd_lists.data.is_null() {
126 return &[];
127 }
128 let len = match usize::try_from(self.cmd_lists_count) {
129 Ok(len) => len,
130 Err(_) => return &[],
131 };
132 slice::from_raw_parts(self.cmd_lists.data, len)
133 }
134 }
135
136 #[doc(alias = "DeIndexAllBuffers")]
140 pub fn deindex_all_buffers(&mut self) {
141 unsafe {
142 sys::ImDrawData_DeIndexAllBuffers(RawWrapper::raw_mut(self));
143 }
144 }
145
146 #[doc(alias = "ScaleClipRects")]
151 pub fn scale_clip_rects(&mut self, fb_scale: [f32; 2]) {
152 unsafe {
153 let scale = sys::ImVec2 {
154 x: fb_scale[0],
155 y: fb_scale[1],
156 };
157 sys::ImDrawData_ScaleClipRects(RawWrapper::raw_mut(self), scale);
158 }
159 }
160}