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
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Helpers to hide some of the verbosity of web_sys
|
|
|
|
|
|
|
|
pub fn console_log(s: String) {
|
|
|
|
web_sys::console::log_1(&s.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn now_sec() -> f64 {
|
|
|
|
web_sys::window()
|
|
|
|
.expect("should have a Window")
|
|
|
|
.performance()
|
|
|
|
.expect("should have a Performance")
|
|
|
|
.now()
|
|
|
|
/ 1000.0
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|