egui/docs/frontend.ts

129 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-12-23 19:06:40 +00:00
interface Vec2 {
x: number;
y: number;
}
2018-12-26 09:46:23 +00:00
/// What the integration gives to the gui.
interface RawInput {
/// Is the button currently down?
mouse_down: boolean;
/// Current position of the mouse in points.
2018-12-23 19:06:40 +00:00
mouse_pos: Vec2;
2018-12-26 09:46:23 +00:00
/// Size of the screen in points.
2018-12-23 19:06:40 +00:00
screen_size: Vec2;
}
// ----------------------------------------------------------------------------
// the `wasm_bindgen` global is set to the exports of the Rust module. Override with wasm-bindgen --no-modules-global
declare var wasm_bindgen: any;
// we'll defer our execution until the wasm is ready to go
function wasm_loaded() {
console.log(`wasm loaded`);
initialize();
}
// here we tell bindgen the path to the wasm file so it can start
// initialization and return to us a promise when it's done
2019-01-04 13:14:32 +00:00
wasm_bindgen("./emgui_wasm_bg.wasm")
.then(wasm_loaded)
.catch(console.error);
// ----------------------------------------------------------------------------
2019-01-04 13:14:32 +00:00
let g_webgl_painter = null;
2019-01-04 13:14:32 +00:00
function paint_gui(canvas, input: RawInput) {
2019-01-05 19:14:16 +00:00
if (g_webgl_painter === null) {
g_webgl_painter = wasm_bindgen.new_webgl_painter("canvas");
}
2019-01-05 19:14:16 +00:00
wasm_bindgen.paint_webgl(g_webgl_painter, JSON.stringify(input));
}
// ----------------------------------------------------------------------------
2018-12-26 09:46:23 +00:00
let g_mouse_pos = { x: -1000.0, y: -1000.0 };
let g_mouse_down = false;
2019-01-04 13:14:32 +00:00
function auto_resize_canvas(canvas) {
2019-01-05 19:14:16 +00:00
if (true) {
2019-01-04 13:14:32 +00:00
canvas.setAttribute("width", window.innerWidth);
canvas.setAttribute("height", window.innerHeight);
} else {
2019-01-05 19:14:16 +00:00
// TODO: this stuff
2019-01-04 13:14:32 +00:00
const pixels_per_point = window.devicePixelRatio || 1;
2018-12-27 23:51:40 +00:00
2019-01-04 13:14:32 +00:00
const ctx = canvas.getContext("2d");
ctx.scale(pixels_per_point, pixels_per_point);
2018-12-27 23:51:40 +00:00
2019-01-04 13:14:32 +00:00
canvas.setAttribute("width", window.innerWidth * pixels_per_point);
canvas.setAttribute("height", window.innerHeight * pixels_per_point);
}
}
2018-12-27 23:51:40 +00:00
2019-01-04 13:14:32 +00:00
function get_input(canvas): RawInput {
2018-12-26 09:46:23 +00:00
return {
mouse_down: g_mouse_down,
mouse_pos: g_mouse_pos,
screen_size: { x: canvas.width, y: canvas.height },
};
}
2018-12-27 23:51:40 +00:00
function mouse_pos_from_event(canvas, event): Vec2 {
const rect = canvas.getBoundingClientRect();
return {
2018-12-27 23:51:40 +00:00
x: event.clientX - rect.left,
y: event.clientY - rect.top,
};
}
function initialize() {
2018-12-27 23:51:40 +00:00
console.log(`window.devicePixelRatio: ${window.devicePixelRatio}`);
const canvas = document.getElementById("canvas");
2019-01-04 13:14:32 +00:00
auto_resize_canvas(canvas);
2018-12-27 23:51:40 +00:00
const repaint = () => paint_gui(canvas, get_input(canvas));
2019-01-04 13:14:32 +00:00
canvas.addEventListener("mousemove", event => {
g_mouse_pos = mouse_pos_from_event(canvas, event);
repaint();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mouseleave", event => {
g_mouse_pos = { x: -1000.0, y: -1000.0 };
repaint();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mousedown", event => {
g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = true;
repaint();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mouseup", event => {
g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = false;
repaint();
event.stopPropagation();
event.preventDefault();
});
2018-12-27 23:51:40 +00:00
window.addEventListener("load", repaint);
window.addEventListener("pagehide", repaint);
window.addEventListener("pageshow", repaint);
window.addEventListener("resize", repaint);
// setInterval(repaint, 16);
repaint();
}