arcdps_imgui\widget/
image.rs

1use std::os::raw::c_void;
2
3use crate::render::renderer::TextureId;
4use crate::sys;
5use crate::Ui;
6
7/// Builder for an image widget
8#[derive(Copy, Clone, Debug)]
9#[must_use]
10pub struct Image {
11    texture_id: TextureId,
12    size: [f32; 2],
13    uv0: [f32; 2],
14    uv1: [f32; 2],
15    tint_col: [f32; 4],
16    border_col: [f32; 4],
17}
18
19impl Image {
20    /// Creates a new image builder with the given texture and size
21    #[doc(alias = "Image")]
22    pub const fn new(texture_id: TextureId, size: [f32; 2]) -> Image {
23        Image {
24            texture_id,
25            size,
26            uv0: [0.0, 0.0],
27            uv1: [1.0, 1.0],
28            tint_col: [1.0, 1.0, 1.0, 1.0],
29            border_col: [0.0, 0.0, 0.0, 0.0],
30        }
31    }
32    /// Sets the image size
33    pub const fn size(mut self, size: [f32; 2]) -> Self {
34        self.size = size;
35        self
36    }
37    /// Sets uv0 (default `[0.0, 0.0]`)
38    pub const fn uv0(mut self, uv0: [f32; 2]) -> Self {
39        self.uv0 = uv0;
40        self
41    }
42    /// Sets uv1 (default `[1.0, 1.0]`)
43    pub const fn uv1(mut self, uv1: [f32; 2]) -> Self {
44        self.uv1 = uv1;
45        self
46    }
47    /// Sets the tint color (default: no tint color)
48    pub const fn tint_col(mut self, tint_col: [f32; 4]) -> Self {
49        self.tint_col = tint_col;
50        self
51    }
52    /// Sets the border color (default: no border)
53    pub const fn border_col(mut self, border_col: [f32; 4]) -> Self {
54        self.border_col = border_col;
55        self
56    }
57    /// Builds the image
58    pub fn build(self, _: &Ui<'_>) {
59        unsafe {
60            sys::igImage(
61                self.texture_id.id() as *mut c_void,
62                self.size.into(),
63                self.uv0.into(),
64                self.uv1.into(),
65                self.tint_col.into(),
66                self.border_col.into(),
67            );
68        }
69    }
70}
71
72/// Builder for an image button widget
73#[derive(Copy, Clone, Debug)]
74#[must_use]
75pub struct ImageButton {
76    texture_id: TextureId,
77    size: [f32; 2],
78    uv0: [f32; 2],
79    uv1: [f32; 2],
80    frame_padding: i32,
81    bg_col: [f32; 4],
82    tint_col: [f32; 4],
83}
84
85impl ImageButton {
86    /// Creates a new image button builder with the given texture and size
87    #[doc(alias = "ImageButton")]
88    pub fn new(texture_id: TextureId, size: [f32; 2]) -> ImageButton {
89        ImageButton {
90            texture_id,
91            size,
92            uv0: [0.0, 0.0],
93            uv1: [1.0, 1.0],
94            frame_padding: -1,
95            bg_col: [0.0, 0.0, 0.0, 0.0],
96            tint_col: [1.0, 1.0, 1.0, 1.0],
97        }
98    }
99    /// Sets the image button size
100    pub fn size(mut self, size: [f32; 2]) -> Self {
101        self.size = size;
102        self
103    }
104    /// Sets uv0 (default `[0.0, 0.0]`)
105    pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
106        self.uv0 = uv0;
107        self
108    }
109    /// Sets uv1 (default `[1.0, 1.0]`)
110    pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
111        self.uv1 = uv1;
112        self
113    }
114    /// Sets the frame padding (default: uses frame padding from style).
115    ///
116    /// - `< 0`: uses frame padding from style (default)
117    /// - `= 0`: no framing
118    /// - `> 0`: set framing size
119    pub fn frame_padding(mut self, frame_padding: i32) -> Self {
120        self.frame_padding = frame_padding;
121        self
122    }
123    /// Sets the background color (default: no background color)
124    pub fn background_col(mut self, bg_col: [f32; 4]) -> Self {
125        self.bg_col = bg_col;
126        self
127    }
128    /// Sets the tint color (default: no tint color)
129    pub fn tint_col(mut self, tint_col: [f32; 4]) -> Self {
130        self.tint_col = tint_col;
131        self
132    }
133    /// Builds the image button
134    pub fn build(self, _: &Ui<'_>) -> bool {
135        unsafe {
136            sys::igImageButton(
137                self.texture_id.id() as *mut c_void,
138                self.size.into(),
139                self.uv0.into(),
140                self.uv1.into(),
141                self.frame_padding,
142                self.bg_col.into(),
143                self.tint_col.into(),
144            )
145        }
146    }
147}