mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 22:30:03 -04:00
[glium] implement reactive run mode
This commit is contained in:
@@ -8,6 +8,16 @@ use crate::{
|
||||
const EGUI_MEMORY_KEY: &str = "egui";
|
||||
const WINDOW_KEY: &str = "window";
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum RunMode {
|
||||
/// Uses `request_animation_frame` to repaint the UI on each display Hz.
|
||||
/// This is good for games and stuff where you want to run logic at e.g. 60 FPS.
|
||||
Continuous,
|
||||
|
||||
/// Only repaint when there are animations or input (mouse movement, keyboard input etc).
|
||||
Reactive,
|
||||
}
|
||||
|
||||
pub trait App {
|
||||
/// Called onced per frame for you to draw the UI.
|
||||
fn ui(&mut self, ui: &mut egui::Ui, runner: &mut Runner);
|
||||
@@ -19,21 +29,31 @@ pub trait App {
|
||||
pub struct Runner {
|
||||
frame_times: egui::MovementTracker<f32>,
|
||||
quit: bool,
|
||||
run_mode: RunMode,
|
||||
}
|
||||
|
||||
impl Runner {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(run_mode: RunMode) -> Self {
|
||||
Self {
|
||||
frame_times: egui::MovementTracker::new(1000, 1.0),
|
||||
quit: false,
|
||||
run_mode,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_mode(&self) -> RunMode {
|
||||
self.run_mode
|
||||
}
|
||||
|
||||
pub fn set_run_mode(&mut self, run_mode: RunMode) {
|
||||
self.run_mode = run_mode;
|
||||
}
|
||||
|
||||
pub fn quit(&mut self) {
|
||||
self.quit = true;
|
||||
}
|
||||
|
||||
pub fn cpu_usage(&self) -> f32 {
|
||||
pub fn cpu_time(&self) -> f32 {
|
||||
self.frame_times.average().unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -43,7 +63,12 @@ impl Runner {
|
||||
}
|
||||
|
||||
/// Run an egui app
|
||||
pub fn run(title: &str, mut persistence: Persistence, mut app: impl App + 'static) -> ! {
|
||||
pub fn run(
|
||||
title: &str,
|
||||
run_mode: RunMode,
|
||||
mut persistence: Persistence,
|
||||
mut app: impl App + 'static,
|
||||
) -> ! {
|
||||
let event_loop = glutin::event_loop::EventLoop::new();
|
||||
let mut window = glutin::window::WindowBuilder::new()
|
||||
.with_decorations(true)
|
||||
@@ -75,7 +100,7 @@ pub fn run(title: &str, mut persistence: Persistence, mut app: impl App + 'stati
|
||||
|
||||
// used to keep track of time for animations
|
||||
let start_time = Instant::now();
|
||||
let mut runner = Runner::new();
|
||||
let mut runner = Runner::new(run_mode);
|
||||
let mut clipboard = init_clipboard();
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
@@ -95,16 +120,18 @@ pub fn run(title: &str, mut persistence: Persistence, mut app: impl App + 'stati
|
||||
runner.frame_times.add(raw_input.time, frame_time);
|
||||
|
||||
painter.paint_jobs(&display, paint_jobs, ctx.texture());
|
||||
handle_output(output, &display, clipboard.as_mut());
|
||||
|
||||
if runner.quit {
|
||||
*control_flow = glutin::event_loop::ControlFlow::Exit
|
||||
} else {
|
||||
display.gl_window().window().request_redraw(); // TODO: only if needed (new events etc)
|
||||
} else if runner.run_mode() == RunMode::Continuous || output.needs_repaint {
|
||||
display.gl_window().window().request_redraw();
|
||||
}
|
||||
|
||||
handle_output(output, &display, clipboard.as_mut());
|
||||
}
|
||||
glutin::event::Event::WindowEvent { event, .. } => {
|
||||
input_to_egui(event, clipboard.as_mut(), &mut raw_input, control_flow);
|
||||
display.gl_window().window().request_redraw(); // TODO: maybe only on some events?
|
||||
}
|
||||
glutin::event::Event::LoopDestroyed => {
|
||||
persistence.set_value(WINDOW_KEY, &WindowSettings::from_display(&display));
|
||||
|
||||
Reference in New Issue
Block a user