Fix hover-to-zoom of font texture in demo
Closes https://github.com/emilk/egui/issues/220
This commit is contained in:
parent
b1883d5d48
commit
6442d254a6
3 changed files with 32 additions and 19 deletions
|
@ -29,26 +29,27 @@ impl Widget for &epaint::Texture {
|
|||
|
||||
let (tex_w, tex_h) = (self.width as f32, self.height as f32);
|
||||
|
||||
let pointer_pos = response.interact_pointer_pos();
|
||||
response
|
||||
.on_hover_cursor(CursorIcon::ZoomIn)
|
||||
.on_hover_ui_at_pointer(|ui| {
|
||||
if let Some(pos) = ui.input().pointer.latest_pos() {
|
||||
let (_id, zoom_rect) = ui.allocate_space(vec2(128.0, 128.0));
|
||||
let u = remap_clamp(pos.x, rect.x_range(), 0.0..=tex_w);
|
||||
let v = remap_clamp(pos.y, rect.y_range(), 0.0..=tex_h);
|
||||
|
||||
response.on_hover_ui(|ui| {
|
||||
let pos = pointer_pos.unwrap_or_else(|| ui.min_rect().left_top());
|
||||
let (_id, zoom_rect) = ui.allocate_space(vec2(128.0, 128.0));
|
||||
let u = remap_clamp(pos.x, rect.x_range(), 0.0..=tex_w);
|
||||
let v = remap_clamp(pos.y, rect.y_range(), 0.0..=tex_h);
|
||||
let texel_radius = 32.0;
|
||||
let u = u.at_least(texel_radius).at_most(tex_w - texel_radius);
|
||||
let v = v.at_least(texel_radius).at_most(tex_h - texel_radius);
|
||||
|
||||
let texel_radius = 32.0;
|
||||
let u = u.at_least(texel_radius).at_most(tex_w - texel_radius);
|
||||
let v = v.at_least(texel_radius).at_most(tex_h - texel_radius);
|
||||
|
||||
let uv_rect = Rect::from_min_max(
|
||||
pos2((u - texel_radius) / tex_w, (v - texel_radius) / tex_h),
|
||||
pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h),
|
||||
);
|
||||
let mut mesh = Mesh::default();
|
||||
mesh.add_rect_with_uv(zoom_rect, uv_rect, Color32::WHITE);
|
||||
ui.painter().add(Shape::mesh(mesh));
|
||||
});
|
||||
let uv_rect = Rect::from_min_max(
|
||||
pos2((u - texel_radius) / tex_w, (v - texel_radius) / tex_h),
|
||||
pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h),
|
||||
);
|
||||
let mut mesh = Mesh::default();
|
||||
mesh.add_rect_with_uv(zoom_rect, uv_rect, Color32::WHITE);
|
||||
ui.painter().add(Shape::mesh(mesh));
|
||||
}
|
||||
});
|
||||
})
|
||||
.response
|
||||
}
|
||||
|
|
|
@ -267,6 +267,18 @@ impl Response {
|
|||
self
|
||||
}
|
||||
|
||||
/// Like `on_hover_ui`, but show the ui next to cursor.
|
||||
pub fn on_hover_ui_at_pointer(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
|
||||
if self.should_show_hover_ui() {
|
||||
crate::containers::show_tooltip_at_pointer(
|
||||
&self.ctx,
|
||||
self.id.with("__tooltip"),
|
||||
add_contents,
|
||||
);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn should_show_hover_ui(&self) -> bool {
|
||||
if self.ctx.memory().everything_is_visible() {
|
||||
true
|
||||
|
|
|
@ -318,7 +318,7 @@ impl Default for Interaction {
|
|||
Self {
|
||||
resize_grab_radius_side: 5.0,
|
||||
resize_grab_radius_corner: 10.0,
|
||||
show_tooltips_only_when_still: true,
|
||||
show_tooltips_only_when_still: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue