egui/src/lib.rs

69 lines
1.6 KiB
Rust
Raw Normal View History

2018-12-26 16:01:46 +00:00
#![deny(warnings)]
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
2018-12-26 16:01:46 +00:00
#[macro_use] // TODO: get rid of this
extern crate serde_derive;
2018-12-23 19:06:40 +00:00
use std::sync::Mutex;
use wasm_bindgen::prelude::*;
2018-12-26 16:01:46 +00:00
use crate::{math::Vec2, types::*};
2018-12-26 09:46:23 +00:00
pub mod app;
pub mod gui;
2018-12-26 16:01:46 +00:00
pub mod math;
2018-12-26 13:38:46 +00:00
pub mod style;
2018-12-26 09:46:23 +00:00
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 16:32:58 +00:00
static ref APP: Mutex<app::App> = Mutex::new(app::App::new());
2018-12-26 09:46:23 +00:00
static ref LAST_INPUT: Mutex<RawInput> = Default::default();
2018-12-26 14:28:38 +00:00
static ref GUI_STATE: Mutex<gui::GuiState> = 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 14:28:38 +00:00
let mut gui = gui::Gui {
commands: Vec::new(),
cursor: Vec2 { x: 32.0, y: 32.0 },
input: gui_input,
state: *GUI_STATE.lock().unwrap(),
};
if !gui_input.mouse_down {
gui.state.active_id = None;
}
2018-12-26 09:46:23 +00:00
APP.lock().unwrap().show_gui(&mut gui);
2018-12-26 14:28:38 +00:00
*GUI_STATE.lock().unwrap() = gui.state;
let commands = style::into_paint_commands(gui.gui_commands());
serde_json::to_string(&commands).unwrap()
}