2018-12-23 19:06:40 +00:00
|
|
|
interface Vec2 {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:42:30 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Paint module:
|
|
|
|
|
2018-12-27 16:47:32 +00:00
|
|
|
/// 0-255 sRGBA
|
|
|
|
interface Color {
|
|
|
|
r: number;
|
|
|
|
g: number;
|
|
|
|
b: number;
|
|
|
|
a: number;
|
|
|
|
}
|
|
|
|
|
2018-12-27 17:19:06 +00:00
|
|
|
interface Outline {
|
|
|
|
color: Color;
|
|
|
|
width: number;
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:42:30 +00:00
|
|
|
interface Clear {
|
|
|
|
kind: "clear";
|
2018-12-27 16:47:32 +00:00
|
|
|
fill_color: Color;
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Line {
|
|
|
|
kind: "line";
|
2018-12-27 16:47:32 +00:00
|
|
|
color: Color;
|
2018-12-27 17:19:06 +00:00
|
|
|
points: Vec2[];
|
2018-12-26 21:17:33 +00:00
|
|
|
width: number;
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Circle {
|
2018-12-27 17:19:06 +00:00
|
|
|
kind: "circle";
|
2018-12-23 19:06:40 +00:00
|
|
|
center: Vec2;
|
2018-12-27 16:47:32 +00:00
|
|
|
fill_color: Color | null;
|
2018-12-26 16:32:58 +00:00
|
|
|
outline: Outline | null;
|
2018-12-23 18:42:30 +00:00
|
|
|
radius: number;
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:32:58 +00:00
|
|
|
interface Rect {
|
2018-12-27 17:19:06 +00:00
|
|
|
kind: "rect";
|
2018-12-23 19:06:40 +00:00
|
|
|
corner_radius: number;
|
2018-12-27 16:47:32 +00:00
|
|
|
fill_color: Color | null;
|
2018-12-26 16:32:58 +00:00
|
|
|
outline: Outline | null;
|
|
|
|
pos: Vec2;
|
2018-12-23 19:06:40 +00:00
|
|
|
size: Vec2;
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Text {
|
|
|
|
kind: "text";
|
2018-12-27 16:47:32 +00:00
|
|
|
fill_color: Color | null;
|
2018-12-27 17:19:06 +00:00
|
|
|
font_name: string; // e.g. "Palatino"
|
2018-12-27 23:51:40 +00:00
|
|
|
font_size: number; // Height in pixels, e.g. 12
|
2018-12-23 19:06:40 +00:00
|
|
|
pos: Vec2;
|
2018-12-27 16:47:32 +00:00
|
|
|
stroke_color: Color | null;
|
2018-12-23 18:42:30 +00:00
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:32:58 +00:00
|
|
|
type PaintCmd = Circle | Clear | Line | Rect | Text;
|
2018-12-23 18:42:30 +00:00
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas painting:
|
|
|
|
|
|
|
|
function style_from_color(color: Color): string {
|
2018-12-27 16:47:32 +00:00
|
|
|
return `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a / 255.0})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function paint_command(canvas, cmd: PaintCmd) {
|
2018-12-23 18:42:30 +00:00
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
2018-12-23 19:06:40 +00:00
|
|
|
// console.log(`cmd: ${JSON.stringify(cmd)}`);
|
|
|
|
|
2018-12-23 18:42:30 +00:00
|
|
|
switch (cmd.kind) {
|
|
|
|
case "circle":
|
|
|
|
ctx.beginPath();
|
2018-12-23 19:06:40 +00:00
|
|
|
ctx.arc(cmd.center.x, cmd.center.y, cmd.radius, 0, 2 * Math.PI, false);
|
2018-12-27 16:47:32 +00:00
|
|
|
if (cmd.fill_color) {
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.fillStyle = style_from_color(cmd.fill_color);
|
2018-12-26 16:32:58 +00:00
|
|
|
ctx.fill();
|
|
|
|
}
|
|
|
|
if (cmd.outline) {
|
|
|
|
ctx.lineWidth = cmd.outline.width;
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.strokeStyle = style_from_color(cmd.outline.color);
|
2018-12-26 16:32:58 +00:00
|
|
|
ctx.stroke();
|
|
|
|
}
|
2018-12-23 18:42:30 +00:00
|
|
|
return;
|
|
|
|
|
2018-12-26 21:17:33 +00:00
|
|
|
case "clear":
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.fillStyle = style_from_color(cmd.fill_color);
|
2018-12-26 21:17:33 +00:00
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case "line":
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(cmd.points[0].x, cmd.points[0].y);
|
|
|
|
for (const point of cmd.points) {
|
|
|
|
ctx.lineTo(point.x, point.y);
|
|
|
|
}
|
|
|
|
ctx.lineWidth = cmd.width;
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.strokeStyle = style_from_color(cmd.color);
|
2018-12-26 21:17:33 +00:00
|
|
|
ctx.stroke();
|
|
|
|
return;
|
|
|
|
|
2018-12-26 16:32:58 +00:00
|
|
|
case "rect":
|
2018-12-23 19:06:40 +00:00
|
|
|
const x = cmd.pos.x;
|
|
|
|
const y = cmd.pos.y;
|
|
|
|
const width = cmd.size.x;
|
|
|
|
const height = cmd.size.y;
|
2018-12-26 16:32:58 +00:00
|
|
|
const r = Math.min(cmd.corner_radius, width / 2, height / 2);
|
2018-12-23 18:42:30 +00:00
|
|
|
ctx.beginPath();
|
2018-12-23 19:06:40 +00:00
|
|
|
ctx.moveTo(x + r, y);
|
|
|
|
ctx.lineTo(x + width - r, y);
|
|
|
|
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
|
|
|
|
ctx.lineTo(x + width, y + height - r);
|
|
|
|
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
|
|
|
|
ctx.lineTo(x + r, y + height);
|
|
|
|
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
|
|
|
|
ctx.lineTo(x, y + r);
|
|
|
|
ctx.quadraticCurveTo(x, y, x + r, y);
|
2018-12-23 18:42:30 +00:00
|
|
|
ctx.closePath();
|
2018-12-27 16:47:32 +00:00
|
|
|
if (cmd.fill_color) {
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.fillStyle = style_from_color(cmd.fill_color);
|
2018-12-26 16:32:58 +00:00
|
|
|
ctx.fill();
|
|
|
|
}
|
|
|
|
if (cmd.outline) {
|
|
|
|
ctx.lineWidth = cmd.outline.width;
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.strokeStyle = style_from_color(cmd.outline.color);
|
2018-12-26 16:32:58 +00:00
|
|
|
ctx.stroke();
|
|
|
|
}
|
2018-12-23 18:42:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case "text":
|
2019-01-04 13:14:32 +00:00
|
|
|
ctx.fillStyle = style_from_color(cmd.fill_color);
|
2018-12-27 17:19:06 +00:00
|
|
|
ctx.font = `${cmd.font_size}px ${cmd.font_name}`;
|
2018-12-27 22:55:16 +00:00
|
|
|
ctx.textBaseline = "middle";
|
2018-12-23 19:06:40 +00:00
|
|
|
ctx.fillText(cmd.text, cmd.pos.x, cmd.pos.y);
|
2018-12-23 18:42:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
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;
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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")
|
2018-12-23 18:42:30 +00:00
|
|
|
.then(wasm_loaded)
|
|
|
|
.catch(console.error);
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
function rust_gui(input: RawInput): PaintCmd[] {
|
2018-12-23 18:42:30 +00:00
|
|
|
return JSON.parse(wasm_bindgen.show_gui(JSON.stringify(input)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
function js_gui(input: RawInput): PaintCmd[] {
|
2018-12-23 18:42:30 +00:00
|
|
|
const commands = [];
|
|
|
|
|
|
|
|
commands.push({
|
|
|
|
fillStyle: "#111111",
|
|
|
|
kind: "clear",
|
|
|
|
});
|
|
|
|
|
|
|
|
commands.push({
|
|
|
|
fillStyle: "#ff1111",
|
2018-12-26 16:32:58 +00:00
|
|
|
kind: "rect",
|
2018-12-23 19:06:40 +00:00
|
|
|
pos: { x: 100, y: 100 },
|
2018-12-23 18:42:30 +00:00
|
|
|
radius: 20,
|
2018-12-23 19:06:40 +00:00
|
|
|
size: { x: 200, y: 100 },
|
2018-12-23 18:42:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
const WEB_GL = true;
|
|
|
|
let g_webgl_painter = null;
|
2018-12-23 18:42:30 +00:00
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
function paint_gui(canvas, input: RawInput) {
|
|
|
|
if (WEB_GL) {
|
|
|
|
if (g_webgl_painter === null) {
|
|
|
|
g_webgl_painter = wasm_bindgen.new_webgl_painter("canvas");
|
|
|
|
}
|
|
|
|
wasm_bindgen.paint_webgl(g_webgl_painter, JSON.stringify(input));
|
|
|
|
} else {
|
|
|
|
let commands = rust_gui(input);
|
|
|
|
for (const cmd of commands) {
|
|
|
|
const commands = rust_gui(input);
|
|
|
|
commands.unshift({
|
|
|
|
fill_color: { r: 0, g: 0, b: 0, a: 0 },
|
|
|
|
kind: "clear",
|
|
|
|
});
|
|
|
|
|
|
|
|
paint_command(canvas, cmd);
|
|
|
|
}
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
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) {
|
|
|
|
if (WEB_GL) {
|
|
|
|
canvas.setAttribute("width", window.innerWidth);
|
|
|
|
canvas.setAttribute("height", window.innerHeight);
|
|
|
|
} else {
|
|
|
|
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 {
|
2018-12-23 18:42:30 +00:00
|
|
|
const 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
|
|
|
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));
|
2018-12-23 18:42:30 +00:00
|
|
|
|
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-23 18:42:30 +00:00
|
|
|
|
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();
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|