|
|
|
import { EVENT } from './enums';
|
|
|
|
|
|
|
|
export function create(size) {
|
|
|
|
const buffer = new ArrayBuffer(size);
|
|
|
|
return {
|
|
|
|
'offset': 0,
|
|
|
|
'size': size,
|
|
|
|
'buffer': buffer,
|
|
|
|
'view': new DataView(buffer),
|
|
|
|
'strview': new Uint8Array(buffer),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function u8(s, value) {
|
|
|
|
s.view.setUint8(s.offset, value);
|
|
|
|
s.offset += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function u16(s, value) {
|
|
|
|
s.view.setUint16(s.offset, value, true);
|
|
|
|
s.offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function f32(s, value) {
|
|
|
|
s.view.setFloat32(s.offset, value, true);
|
|
|
|
s.offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function u32(s, value) {
|
|
|
|
s.view.setUint32(s.offset, value, true);
|
|
|
|
s.offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function s32(s, value) {
|
|
|
|
s.view.setInt32(s.offset, value, true);
|
|
|
|
s.offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function bytes(s, bytes) {
|
|
|
|
s.strview.set(new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength), s.offset);
|
|
|
|
s.offset += bytes.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function align(s, to) {
|
|
|
|
// TODO: non-stupid version of this
|
|
|
|
while (s.offset % to != 0) {
|
|
|
|
s.offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function event(s, event) {
|
|
|
|
u32(s, event.type); // for alignment reasons
|
|
|
|
u32(s, event.user_id);
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case EVENT.PREDRAW: {
|
|
|
|
f32(s, event.x);
|
|
|
|
f32(s, event.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.MOVE_CURSOR: {
|
|
|
|
f32(s, event.x);
|
|
|
|
f32(s, event.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.MOVE_CANVAS: {
|
|
|
|
u32(s, event.offset_x);
|
|
|
|
u32(s, event.offset_y);
|
|
|
|
s32(s, event.zoom_level);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.ZOOM_CANVAS: {
|
|
|
|
s32(s, event.zoom_level);
|
|
|
|
f32(s, event.zoom_cx);
|
|
|
|
f32(s, event.zoom_cy);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.USER_JOINED:
|
|
|
|
case EVENT.LEAVE:
|
|
|
|
case EVENT.CLEAR: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.SET_COLOR: {
|
|
|
|
u32(s, event.color);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.SET_WIDTH: {
|
|
|
|
u16(s, event.width);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.STROKE: {
|
|
|
|
const points_bytes = event.points;
|
|
|
|
const pressures_bytes = event.pressures;
|
|
|
|
u32(s, event.stroke_id);
|
|
|
|
u16(s, points_bytes.byteLength / 2 / 4); // each point is 2 * f32
|
|
|
|
u16(s, event.width);
|
|
|
|
u32(s, event.color);
|
|
|
|
bytes(s, points_bytes);
|
|
|
|
bytes(s, pressures_bytes);
|
|
|
|
align(s, 4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.IMAGE:
|
|
|
|
case EVENT.IMAGE_MOVE: {
|
|
|
|
u32(s, event.image_id);
|
|
|
|
f32(s, event.x);
|
|
|
|
f32(s, event.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.UNDO:
|
|
|
|
case EVENT.REDO: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.ERASER: {
|
|
|
|
u32(s, event.stroke_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
console.error('fuck');
|
|
|
|
console.trace();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|