egui/docs/index.html

284 lines
11 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2019-01-17 23:34:01 +00:00
<!-- Disable zooming: -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!-- TODO: read https://www.html5rocks.com/en/mobile/mobifying/#toc-meta-viewport -->
<head>
<title>Egui An experimental immediate mode GUI written in Rust</title>
<style>
2020-04-12 10:07:51 +00:00
html {
/* Remove touch delay: */
touch-action: manipulation;
}
body {
2020-04-23 20:08:42 +00:00
background: #101010;
2020-04-12 10:07:51 +00:00
}
2020-04-23 20:08:42 +00:00
/* Allow canvas to fill entire web page: */
2020-04-12 10:07:51 +00:00
html,
body {
overflow: hidden;
2020-04-23 20:08:42 +00:00
margin: 0 !important;
padding: 0 !important;
2020-04-12 10:07:51 +00:00
}
</style>
</head>
<body>
2020-07-18 08:54:31 +00:00
<!-- We later make this cover the entire screen even when resized -->
<canvas id="canvas" width="1024" height="1024"></canvas>
<script>
2020-04-12 10:07:51 +00:00
// The `--no-modules`-generated JS from `wasm-bindgen` attempts to use
// `WebAssembly.instantiateStreaming` to instantiate the wasm module,
// but this doesn't work with `file://` urls. This example is frequently
// viewed by simply opening `index.html` in a browser (with a `file://`
// url), so it would fail if we were to call this function!
//
// Work around this for now by deleting the function to ensure that the
// `no_modules.js` script doesn't have access to it. You won't need this
// hack when deploying over HTTP.
delete WebAssembly.instantiateStreaming;
</script>
2019-02-09 22:00:07 +00:00
<!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
2019-03-16 11:55:42 +00:00
<script src="example_wasm.js"></script>
2019-02-09 22:00:07 +00:00
<script>
2020-04-12 10:07:51 +00:00
// we'll defer our execution until the wasm is ready to go
// 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
wasm_bindgen("./example_wasm_bg.wasm")
.then(on_wasm_loaded)["catch"](console.error);
2020-07-18 08:54:31 +00:00
// ----------------------------------------------------------------------------
2020-05-21 10:04:14 +00:00
2020-07-18 08:54:31 +00:00
var g_wasm_app = null;
2020-04-23 17:15:17 +00:00
2020-07-18 08:54:31 +00:00
function egui_state() {
window.g_egui_state = window.g_egui_state || {
mouse_pos: null,
mouse_down: false,
is_touch: false, // we don't know yet
scroll_delta_x: 0,
scroll_delta_y: 0,
events: [],
};
return window.g_egui_state;
};
2020-04-23 17:15:17 +00:00
2020-04-12 10:07:51 +00:00
// ----------------------------------------------------------------------------
function pixels_per_point() {
return window.devicePixelRatio || 1.0;
}
2020-07-18 08:54:31 +00:00
function get_egui_input() {
let state = egui_state();
2020-04-12 10:07:51 +00:00
2020-04-23 07:50:03 +00:00
var input = {
2020-07-18 08:54:31 +00:00
mouse_down: state.mouse_down,
mouse_pos: state.mouse_pos,
scroll_delta: { x: -state.scroll_delta_x, y: -state.scroll_delta_y }, // TODO: standardize scroll direction
2020-04-12 10:07:51 +00:00
screen_size: { x: window.innerWidth, y: window.innerHeight },
pixels_per_point: pixels_per_point(),
time: window.performance.now() / 1000.0,
seconds_since_midnight: seconds_since_midnight(),
2020-07-18 08:54:31 +00:00
events: state.events,
2020-04-12 10:07:51 +00:00
};
2020-07-18 08:54:31 +00:00
state.scroll_delta_x = 0;
state.scroll_delta_y = 0;
state.events = [];
2020-04-23 07:50:03 +00:00
return input;
2020-04-12 10:07:51 +00:00
}
function seconds_since_midnight() {
var d = new Date();
return (d.getHours() * 60.0 + d.getMinutes()) * 60.0 + d.getSeconds() + 1e-3 * d.getMilliseconds();
}
2020-04-12 10:07:51 +00:00
function mouse_pos_from_event(canvas, event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
2020-04-23 07:50:03 +00:00
// If true, paint at full framerate always.
// If false, only paint on input.
// TODO: if this is turned off we must turn off animations too (which hasn't been implemented yet).
2020-04-12 10:07:51 +00:00
const ANIMATION_FRAME = true;
function paint() {
2020-07-18 08:54:31 +00:00
wasm_bindgen.resize_to_screen_size("canvas");
if (g_wasm_app === null) {
g_wasm_app = wasm_bindgen.new_webgl_gui("canvas");
}
let input = get_egui_input();
wasm_bindgen.run_gui(g_wasm_app, JSON.stringify(input));
2020-04-12 10:07:51 +00:00
}
function paint_and_schedule() {
paint();
if (ANIMATION_FRAME) {
window.requestAnimationFrame(paint_and_schedule);
}
2019-03-16 13:12:40 +00:00
}
2020-04-12 10:07:51 +00:00
function on_wasm_loaded() {
var canvas = document.getElementById("canvas");
2020-07-18 08:54:31 +00:00
console.assert(canvas);
install_canvas_events(canvas, paint);
install_document_events(paint);
paint_and_schedule();
}
function install_document_events(paint) {
2020-04-12 10:07:51 +00:00
var invalidate = function () {
if (!ANIMATION_FRAME) {
paint();
}
};
2020-07-18 08:54:31 +00:00
document.addEventListener("keydown", function (event) {
var key = translate_key(event.key);
if (key) {
egui_state().events.push({ "key": { "key": key, 'pressed': true } });
} else {
egui_state().events.push({ "text": event.key });
}
invalidate();
// event.stopPropagation();
// event.preventDefault();
});
// document.addEventListener("keypress", (event)=>{
// console.log(`keypress: ${event.key} ${JSON.stringify(event)}`);
// invalidate();
// event.stopPropagation();
// event.preventDefault();
// });
document.addEventListener("keyup", function (event) {
// console.log(`keyup: ${event.key} ${JSON.stringify(event)}`);
var key = translate_key(event.key);
if (key) {
egui_state().events.push({ "key": { "key": key, 'pressed': false } });
}
invalidate();
// event.stopPropagation();
// event.preventDefault();
});
if (!ANIMATION_FRAME) {
window.addEventListener("load", invalidate);
window.addEventListener("pagehide", invalidate);
window.addEventListener("pageshow", invalidate);
window.addEventListener("resize", invalidate);
}
}
function install_canvas_events(canvas, paint) {
var invalidate = function () {
if (!ANIMATION_FRAME) {
paint();
}
};
2020-04-12 10:07:51 +00:00
canvas.addEventListener("mousedown", function (event) {
2020-07-18 08:54:31 +00:00
if (egui_state().is_touch) { return; }
egui_state().mouse_pos = mouse_pos_from_event(canvas, event);
egui_state().mouse_down = true;
2020-04-12 10:07:51 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mousemove", function (event) {
2020-07-18 08:54:31 +00:00
if (egui_state().is_touch) { return; }
egui_state().mouse_pos = mouse_pos_from_event(canvas, event);
2020-04-12 10:07:51 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mouseup", function (event) {
2020-07-18 08:54:31 +00:00
if (egui_state().is_touch) { return; }
egui_state().mouse_pos = mouse_pos_from_event(canvas, event);
egui_state().mouse_down = false;
2020-04-12 10:07:51 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mouseleave", function (event) {
2020-07-18 08:54:31 +00:00
if (egui_state().is_touch) { return; }
egui_state().mouse_pos = null;
2020-04-12 10:07:51 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("touchstart", function (event) {
2020-07-18 08:54:31 +00:00
egui_state().is_touch = true;
egui_state().mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
egui_state().mouse_down = true;
2020-04-12 10:07:51 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("touchmove", function (event) {
2020-07-18 08:54:31 +00:00
egui_state().is_touch = true;
egui_state().mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
2020-04-12 10:07:51 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("touchend", function (event) {
2020-07-18 08:54:31 +00:00
egui_state().is_touch = true;
egui_state().mouse_down = false; // First release mouse to click...
paint(); // ...do the clicking...
egui_state().mouse_pos = null; // ...remove hover effect
2020-04-23 19:27:05 +00:00
invalidate();
2020-04-12 10:07:51 +00:00
event.stopPropagation();
event.preventDefault();
});
2020-04-23 07:50:03 +00:00
canvas.addEventListener("wheel", function (event) {
2020-07-18 08:54:31 +00:00
egui_state().scroll_delta_x += event.deltaX;
egui_state().scroll_delta_y += event.deltaY;
2020-04-23 07:50:03 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
2020-04-12 10:07:51 +00:00
}
2020-04-29 19:25:49 +00:00
function translate_key(key) {
if (key == "Alt") { return "alt"; }
if (key == "Backspace") { return "backspace"; }
if (key == "Control") { return "control"; }
if (key == "Delete") { return "delete"; }
if (key == "ArrowDown") { return "down"; }
if (key == "End") { return "end"; }
if (key == "Escape") { return "escape"; }
if (key == "Home") { return "home"; }
if (key == "Help") { return "insert"; }
if (key == "ArrowLeft") { return "left"; }
if (key == "Meta") { return "logo"; }
if (key == "PageDown") { return "page_down"; }
if (key == "PageUp") { return "page_up"; }
if (key == "Enter") { return "return"; }
if (key == "ArrowRight") { return "right"; }
if (key == "Shift") { return "shift"; }
// if (key == " ") { return "space"; }
if (key == "Tab") { return "tab"; }
if (key == "ArrowUp") { return "up"; }
return null;
}
</script>
</body>
</html>