Code cleanup: allow None mouse_pos + clippy fixes
This commit is contained in:
parent
1beed16053
commit
f0c879b2f4
9 changed files with 81 additions and 67 deletions
5
TODO.md
5
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
|
# Code
|
||||||
* Break off example app from emigui_wasm
|
|
||||||
* Try it on iPhone
|
* Try it on iPhone
|
||||||
* Read TTF from browser?
|
* Read TTF from browser?
|
||||||
* requestAnimationFrame
|
* requestAnimationFrame
|
||||||
|
|
|
@ -59,7 +59,7 @@
|
||||||
wasm_bindgen.run_gui(g_wasm_app, JSON.stringify(input));
|
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;
|
var g_mouse_down = false;
|
||||||
|
|
||||||
function pixels_per_point() {
|
function pixels_per_point() {
|
||||||
|
@ -120,14 +120,13 @@
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
canvas.addEventListener("mouseleave", function(event) {
|
canvas.addEventListener("mouseleave", function(event) {
|
||||||
g_mouse_pos = { x: -1000.0, y: -1000.0 };
|
g_mouse_pos = null;
|
||||||
repaint();
|
repaint();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("touchstart", function(event) {
|
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_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
|
||||||
g_mouse_down = true;
|
g_mouse_down = true;
|
||||||
repaint();
|
repaint();
|
||||||
|
@ -135,17 +134,15 @@
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
canvas.addEventListener("touchmove", function(event) {
|
canvas.addEventListener("touchmove", function(event) {
|
||||||
// console.log(`touchmove ${JSON.stringify(event)}`);
|
|
||||||
g_mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
|
g_mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
|
||||||
repaint();
|
repaint();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
canvas.addEventListener("touchend", function(event) {
|
canvas.addEventListener("touchend", function(event) {
|
||||||
// console.log(`touchend ${JSON.stringify(event)}`);
|
|
||||||
g_mouse_down = false;
|
g_mouse_down = false;
|
||||||
repaint();
|
repaint();
|
||||||
g_mouse_pos = { x: -1000.0, y: -1000.0 };
|
g_mouse_pos = null;
|
||||||
repaint();
|
repaint();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
|
@ -72,44 +72,46 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) {
|
||||||
frame.add_rect(top_left, bottom_right);
|
frame.add_rect(top_left, bottom_right);
|
||||||
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)]));
|
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)]));
|
||||||
|
|
||||||
if interact.hovered {
|
if let Some(mouse_pos) = gui.input().mouse_pos {
|
||||||
show_popup(gui.data(), gui.input().mouse_pos, |gui| {
|
if interact.hovered {
|
||||||
let zoom_rect = gui.reserve_space(vec2(128.0, 128.0), None).rect;
|
show_popup(gui.data(), mouse_pos, |gui| {
|
||||||
let u = remap_clamp(
|
let zoom_rect = gui.reserve_space(vec2(128.0, 128.0), None).rect;
|
||||||
gui.input().mouse_pos.x,
|
let u = remap_clamp(
|
||||||
rect.min().x,
|
mouse_pos.x,
|
||||||
rect.max().x,
|
rect.min().x,
|
||||||
0.0,
|
rect.max().x,
|
||||||
texture.width as f32 - 1.0,
|
0.0,
|
||||||
)
|
texture.width as f32 - 1.0,
|
||||||
.round();
|
)
|
||||||
let v = remap_clamp(
|
.round();
|
||||||
gui.input().mouse_pos.y,
|
let v = remap_clamp(
|
||||||
rect.min().y,
|
mouse_pos.y,
|
||||||
rect.max().y,
|
rect.min().y,
|
||||||
0.0,
|
rect.max().y,
|
||||||
texture.height as f32 - 1.0,
|
0.0,
|
||||||
)
|
texture.height as f32 - 1.0,
|
||||||
.round();
|
)
|
||||||
|
.round();
|
||||||
|
|
||||||
let texel_radius = 32.0;
|
let texel_radius = 32.0;
|
||||||
let u = clamp(u, texel_radius, texture.width as f32 - 1.0 - texel_radius);
|
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 v = clamp(v, texel_radius, texture.height as f32 - 1.0 - texel_radius);
|
||||||
|
|
||||||
let top_left = Vertex {
|
let top_left = Vertex {
|
||||||
pos: zoom_rect.min(),
|
pos: zoom_rect.min(),
|
||||||
uv: ((u - texel_radius) as u16, (v - texel_radius) as u16),
|
uv: ((u - texel_radius) as u16, (v - texel_radius) as u16),
|
||||||
color: Color::WHITE,
|
color: Color::WHITE,
|
||||||
};
|
};
|
||||||
let bottom_right = Vertex {
|
let bottom_right = Vertex {
|
||||||
pos: zoom_rect.max(),
|
pos: zoom_rect.max(),
|
||||||
uv: ((u + texel_radius) as u16, (v + texel_radius) as u16),
|
uv: ((u + texel_radius) as u16, (v + texel_radius) as u16),
|
||||||
color: Color::WHITE,
|
color: Color::WHITE,
|
||||||
};
|
};
|
||||||
let mut frame = Frame::default();
|
let mut frame = Frame::default();
|
||||||
frame.add_rect(top_left, bottom_right);
|
frame.add_rect(top_left, bottom_right);
|
||||||
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)]));
|
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)]));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,11 +201,11 @@ impl Emigui {
|
||||||
gui.input().screen_size.y,
|
gui.input().screen_size.y,
|
||||||
gui.input().pixels_per_point,
|
gui.input().pixels_per_point,
|
||||||
));
|
));
|
||||||
gui.add(label!(
|
if let Some(mouse_pos) = gui.input().mouse_pos {
|
||||||
"mouse_pos: {} x {}",
|
gui.add(label!("mouse_pos: {} x {}", mouse_pos.x, mouse_pos.y,));
|
||||||
gui.input().mouse_pos.x,
|
} else {
|
||||||
gui.input().mouse_pos.y,
|
gui.add(label!("mouse_pos: None"));
|
||||||
));
|
}
|
||||||
gui.add(label!(
|
gui.add(label!(
|
||||||
"gui cursor: {} x {}",
|
"gui cursor: {} x {}",
|
||||||
gui.cursor().x,
|
gui.cursor().x,
|
||||||
|
|
|
@ -76,8 +76,10 @@ impl GuiResponse {
|
||||||
F: FnOnce(&mut Region),
|
F: FnOnce(&mut Region),
|
||||||
{
|
{
|
||||||
if self.hovered {
|
if self.hovered {
|
||||||
let window_pos = self.data.input().mouse_pos + vec2(16.0, 16.0);
|
if let Some(mouse_pos) = self.data.input().mouse_pos {
|
||||||
show_popup(&self.data, window_pos, add_contents);
|
let window_pos = mouse_pos + vec2(16.0, 16.0);
|
||||||
|
show_popup(&self.data, window_pos, add_contents);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -214,7 +216,7 @@ impl Data {
|
||||||
// TODO: move
|
// TODO: move
|
||||||
pub fn new_frame(&mut self, gui_input: GuiInput) {
|
pub fn new_frame(&mut self, gui_input: GuiInput) {
|
||||||
self.input = gui_input;
|
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;
|
self.memory.lock().unwrap().active_id = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -490,7 +492,11 @@ impl Region {
|
||||||
let is_something_else_active =
|
let is_something_else_active =
|
||||||
memory.active_id.is_some() && memory.active_id != interaction_id;
|
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 clicked = hovered && self.input().mouse_clicked;
|
||||||
let active = if interaction_id.is_some() {
|
let active = if interaction_id.is_some() {
|
||||||
if clicked {
|
if clicked {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::identity_op)]
|
||||||
|
|
||||||
const ANTI_ALIAS: bool = true;
|
const ANTI_ALIAS: bool = true;
|
||||||
const AA_SIZE: f32 = 0.5; // TODO: 1.0 / pixels_per_point
|
const AA_SIZE: f32 = 0.5; // TODO: 1.0 / pixels_per_point
|
||||||
|
|
||||||
|
@ -129,7 +131,7 @@ impl Frame {
|
||||||
let mut color_inner = color;
|
let mut color_inner = color;
|
||||||
if thin_line {
|
if thin_line {
|
||||||
// Fade out as it gets thinner:
|
// 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 ?
|
// TODO: line caps ?
|
||||||
let mut i0 = n - 1;
|
let mut i0 = n - 1;
|
||||||
|
|
|
@ -242,7 +242,7 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_paint_commands<'a, GuiCmdIterator>(
|
pub fn into_paint_commands<GuiCmdIterator>(
|
||||||
gui_commands: GuiCmdIterator,
|
gui_commands: GuiCmdIterator,
|
||||||
style: &Style,
|
style: &Style,
|
||||||
) -> Vec<PaintCmd>
|
) -> Vec<PaintCmd>
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub struct RawInput {
|
||||||
pub mouse_down: bool,
|
pub mouse_down: bool,
|
||||||
|
|
||||||
/// Current position of the mouse in points.
|
/// Current position of the mouse in points.
|
||||||
pub mouse_pos: Vec2,
|
pub mouse_pos: Option<Vec2>,
|
||||||
|
|
||||||
/// Size of the screen in points.
|
/// Size of the screen in points.
|
||||||
pub screen_size: Vec2,
|
pub screen_size: Vec2,
|
||||||
|
@ -35,7 +35,7 @@ pub struct GuiInput {
|
||||||
pub mouse_released: bool,
|
pub mouse_released: bool,
|
||||||
|
|
||||||
/// Current position of the mouse in points.
|
/// Current position of the mouse in points.
|
||||||
pub mouse_pos: Vec2,
|
pub mouse_pos: Option<Vec2>,
|
||||||
|
|
||||||
/// Size of the screen in points.
|
/// Size of the screen in points.
|
||||||
pub screen_size: Vec2,
|
pub screen_size: Vec2,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::new_without_default_derive)]
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
fonts::TextStyle,
|
fonts::TextStyle,
|
||||||
layout::{make_id, Align, Direction, GuiResponse, Id, Region},
|
layout::{make_id, Align, Direction, GuiResponse, Id, Region},
|
||||||
|
@ -289,14 +291,16 @@ impl<'a> Widget for Slider<'a> {
|
||||||
id,
|
id,
|
||||||
);
|
);
|
||||||
|
|
||||||
if interact.active {
|
if let Some(mouse_pos) = region.input().mouse_pos {
|
||||||
*value = remap_clamp(
|
if interact.active {
|
||||||
region.input().mouse_pos.x,
|
*value = remap_clamp(
|
||||||
interact.rect.min().x,
|
mouse_pos.x,
|
||||||
interact.rect.max().x,
|
interact.rect.min().x,
|
||||||
min,
|
interact.rect.max().x,
|
||||||
max,
|
min,
|
||||||
);
|
max,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
region.add_graphic(GuiCmd::Slider {
|
region.add_graphic(GuiCmd::Slider {
|
||||||
|
|
|
@ -119,7 +119,7 @@ impl Painter {
|
||||||
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture));
|
gl.bind_texture(Gl::TEXTURE_2D, Some(&self.texture));
|
||||||
|
|
||||||
// TODO: remove once https://github.com/rustwasm/wasm-bindgen/issues/1005 is fixed.
|
// 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 level = 0;
|
||||||
let internal_format = Gl::ALPHA;
|
let internal_format = Gl::ALPHA;
|
||||||
|
@ -294,8 +294,8 @@ impl Painter {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
gl.uniform2f(
|
gl.uniform2f(
|
||||||
Some(&u_tex_size_loc),
|
Some(&u_tex_size_loc),
|
||||||
self.tex_size.0 as f32,
|
f32::from(self.tex_size.0),
|
||||||
self.tex_size.1 as f32,
|
f32::from(self.tex_size.1),
|
||||||
);
|
);
|
||||||
|
|
||||||
let u_sampler_loc = gl.get_uniform_location(&self.program, "u_sampler").unwrap();
|
let u_sampler_loc = gl.get_uniform_location(&self.program, "u_sampler").unwrap();
|
||||||
|
|
Loading…
Reference in a new issue