[app] unify web and glium demo app
This commit is contained in:
+2
-1
@@ -19,9 +19,10 @@ ahash = "0.4"
|
||||
parking_lot = "0.11"
|
||||
rusttype = "0.9"
|
||||
serde = { version = "1", features = ["derive"], optional = true }
|
||||
serde_json = { version = "1", optional = true }
|
||||
|
||||
[features]
|
||||
with_serde = ["serde"]
|
||||
with_serde = ["serde", "serde_json"]
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.3", default-features = false }
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
//! Traits and helper for writing Egui apps.
|
||||
//!
|
||||
//! Egui can be used as a library, but you can also use it as a framework to write apps in.
|
||||
//! This module defined the `App` trait that can be implemented and used with the `egui_web` and `egui_glium` crates.
|
||||
|
||||
use crate::Ui;
|
||||
|
||||
/// Implement this trait to write apps that can be compiled both natively using the `egui_glium` crate,
|
||||
/// and deployed as a web site using the `egui_web` crate.
|
||||
pub trait App {
|
||||
/// Called each time the UI needs repainting, which may be many times per second.
|
||||
fn ui(&mut self, ui: &mut Ui, backend: &mut dyn Backend);
|
||||
|
||||
/// Called once on shutdown. Allows you to save state.
|
||||
fn on_exit(&mut self, _storage: &mut dyn Storage) {}
|
||||
}
|
||||
|
||||
// TODO: replace with manually calling `egui::Context::request_repaint()` each frame.
|
||||
/// How the backend runs the app
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum RunMode {
|
||||
/// Rapint the UI all the time (at the display refresh rate of e.g. 60 Hz).
|
||||
/// This is good for games where things are constantly moving.
|
||||
/// This can also be achieved with `RunMode::Reactive` combined with calling `egui::Context::request_repaint()` each frame.
|
||||
Continuous,
|
||||
|
||||
/// Only repaint when there are animations or input (mouse movement, keyboard input etc).
|
||||
/// This saves CPU.
|
||||
Reactive,
|
||||
}
|
||||
|
||||
pub struct WebInfo {
|
||||
/// e.g. "#fragment" part of "www.example.com/index.html#fragment"
|
||||
pub web_location_hash: String,
|
||||
}
|
||||
|
||||
pub trait Backend {
|
||||
fn run_mode(&self) -> RunMode;
|
||||
fn set_run_mode(&mut self, run_mode: RunMode);
|
||||
|
||||
/// If the app is running in a Web context, this returns information about the environment.
|
||||
fn web_info(&self) -> Option<WebInfo> {
|
||||
None
|
||||
}
|
||||
|
||||
/// excludes painting
|
||||
fn cpu_time(&self) -> f32;
|
||||
|
||||
/// Smoothed frames per second
|
||||
fn fps(&self) -> f32;
|
||||
|
||||
/// Signal the backend that we'd like to exit the app now.
|
||||
/// This does nothing for web apps.s
|
||||
fn quit(&mut self) {}
|
||||
}
|
||||
|
||||
/// A place where you can store custom data in a way that persists when you restart the app.
|
||||
///
|
||||
/// On the web this is backed by [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
|
||||
/// On desktop this is backed by the file system.
|
||||
pub trait Storage {
|
||||
fn get_string(&self, key: &str) -> Option<&str>;
|
||||
fn set_string(&mut self, key: &str, value: String);
|
||||
}
|
||||
|
||||
#[cfg(feature = "with_serde")]
|
||||
pub fn get_value<T: serde::de::DeserializeOwned>(storage: &dyn Storage, key: &str) -> Option<T> {
|
||||
storage
|
||||
.get_string(key)
|
||||
.and_then(|value| serde_json::from_str(value).ok())
|
||||
}
|
||||
|
||||
#[cfg(feature = "with_serde")]
|
||||
pub fn set_value<T: serde::Serialize>(storage: &mut dyn Storage, key: &str, value: &T) {
|
||||
storage.set_string(key, serde_json::to_string(value).unwrap());
|
||||
}
|
||||
|
||||
/// storage key used for app
|
||||
pub const APP_KEY: &str = "app";
|
||||
+80
-1
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{color::*, containers::*, demos::FractalClock, paint::*, widgets::*, *};
|
||||
use crate::{app, color::*, containers::*, demos::FractalClock, paint::*, widgets::*, *};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -12,6 +12,7 @@ pub struct DemoApp {
|
||||
open_windows: OpenWindows,
|
||||
demo_window: DemoWindow,
|
||||
fractal_clock: FractalClock,
|
||||
num_frames_painted: u64,
|
||||
}
|
||||
|
||||
impl DemoApp {
|
||||
@@ -68,6 +69,84 @@ impl DemoApp {
|
||||
|
||||
fractal_clock.window(ctx, &mut open_windows.fractal_clock);
|
||||
}
|
||||
|
||||
fn backend_ui(&mut self, ui: &mut Ui, backend: &mut dyn app::Backend) {
|
||||
let is_web = backend.web_info().is_some();
|
||||
|
||||
if is_web {
|
||||
ui.label("Egui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.");
|
||||
ui.label(
|
||||
"Everything you see is rendered as textured triangles. There is no DOM. There are no HTML elements."
|
||||
);
|
||||
ui.label("This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech.");
|
||||
ui.label("This is also work in progress, and not ready for production... yet :)");
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Project home page:");
|
||||
ui.hyperlink("https://github.com/emilk/emigui/");
|
||||
});
|
||||
} else {
|
||||
ui.add(label!("Egui").text_style(TextStyle::Heading));
|
||||
if ui.add(Button::new("Quit")).clicked {
|
||||
backend.quit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.add(
|
||||
label!(
|
||||
"CPU usage: {:.2} ms / frame (excludes painting)",
|
||||
1e3 * backend.cpu_time()
|
||||
)
|
||||
.text_style(TextStyle::Monospace),
|
||||
);
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let mut run_mode = backend.run_mode();
|
||||
ui.label("Run mode:");
|
||||
ui.radio_value("Continuous", &mut run_mode, app::RunMode::Continuous)
|
||||
.tooltip_text("Repaint everything each frame");
|
||||
ui.radio_value("Reactive", &mut run_mode, app::RunMode::Reactive)
|
||||
.tooltip_text("Repaint when there are animations or input (e.g. mouse movement)");
|
||||
backend.set_run_mode(run_mode);
|
||||
});
|
||||
|
||||
if backend.run_mode() == app::RunMode::Continuous {
|
||||
ui.add(
|
||||
label!("Repainting the UI each frame. FPS: {:.1}", backend.fps())
|
||||
.text_style(TextStyle::Monospace),
|
||||
);
|
||||
} else {
|
||||
ui.label("Only running UI code when there are animations or input");
|
||||
}
|
||||
|
||||
self.num_frames_painted += 1;
|
||||
ui.label(format!("Total frames painted: {}", self.num_frames_painted));
|
||||
}
|
||||
}
|
||||
|
||||
impl app::App for DemoApp {
|
||||
fn ui(&mut self, ui: &mut Ui, backend: &mut dyn app::Backend) {
|
||||
Window::new("Backend")
|
||||
.default_width(500.0)
|
||||
.show(ui.ctx(), |ui| {
|
||||
self.backend_ui(ui, backend);
|
||||
});
|
||||
|
||||
let web_info = backend.web_info();
|
||||
let web_location_hash = web_info
|
||||
.as_ref()
|
||||
.map(|info| info.web_location_hash.as_str())
|
||||
.unwrap_or_default();
|
||||
self.ui(ui, web_location_hash);
|
||||
}
|
||||
|
||||
fn on_exit(&mut self, storage: &mut dyn app::Storage) {
|
||||
app::set_value(storage, app::APP_KEY, self);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
rust_2018_idioms,
|
||||
)]
|
||||
|
||||
pub mod app;
|
||||
pub mod containers;
|
||||
mod context;
|
||||
pub mod demos;
|
||||
|
||||
Reference in New Issue
Block a user