From 46293f6fd48a71989aa07a5fba903f91373d5aa2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 17 Jan 2019 17:33:20 -0600 Subject: [PATCH] Allow zooming font atlas --- emigui/src/emigui.rs | 89 ++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/emigui/src/emigui.rs b/emigui/src/emigui.rs index 5045259e..4e1b9d7e 100644 --- a/emigui/src/emigui.rs +++ b/emigui/src/emigui.rs @@ -2,8 +2,8 @@ use std::sync::Arc; use crate::{ layout, - layout::{LayoutOptions, Region}, - math::vec2, + layout::{show_popup, LayoutOptions, Region}, + math::{clamp, remap_clamp, vec2}, mesher::Vertex, style, types::{Color, GuiCmd, GuiInput, PaintCmd}, @@ -45,6 +45,69 @@ fn show_font_sizes(font_sizes: &mut FontSizes, gui: &mut Region) { } } +fn show_font_texture(texture: &Texture, gui: &mut Region) { + gui.add(label(format!( + "Font texture size: {} x {} (hover to zoom)", + texture.width, texture.height + ))); + let size = vec2(texture.width as f32, texture.height as f32); + let interact = gui.reserve_space(size, None); + let rect = interact.rect; + let top_left = Vertex { + pos: rect.min(), + uv: (0, 0), + color: Color::WHITE, + }; + let bottom_right = Vertex { + pos: rect.max(), + uv: (texture.width as u16 - 1, texture.height as u16 - 1), + color: Color::WHITE, + }; + let mut frame = Frame::default(); + 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(); + + 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)])); + }); + } +} + /// Encapsulates input, layout and painting for ease of use. pub struct Emigui { pub last_input: RawInput, @@ -112,27 +175,7 @@ impl Emigui { }); region.foldable("Fonts", |gui| { - let texture = self.texture(); - gui.add(label(format!( - "Font texture size: {} x {}", - texture.width, texture.height - ))); - let size = vec2(texture.width as f32, texture.height as f32); - let rect = gui.reserve_space(size, None).rect; - let top_left = Vertex { - pos: rect.min(), - uv: (0, 0), - color: Color::WHITE, - }; - let bottom_right = Vertex { - pos: rect.max(), - uv: (texture.width as u16 - 1, texture.height as u16 - 1), - color: Color::WHITE, - }; - let mut frame = Frame::default(); - frame.add_rect(top_left, bottom_right); - gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Frame(frame)])); - + show_font_texture(self.texture(), gui); let old_font_sizes = self.data.fonts.sizes(); let mut new_font_sizes = old_font_sizes.clone(); show_font_sizes(&mut new_font_sizes, gui);