1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 07:03:14 -04:00

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

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

View File

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

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,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 {