diff --git a/TODO.md b/TODO.md index 273568c5..9f8714d1 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,8 @@ +# Infrastructure +We need to make it easy to make your own library using emigui_wasm. +Maybe we can use build.rs to generate needed stuff, e.g. index.hmtl? + # Code -* Break off example app from emigui_wasm * Try it on iPhone * Read TTF from browser? * requestAnimationFrame diff --git a/docs/index.html b/docs/index.html index 16409296..449ee6f9 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wasm_bindgen.run_gui(g_wasm_app, JSON.stringify(input)); } // ---------------------------------------------------------------------------- - var g_mouse_pos = { x: -1000.0, y: -1000.0 }; + var g_mouse_pos = null; var g_mouse_down = false; function pixels_per_point() { @@ -120,14 +120,13 @@ event.preventDefault(); }); canvas.addEventListener("mouseleave", function(event) { - g_mouse_pos = { x: -1000.0, y: -1000.0 }; + g_mouse_pos = null; repaint(); event.stopPropagation(); event.preventDefault(); }); canvas.addEventListener("touchstart", function(event) { - // console.log(`touchstart ${JSON.stringify(event)}`); g_mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY }; g_mouse_down = true; repaint(); @@ -135,17 +134,15 @@ event.preventDefault(); }); canvas.addEventListener("touchmove", function(event) { - // console.log(`touchmove ${JSON.stringify(event)}`); g_mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY }; repaint(); event.stopPropagation(); event.preventDefault(); }); canvas.addEventListener("touchend", function(event) { - // console.log(`touchend ${JSON.stringify(event)}`); g_mouse_down = false; repaint(); - g_mouse_pos = { x: -1000.0, y: -1000.0 }; + g_mouse_pos = null; repaint(); event.stopPropagation(); event.preventDefault(); diff --git a/emigui/src/emigui.rs b/emigui/src/emigui.rs index 13e5fdbe..658f628a 100644 --- a/emigui/src/emigui.rs +++ b/emigui/src/emigui.rs @@ -72,44 +72,46 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) { frame.add_rect(top_left, bottom_right); gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)])); - if interact.hovered { - show_popup(gui.data(), gui.input().mouse_pos, |gui| { - let zoom_rect = gui.reserve_space(vec2(128.0, 128.0), None).rect; - let u = remap_clamp( - gui.input().mouse_pos.x, - rect.min().x, - rect.max().x, - 0.0, - texture.width as f32 - 1.0, - ) - .round(); - let v = remap_clamp( - gui.input().mouse_pos.y, - rect.min().y, - rect.max().y, - 0.0, - texture.height as f32 - 1.0, - ) - .round(); + if let Some(mouse_pos) = gui.input().mouse_pos { + if interact.hovered { + show_popup(gui.data(), mouse_pos, |gui| { + let zoom_rect = gui.reserve_space(vec2(128.0, 128.0), None).rect; + let u = remap_clamp( + mouse_pos.x, + rect.min().x, + rect.max().x, + 0.0, + texture.width as f32 - 1.0, + ) + .round(); + let v = remap_clamp( + mouse_pos.y, + rect.min().y, + rect.max().y, + 0.0, + texture.height as f32 - 1.0, + ) + .round(); - let texel_radius = 32.0; - let u = clamp(u, texel_radius, texture.width as f32 - 1.0 - texel_radius); - let v = clamp(v, texel_radius, texture.height as f32 - 1.0 - texel_radius); + let texel_radius = 32.0; + let u = clamp(u, texel_radius, texture.width as f32 - 1.0 - texel_radius); + let v = clamp(v, texel_radius, texture.height as f32 - 1.0 - texel_radius); - let top_left = Vertex { - pos: zoom_rect.min(), - uv: ((u - texel_radius) as u16, (v - texel_radius) as u16), - color: Color::WHITE, - }; - let bottom_right = Vertex { - pos: zoom_rect.max(), - uv: ((u + texel_radius) as u16, (v + texel_radius) as u16), - color: Color::WHITE, - }; - let mut frame = Frame::default(); - frame.add_rect(top_left, bottom_right); - gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)])); - }); + let top_left = Vertex { + pos: zoom_rect.min(), + uv: ((u - texel_radius) as u16, (v - texel_radius) as u16), + color: Color::WHITE, + }; + let bottom_right = Vertex { + pos: zoom_rect.max(), + uv: ((u + texel_radius) as u16, (v + texel_radius) as u16), + color: Color::WHITE, + }; + let mut frame = Frame::default(); + frame.add_rect(top_left, bottom_right); + gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)])); + }); + } } } @@ -199,11 +201,11 @@ impl Emigui { gui.input().screen_size.y, gui.input().pixels_per_point, )); - gui.add(label!( - "mouse_pos: {} x {}", - gui.input().mouse_pos.x, - gui.input().mouse_pos.y, - )); + if let Some(mouse_pos) = gui.input().mouse_pos { + gui.add(label!("mouse_pos: {} x {}", mouse_pos.x, mouse_pos.y,)); + } else { + gui.add(label!("mouse_pos: None")); + } gui.add(label!( "gui cursor: {} x {}", gui.cursor().x, diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index 3706d018..90e2e44f 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -76,8 +76,10 @@ impl GuiResponse { F: FnOnce(&mut Region), { if self.hovered { - let window_pos = self.data.input().mouse_pos + vec2(16.0, 16.0); - show_popup(&self.data, window_pos, add_contents); + if let Some(mouse_pos) = self.data.input().mouse_pos { + let window_pos = mouse_pos + vec2(16.0, 16.0); + show_popup(&self.data, window_pos, add_contents); + } } self } @@ -214,7 +216,7 @@ impl Data { // TODO: move pub fn new_frame(&mut self, gui_input: GuiInput) { self.input = gui_input; - if !gui_input.mouse_down { + if !gui_input.mouse_down || gui_input.mouse_pos.is_none() { self.memory.lock().unwrap().active_id = None; } } @@ -490,7 +492,11 @@ impl Region { let is_something_else_active = memory.active_id.is_some() && memory.active_id != interaction_id; - let hovered = !is_something_else_active && rect.contains(self.input().mouse_pos); + let hovered = if let Some(mouse_pos) = self.input().mouse_pos { + !is_something_else_active && rect.contains(mouse_pos) + } else { + false + }; let clicked = hovered && self.input().mouse_clicked; let active = if interaction_id.is_some() { if clicked { diff --git a/emigui/src/mesher.rs b/emigui/src/mesher.rs index 2ce561bc..03ed1188 100644 --- a/emigui/src/mesher.rs +++ b/emigui/src/mesher.rs @@ -1,3 +1,5 @@ +#![allow(clippy::identity_op)] + const ANTI_ALIAS: bool = true; const AA_SIZE: f32 = 0.5; // TODO: 1.0 / pixels_per_point @@ -129,7 +131,7 @@ impl Frame { let mut color_inner = color; if thin_line { // Fade out as it gets thinner: - color_inner.a = (color_inner.a as f32 * width).round() as u8; + color_inner.a = (f32::from(color_inner.a) * width).round() as u8; } // TODO: line caps ? let mut i0 = n - 1; diff --git a/emigui/src/style.rs b/emigui/src/style.rs index baa36475..c37e3925 100644 --- a/emigui/src/style.rs +++ b/emigui/src/style.rs @@ -242,7 +242,7 @@ fn translate_cmd(out_commands: &mut Vec, style: &Style, cmd: GuiCmd) { } } -pub fn into_paint_commands<'a, GuiCmdIterator>( +pub fn into_paint_commands( gui_commands: GuiCmdIterator, style: &Style, ) -> Vec diff --git a/emigui/src/types.rs b/emigui/src/types.rs index 348264f9..f6384292 100644 --- a/emigui/src/types.rs +++ b/emigui/src/types.rs @@ -13,7 +13,7 @@ pub struct RawInput { pub mouse_down: bool, /// Current position of the mouse in points. - pub mouse_pos: Vec2, + pub mouse_pos: Option, /// Size of the screen in points. pub screen_size: Vec2, @@ -35,7 +35,7 @@ pub struct GuiInput { pub mouse_released: bool, /// Current position of the mouse in points. - pub mouse_pos: Vec2, + pub mouse_pos: Option, /// Size of the screen in points. pub screen_size: Vec2, diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index 9654f86e..397e4335 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -1,3 +1,5 @@ +#![allow(clippy::new_without_default_derive)] + use crate::{ fonts::TextStyle, layout::{make_id, Align, Direction, GuiResponse, Id, Region}, @@ -289,14 +291,16 @@ impl<'a> Widget for Slider<'a> { id, ); - if interact.active { - *value = remap_clamp( - region.input().mouse_pos.x, - interact.rect.min().x, - interact.rect.max().x, - min, - max, - ); + if let Some(mouse_pos) = region.input().mouse_pos { + if interact.active { + *value = remap_clamp( + mouse_pos.x, + interact.rect.min().x, + interact.rect.max().x, + min, + max, + ); + } } region.add_graphic(GuiCmd::Slider { diff --git a/emigui_wasm/src/webgl.rs b/emigui_wasm/src/webgl.rs index 5f51c828..cffa38ac 100644 --- a/emigui_wasm/src/webgl.rs +++ b/emigui_wasm/src/webgl.rs @@ -119,7 +119,7 @@ impl Painter { gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture)); // TODO: remove once https://github.com/rustwasm/wasm-bindgen/issues/1005 is fixed. - let mut pixels: Vec<_> = texture.pixels.iter().cloned().collect(); + let mut pixels: Vec<_> = texture.pixels.to_vec(); let level = 0; let internal_format = Gl::ALPHA; @@ -294,8 +294,8 @@ impl Painter { .unwrap(); gl.uniform2f( Some(&u_tex_size_loc), - self.tex_size.0 as f32, - self.tex_size.1 as f32, + f32::from(self.tex_size.0), + f32::from(self.tex_size.1), ); let u_sampler_loc = gl.get_uniform_location(&self.program, "u_sampler").unwrap();