2018-12-23 18:42:30 +00:00
|
|
|
// 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")
|
2018-12-23 18:42:30 +00:00
|
|
|
.then(wasm_loaded)["catch"](console.error);
|
|
|
|
// ----------------------------------------------------------------------------
|
2019-01-04 13:14:32 +00:00
|
|
|
var g_webgl_painter = null;
|
2018-12-26 09:46:23 +00:00
|
|
|
function paint_gui(canvas, input) {
|
2019-01-05 19:14:16 +00:00
|
|
|
if (g_webgl_painter === null) {
|
|
|
|
g_webgl_painter = wasm_bindgen.new_webgl_painter("canvas");
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
2019-01-05 19:14:16 +00:00
|
|
|
wasm_bindgen.paint_webgl(g_webgl_painter, JSON.stringify(input));
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-26 09:46:23 +00:00
|
|
|
var g_mouse_pos = { x: -1000.0, y: -1000.0 };
|
|
|
|
var 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
|
|
|
var pixels_per_point = window.devicePixelRatio || 1;
|
|
|
|
var ctx = canvas.getContext("2d");
|
|
|
|
ctx.scale(pixels_per_point, pixels_per_point);
|
|
|
|
canvas.setAttribute("width", window.innerWidth * pixels_per_point);
|
|
|
|
canvas.setAttribute("height", window.innerHeight * pixels_per_point);
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 09:46:23 +00:00
|
|
|
function get_input(canvas) {
|
|
|
|
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) {
|
2018-12-23 18:42:30 +00:00
|
|
|
var rect = canvas.getBoundingClientRect();
|
|
|
|
return {
|
2018-12-27 23:51:40 +00:00
|
|
|
x: event.clientX - rect.left,
|
|
|
|
y: event.clientY - rect.top
|
2018-12-23 18:42:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function initialize() {
|
2018-12-27 23:51:40 +00:00
|
|
|
console.log("window.devicePixelRatio: " + window.devicePixelRatio);
|
2018-12-23 18:42:30 +00:00
|
|
|
var canvas = document.getElementById("canvas");
|
2019-01-04 13:14:32 +00:00
|
|
|
auto_resize_canvas(canvas);
|
2018-12-27 23:51:40 +00:00
|
|
|
var repaint = function () { return paint_gui(canvas, get_input(canvas)); };
|
|
|
|
canvas.addEventListener("mousemove", function (event) {
|
|
|
|
g_mouse_pos = mouse_pos_from_event(canvas, event);
|
|
|
|
repaint();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
canvas.addEventListener("mouseleave", function (event) {
|
2018-12-26 09:46:23 +00:00
|
|
|
g_mouse_pos = { x: -1000.0, y: -1000.0 };
|
2018-12-27 23:51:40 +00:00
|
|
|
repaint();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
canvas.addEventListener("mousedown", function (event) {
|
|
|
|
g_mouse_pos = mouse_pos_from_event(canvas, event);
|
2018-12-26 09:46:23 +00:00
|
|
|
g_mouse_down = true;
|
2018-12-27 23:51:40 +00:00
|
|
|
repaint();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
canvas.addEventListener("mouseup", function (event) {
|
|
|
|
g_mouse_pos = mouse_pos_from_event(canvas, event);
|
2018-12-26 09:46:23 +00:00
|
|
|
g_mouse_down = false;
|
2018-12-27 23:51:40 +00:00
|
|
|
repaint();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
window.addEventListener("load", repaint);
|
|
|
|
window.addEventListener("pagehide", repaint);
|
|
|
|
window.addEventListener("pageshow", repaint);
|
|
|
|
window.addEventListener("resize", repaint);
|
|
|
|
// setInterval(repaint, 16);
|
|
|
|
repaint();
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|