use crate::sys;
use crate::texture::TextureRef;
use crate::ui::Ui;
use std::borrow::Cow;
impl Ui {
#[doc(alias = "Image")]
pub fn image(&self, texture: impl Into<TextureRef>, size: [f32; 2]) {
self.image_config(texture, size).build()
}
#[doc(alias = "ImageButton")]
pub fn image_button(
&self,
str_id: impl AsRef<str>,
texture: impl Into<TextureRef>,
size: [f32; 2],
) -> bool {
self.image_button_config(str_id.as_ref(), texture, size)
.build()
}
pub fn image_config(&self, texture: impl Into<TextureRef>, size: [f32; 2]) -> Image<'_> {
Image::new(self, texture, size)
}
pub fn image_button_config<'ui>(
&'ui self,
str_id: impl Into<Cow<'ui, str>>,
texture: impl Into<TextureRef>,
size: [f32; 2],
) -> ImageButton<'ui> {
ImageButton::new(self, str_id, texture, size)
}
}
#[derive(Debug)]
#[must_use]
pub struct Image<'ui> {
_ui: &'ui Ui,
texture: TextureRef,
size: [f32; 2],
uv0: [f32; 2],
uv1: [f32; 2],
tint_color: [f32; 4],
border_color: [f32; 4],
}
impl<'ui> Image<'ui> {
pub fn new(ui: &'ui Ui, texture: impl Into<TextureRef>, size: [f32; 2]) -> Self {
Self {
_ui: ui,
texture: texture.into(),
size,
uv0: [0.0, 0.0],
uv1: [1.0, 1.0],
tint_color: [1.0, 1.0, 1.0, 1.0],
border_color: [0.0, 0.0, 0.0, 0.0],
}
}
pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
self.uv0 = uv0;
self
}
pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
self.uv1 = uv1;
self
}
pub fn tint_color(mut self, tint_color: [f32; 4]) -> Self {
self.tint_color = tint_color;
self
}
pub fn border_color(mut self, border_color: [f32; 4]) -> Self {
self.border_color = border_color;
self
}
pub fn build(self) {
let size_vec: sys::ImVec2 = self.size.into();
let uv0_vec: sys::ImVec2 = self.uv0.into();
let uv1_vec: sys::ImVec2 = self.uv1.into();
let _tint_vec: sys::ImVec4 = sys::ImVec4 {
x: self.tint_color[0],
y: self.tint_color[1],
z: self.tint_color[2],
w: self.tint_color[3],
};
let _border_vec: sys::ImVec4 = sys::ImVec4 {
x: self.border_color[0],
y: self.border_color[1],
z: self.border_color[2],
w: self.border_color[3],
};
unsafe { sys::igImage(self.texture.raw(), size_vec, uv0_vec, uv1_vec) }
}
pub fn build_with_bg(self, bg_color: [f32; 4], tint_color: [f32; 4]) {
let size_vec: sys::ImVec2 = self.size.into();
let uv0_vec: sys::ImVec2 = self.uv0.into();
let uv1_vec: sys::ImVec2 = self.uv1.into();
let bg_vec = sys::ImVec4 {
x: bg_color[0],
y: bg_color[1],
z: bg_color[2],
w: bg_color[3],
};
let tint_vec = sys::ImVec4 {
x: tint_color[0],
y: tint_color[1],
z: tint_color[2],
w: tint_color[3],
};
unsafe {
sys::igImageWithBg(
self.texture.raw(),
size_vec,
uv0_vec,
uv1_vec,
bg_vec,
tint_vec,
)
}
}
}
#[derive(Debug)]
#[must_use]
pub struct ImageButton<'ui> {
ui: &'ui Ui,
str_id: Cow<'ui, str>,
texture: TextureRef,
size: [f32; 2],
uv0: [f32; 2],
uv1: [f32; 2],
bg_color: [f32; 4],
tint_color: [f32; 4],
}
impl<'ui> ImageButton<'ui> {
pub fn new(
ui: &'ui Ui,
str_id: impl Into<Cow<'ui, str>>,
texture: impl Into<TextureRef>,
size: [f32; 2],
) -> Self {
Self {
ui,
str_id: str_id.into(),
texture: texture.into(),
size,
uv0: [0.0, 0.0],
uv1: [1.0, 1.0],
bg_color: [0.0, 0.0, 0.0, 0.0],
tint_color: [1.0, 1.0, 1.0, 1.0],
}
}
pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
self.uv0 = uv0;
self
}
pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
self.uv1 = uv1;
self
}
pub fn bg_color(mut self, bg_color: [f32; 4]) -> Self {
self.bg_color = bg_color;
self
}
pub fn tint_color(mut self, tint_color: [f32; 4]) -> Self {
self.tint_color = tint_color;
self
}
pub fn build(self) -> bool {
let str_id_ptr = self.ui.scratch_txt(self.str_id.as_ref());
let size_vec: sys::ImVec2 = self.size.into();
let uv0_vec: sys::ImVec2 = self.uv0.into();
let uv1_vec: sys::ImVec2 = self.uv1.into();
let bg_vec: sys::ImVec4 = sys::ImVec4 {
x: self.bg_color[0],
y: self.bg_color[1],
z: self.bg_color[2],
w: self.bg_color[3],
};
let tint_vec: sys::ImVec4 = sys::ImVec4 {
x: self.tint_color[0],
y: self.tint_color[1],
z: self.tint_color[2],
w: self.tint_color[3],
};
unsafe {
sys::igImageButton(
str_id_ptr,
self.texture.raw(),
size_vec,
uv0_vec,
uv1_vec,
bg_vec,
tint_vec,
)
}
}
}