file stuff

This commit is contained in:
kirjavascript 2022-06-07 17:29:52 +01:00
parent 550d1cf642
commit 440b60d1ec
6 changed files with 129 additions and 87 deletions

View file

@ -40,7 +40,7 @@ CRATE_NAME_SNAKE_CASE="${CRATE_NAME//-/_}" # for those who name crates with-keba
export RUSTFLAGS=--cfg=web_sys_unstable_apis
# Clear output from old stuff:
rm -f "docs/${CRATE_NAME_SNAKE_CASE}_bg.wasm"
rm -f "static/${CRATE_NAME_SNAKE_CASE}_bg.wasm"
echo "Building rust…"
BUILD=release
@ -52,16 +52,16 @@ TARGET=$(cargo metadata --format-version=1 | jq --raw-output .target_directory)
echo "Generating JS bindings for wasm…"
TARGET_NAME="${CRATE_NAME_SNAKE_CASE}.wasm"
WASM_PATH="${TARGET}/wasm32-unknown-unknown/${BUILD}/${TARGET_NAME}"
wasm-bindgen "${WASM_PATH}" --out-dir docs --no-modules --no-typescript
wasm-bindgen "${WASM_PATH}" --out-dir static --no-modules --no-typescript
if [[ "${FAST}" == false ]]; then
echo "Optimizing wasm…"
# to get wasm-opt: apt/brew/dnf install binaryen
# https://github.com/WebAssembly/binaryen/releases
./wasm-opt "docs/${CRATE_NAME}_bg.wasm" -O2 --fast-math -o "docs/${CRATE_NAME}_bg.wasm" # add -g to get debug symbols
./wasm-opt "static/${CRATE_NAME}_bg.wasm" -O2 --fast-math -o "static/${CRATE_NAME}_bg.wasm" # add -g to get debug symbols
fi
echo "Finished: docs/${CRATE_NAME_SNAKE_CASE}.wasm"
echo "Finished: static/${CRATE_NAME_SNAKE_CASE}.wasm"
if [[ "${OPEN}" == true ]]; then
if [[ "$OSTYPE" == "linux-gnu"* ]]; then

View file

@ -1,3 +0,0 @@
This folder contains the files required for the web app.
The reason the folder is called "docs" is because that is the name that GitHub requires in order to host a web page from the `master` branch of a repository. You can test the `frontend` at <https://emilk.github.io/frontend/>.

View file

@ -1,11 +1,11 @@
use emu::Megadrive;
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
// #[derive(serde::Deserialize, serde::Serialize)]
// #[serde(default)] // if we add new fields, give them default values when deserializing old state
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct Frontend {
// Example stuff:
// #[serde(skip)]
#[serde(skip)]
emu: Megadrive,
fullscreen: bool,
tmp_zoom: f32,
@ -24,10 +24,21 @@ impl Default for Frontend {
}
impl Frontend {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customized the look at feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
cc.egui_ctx.set_visuals(egui::Visuals {
dark_mode: true,
..egui::Visuals::default()
});
// let mut fonts = egui::FontDefinitions::default();
// fonts
// .families
// .entry(egui::FontFamily::Monospace)
// .or_default()
// .push("Hack".to_string());
// cc.egui_ctx.set_fonts(fonts);
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
@ -35,54 +46,55 @@ impl Frontend {
// return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
// }
Default::default()
}
}
impl eframe::App for Frontend {
/// Called by the frame work to save state before shutdown.
// fn save(&mut self, storage: &mut dyn eframe::Storage) {
// eframe::set_value(storage, eframe::APP_KEY, self);
// }
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, self);
}
/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
if self.fullscreen {
// egui::CentralPanel::default().show(ctx, |ui| {
// ctx.request_repaint();
egui::CentralPanel::default().show(ctx, |ui| {
ctx.request_repaint();
// let response = ui.interact(
// egui::Rect::EVERYTHING,
// ui.id(),
// egui::Sense::click()
// );
// if response.double_clicked() {
// self.fullscreen = false;
// }
let response = ui.interact(
egui::Rect::EVERYTHING,
ui.id(),
egui::Sense::click()
);
if response.double_clicked() {
self.fullscreen = false;
}
// self.emu.frame(true);
// let pixels = self.emu.gfx.screen.chunks_exact(3)
// .map(|p| egui::Color32::from_rgba_unmultiplied(p[0], p[1], p[2], 255))
// .collect();
// let texture: &egui::TextureHandle = &ui.ctx().load_texture(
// "viewport",
// egui::ColorImage {
// size: [320, 240],
// pixels,
// },
// );
// let img_size = ui.available_height() * texture.size_vec2() / texture.size_vec2().y;
self.emu.frame(true);
let pixels = self.emu.gfx.screen.chunks_exact(3)
.map(|p| egui::Color32::from_rgba_unmultiplied(p[0], p[1], p[2], 255))
.collect();
let texture: &egui::TextureHandle = &ui.ctx().load_texture(
"viewport",
egui::ColorImage {
size: [320, 240],
pixels,
},
);
let img_size = ui.available_height() * texture.size_vec2() / texture.size_vec2().y;
// // let mut size = egui::Vec2::new(image.size[0] as f32, image.size[1] as f32);
// // size *= (ui.available_width() / size.x).min(1.0);
// // ui.image(texture_id, size);
// let mut size = egui::Vec2::new(image.size[0] as f32, image.size[1] as f32);
// size *= (ui.available_width() / size.x).min(1.0);
// ui.image(texture_id, size);
// ui.image(texture, img_size);
// });
ui.image(texture, img_size);
});
// return
return
}
// ui.heading("Side Panel");
@ -99,6 +111,30 @@ impl eframe::App for Frontend {
// value += 1.0;
// }
#[cfg(target_arch = "wasm32")]
egui::Window::new("file input").show(ctx, |ui| {
use eframe::{wasm_bindgen, web_sys};
use wasm_bindgen::JsCast;
let text_agent: web_sys::HtmlInputElement = web_sys::window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("egui_text_agent")
.unwrap()
.dyn_into()
.unwrap();
text_agent.set_type("file");
// file_agent / widget
if ui.button("file").clicked() {
text_agent.click();
}
});
egui::Window::new("test").show(ctx, |ui| {
let texture: &egui::TextureHandle = &ui.ctx().load_texture(
@ -138,7 +174,6 @@ impl eframe::App for Frontend {
self.fullscreen = true;
}
// TODO: blurryness https://github.com/emilk/egui/pull/1636
self.emu.frame(true);
let pixels = self.emu.gfx.screen.chunks_exact(3)
@ -150,6 +185,7 @@ impl eframe::App for Frontend {
size: [320, 240],
pixels,
},
// TODO: blurryness https://github.com/emilk/egui/pull/1636
// egui::TextureFilter::Nearest
);
let img_size = self.tmp_zoom * texture.size_vec2() / texture.size_vec2().y;

View file

@ -23,6 +23,31 @@ function takeObject(idx) {
return ret;
}
const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachegetUint8Memory0 = null;
function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
function isLikeNone(x) {
return x === undefined || x === null;
}
@ -45,14 +70,6 @@ function getInt32Memory0() {
let WASM_VECTOR_LEN = 0;
let cachegetUint8Memory0 = null;
function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory0;
}
const cachedTextEncoder = new TextEncoder('utf-8');
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
@ -106,23 +123,6 @@ function passStringToWasm0(arg, malloc, realloc) {
return ptr;
}
const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
function debugString(val) {
// primitive types
const type = typeof val;
@ -316,6 +316,13 @@ async function init(input) {
}
const imports = {};
imports.wbg = {};
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbg_log_02e20a3c32305fb7 = function(arg0, arg1) {
try {
console.log(getStringFromWasm0(arg0, arg1));
@ -333,9 +340,6 @@ async function init(input) {
imports.wbg.__wbg_mark_abc7631bdced64f0 = function(arg0, arg1) {
performance.mark(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbg_measure_c528ff64085b7146 = function() { return handleError(function (arg0, arg1, arg2, arg3) {
try {
performance.measure(getStringFromWasm0(arg0, arg1), getStringFromWasm0(arg2, arg3));
@ -385,10 +389,6 @@ async function init(input) {
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_boolean_get = function(arg0) {
const v = getObject(arg0);
const ret = typeof(v) === 'boolean' ? (v ? 1 : 0) : 2;
@ -706,6 +706,9 @@ async function init(input) {
imports.wbg.__wbg_setsize_1d5b32171e4916c6 = function(arg0, arg1) {
getObject(arg0).size = arg1 >>> 0;
};
imports.wbg.__wbg_settype_69fae83a51e7f4fa = function(arg0, arg1, arg2) {
getObject(arg0).type = getStringFromWasm0(arg1, arg2);
};
imports.wbg.__wbg_value_14b43f7df5bd6160 = function(arg0, arg1) {
const ret = getObject(arg1).value;
const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@ -1061,6 +1064,9 @@ async function init(input) {
const ret = getObject(arg0).getBoundingClientRect();
return addHeapObject(ret);
};
imports.wbg.__wbg_log_e8ba7b992c7ad0eb = function(arg0) {
console.log(getObject(arg0));
};
imports.wbg.__wbg_scrollTop_f1031b88f039d8df = function(arg0) {
const ret = getObject(arg0).scrollTop;
return ret;
@ -1091,6 +1097,9 @@ async function init(input) {
imports.wbg.__wbg_blur_685fb8c8baa471a1 = function() { return handleError(function (arg0) {
getObject(arg0).blur();
}, arguments) };
imports.wbg.__wbg_click_e2237d337e1f3b29 = function(arg0) {
getObject(arg0).click();
};
imports.wbg.__wbg_focus_42ad8e77a7a0b22a = function() { return handleError(function (arg0) {
getObject(arg0).focus();
}, arguments) };
@ -1342,24 +1351,24 @@ async function init(input) {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper812 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 204, __wbg_adapter_24);
imports.wbg.__wbindgen_closure_wrapper840 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 225, __wbg_adapter_24);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper814 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 204, __wbg_adapter_27);
imports.wbg.__wbindgen_closure_wrapper842 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 225, __wbg_adapter_27);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper816 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 204, __wbg_adapter_30);
imports.wbg.__wbindgen_closure_wrapper844 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 225, __wbg_adapter_30);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper818 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 204, __wbg_adapter_33);
imports.wbg.__wbindgen_closure_wrapper846 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 225, __wbg_adapter_33);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper1031 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 327, __wbg_adapter_36);
imports.wbg.__wbindgen_closure_wrapper1059 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 347, __wbg_adapter_36);
return addHeapObject(ret);
};