You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.2 KiB

2 years ago
import { EVENT } from './enums';
export function create(dataview) {
return {
'offset': 0,
'size': dataview.byteLength,
'view': dataview,
};
}
export function u8(d) {
const value = d.view.getUint8(d.offset);
d.offset += 1;
return value;
}
export function u16(d) {
const value = d.view.getUint16(d.offset, true);
d.offset += 2;
return value;
}
export function u32(d) {
const value = d.view.getUint32(d.offset, true);
d.offset += 4;
return value;
}
2 years ago
export function f32(d) {
const value = d.view.getFloat32(d.offset, true);
d.offset += 4;
return value;
}
function f32array(d, count) {
const array = new Float32Array(d.view.buffer, d.offset, count);
d.offset += count * 4;
2 years ago
return array;
}
2 years ago
export function align(d, to) {
while (d.offset % to !== 0) {
d.offset++;
}
}
2 years ago
export function event(d) {
const event = {};
event.type = u8(d);
switch (event.type) {
case EVENT.PREDRAW: {
2 years ago
event.x = f32(d);
event.y = f32(d);
2 years ago
break;
}
case EVENT.CLEAR: {
break;
}
case EVENT.SET_COLOR: {
event.color = u32(d);
break;
}
case EVENT.SET_WIDTH: {
event.width = u16(d);
break;
}
2 years ago
case EVENT.STROKE: {
2 years ago
// point_count + width align to 4 bytes :D
2 years ago
const point_count = u16(d);
const width = u16(d);
const color = u32(d);
2 years ago
align(d, 4);
event.width = width;
event.color = color;
2 years ago
event.points = f32array(d, point_count * 2);
2 years ago
break;
}
case EVENT.IMAGE:
case EVENT.IMAGE_MOVE: {
2 years ago
event.image_id = u32(d);
2 years ago
event.x = f32(d);
event.y = f32(d);
2 years ago
break;
}
2 years ago
case EVENT.UNDO:
case EVENT.REDO: {
break;
}
2 years ago
case EVENT.ERASER: {
event.stroke_id = u32(d);
break;
}
2 years ago
default: {
2 years ago
console.error('fuck', event.type);
2 years ago
console.trace();
process.exit(1);
}
}
return event;
}