From b23e0483aec27815858bf154be48a31d3901b889 Mon Sep 17 00:00:00 2001 From: Jack Mac Date: Thu, 23 Sep 2021 12:44:00 -0400 Subject: [PATCH] init effort --- imgui-sys/Cargo.toml | 1 + imgui-sys/src/lib.rs | 6 ++++++ imgui/Cargo.toml | 1 + imgui/src/lib.rs | 1 + imgui/src/math.rs | 1 + imgui/src/window/mod.rs | 45 +++++++++++++++++++++++------------------ 6 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 imgui/src/math.rs diff --git a/imgui-sys/Cargo.toml b/imgui-sys/Cargo.toml index 54cd05c..1cbe665 100644 --- a/imgui-sys/Cargo.toml +++ b/imgui-sys/Cargo.toml @@ -15,6 +15,7 @@ exclude = ["third-party/*.json", "third-party/*.lua", "third-party/imgui/*/"] [dependencies] chlorine = "1.0.7" +mint = "0.5" [build-dependencies] cc = "1.0" diff --git a/imgui-sys/src/lib.rs b/imgui-sys/src/lib.rs index d7afc7f..0660381 100644 --- a/imgui-sys/src/lib.rs +++ b/imgui-sys/src/lib.rs @@ -71,6 +71,12 @@ impl From for (f32, f32) { } } +impl From> for ImVec2 { + fn from(o: mint::Vector2) -> Self { + Self { x: o.x, y: o.y } + } +} + impl ImVec4 { #[inline] pub const fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 { diff --git a/imgui/Cargo.toml b/imgui/Cargo.toml index b2d5e28..b126807 100644 --- a/imgui/Cargo.toml +++ b/imgui/Cargo.toml @@ -15,6 +15,7 @@ exclude = ["/resources"] [dependencies] bitflags = "1" imgui-sys = { version = "0.8.1-alpha.0", path = "../imgui-sys" } +mint = "0.5.6" parking_lot = "0.11" [features] diff --git a/imgui/src/lib.rs b/imgui/src/lib.rs index fa7f727..73a0187 100644 --- a/imgui/src/lib.rs +++ b/imgui/src/lib.rs @@ -81,6 +81,7 @@ mod test; mod utils; mod widget; mod window; +mod math; // Used by macros. Underscores are just to make it clear it's not part of the // public API. diff --git a/imgui/src/math.rs b/imgui/src/math.rs new file mode 100644 index 0000000..c3f0269 --- /dev/null +++ b/imgui/src/math.rs @@ -0,0 +1 @@ +pub type MintVec2 = mint::Vector2; diff --git a/imgui/src/window/mod.rs b/imgui/src/window/mod.rs index a0f873c..6183ca8 100644 --- a/imgui/src/window/mod.rs +++ b/imgui/src/window/mod.rs @@ -2,6 +2,7 @@ use bitflags::bitflags; use std::f32; use std::ptr; +use crate::math::MintVec2; use crate::sys; use crate::{Condition, Ui}; @@ -165,13 +166,13 @@ pub struct Window<'ui, 'a, Label> { name: Label, opened: Option<&'a mut bool>, flags: WindowFlags, - pos: [f32; 2], + pos: MintVec2, pos_cond: Condition, - pos_pivot: [f32; 2], - size: [f32; 2], + pos_pivot: MintVec2, + size: MintVec2, size_cond: Condition, - size_constraints: Option<([f32; 2], [f32; 2])>, - content_size: [f32; 2], + size_constraints: Option<(MintVec2, MintVec2)>, + content_size: MintVec2, collapsed: bool, collapsed_cond: Condition, focused: bool, @@ -186,13 +187,13 @@ impl<'ui, 'a, Label: AsRef> Window<'ui, 'a, Label> { name, opened: None, flags: WindowFlags::empty(), - pos: [0.0, 0.0], + pos: [0.0, 0.0].into(), pos_cond: Condition::Never, - pos_pivot: [0.0, 0.0], - size: [0.0, 0.0], + pos_pivot: [0.0, 0.0].into(), + size: [0.0, 0.0].into(), size_cond: Condition::Never, size_constraints: None, - content_size: [0.0, 0.0], + content_size: [0.0, 0.0].into(), collapsed: false, collapsed_cond: Condition::Never, focused: false, @@ -213,8 +214,8 @@ impl<'ui, 'a, Label: AsRef> Window<'ui, 'a, Label> { } /// Sets the window position, which is applied based on the given condition value #[inline] - pub fn position(mut self, position: [f32; 2], condition: Condition) -> Self { - self.pos = position; + pub fn position(mut self, position: impl Into, condition: Condition) -> Self { + self.pos = position.into(); self.pos_cond = condition; self } @@ -224,14 +225,14 @@ impl<'ui, 'a, Label: AsRef> Window<'ui, 'a, Label> { /// For example, pass [0.5, 0.5] to center the window on the position. /// Does nothing if window position is not also set with `position()`. #[inline] - pub fn position_pivot(mut self, pivot: [f32; 2]) -> Self { - self.pos_pivot = pivot; + pub fn position_pivot(mut self, pivot: impl Into) -> Self { + self.pos_pivot = pivot.into(); self } /// Sets the window size, which is applied based on the given condition value #[inline] - pub fn size(mut self, size: [f32; 2], condition: Condition) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into, condition: Condition) -> Self { + self.size = size.into(); self.size_cond = condition; self } @@ -239,8 +240,12 @@ impl<'ui, 'a, Label: AsRef> Window<'ui, 'a, Label> { /// /// Use -1.0, -1.0 on either X or Y axis to preserve current size. #[inline] - pub fn size_constraints(mut self, size_min: [f32; 2], size_max: [f32; 2]) -> Self { - self.size_constraints = Some((size_min, size_max)); + pub fn size_constraints( + mut self, + size_min: impl Into, + size_max: impl Into, + ) -> Self { + self.size_constraints = Some((size_min.into(), size_max.into())); self } /// Sets the window content size, which can be used to enforce scrollbars. @@ -248,8 +253,8 @@ impl<'ui, 'a, Label: AsRef> Window<'ui, 'a, Label> { /// Does not include window decorations (title bar, menu bar, etc.). Set one of the values to /// 0.0 to leave the size automatic. #[inline] - pub fn content_size(mut self, size: [f32; 2]) -> Self { - self.content_size = size; + pub fn content_size(mut self, size: impl Into) -> Self { + self.content_size = size.into(); self } /// Sets the window collapse state, which is applied based on the given condition value @@ -513,7 +518,7 @@ impl<'ui, 'a, Label: AsRef> Window<'ui, 'a, Label> { ) }; } - if self.content_size[0] != 0.0 || self.content_size[1] != 0.0 { + if self.content_size.x != 0.0 || self.content_size.y != 0.0 { unsafe { sys::igSetNextWindowContentSize(self.content_size.into()) }; } if self.collapsed_cond != Condition::Never {