2019-01-12 22:07:30 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-10 17:04:10 +00:00
|
|
|
#![warn(clippy::all)]
|
2019-01-12 22:07:30 +00:00
|
|
|
|
2019-02-09 22:00:07 +00:00
|
|
|
pub mod webgl;
|
2019-02-11 19:27:32 +00:00
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
pub struct WebInfo {
|
2020-07-18 16:35:17 +00:00
|
|
|
/// e.g. "#fragment" part of "www.example.com/index.html#fragment"
|
|
|
|
pub web_location_hash: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implement this and use `egui_web::AppRunner` to run your app.
|
|
|
|
pub trait App {
|
2020-07-18 17:40:24 +00:00
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, backend: &mut Backend, info: &WebInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
|
2020-07-18 22:44:06 +00:00
|
|
|
/// Only repaint when there are animations or input (mouse movement, keyboard input etc).
|
2020-07-18 17:40:24 +00:00
|
|
|
Reactive,
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct Backend {
|
2020-07-18 08:54:31 +00:00
|
|
|
ctx: Arc<egui::Context>,
|
2020-07-18 16:35:17 +00:00
|
|
|
painter: webgl::Painter,
|
2020-07-18 08:54:31 +00:00
|
|
|
frame_times: egui::MovementTracker<f32>,
|
|
|
|
frame_start: Option<f64>,
|
2020-07-18 17:40:24 +00:00
|
|
|
/// If true, paint at full framerate always.
|
|
|
|
/// If false, only paint on input.
|
|
|
|
run_mode: RunMode,
|
2020-07-18 21:56:37 +00:00
|
|
|
last_save_time: Option<f64>,
|
2020-07-18 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
impl Backend {
|
2020-07-18 17:40:24 +00:00
|
|
|
pub fn new(canvas_id: &str, run_mode: RunMode) -> Result<Backend, JsValue> {
|
2020-07-18 08:54:31 +00:00
|
|
|
let ctx = egui::Context::new();
|
|
|
|
load_memory(&ctx);
|
2020-07-18 16:35:17 +00:00
|
|
|
Ok(Backend {
|
2020-07-18 08:54:31 +00:00
|
|
|
ctx,
|
2020-07-18 16:35:17 +00:00
|
|
|
painter: webgl::Painter::new(canvas_id)?,
|
2020-07-18 08:54:31 +00:00
|
|
|
frame_times: egui::MovementTracker::new(1000, 1.0),
|
|
|
|
frame_start: None,
|
2020-07-18 17:40:24 +00:00
|
|
|
run_mode,
|
2020-07-18 21:56:37 +00:00
|
|
|
last_save_time: None,
|
2020-07-18 08:54:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
pub fn run_mode(&self) -> RunMode {
|
|
|
|
self.run_mode
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_run_mode(&mut self, run_mode: RunMode) {
|
|
|
|
self.run_mode = run_mode;
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:00:05 +00:00
|
|
|
/// id of the canvas html element containing the rendering
|
|
|
|
pub fn canvas_id(&self) -> &str {
|
2020-07-18 16:35:17 +00:00
|
|
|
self.painter.canvas_id()
|
2020-07-18 16:00:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
pub fn begin_frame(&mut self, raw_input: egui::RawInput) -> egui::Ui {
|
|
|
|
self.frame_start = Some(now_sec());
|
|
|
|
self.ctx.begin_frame(raw_input)
|
|
|
|
}
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
pub fn end_frame(&mut self) -> Result<(egui::Output, egui::PaintJobs), JsValue> {
|
2020-07-18 08:54:31 +00:00
|
|
|
let frame_start = self
|
|
|
|
.frame_start
|
|
|
|
.take()
|
|
|
|
.expect("unmatched calls to begin_frame/end_frame");
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
let (output, paint_jobs) = self.ctx.end_frame();
|
2020-07-18 08:54:31 +00:00
|
|
|
|
2020-07-18 21:56:37 +00:00
|
|
|
self.auto_save();
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
let now = now_sec();
|
|
|
|
self.frame_times.add(now, (now - frame_start) as f32);
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
Ok((output, paint_jobs))
|
2020-07-18 21:56:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
|
2020-07-18 21:56:37 +00:00
|
|
|
let bg_color = egui::color::TRANSPARENT; // Use background css color.
|
2020-07-18 22:01:13 +00:00
|
|
|
self.painter.paint_jobs(
|
2020-07-18 08:54:31 +00:00
|
|
|
bg_color,
|
2020-07-18 22:01:13 +00:00
|
|
|
paint_jobs,
|
2020-07-18 08:54:31 +00:00
|
|
|
self.ctx.texture(),
|
|
|
|
self.ctx.pixels_per_point(),
|
2020-07-18 21:56:37 +00:00
|
|
|
)
|
|
|
|
}
|
2020-07-18 08:54:31 +00:00
|
|
|
|
2020-07-18 21:56:37 +00:00
|
|
|
pub fn auto_save(&mut self) {
|
|
|
|
let now = now_sec();
|
2020-07-18 22:16:24 +00:00
|
|
|
let time_since_last_save = now - self.last_save_time.unwrap_or(std::f64::NEG_INFINITY);
|
2020-07-18 21:56:37 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-07-18 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn painter_debug_info(&self) -> String {
|
2020-07-18 16:35:17 +00:00
|
|
|
self.painter.debug_info()
|
2020-07-18 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// excludes painting
|
2020-07-18 16:35:17 +00:00
|
|
|
pub fn cpu_time(&self) -> f32 {
|
2020-07-18 08:54:31 +00:00
|
|
|
self.frame_times.average().unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fps(&self) -> f32 {
|
|
|
|
1.0 / self.frame_times.mean_time_interval().unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Data gathered between frames.
|
|
|
|
/// Is translated to `egui::RawInput` at the start of each frame.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WebInput {
|
|
|
|
pub mouse_pos: Option<egui::Pos2>,
|
|
|
|
pub mouse_down: bool, // TODO: which button
|
|
|
|
pub is_touch: bool,
|
|
|
|
pub scroll_delta: egui::Vec2,
|
|
|
|
pub events: Vec<egui::Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebInput {
|
|
|
|
pub fn new_frame(&mut self) -> egui::RawInput {
|
|
|
|
egui::RawInput {
|
|
|
|
mouse_down: self.mouse_down,
|
|
|
|
mouse_pos: self.mouse_pos,
|
|
|
|
scroll_delta: std::mem::take(&mut self.scroll_delta),
|
|
|
|
screen_size: screen_size().unwrap(),
|
|
|
|
pixels_per_point: Some(pixels_per_point()),
|
|
|
|
time: now_sec(),
|
|
|
|
seconds_since_midnight: Some(seconds_since_midnight()),
|
|
|
|
events: std::mem::take(&mut self.events),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct AppRunner {
|
|
|
|
pub backend: Backend,
|
|
|
|
pub web_input: WebInput,
|
|
|
|
pub app: Box<dyn App>,
|
2020-07-18 21:56:37 +00:00
|
|
|
pub needs_repaint: bool, // TODO: move
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppRunner {
|
2020-07-18 17:40:24 +00:00
|
|
|
pub fn new(backend: Backend, app: Box<dyn App>) -> Result<Self, JsValue> {
|
2020-07-18 16:35:17 +00:00
|
|
|
Ok(Self {
|
2020-07-18 17:40:24 +00:00
|
|
|
backend,
|
2020-07-18 16:35:17 +00:00
|
|
|
web_input: Default::default(),
|
|
|
|
app,
|
2020-07-18 21:56:37 +00:00
|
|
|
needs_repaint: true, // TODO: move
|
2020-07-18 16:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn canvas_id(&self) -> &str {
|
|
|
|
self.backend.canvas_id()
|
|
|
|
}
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
pub fn logic(&mut self) -> Result<(egui::Output, egui::PaintJobs), JsValue> {
|
2020-07-18 16:35:17 +00:00
|
|
|
resize_to_screen_size(self.backend.canvas_id());
|
|
|
|
|
|
|
|
let raw_input = self.web_input.new_frame();
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
let info = WebInfo {
|
2020-07-18 16:35:17 +00:00
|
|
|
web_location_hash: location_hash().unwrap_or_default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut ui = self.backend.begin_frame(raw_input);
|
2020-07-18 17:40:24 +00:00
|
|
|
self.app.ui(&mut ui, &mut self.backend, &info);
|
2020-07-18 22:01:13 +00:00
|
|
|
let (output, paint_jobs) = self.backend.end_frame()?;
|
2020-07-18 16:35:17 +00:00
|
|
|
handle_output(&output);
|
2020-07-18 22:01:13 +00:00
|
|
|
Ok((output, paint_jobs))
|
2020-07-18 21:56:37 +00:00
|
|
|
}
|
2020-07-18 16:35:17 +00:00
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
|
|
|
|
self.backend.paint(paint_jobs)
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Install event listeners to register different input events
|
|
|
|
/// and starts running the given `AppRunner`.
|
2020-07-18 17:40:24 +00:00
|
|
|
pub fn run(app_runner: AppRunner) -> Result<AppRunnerRef, JsValue> {
|
|
|
|
let runner_ref = AppRunnerRef(Arc::new(Mutex::new(app_runner)));
|
|
|
|
install_canvas_events(&runner_ref)?;
|
|
|
|
install_document_events(&runner_ref)?;
|
|
|
|
paint_and_schedule(runner_ref.clone())?;
|
|
|
|
Ok(runner_ref)
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Helpers to hide some of the verbosity of web_sys
|
|
|
|
|
|
|
|
pub fn console_log(s: String) {
|
|
|
|
web_sys::console::log_1(&s.into());
|
|
|
|
}
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
pub fn screen_size() -> Option<egui::Vec2> {
|
|
|
|
let window = web_sys::window()?;
|
|
|
|
Some(egui::Vec2::new(
|
|
|
|
window.inner_width().ok()?.as_f64()? as f32,
|
|
|
|
window.inner_height().ok()?.as_f64()? as f32,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
pub fn now_sec() -> f64 {
|
|
|
|
web_sys::window()
|
|
|
|
.expect("should have a Window")
|
|
|
|
.performance()
|
|
|
|
.expect("should have a Performance")
|
|
|
|
.now()
|
|
|
|
/ 1000.0
|
|
|
|
}
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
pub fn seconds_since_midnight() -> f64 {
|
|
|
|
let d = js_sys::Date::new_0();
|
|
|
|
let seconds = (d.get_hours() * 60 + d.get_minutes()) * 60 + d.get_seconds();
|
|
|
|
return seconds as f64 + 1e-3 * (d.get_milliseconds() as f64);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pixels_per_point() -> f32 {
|
|
|
|
let pixels_per_point = web_sys::window().unwrap().device_pixel_ratio() as f32;
|
|
|
|
if pixels_per_point > 0.0 && pixels_per_point.is_finite() {
|
|
|
|
pixels_per_point
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:00:05 +00:00
|
|
|
pub fn canvas_element(canvas_id: &str) -> Option<web_sys::HtmlCanvasElement> {
|
2020-07-18 08:54:31 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
let document = web_sys::window()?.document()?;
|
|
|
|
let canvas = document.get_element_by_id(canvas_id)?;
|
2020-07-18 16:00:05 +00:00
|
|
|
canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn canvas_element_or_die(canvas_id: &str) -> web_sys::HtmlCanvasElement {
|
|
|
|
crate::canvas_element(canvas_id)
|
|
|
|
.unwrap_or_else(|| panic!("Failed to find canvas with id '{}'", canvas_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pos_from_mouse_event(canvas_id: &str, event: &web_sys::MouseEvent) -> egui::Pos2 {
|
|
|
|
let canvas = canvas_element(canvas_id).unwrap();
|
|
|
|
let rect = canvas.get_bounding_client_rect();
|
|
|
|
egui::Pos2 {
|
|
|
|
x: event.client_x() as f32 - rect.left() as f32,
|
|
|
|
y: event.client_y() as f32 - rect.top() as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pos_from_touch_event(event: &web_sys::TouchEvent) -> egui::Pos2 {
|
|
|
|
let t = event.touches().get(0).unwrap();
|
|
|
|
egui::Pos2 {
|
|
|
|
x: t.page_x() as f32,
|
|
|
|
y: t.page_y() as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resize_to_screen_size(canvas_id: &str) -> Option<()> {
|
|
|
|
let canvas = canvas_element(canvas_id)?;
|
2020-07-18 08:54:31 +00:00
|
|
|
|
|
|
|
let screen_size = screen_size()?;
|
|
|
|
let pixels_per_point = pixels_per_point();
|
|
|
|
canvas
|
|
|
|
.style()
|
|
|
|
.set_property("width", &format!("{}px", screen_size.x))
|
|
|
|
.ok()?;
|
|
|
|
canvas
|
|
|
|
.style()
|
|
|
|
.set_property("height", &format!("{}px", screen_size.y))
|
|
|
|
.ok()?;
|
|
|
|
canvas.set_width((screen_size.x * pixels_per_point).round() as u32);
|
|
|
|
canvas.set_height((screen_size.y * pixels_per_point).round() as u32);
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
pub fn local_storage() -> Option<web_sys::Storage> {
|
|
|
|
web_sys::window()?.local_storage().ok()?
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn local_storage_get(key: &str) -> Option<String> {
|
|
|
|
local_storage().map(|storage| storage.get_item(key).ok())??
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn local_storage_set(key: &str, value: &str) {
|
|
|
|
local_storage().map(|storage| storage.set_item(key, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn local_storage_remove(key: &str) {
|
|
|
|
local_storage().map(|storage| storage.remove_item(key));
|
|
|
|
}
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
pub fn load_memory(ctx: &egui::Context) {
|
|
|
|
if let Some(memory_string) = local_storage_get("egui_memory_json") {
|
2020-05-02 09:37:12 +00:00
|
|
|
match serde_json::from_str(&memory_string) {
|
|
|
|
Ok(memory) => {
|
|
|
|
*ctx.memory() = memory;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
console_log(format!("ERROR: Failed to parse memory json: {}", err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
pub fn save_memory(ctx: &egui::Context) {
|
2020-05-02 09:37:12 +00:00
|
|
|
match serde_json::to_string(&*ctx.memory()) {
|
|
|
|
Ok(json) => {
|
2020-05-30 08:22:35 +00:00
|
|
|
local_storage_set("egui_memory_json", &json);
|
2020-05-02 09:37:12 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
console_log(format!(
|
|
|
|
"ERROR: Failed to seriealize memory as json: {}",
|
|
|
|
err
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 08:54:31 +00:00
|
|
|
|
|
|
|
pub fn handle_output(output: &egui::Output) {
|
|
|
|
set_cursor_icon(output.cursor_icon);
|
|
|
|
if let Some(url) = &output.open_url {
|
|
|
|
open_url(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> {
|
|
|
|
let document = web_sys::window()?.document()?;
|
|
|
|
document
|
|
|
|
.body()?
|
|
|
|
.style()
|
|
|
|
.set_property("cursor", cursor_web_name(cursor))
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cursor_web_name(cursor: egui::CursorIcon) -> &'static str {
|
|
|
|
use egui::CursorIcon::*;
|
|
|
|
match cursor {
|
|
|
|
Default => "default",
|
|
|
|
PointingHand => "pointer",
|
|
|
|
ResizeHorizontal => "ew-resize",
|
|
|
|
ResizeNeSw => "nesw-resize",
|
|
|
|
ResizeNwSe => "nwse-resize",
|
|
|
|
ResizeVertical => "ns-resize",
|
|
|
|
Text => "text",
|
|
|
|
// "no-drop"
|
|
|
|
// "not-allowed"
|
|
|
|
// default, help, pointer, progress, wait, cell, crosshair, text, alias, copy, move, grab, grabbing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open_url(url: &str) -> Option<()> {
|
|
|
|
web_sys::window()?
|
|
|
|
.open_with_url_and_target(url, "_self")
|
|
|
|
.ok()?;
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// e.g. "#fragment" part of "www.example.com/index.html#fragment"
|
|
|
|
pub fn location_hash() -> Option<String> {
|
|
|
|
web_sys::window()?.location().hash().ok()
|
|
|
|
}
|
2020-07-18 16:00:05 +00:00
|
|
|
|
|
|
|
pub fn translate_key(key: &str) -> Option<egui::Key> {
|
|
|
|
match key {
|
|
|
|
"Alt" => Some(egui::Key::Alt),
|
|
|
|
"Backspace" => Some(egui::Key::Backspace),
|
|
|
|
"Control" => Some(egui::Key::Control),
|
|
|
|
"Delete" => Some(egui::Key::Delete),
|
|
|
|
"ArrowDown" => Some(egui::Key::Down),
|
|
|
|
"End" => Some(egui::Key::End),
|
|
|
|
"Escape" => Some(egui::Key::Escape),
|
|
|
|
"Home" => Some(egui::Key::Home),
|
|
|
|
"Help" => Some(egui::Key::Insert),
|
|
|
|
"ArrowLeft" => Some(egui::Key::Left),
|
|
|
|
"Meta" => Some(egui::Key::Logo),
|
|
|
|
"PageDown" => Some(egui::Key::PageDown),
|
|
|
|
"PageUp" => Some(egui::Key::PageUp),
|
|
|
|
"Enter" => Some(egui::Key::Return),
|
|
|
|
"ArrowRight" => Some(egui::Key::Right),
|
|
|
|
"Shift" => Some(egui::Key::Shift),
|
|
|
|
"Tab" => Some(egui::Key::Tab),
|
|
|
|
"ArrowUp" => Some(egui::Key::Up),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 16:35:17 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppRunnerRef(Arc<Mutex<AppRunner>>);
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
fn paint_and_schedule(runner_ref: AppRunnerRef) -> Result<(), JsValue> {
|
2020-07-18 22:44:06 +00:00
|
|
|
fn paint_if_needed(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
2020-07-18 21:56:37 +00:00
|
|
|
if runner_lock.backend.run_mode() == RunMode::Continuous || runner_lock.needs_repaint {
|
|
|
|
runner_lock.needs_repaint = false;
|
2020-07-18 22:01:13 +00:00
|
|
|
let (output, paint_jobs) = runner_lock.logic()?;
|
|
|
|
runner_lock.paint(paint_jobs)?;
|
2020-07-18 22:44:06 +00:00
|
|
|
runner_lock.needs_repaint |= output.needs_repaint;
|
2020-07-18 21:56:37 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
2020-07-18 17:40:24 +00:00
|
|
|
|
2020-07-18 21:56:37 +00:00
|
|
|
fn request_animation_frame(runner_ref: AppRunnerRef) -> Result<(), JsValue> {
|
2020-07-18 17:40:24 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let closure = Closure::once(move || paint_and_schedule(runner_ref));
|
|
|
|
window.request_animation_frame(closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget(); // We must forget it, or else the callback is canceled on drop
|
2020-07-18 21:56:37 +00:00
|
|
|
Ok(())
|
2020-07-18 17:40:24 +00:00
|
|
|
}
|
2020-07-18 16:35:17 +00:00
|
|
|
|
2020-07-18 22:44:06 +00:00
|
|
|
paint_if_needed(&runner_ref)?;
|
2020-07-18 17:40:24 +00:00
|
|
|
request_animation_frame(runner_ref)
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
|
2020-07-18 16:35:17 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
let document = web_sys::window().unwrap().document().unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
// keydown
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
2020-07-18 16:35:17 +00:00
|
|
|
let key = event.key();
|
|
|
|
if let Some(key) = translate_key(&key) {
|
2020-07-18 17:40:24 +00:00
|
|
|
runner_lock
|
2020-07-18 16:35:17 +00:00
|
|
|
.web_input
|
|
|
|
.events
|
|
|
|
.push(egui::Event::Key { key, pressed: true });
|
|
|
|
} else {
|
2020-07-18 17:40:24 +00:00
|
|
|
runner_lock.web_input.events.push(egui::Event::Text(key));
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
document.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// keyup
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
2020-07-18 16:35:17 +00:00
|
|
|
let key = event.key();
|
|
|
|
if let Some(key) = translate_key(&key) {
|
2020-07-18 17:40:24 +00:00
|
|
|
runner_lock.web_input.events.push(egui::Event::Key {
|
2020-07-18 16:35:17 +00:00
|
|
|
key,
|
|
|
|
pressed: false,
|
|
|
|
});
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
}
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
document.add_event_listener_with_callback("keyup", closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
for event_name in &["load", "pagehide", "pageshow", "resize"] {
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move || {
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_ref.0.lock().needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
}) as Box<dyn FnMut()>);
|
|
|
|
document.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
fn install_canvas_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
|
2020-07-18 16:35:17 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
2020-07-18 17:40:24 +00:00
|
|
|
let canvas = canvas_element(runner_ref.0.lock().canvas_id()).unwrap();
|
2020-07-18 16:35:17 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "mousedown";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
if !runner_lock.web_input.is_touch {
|
|
|
|
runner_lock.web_input.mouse_pos =
|
|
|
|
Some(pos_from_mouse_event(runner_lock.canvas_id(), &event));
|
|
|
|
runner_lock.web_input.mouse_down = true;
|
2020-07-18 22:44:06 +00:00
|
|
|
runner_lock.logic().unwrap(); // in case we get "mouseup" the same frame. TODO: handle via events instead
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "mousemove";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
if !runner_lock.web_input.is_touch {
|
|
|
|
runner_lock.web_input.mouse_pos =
|
|
|
|
Some(pos_from_mouse_event(runner_lock.canvas_id(), &event));
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "mouseup";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
if !runner_lock.web_input.is_touch {
|
|
|
|
runner_lock.web_input.mouse_pos =
|
|
|
|
Some(pos_from_mouse_event(runner_lock.canvas_id(), &event));
|
|
|
|
runner_lock.web_input.mouse_down = false;
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "mouseleave";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
if !runner_lock.web_input.is_touch {
|
|
|
|
runner_lock.web_input.mouse_pos = None;
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "touchstart";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
runner_lock.web_input.is_touch = true;
|
|
|
|
runner_lock.web_input.mouse_pos = Some(pos_from_touch_event(&event));
|
|
|
|
runner_lock.web_input.mouse_down = true;
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "touchmove";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
runner_lock.web_input.is_touch = true;
|
|
|
|
runner_lock.web_input.mouse_pos = Some(pos_from_touch_event(&event));
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "touchend";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
runner_lock.web_input.is_touch = true;
|
|
|
|
runner_lock.web_input.mouse_down = false; // First release mouse to click...
|
2020-07-18 22:44:06 +00:00
|
|
|
runner_lock.logic().unwrap(); // ...do the clicking... (TODO: handle via events instead)
|
2020-07-18 17:40:24 +00:00
|
|
|
runner_lock.web_input.mouse_pos = None; // ...remove hover effect
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_name = "wheel";
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner_ref = runner_ref.clone();
|
2020-07-18 16:35:17 +00:00
|
|
|
let closure = Closure::wrap(Box::new(move |event: web_sys::WheelEvent| {
|
2020-07-18 17:40:24 +00:00
|
|
|
let mut runner_lock = runner_ref.0.lock();
|
|
|
|
runner_lock.web_input.scroll_delta.x -= event.delta_x() as f32;
|
|
|
|
runner_lock.web_input.scroll_delta.y -= event.delta_y() as f32;
|
2020-07-18 21:56:37 +00:00
|
|
|
runner_lock.needs_repaint = true;
|
2020-07-18 16:35:17 +00:00
|
|
|
event.stop_propagation();
|
|
|
|
event.prevent_default();
|
|
|
|
}) as Box<dyn FnMut(_)>);
|
|
|
|
canvas.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
|
|
|
|
closure.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|