|
|
|
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 u32(s, value) {
|
|
|
|
s.view.setUint32(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 event(s, event) {
|
|
|
|
u8(s, event.type);
|
|
|
|
u32(s, event.user_id);
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case EVENT.PREDRAW: {
|
|
|
|
u16(s, event.x);
|
|
|
|
u16(s, event.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.STROKE: {
|
|
|
|
const points_bytes = event.points;
|
|
|
|
u32(s, event.stroke_id);
|
|
|
|
u16(s, points_bytes.byteLength / 2 / 2); // each point is 2 u16s
|
|
|
|
u16(s, event.width);
|
|
|
|
u32(s, event.color);
|
|
|
|
bytes(s, points_bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EVENT.IMAGE:
|
|
|
|
case EVENT.IMAGE_MOVE: {
|
|
|
|
u32(s, event.image_id);
|
|
|
|
u16(s, event.x);
|
|
|
|
u16(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|