2020-07-23 16:54:16 +00:00
|
|
|
use crate::*;
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
pub use egui::{
|
2020-10-17 10:33:30 +00:00
|
|
|
app::{App, WebInfo},
|
2020-10-17 21:54:46 +00:00
|
|
|
pos2, Srgba,
|
2020-09-11 06:56:47 +00:00
|
|
|
};
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct WebBackend {
|
|
|
|
ctx: Arc<egui::Context>,
|
|
|
|
painter: webgl::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>,
|
|
|
|
last_save_time: Option<f64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebBackend {
|
2020-09-16 06:03:40 +00:00
|
|
|
pub fn new(canvas_id: &str) -> Result<Self, JsValue> {
|
2020-07-23 16:54:16 +00:00
|
|
|
let ctx = egui::Context::new();
|
|
|
|
load_memory(&ctx);
|
|
|
|
Ok(Self {
|
|
|
|
ctx,
|
|
|
|
painter: webgl::Painter::new(canvas_id)?,
|
2020-10-01 20:25:44 +00:00
|
|
|
previous_frame_time: None,
|
2020-07-23 16:54:16 +00:00
|
|
|
frame_start: None,
|
|
|
|
last_save_time: 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");
|
|
|
|
|
2020-11-07 10:44:32 +00:00
|
|
|
let (output, paint_commands) = self.ctx.end_frame();
|
|
|
|
let paint_jobs = self.ctx.tesselate(paint_commands);
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
self.auto_save();
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
|
|
|
|
let bg_color = egui::color::TRANSPARENT; // Use background css color.
|
|
|
|
self.painter.paint_jobs(
|
|
|
|
bg_color,
|
|
|
|
paint_jobs,
|
2020-09-09 12:24:13 +00:00
|
|
|
&self.ctx.texture(),
|
2020-07-23 16:54:16 +00:00
|
|
|
self.ctx.pixels_per_point(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn auto_save(&mut self) {
|
|
|
|
let now = now_sec();
|
|
|
|
let time_since_last_save = now - self.last_save_time.unwrap_or(std::f64::NEG_INFINITY);
|
|
|
|
const AUTO_SAVE_INTERVAL: f64 = 5.0;
|
|
|
|
if time_since_last_save > AUTO_SAVE_INTERVAL {
|
|
|
|
self.last_save_time = Some(now);
|
|
|
|
save_memory(&self.ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn painter_debug_info(&self) -> String {
|
|
|
|
self.painter.debug_info()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
impl egui::app::TextureAllocator for webgl::Painter {
|
2020-11-18 20:38:29 +00:00
|
|
|
fn alloc(&mut self) -> egui::TextureId {
|
|
|
|
self.alloc_user_texture()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_srgba_premultiplied(
|
2020-09-11 06:56:47 +00:00
|
|
|
&mut self,
|
2020-11-18 20:38:29 +00:00
|
|
|
id: egui::TextureId,
|
2020-09-11 06:56:47 +00:00
|
|
|
size: (usize, usize),
|
2020-11-18 20:38:29 +00:00
|
|
|
srgba_pixels: &[Srgba],
|
|
|
|
) {
|
|
|
|
self.set_user_texture(id, size, srgba_pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn free(&mut self, id: egui::TextureId) {
|
|
|
|
self.free_user_texture(id)
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// 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-11-28 09:46:31 +00:00
|
|
|
pub fn new_frame(&mut self) -> egui::RawInput {
|
2020-07-23 16:54:16 +00:00
|
|
|
egui::RawInput {
|
2020-11-28 09:46:31 +00:00
|
|
|
screen_size: screen_size_in_native_points().unwrap(),
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl egui::app::RepaintSignal for NeedRepaint {
|
|
|
|
fn request_repaint(&self) {
|
|
|
|
self.0.store(true, SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub struct AppRunner {
|
|
|
|
pub web_backend: WebBackend,
|
2020-11-15 13:21:21 +00:00
|
|
|
pub input: WebInput,
|
2020-07-23 16:54:16 +00:00
|
|
|
pub app: Box<dyn App>,
|
2020-11-20 19:35:16 +00:00
|
|
|
pub needs_repaint: std::sync::Arc<NeedRepaint>,
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppRunner {
|
2020-10-31 17:03:13 +00:00
|
|
|
pub fn new(web_backend: WebBackend, mut app: Box<dyn App>) -> Result<Self, JsValue> {
|
|
|
|
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-07-23 16:54:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn canvas_id(&self) -> &str {
|
|
|
|
self.web_backend.canvas_id()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-23 16:54:16 +00:00
|
|
|
|
2020-11-28 09:46:31 +00:00
|
|
|
let raw_input = self.input.new_frame();
|
2020-10-24 17:23:16 +00:00
|
|
|
self.web_backend.begin_frame(raw_input);
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2020-10-24 17:23:16 +00:00
|
|
|
let mut integration_context = egui::app::IntegrationContext {
|
|
|
|
info: egui::app::IntegrationInfo {
|
|
|
|
web_info: Some(WebInfo {
|
|
|
|
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()),
|
|
|
|
},
|
|
|
|
tex_allocator: Some(&mut self.web_backend.painter),
|
|
|
|
output: Default::default(),
|
2020-11-20 19:35:16 +00:00
|
|
|
repaint_signal: self.needs_repaint.clone(),
|
2020-10-17 10:33:30 +00:00
|
|
|
};
|
|
|
|
|
2020-10-24 08:56:23 +00:00
|
|
|
let egui_ctx = &self.web_backend.ctx;
|
2020-10-24 17:23:16 +00:00
|
|
|
self.app.ui(egui_ctx, &mut integration_context);
|
|
|
|
let app_output = integration_context.output;
|
2020-10-17 10:33:30 +00:00
|
|
|
let (egui_output, paint_jobs) = self.web_backend.end_frame()?;
|
|
|
|
handle_output(&egui_output);
|
|
|
|
|
|
|
|
{
|
2020-10-17 21:54:46 +00:00
|
|
|
let egui::app::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> {
|
|
|
|
self.web_backend.paint(paint_jobs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Install event listeners to register different input events
|
|
|
|
/// and starts running the given `AppRunner`.
|
2020-10-17 10:33:30 +00:00
|
|
|
pub fn start(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)
|
|
|
|
}
|