2018-12-23 19:06:40 +00:00
|
|
|
interface Vec2 {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:42:30 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Paint module:
|
|
|
|
|
|
|
|
interface Clear {
|
|
|
|
kind: "clear";
|
|
|
|
fill_style: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Line {
|
|
|
|
kind: "line";
|
2018-12-23 19:06:40 +00:00
|
|
|
from: Vec2;
|
2018-12-23 18:42:30 +00:00
|
|
|
line_width: number;
|
|
|
|
stroke_style: string;
|
2018-12-23 19:06:40 +00:00
|
|
|
to: Vec2;
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Circle {
|
|
|
|
kind: "circle";
|
2018-12-23 19:06:40 +00:00
|
|
|
center: Vec2;
|
2018-12-23 18:42:30 +00:00
|
|
|
fill_style: string;
|
|
|
|
radius: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RoundedRect {
|
|
|
|
kind: "rounded_rect";
|
|
|
|
fill_style: string;
|
2018-12-23 19:06:40 +00:00
|
|
|
pos: Vec2;
|
|
|
|
corner_radius: number;
|
|
|
|
size: Vec2;
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Text {
|
|
|
|
kind: "text";
|
|
|
|
fill_style: string;
|
|
|
|
font: string;
|
2018-12-23 19:06:40 +00:00
|
|
|
pos: Vec2;
|
2018-12-23 18:42:30 +00:00
|
|
|
text: string;
|
|
|
|
text_align: "start" | "center" | "end";
|
|
|
|
}
|
|
|
|
|
|
|
|
type PaintCmd = Clear | Line | Circle | RoundedRect | Text;
|
|
|
|
|
|
|
|
function paintCommand(canvas, cmd: PaintCmd) {
|
|
|
|
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 "clear":
|
|
|
|
ctx.fillStyle = cmd.fill_style;
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case "line":
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.lineWidth = cmd.line_width;
|
|
|
|
ctx.strokeStyle = cmd.stroke_style;
|
2018-12-23 19:06:40 +00:00
|
|
|
ctx.moveTo(cmd.from.x, cmd.from.y);
|
|
|
|
ctx.lineTo(cmd.to.x, cmd.to.y);
|
2018-12-23 18:42:30 +00:00
|
|
|
ctx.stroke();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case "circle":
|
|
|
|
ctx.fillStyle = cmd.fill_style;
|
|
|
|
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-23 18:42:30 +00:00
|
|
|
ctx.fill();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case "rounded_rect":
|
|
|
|
ctx.fillStyle = cmd.fill_style;
|
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;
|
|
|
|
const r = cmd.corner_radius;
|
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();
|
|
|
|
ctx.fill();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case "text":
|
|
|
|
ctx.font = cmd.font;
|
|
|
|
ctx.fillStyle = cmd.fill_style;
|
|
|
|
ctx.textAlign = cmd.text_align;
|
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
|
|
|
|
wasm_bindgen("./emgui_bg.wasm")
|
|
|
|
.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",
|
|
|
|
kind: "rounded_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;
|
|
|
|
}
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
function paint_gui(canvas, input: RawInput) {
|
2018-12-23 18:42:30 +00:00
|
|
|
const commands = rust_gui(input);
|
2018-12-26 13:38:46 +00:00
|
|
|
commands.unshift({
|
|
|
|
fill_style: "#00000000",
|
|
|
|
kind: "clear",
|
|
|
|
});
|
2018-12-23 18:42:30 +00:00
|
|
|
|
|
|
|
for (const cmd of commands) {
|
|
|
|
paintCommand(canvas, cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
let g_mouse_pos = { x: -1000.0, y: -1000.0 };
|
|
|
|
let g_mouse_down = false;
|
|
|
|
|
|
|
|
function get_input(canvas): RawInput {
|
|
|
|
return {
|
|
|
|
mouse_down: g_mouse_down,
|
|
|
|
mouse_pos: g_mouse_pos,
|
|
|
|
screen_size: { x: canvas.width, y: canvas.height },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:06:40 +00:00
|
|
|
function mouse_pos_from_event(canvas, evt): Vec2 {
|
2018-12-23 18:42:30 +00:00
|
|
|
const rect = canvas.getBoundingClientRect();
|
|
|
|
return {
|
|
|
|
x: evt.clientX - rect.left,
|
|
|
|
y: evt.clientY - rect.top,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initialize() {
|
|
|
|
const canvas = document.getElementById("canvas");
|
|
|
|
|
|
|
|
canvas.addEventListener(
|
|
|
|
"mousemove",
|
|
|
|
(evt) => {
|
2018-12-26 09:46:23 +00:00
|
|
|
g_mouse_pos = mouse_pos_from_event(canvas, evt);
|
|
|
|
paint_gui(canvas, get_input(canvas));
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
canvas.addEventListener(
|
|
|
|
"mouseleave",
|
|
|
|
(evt) => {
|
|
|
|
g_mouse_pos = { x: -1000.0, y: -1000.0 };
|
|
|
|
paint_gui(canvas, get_input(canvas));
|
2018-12-23 18:42:30 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
canvas.addEventListener(
|
|
|
|
"mousedown",
|
|
|
|
(evt) => {
|
2018-12-26 09:46:23 +00:00
|
|
|
g_mouse_pos = mouse_pos_from_event(canvas, evt);
|
|
|
|
g_mouse_down = true;
|
|
|
|
paint_gui(canvas, get_input(canvas));
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
canvas.addEventListener(
|
|
|
|
"mouseup",
|
|
|
|
(evt) => {
|
|
|
|
g_mouse_pos = mouse_pos_from_event(canvas, evt);
|
|
|
|
g_mouse_down = false;
|
|
|
|
paint_gui(canvas, get_input(canvas));
|
2018-12-23 18:42:30 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
paint_gui(canvas, get_input(canvas));
|
2018-12-23 18:42:30 +00:00
|
|
|
}
|