mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
[app] Add demo app slider to change scale of all of Egui
This commit is contained in:
@@ -51,7 +51,10 @@ pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) -
|
||||
let mut ctx = egui::Context::new();
|
||||
*ctx.memory() = egui::app::get_value(&storage, EGUI_MEMORY_KEY).unwrap_or_default();
|
||||
|
||||
let mut raw_input = make_raw_input(&display);
|
||||
let mut raw_input = egui::RawInput {
|
||||
pixels_per_point: Some(native_pixels_per_point(&display)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let start_time = Instant::now();
|
||||
let mut previous_frame_time = None;
|
||||
@@ -62,11 +65,14 @@ pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) -
|
||||
let mut redraw = || {
|
||||
let egui_start = Instant::now();
|
||||
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
||||
raw_input.screen_size =
|
||||
screen_size_in_pixels(&display) / raw_input.pixels_per_point.unwrap();
|
||||
|
||||
let backend_info = egui::app::BackendInfo {
|
||||
web_info: None,
|
||||
cpu_usage: previous_frame_time,
|
||||
seconds_since_midnight: Some(seconds_since_midnight()),
|
||||
native_pixels_per_point: Some(native_pixels_per_point(&display)),
|
||||
};
|
||||
|
||||
let mut ui = ctx.begin_frame(raw_input.take());
|
||||
@@ -75,10 +81,17 @@ pub fn run(title: &str, mut storage: FileStorage, mut app: impl App + 'static) -
|
||||
|
||||
let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32;
|
||||
previous_frame_time = Some(frame_time);
|
||||
painter.paint_jobs(&display, paint_jobs, &ctx.texture());
|
||||
painter.paint_jobs(&display, ctx.pixels_per_point(), paint_jobs, &ctx.texture());
|
||||
|
||||
{
|
||||
let egui::app::AppOutput { quit } = app_output;
|
||||
let egui::app::AppOutput {
|
||||
quit,
|
||||
pixels_per_point,
|
||||
} = app_output;
|
||||
if let Some(pixels_per_point) = pixels_per_point {
|
||||
// User changed GUI scale
|
||||
raw_input.pixels_per_point = Some(pixels_per_point);
|
||||
}
|
||||
|
||||
*control_flow = if quit {
|
||||
glutin::event_loop::ControlFlow::Exit
|
||||
|
||||
@@ -27,30 +27,16 @@ pub fn input_to_egui(
|
||||
use glutin::event::WindowEvent::*;
|
||||
match event {
|
||||
CloseRequested | Destroyed => *control_flow = ControlFlow::Exit,
|
||||
|
||||
Resized(physical_size) => {
|
||||
raw_input.screen_size =
|
||||
egui::vec2(physical_size.width as f32, physical_size.height as f32)
|
||||
/ raw_input.pixels_per_point.unwrap();
|
||||
}
|
||||
|
||||
ScaleFactorChanged {
|
||||
scale_factor,
|
||||
new_inner_size,
|
||||
} => {
|
||||
raw_input.pixels_per_point = Some(scale_factor as f32);
|
||||
raw_input.screen_size =
|
||||
egui::vec2(new_inner_size.width as f32, new_inner_size.height as f32)
|
||||
/ (scale_factor as f32);
|
||||
}
|
||||
|
||||
MouseInput { state, .. } => {
|
||||
raw_input.mouse_down = state == glutin::event::ElementState::Pressed;
|
||||
}
|
||||
CursorMoved { position, .. } => {
|
||||
CursorMoved {
|
||||
position: pos_in_pixels,
|
||||
..
|
||||
} => {
|
||||
raw_input.mouse_pos = Some(pos2(
|
||||
position.x as f32 / raw_input.pixels_per_point.unwrap(),
|
||||
position.y as f32 / raw_input.pixels_per_point.unwrap(),
|
||||
pos_in_pixels.x as f32 / raw_input.pixels_per_point.unwrap(),
|
||||
pos_in_pixels.y as f32 / raw_input.pixels_per_point.unwrap(),
|
||||
));
|
||||
}
|
||||
CursorLeft { .. } => {
|
||||
@@ -213,14 +199,11 @@ pub fn seconds_since_midnight() -> f64 {
|
||||
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64)
|
||||
}
|
||||
|
||||
pub fn make_raw_input(display: &glium::Display) -> egui::RawInput {
|
||||
let pixels_per_point = display.gl_window().window().scale_factor() as f32;
|
||||
egui::RawInput {
|
||||
screen_size: {
|
||||
let (width, height) = display.get_framebuffer_dimensions();
|
||||
vec2(width as f32, height as f32) / pixels_per_point
|
||||
},
|
||||
pixels_per_point: Some(pixels_per_point),
|
||||
..Default::default()
|
||||
}
|
||||
pub fn screen_size_in_pixels(display: &glium::Display) -> Vec2 {
|
||||
let (width_in_pixels, height_in_pixels) = display.get_framebuffer_dimensions();
|
||||
vec2(width_in_pixels as f32, height_in_pixels as f32)
|
||||
}
|
||||
|
||||
pub fn native_pixels_per_point(display: &glium::Display) -> f32 {
|
||||
display.gl_window().window().scale_factor() as f32
|
||||
}
|
||||
|
||||
@@ -152,16 +152,23 @@ impl Painter {
|
||||
pub fn paint_jobs(
|
||||
&mut self,
|
||||
display: &glium::Display,
|
||||
pixels_per_point: f32,
|
||||
jobs: PaintJobs,
|
||||
texture: &egui::Texture,
|
||||
egui_texture: &egui::Texture,
|
||||
) {
|
||||
self.upload_egui_texture(display, texture);
|
||||
self.upload_egui_texture(display, egui_texture);
|
||||
self.upload_pending_user_textures(display);
|
||||
|
||||
let mut target = display.draw();
|
||||
target.clear_color(0.0, 0.0, 0.0, 0.0);
|
||||
for (clip_rect, triangles) in jobs {
|
||||
self.paint_job(&mut target, display, clip_rect, &triangles)
|
||||
self.paint_job(
|
||||
&mut target,
|
||||
display,
|
||||
pixels_per_point,
|
||||
clip_rect,
|
||||
&triangles,
|
||||
)
|
||||
}
|
||||
target.finish().unwrap();
|
||||
}
|
||||
@@ -183,6 +190,7 @@ impl Painter {
|
||||
&mut self,
|
||||
target: &mut Frame,
|
||||
display: &glium::Display,
|
||||
pixels_per_point: f32,
|
||||
clip_rect: Rect,
|
||||
triangles: &Triangles,
|
||||
) {
|
||||
@@ -217,15 +225,14 @@ impl Painter {
|
||||
let index_buffer =
|
||||
glium::IndexBuffer::new(display, PrimitiveType::TrianglesList, &indices).unwrap();
|
||||
|
||||
let pixels_per_point = display.gl_window().window().scale_factor() as f32;
|
||||
let (width_pixels, height_pixels) = display.get_framebuffer_dimensions();
|
||||
let width_points = width_pixels as f32 / pixels_per_point;
|
||||
let height_points = height_pixels as f32 / pixels_per_point;
|
||||
let (width_in_pixels, height_in_pixels) = display.get_framebuffer_dimensions();
|
||||
let width_in_points = width_in_pixels as f32 / pixels_per_point;
|
||||
let height_in_points = height_in_pixels as f32 / pixels_per_point;
|
||||
|
||||
let texture = self.get_texture(triangles.texture_id);
|
||||
|
||||
let uniforms = uniform! {
|
||||
u_screen_size: [width_points, height_points],
|
||||
u_screen_size: [width_in_points, height_in_points],
|
||||
u_sampler: texture.sampled().wrap_function(SamplerWrapFunction::Clamp),
|
||||
};
|
||||
|
||||
@@ -255,10 +262,10 @@ impl Painter {
|
||||
let clip_max_y = pixels_per_point * clip_rect.max.y;
|
||||
|
||||
// Make sure clip rect can fit withing an `u32`:
|
||||
let clip_min_x = clamp(clip_min_x, 0.0..=width_pixels as f32);
|
||||
let clip_min_y = clamp(clip_min_y, 0.0..=height_pixels as f32);
|
||||
let clip_max_x = clamp(clip_max_x, clip_min_x..=width_pixels as f32);
|
||||
let clip_max_y = clamp(clip_max_y, clip_min_y..=height_pixels as f32);
|
||||
let clip_min_x = clamp(clip_min_x, 0.0..=width_in_pixels as f32);
|
||||
let clip_min_y = clamp(clip_min_y, 0.0..=height_in_pixels as f32);
|
||||
let clip_max_x = clamp(clip_max_x, clip_min_x..=width_in_pixels as f32);
|
||||
let clip_max_y = clamp(clip_max_y, clip_min_y..=height_in_pixels as f32);
|
||||
|
||||
let clip_min_x = clip_min_x.round() as u32;
|
||||
let clip_min_y = clip_min_y.round() as u32;
|
||||
@@ -269,7 +276,7 @@ impl Painter {
|
||||
blend,
|
||||
scissor: Some(glium::Rect {
|
||||
left: clip_min_x,
|
||||
bottom: height_pixels - clip_max_y,
|
||||
bottom: height_in_pixels - clip_max_y,
|
||||
width: clip_max_x - clip_min_x,
|
||||
height: clip_max_y - clip_min_y,
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user