egui/src/lib.rs

53 lines
1.1 KiB
Rust
Raw Normal View History

2018-12-23 19:06:40 +00:00
extern crate lazy_static;
extern crate serde;
extern crate serde_json;
extern crate wasm_bindgen;
extern crate web_sys;
2018-12-23 19:06:40 +00:00
#[macro_use]
extern crate serde_derive;
2018-12-23 19:06:40 +00:00
use std::sync::Mutex;
use wasm_bindgen::prelude::*;
2018-12-23 19:06:40 +00:00
use types::*;
2018-12-26 09:46:23 +00:00
pub mod app;
pub mod gui;
pub mod types;
2018-12-23 23:15:18 +00:00
/*
// Fast compilation, slow code:
fn foo(x: &dyn Trait);
// Fast code, slow compilation:
fn foo<T: Trait>(x: &dyn T);
// Compiles quickly in debug, fast in release:
#[dynimp(Trait)]
fn foo(x: &Trait);
*/
#[wasm_bindgen]
2018-12-26 09:46:23 +00:00
pub fn show_gui(raw_input_json: &str) -> String {
// TODO: faster interface than JSON
let raw_input: RawInput = serde_json::from_str(raw_input_json).unwrap();
2018-12-23 19:06:40 +00:00
lazy_static::lazy_static! {
2018-12-26 09:46:23 +00:00
static ref APP: Mutex<app::App> = Mutex::new(app::App::new());
static ref LAST_INPUT: Mutex<RawInput> = Default::default();
2018-12-23 19:06:40 +00:00
}
2018-12-26 09:46:23 +00:00
let gui_input = GuiInput::from_last_and_new(&LAST_INPUT.lock().unwrap(), &raw_input);
*LAST_INPUT.lock().unwrap() = raw_input;
2018-12-23 23:15:18 +00:00
2018-12-26 09:46:23 +00:00
let mut gui = gui::Gui::new(gui_input);
APP.lock().unwrap().show_gui(&mut gui);
2018-12-23 23:15:18 +00:00
let commands = gui.into_commands();
serde_json::to_string(&commands).unwrap()
}