diff --git a/build.sh b/build.sh index 51897652..e0c2436b 100755 --- a/build.sh +++ b/build.sh @@ -4,7 +4,7 @@ set -eu # Pre-requisites: rustup target add wasm32-unknown-unknown if ! [[ $(wasm-bindgen --version) ]]; then - cargo update -p wasm-bindgen + cargo clean cargo install -f wasm-bindgen-cli cargo update fi @@ -15,17 +15,11 @@ BUILD=debug # Clear output from old stuff: rm -rf docs/*.wasm -function build_rust -{ - echo "Build rust:" - cargo build --target wasm32-unknown-unknown +echo "Build rust:" +cargo build --target wasm32-unknown-unknown - echo "Generate JS bindings for wasm:" - FOLDER_NAME=${PWD##*/} - TARGET_NAME="example.wasm" - wasm-bindgen "target/wasm32-unknown-unknown/$BUILD/$TARGET_NAME" \ - --out-dir docs --no-modules --no-typescript - # --no-modules-global hoboho -} - -build_rust +echo "Generate JS bindings for wasm:" +FOLDER_NAME=${PWD##*/} +TARGET_NAME="example.wasm" +wasm-bindgen "target/wasm32-unknown-unknown/$BUILD/$TARGET_NAME" \ + --out-dir docs --no-modules --no-typescript diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index 90e2e44f..7a8309f6 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -409,7 +409,7 @@ impl Region { options: self.options, id: self.id, dir: self.dir, - cursor: vec2((self.available_space.x - width) / 2.0, self.cursor.y), + cursor: self.cursor + vec2((self.available_space.x - width) / 2.0, 0.0), align, bounding_size: vec2(0.0, 0.0), available_space: vec2(width, self.available_space.y), @@ -560,6 +560,31 @@ impl Region { }) } + // Helper function + pub fn floating_text( + &mut self, + pos: Vec2, + text: &str, + text_style: TextStyle, + align: (Align, Align), + text_color: Option, + ) { + let font = &self.fonts()[text_style]; + let (text, text_size) = font.layout_multiline(text, std::f32::INFINITY); + + let x = match align.0 { + Align::Min => pos.x, + Align::Center => pos.x - 0.5 * text_size.x, + Align::Max => pos.x - text_size.x, + }; + let y = match align.1 { + Align::Min => pos.y, + Align::Center => pos.y - 0.5 * text_size.y, + Align::Max => pos.y - text_size.y, + }; + self.add_text(vec2(x, y), text_style, text, text_color); + } + pub fn add_text( &mut self, pos: Vec2, diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 8fcc43e2..fd8ddc52 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -134,6 +134,7 @@ impl Rect { pub fn min(&self) -> Vec2 { self.pos } + pub fn max(&self) -> Vec2 { self.pos + self.size } @@ -141,6 +142,12 @@ impl Rect { pub fn size(&self) -> Vec2 { self.size } + pub fn width(&self) -> f32 { + self.size.x + } + pub fn height(&self) -> f32 { + self.size.y + } // Convenience functions: pub fn left_top(&self) -> Vec2 { diff --git a/example/src/app.rs b/example/src/app.rs index 28c181bd..1e7617a6 100644 --- a/example/src/app.rs +++ b/example/src/app.rs @@ -84,8 +84,9 @@ impl App { .rect .min(); + let mut cmds = vec![]; for i in 0..self.num_boxes { - gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Rect { + cmds.push(PaintCmd::Rect { corner_radius: self.corner_radius, fill_color: Some(srgba(136, 136, 136, 255)), rect: Rect::from_min_size( @@ -96,8 +97,9 @@ impl App { width: self.stroke_width, color: srgba(255, 255, 255, 255), }), - }])); + }); } + gui.add_graphic(GuiCmd::PaintCommands(cmds)); }); } }