From 4efbb94e1b9fac89ffa93961d2c6c79cc559ba6b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 21 Apr 2020 20:48:31 +0200 Subject: [PATCH] Various small improvements --- emigui/src/example_app.rs | 8 +++-- emigui/src/id.rs | 4 +-- emigui/src/math.rs | 61 ++++++++++++++++++++++++--------------- emigui/src/memory.rs | 3 +- emigui/src/region.rs | 2 +- emigui/src/style.rs | 2 +- example_glium/src/main.rs | 14 +++++++-- example_wasm/src/lib.rs | 2 +- 8 files changed, 60 insertions(+), 36 deletions(-) diff --git a/emigui/src/example_app.rs b/emigui/src/example_app.rs index 7b838eb9..0448e64a 100644 --- a/emigui/src/example_app.rs +++ b/emigui/src/example_app.rs @@ -37,10 +37,12 @@ impl Default for ExampleApp { impl ExampleApp { pub fn ui(&mut self, region: &mut Region) { region.foldable("About Emigui", |region| { - region.add(label!("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.")); + region.add(label!( + "Emigui is an experimental immediate mode GUI written in Rust." + )); }); - region.foldable("Widget examples", |region| { + region.foldable("Widgets", |region| { region.horizontal(Align::Min, |region| { region.add(label!("Text can have").text_color(srgba(110, 255, 110, 255))); region.add(label!("color").text_color(srgba(128, 140, 255, 255))); @@ -134,7 +136,7 @@ impl ExampleApp { region.add_label("\ For instance, foldable regions needs to store wether or not they are open. \ If you fail to give them unique names then clicking one will open both. \ - To help you debug this, a error message is printed on screen:"); + To help you debug this, an error message is printed on screen:"); region.foldable("Foldable", |region| { region.add_label("Contents of first folddable region"); diff --git a/emigui/src/id.rs b/emigui/src/id.rs index 5d1f93f0..334092b6 100644 --- a/emigui/src/id.rs +++ b/emigui/src/id.rs @@ -43,14 +43,14 @@ impl Id { Self(1) } - pub fn new(source: &H) -> Id { + pub fn new(source: H) -> Id { use std::hash::Hasher; let mut hasher = DefaultHasher::new(); source.hash(&mut hasher); Id(hasher.finish()) } - pub fn with(self, child: &H) -> Id { + pub fn with(self, child: H) -> Id { use std::hash::Hasher; let mut hasher = DefaultHasher::new(); hasher.write_u64(self.0); diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 965300a9..b5b9ebae 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -1,3 +1,5 @@ +use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; + #[derive(Clone, Copy, Default, Deserialize, Serialize)] pub struct Vec2 { pub x: f32, @@ -80,7 +82,7 @@ impl PartialEq for Vec2 { } impl Eq for Vec2 {} -impl std::ops::Neg for Vec2 { +impl Neg for Vec2 { type Output = Vec2; fn neg(self) -> Vec2 { @@ -88,7 +90,7 @@ impl std::ops::Neg for Vec2 { } } -impl std::ops::AddAssign for Vec2 { +impl AddAssign for Vec2 { fn add_assign(&mut self, rhs: Vec2) { *self = Vec2 { x: self.x + rhs.x, @@ -97,7 +99,16 @@ impl std::ops::AddAssign for Vec2 { } } -impl std::ops::Add for Vec2 { +impl SubAssign for Vec2 { + fn sub_assign(&mut self, rhs: Vec2) { + *self = Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + }; + } +} + +impl Add for Vec2 { type Output = Vec2; fn add(self, rhs: Vec2) -> Vec2 { Vec2 { @@ -107,7 +118,7 @@ impl std::ops::Add for Vec2 { } } -impl std::ops::Sub for Vec2 { +impl Sub for Vec2 { type Output = Vec2; fn sub(self, rhs: Vec2) -> Vec2 { Vec2 { @@ -117,14 +128,14 @@ impl std::ops::Sub for Vec2 { } } -impl std::ops::MulAssign for Vec2 { +impl MulAssign for Vec2 { fn mul_assign(&mut self, rhs: f32) { self.x *= rhs; self.y *= rhs; } } -impl std::ops::Mul for Vec2 { +impl Mul for Vec2 { type Output = Vec2; fn mul(self, factor: f32) -> Vec2 { Vec2 { @@ -134,7 +145,7 @@ impl std::ops::Mul for Vec2 { } } -impl std::ops::Mul for f32 { +impl Mul for f32 { type Output = Vec2; fn mul(self, vec: Vec2) -> Vec2 { Vec2 { @@ -144,7 +155,7 @@ impl std::ops::Mul for f32 { } } -impl std::ops::Div for Vec2 { +impl Div for Vec2 { type Output = Vec2; fn div(self, factor: f32) -> Vec2 { Vec2 { @@ -223,7 +234,7 @@ impl PartialEq for Pos2 { } impl Eq for Pos2 {} -impl std::ops::AddAssign for Pos2 { +impl AddAssign for Pos2 { fn add_assign(&mut self, rhs: Vec2) { *self = Pos2 { x: self.x + rhs.x, @@ -232,7 +243,16 @@ impl std::ops::AddAssign for Pos2 { } } -impl std::ops::Add for Pos2 { +impl SubAssign for Pos2 { + fn sub_assign(&mut self, rhs: Vec2) { + *self = Pos2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + }; + } +} + +impl Add for Pos2 { type Output = Pos2; fn add(self, rhs: Vec2) -> Pos2 { Pos2 { @@ -242,17 +262,7 @@ impl std::ops::Add for Pos2 { } } -// impl std::ops::Add for Vec2 { -// type Output = Pos2; -// fn add(self, rhs: Pos2) -> Pos2 { -// Pos2 { -// x: self.x + rhs.x, -// y: self.y + rhs.y, -// } -// } -// } - -impl std::ops::Sub for Pos2 { +impl Sub for Pos2 { type Output = Vec2; fn sub(self, rhs: Pos2) -> Vec2 { Vec2 { @@ -262,7 +272,7 @@ impl std::ops::Sub for Pos2 { } } -impl std::ops::Sub for Pos2 { +impl Sub for Pos2 { type Output = Pos2; fn sub(self, rhs: Vec2) -> Pos2 { Pos2 { @@ -323,14 +333,17 @@ impl Rect { } /// Expand by this much in each direction + #[must_use] pub fn expand(self, amnt: f32) -> Self { Rect::from_center_size(self.center(), self.size() + 2.0 * vec2(amnt, amnt)) } + #[must_use] pub fn translate(self, amnt: Vec2) -> Self { Rect::from_min_size(self.min() + amnt, self.size()) } + #[must_use] pub fn intersect(self, other: Rect) -> Self { Self { min: self.min.max(other.min), @@ -423,8 +436,8 @@ impl std::fmt::Debug for Rect { pub fn lerp(min: T, max: T, t: f32) -> T where - f32: std::ops::Mul, - T: std::ops::Add, + f32: Mul, + T: Add, { (1.0 - t) * min + t * max } diff --git a/emigui/src/memory.rs b/emigui/src/memory.rs index a91adb56..344a5696 100644 --- a/emigui/src/memory.rs +++ b/emigui/src/memory.rs @@ -23,9 +23,8 @@ pub struct Memory { /// The widget being interacted with (e.g. dragged, in case of a slider). pub(crate) active_id: Option, - /// Which foldable regions are open. + // states of various types of widgets pub(crate) foldables: HashMap, - windows: HashMap, /// Top is last diff --git a/emigui/src/region.rs b/emigui/src/region.rs index f0e9d1a9..bffe3d29 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -319,7 +319,7 @@ impl Region { )) } - /// Return a sub-region relative to the parent + /// Return a sub-region relative to the cursor pub fn relative_region(&mut self, rect: Rect) -> Region { let region_pos = self.cursor + rect.min().to_vec2(); let child_rect = Rect::from_min_size(region_pos, rect.size()); diff --git a/emigui/src/style.rs b/emigui/src/style.rs index 945ed9eb..7322329e 100644 --- a/emigui/src/style.rs +++ b/emigui/src/style.rs @@ -79,7 +79,7 @@ impl Style { /// Fill color of the interactive part of a component (button, slider grab, checkbox, ...) pub fn interact_fill_color(&self, interact: &InteractInfo) -> Option { if interact.active { - Some(srgba(200, 200, 200, 255)) + Some(srgba(120, 120, 200, 255)) } else if interact.hovered { Some(srgba(100, 100, 150, 255)) } else { diff --git a/example_glium/src/main.rs b/example_glium/src/main.rs index 7353f7ff..abbd41a7 100644 --- a/example_glium/src/main.rs +++ b/example_glium/src/main.rs @@ -92,8 +92,18 @@ fn main() { if region.add(Button::new("Quit")).clicked { quit = true; } - example_app.ui(&mut region); - emigui.ui(&mut region); + + Window::new("Example APP") + .default_pos(pos2(200.0, 100.0)) + .show(region.ctx(), |region| { + example_app.ui(region); + }); + + Window::new("Emigui settings") + .default_pos(pos2(200.0, 500.0)) + .show(region.ctx(), |region| { + emigui.ui(region); + }); // TODO: Make it even simpler to show a window Window::new("Test window") diff --git a/example_wasm/src/lib.rs b/example_wasm/src/lib.rs index 0e16f54b..d7dd0a16 100644 --- a/example_wasm/src/lib.rs +++ b/example_wasm/src/lib.rs @@ -59,7 +59,7 @@ impl State { region.set_align(Align::Min); region.add_label("WebGl painter info:"); - region.indent(Id::new(&"webgl region"), |region| { + region.indent(Id::new("webgl region"), |region| { region.add_label(self.webgl_painter.debug_info()); });