Code cleanup: allow None mouse_pos + clippy fixes

This commit is contained in:
Emil Ernerfeldt 2019-02-10 15:30:48 +01:00
parent 1beed16053
commit f0c879b2f4
9 changed files with 81 additions and 67 deletions

View file

@ -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

View file

@ -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();

View file

@ -72,11 +72,12 @@ 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 let Some(mouse_pos) = gui.input().mouse_pos {
if interact.hovered {
show_popup(gui.data(), gui.input().mouse_pos, |gui| {
show_popup(gui.data(), 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,
mouse_pos.x,
rect.min().x,
rect.max().x,
0.0,
@ -84,7 +85,7 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) {
)
.round();
let v = remap_clamp(
gui.input().mouse_pos.y,
mouse_pos.y,
rect.min().y,
rect.max().y,
0.0,
@ -111,6 +112,7 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) {
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)]));
});
}
}
}
/// Encapsulates input, layout and painting for ease of use.
@ -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,

View file

@ -76,9 +76,11 @@ impl GuiResponse {
F: FnOnce(&mut Region),
{
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 {
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 {

View file

@ -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;

View file

@ -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,
style: &Style,
) -> Vec<PaintCmd>

View file

@ -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<Vec2>,
/// 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<Vec2>,
/// Size of the screen in points.
pub screen_size: Vec2,

View file

@ -1,3 +1,5 @@
#![allow(clippy::new_without_default_derive)]
use crate::{
fonts::TextStyle,
layout::{make_id, Align, Direction, GuiResponse, Id, Region},
@ -289,15 +291,17 @@ impl<'a> Widget for Slider<'a> {
id,
);
if let Some(mouse_pos) = region.input().mouse_pos {
if interact.active {
*value = remap_clamp(
region.input().mouse_pos.x,
mouse_pos.x,
interact.rect.min().x,
interact.rect.max().x,
min,
max,
);
}
}
region.add_graphic(GuiCmd::Slider {
interact,

View file

@ -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();