use super::super::validation::validate_input_scalar_flags;
use crate::internal::{DataTypeKind, component_count_i32};
use crate::ui::Ui;
use crate::{InputScalarFlags, sys};
use std::ffi::c_void;
use std::ptr;
#[must_use]
pub struct InputScalar<'ui, 'p, T, L, F = &'static str> {
value: &'p mut T,
label: L,
step: Option<T>,
step_fast: Option<T>,
display_format: Option<F>,
flags: InputScalarFlags,
ui: &'ui Ui,
}
impl<'ui, 'p, L: AsRef<str>, T: DataTypeKind> InputScalar<'ui, 'p, T, L> {
#[doc(alias = "InputScalar")]
pub fn new(ui: &'ui Ui, label: L, value: &'p mut T) -> Self {
InputScalar {
value,
label,
step: None,
step_fast: None,
display_format: None,
flags: InputScalarFlags::empty(),
ui,
}
}
}
impl<'ui, 'p, L: AsRef<str>, T: DataTypeKind, F: AsRef<str>> InputScalar<'ui, 'p, T, L, F> {
pub fn display_format<F2: AsRef<str>>(
self,
display_format: F2,
) -> InputScalar<'ui, 'p, T, L, F2> {
InputScalar {
value: self.value,
label: self.label,
step: self.step,
step_fast: self.step_fast,
display_format: Some(display_format),
flags: self.flags,
ui: self.ui,
}
}
#[inline]
pub fn step(mut self, value: T) -> Self {
self.step = Some(value);
self
}
#[inline]
pub fn step_fast(mut self, value: T) -> Self {
self.step_fast = Some(value);
self
}
#[inline]
pub fn flags(mut self, flags: InputScalarFlags) -> Self {
self.flags = flags;
self
}
pub fn build(self) -> bool {
validate_input_scalar_flags("InputScalar::build()", self.flags);
unsafe {
let (one, two) = self
.ui
.scratch_txt_with_opt(self.label, self.display_format);
sys::igInputScalar(
one,
T::KIND as i32,
self.value as *mut T as *mut c_void,
self.step
.as_ref()
.map(|step| step as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.step_fast
.as_ref()
.map(|step| step as *const T)
.unwrap_or(ptr::null()) as *const c_void,
two,
self.flags.raw(),
)
}
}
}
#[must_use]
pub struct InputScalarN<'ui, 'p, T, L, F = &'static str> {
values: &'p mut [T],
label: L,
step: Option<T>,
step_fast: Option<T>,
display_format: Option<F>,
flags: InputScalarFlags,
ui: &'ui Ui,
}
impl<'ui, 'p, L: AsRef<str>, T: DataTypeKind> InputScalarN<'ui, 'p, T, L> {
#[doc(alias = "InputScalarN")]
pub fn new(ui: &'ui Ui, label: L, values: &'p mut [T]) -> Self {
InputScalarN {
values,
label,
step: None,
step_fast: None,
display_format: None,
flags: InputScalarFlags::empty(),
ui,
}
}
}
impl<'ui, 'p, L: AsRef<str>, T: DataTypeKind, F: AsRef<str>> InputScalarN<'ui, 'p, T, L, F> {
pub fn display_format<F2: AsRef<str>>(
self,
display_format: F2,
) -> InputScalarN<'ui, 'p, T, L, F2> {
InputScalarN {
values: self.values,
label: self.label,
step: self.step,
step_fast: self.step_fast,
display_format: Some(display_format),
flags: self.flags,
ui: self.ui,
}
}
#[inline]
pub fn step(mut self, value: T) -> Self {
self.step = Some(value);
self
}
#[inline]
pub fn step_fast(mut self, value: T) -> Self {
self.step_fast = Some(value);
self
}
#[inline]
pub fn flags(mut self, flags: InputScalarFlags) -> Self {
self.flags = flags;
self
}
pub fn build(self) -> bool {
validate_input_scalar_flags("InputScalarN::build()", self.flags);
let count = component_count_i32("InputScalarN::build()", self.values.len());
unsafe {
let (one, two) = self
.ui
.scratch_txt_with_opt(self.label, self.display_format);
sys::igInputScalarN(
one,
T::KIND as i32,
self.values.as_mut_ptr() as *mut c_void,
count,
self.step
.as_ref()
.map(|step| step as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.step_fast
.as_ref()
.map(|step| step as *const T)
.unwrap_or(ptr::null()) as *const c_void,
two,
self.flags.raw(),
)
}
}
}