2020-07-23 16:54:16 +00:00
|
|
|
use crate::*;
|
|
|
|
|
2021-01-02 16:02:18 +00:00
|
|
|
pub use egui::{pos2, Color32};
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct WebBackend {
|
2020-12-19 13:48:04 +00:00
|
|
|
ctx: egui::CtxRef,
|
2021-01-16 00:30:00 +00:00
|
|
|
painter: Box<dyn Painter>,
|
2020-10-01 20:25:44 +00:00
|
|
|
previous_frame_time: Option<f32>,
|
2020-07-23 16:54:16 +00:00
|
|
|
frame_start: Option<f64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebBackend {
|
2020-09-16 06:03:40 +00:00
|
|
|
pub fn new(canvas_id: &str) -> Result<Self, JsValue> {
|
2020-12-19 13:48:04 +00:00
|
|
|
let ctx = egui::CtxRef::default();
|
2021-01-16 00:30:00 +00:00
|
|
|
|
|
|
|
let painter: Box<dyn Painter> =
|
|
|
|
if let Ok(webgl2_painter) = webgl2::WebGl2Painter::new(canvas_id) {
|
|
|
|
console_log("Using WebGL2 backend");
|
|
|
|
Box::new(webgl2_painter)
|
|
|
|
} else {
|
|
|
|
console_log("Falling back to WebGL1 backend");
|
|
|
|
Box::new(webgl1::WebGlPainter::new(canvas_id)?)
|
|
|
|
};
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
Ok(Self {
|
|
|
|
ctx,
|
2021-01-16 00:30:00 +00:00
|
|
|
painter,
|
2020-10-01 20:25:44 +00:00
|
|
|
previous_frame_time: None,
|
2020-07-23 16:54:16 +00:00
|
|
|
frame_start: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// id of the canvas html element containing the rendering
|
|
|
|
pub fn canvas_id(&self) -> &str {
|
|
|
|
self.painter.canvas_id()
|
|
|
|
}
|
|
|
|
|
2020-10-24 08:56:23 +00:00
|
|
|
pub fn begin_frame(&mut self, raw_input: egui::RawInput) {
|
2020-07-23 16:54:16 +00:00
|
|
|
self.frame_start = Some(now_sec());
|
|
|
|
self.ctx.begin_frame(raw_input)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn end_frame(&mut self) -> Result<(egui::Output, egui::PaintJobs), JsValue> {
|
|
|
|
let frame_start = self
|
|
|
|
.frame_start
|
|
|
|
.take()
|
|
|
|
.expect("unmatched calls to begin_frame/end_frame");
|
|
|
|
|
2021-01-10 10:43:01 +00:00
|
|
|
let (output, shapes) = self.ctx.end_frame();
|
|
|
|
let paint_jobs = self.ctx.tessellate(shapes);
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
let now = now_sec();
|
2020-10-01 20:25:44 +00:00
|
|
|
self.previous_frame_time = Some((now - frame_start) as f32);
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
Ok((output, paint_jobs))
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:34:48 +00:00
|
|
|
pub fn paint(
|
|
|
|
&mut self,
|
|
|
|
clear_color: egui::Rgba,
|
|
|
|
paint_jobs: egui::PaintJobs,
|
|
|
|
) -> Result<(), JsValue> {
|
2021-01-16 00:30:00 +00:00
|
|
|
self.painter.upload_egui_texture(&self.ctx.texture());
|
|
|
|
self.painter.clear(clear_color);
|
|
|
|
self.painter
|
|
|
|
.paint_jobs(paint_jobs, self.ctx.pixels_per_point())
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn painter_debug_info(&self) -> String {
|
|
|
|
self.painter.debug_info()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Data gathered between frames.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WebInput {
|
2020-11-15 13:21:21 +00:00
|
|
|
/// Is this a touch screen? If so, we ignore mouse events.
|
2020-07-23 16:54:16 +00:00
|
|
|
pub is_touch: bool,
|
2020-11-15 13:21:21 +00:00
|
|
|
|
|
|
|
pub raw: egui::RawInput,
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WebInput {
|
2020-12-18 21:51:23 +00:00
|
|
|
pub fn new_frame(&mut self, canvas_size: egui::Vec2) -> egui::RawInput {
|
2020-07-23 16:54:16 +00:00
|
|
|
egui::RawInput {
|
2020-12-18 21:51:23 +00:00
|
|
|
screen_rect: Some(egui::Rect::from_min_size(Default::default(), canvas_size)),
|
2020-11-28 09:46:31 +00:00
|
|
|
pixels_per_point: Some(native_pixels_per_point()),
|
2020-12-16 20:22:45 +00:00
|
|
|
time: Some(now_sec()),
|
2020-11-15 13:21:21 +00:00
|
|
|
..self.raw.take()
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-11-20 19:35:16 +00:00
|
|
|
use std::sync::atomic::Ordering::SeqCst;
|
|
|
|
|
|
|
|
pub struct NeedRepaint(std::sync::atomic::AtomicBool);
|
|
|
|
|
|
|
|
impl Default for NeedRepaint {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(true.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NeedRepaint {
|
|
|
|
pub fn fetch_and_clear(&self) -> bool {
|
|
|
|
self.0.swap(false, SeqCst)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_true(&self) {
|
|
|
|
self.0.store(true, SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 13:15:46 +00:00
|
|
|
impl epi::RepaintSignal for NeedRepaint {
|
2020-11-20 19:35:16 +00:00
|
|
|
fn request_repaint(&self) {
|
|
|
|
self.0.store(true, SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub struct AppRunner {
|
2020-12-31 13:31:11 +00:00
|
|
|
web_backend: WebBackend,
|
|
|
|
pub(crate) input: WebInput,
|
|
|
|
app: Box<dyn epi::App>,
|
|
|
|
pub(crate) needs_repaint: std::sync::Arc<NeedRepaint>,
|
|
|
|
storage: LocalStorage,
|
|
|
|
last_save_time: f64,
|
2021-01-04 00:44:02 +00:00
|
|
|
#[cfg(feature = "http")]
|
|
|
|
http: Arc<http::WebHttp>,
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppRunner {
|
2020-12-29 13:15:46 +00:00
|
|
|
pub fn new(web_backend: WebBackend, mut app: Box<dyn epi::App>) -> Result<Self, JsValue> {
|
2020-12-19 19:50:00 +00:00
|
|
|
load_memory(&web_backend.ctx);
|
|
|
|
let storage = LocalStorage::default();
|
|
|
|
app.load(&storage);
|
2020-10-31 17:03:13 +00:00
|
|
|
app.setup(&web_backend.ctx);
|
2020-07-23 16:54:16 +00:00
|
|
|
Ok(Self {
|
|
|
|
web_backend,
|
2020-11-15 13:21:21 +00:00
|
|
|
input: Default::default(),
|
2020-07-23 16:54:16 +00:00
|
|
|
app,
|
2020-11-20 19:35:16 +00:00
|
|
|
needs_repaint: Default::default(),
|
2020-12-19 19:50:00 +00:00
|
|
|
storage,
|
|
|
|
last_save_time: now_sec(),
|
2021-01-04 00:44:02 +00:00
|
|
|
#[cfg(feature = "http")]
|
|
|
|
http: Arc::new(http::WebHttp {}),
|
2020-07-23 16:54:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-17 11:22:19 +00:00
|
|
|
pub fn egui_ctx(&self) -> &egui::CtxRef {
|
|
|
|
&self.web_backend.ctx
|
|
|
|
}
|
|
|
|
|
2020-12-19 19:50:00 +00:00
|
|
|
pub fn auto_save(&mut self) {
|
|
|
|
let now = now_sec();
|
|
|
|
let time_since_last_save = now - self.last_save_time;
|
|
|
|
|
|
|
|
if time_since_last_save > self.app.auto_save_interval().as_secs_f64() {
|
|
|
|
save_memory(&self.web_backend.ctx);
|
|
|
|
self.app.save(&mut self.storage);
|
|
|
|
self.last_save_time = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub fn canvas_id(&self) -> &str {
|
|
|
|
self.web_backend.canvas_id()
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:42:43 +00:00
|
|
|
pub fn warm_up(&mut self) -> Result<(), JsValue> {
|
|
|
|
if self.app.warm_up_enabled() {
|
|
|
|
let saved_memory = self.web_backend.ctx.memory().clone();
|
|
|
|
self.web_backend
|
|
|
|
.ctx
|
|
|
|
.memory()
|
|
|
|
.set_everything_is_visible(true);
|
|
|
|
self.logic()?;
|
|
|
|
*self.web_backend.ctx.memory() = saved_memory; // We don't want to remember that windows were huge.
|
|
|
|
self.web_backend.ctx.clear_animations();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub fn logic(&mut self) -> Result<(egui::Output, egui::PaintJobs), JsValue> {
|
2020-10-17 21:54:46 +00:00
|
|
|
resize_canvas_to_screen_size(self.web_backend.canvas_id());
|
2020-12-18 21:51:23 +00:00
|
|
|
let canvas_size = canvas_size_in_points(self.web_backend.canvas_id());
|
|
|
|
let raw_input = self.input.new_frame(canvas_size);
|
2020-10-24 17:23:16 +00:00
|
|
|
self.web_backend.begin_frame(raw_input);
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2020-12-31 13:31:11 +00:00
|
|
|
let mut app_output = epi::backend::AppOutput::default();
|
|
|
|
let mut frame = epi::backend::FrameBuilder {
|
2020-12-29 13:15:46 +00:00
|
|
|
info: epi::IntegrationInfo {
|
|
|
|
web_info: Some(epi::WebInfo {
|
2020-10-24 17:23:16 +00:00
|
|
|
web_location_hash: location_hash().unwrap_or_default(),
|
|
|
|
}),
|
|
|
|
cpu_usage: self.web_backend.previous_frame_time,
|
|
|
|
seconds_since_midnight: Some(seconds_since_midnight()),
|
|
|
|
native_pixels_per_point: Some(native_pixels_per_point()),
|
|
|
|
},
|
2021-01-16 00:30:00 +00:00
|
|
|
tex_allocator: Some(self.web_backend.painter.as_tex_allocator()),
|
2021-01-04 00:44:02 +00:00
|
|
|
#[cfg(feature = "http")]
|
2020-12-31 13:31:11 +00:00
|
|
|
http: self.http.clone(),
|
|
|
|
output: &mut app_output,
|
2020-11-20 19:35:16 +00:00
|
|
|
repaint_signal: self.needs_repaint.clone(),
|
2020-12-31 13:31:11 +00:00
|
|
|
}
|
|
|
|
.build();
|
2020-10-17 10:33:30 +00:00
|
|
|
|
2020-10-24 08:56:23 +00:00
|
|
|
let egui_ctx = &self.web_backend.ctx;
|
2021-01-01 19:22:18 +00:00
|
|
|
self.app.update(egui_ctx, &mut frame);
|
2020-10-17 10:33:30 +00:00
|
|
|
let (egui_output, paint_jobs) = self.web_backend.end_frame()?;
|
|
|
|
handle_output(&egui_output);
|
|
|
|
|
|
|
|
{
|
2020-12-31 13:31:11 +00:00
|
|
|
let epi::backend::AppOutput {
|
2020-11-28 09:46:31 +00:00
|
|
|
quit: _, // Can't quit a web page
|
|
|
|
window_size: _, // Can't resize a web page
|
|
|
|
pixels_per_point: _, // Can't zoom from within the app (we respect the web browser's zoom level)
|
2020-10-17 21:54:46 +00:00
|
|
|
} = app_output;
|
2020-10-17 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok((egui_output, paint_jobs))
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
|
2020-12-18 21:34:48 +00:00
|
|
|
self.web_backend.paint(self.app.clear_color(), paint_jobs)
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 20:15:07 +00:00
|
|
|
/// Install event listeners to register different input events
|
2020-12-29 14:57:13 +00:00
|
|
|
/// and start running the given app.
|
2020-12-29 13:15:46 +00:00
|
|
|
pub fn start(canvas_id: &str, app: Box<dyn epi::App>) -> Result<AppRunnerRef, JsValue> {
|
2020-12-19 20:15:07 +00:00
|
|
|
let backend = WebBackend::new(canvas_id)?;
|
2021-01-02 13:42:43 +00:00
|
|
|
let mut runner = AppRunner::new(backend, app)?;
|
|
|
|
runner.warm_up()?;
|
2020-12-19 20:15:07 +00:00
|
|
|
start_runner(runner)
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
/// Install event listeners to register different input events
|
|
|
|
/// and starts running the given `AppRunner`.
|
2020-12-19 20:15:07 +00:00
|
|
|
fn start_runner(app_runner: AppRunner) -> Result<AppRunnerRef, JsValue> {
|
2020-07-23 16:54:16 +00:00
|
|
|
let runner_ref = AppRunnerRef(Arc::new(Mutex::new(app_runner)));
|
|
|
|
install_canvas_events(&runner_ref)?;
|
|
|
|
install_document_events(&runner_ref)?;
|
2020-11-18 00:07:32 +00:00
|
|
|
repaint_every_ms(&runner_ref, 1000)?; // just in case. TODO: make it a parameter
|
2020-07-23 16:54:16 +00:00
|
|
|
paint_and_schedule(runner_ref.clone())?;
|
|
|
|
Ok(runner_ref)
|
|
|
|
}
|