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; } export function s32(d) { const value = d.view.getInt32(d.offset, true); d.offset += 4; return value; } 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; return array; } function u8array(d, count) { const array = new Uint8Array(d.view.buffer, d.offset, count); d.offset += count; return array; } export function align(d, to) { while (d.offset % to !== 0) { d.offset++; } } export function event(d) { const event = {}; event.type = u32(d); switch (event.type) { case EVENT.PREDRAW: { event.x = f32(d); event.y = f32(d); break; } case EVENT.MOVE_CURSOR: { event.x = f32(d); event.y = f32(d); break; } case EVENT.MOVE_CANVAS: { event.offset_x = u32(d); event.offset_y = u32(d); event.zoom_level = s32(d); break; } case EVENT.ZOOM_CANVAS: { event.zoom_level = s32(d); event.zoom_cx = f32(d); event.zoom_cy = f32(d); break; } case EVENT.CLEAR: { break; } case EVENT.SET_COLOR: { event.color = u32(d); break; } case EVENT.SET_WIDTH: { event.width = u16(d); break; } case EVENT.STROKE: { // point_count + width align to 4 bytes :D const point_count = u16(d); const width = u16(d); const color = u32(d); event.width = width; event.color = color; event.points = f32array(d, point_count * 2); event.pressures = u8array(d, point_count); align(d, 4); break; } case EVENT.IMAGE: { event.image_id = u32(d); event.x = f32(d); event.y = f32(d); event.width = u32(d); event.height = u32(d); break; } case EVENT.IMAGE_MOVE: { event.image_id = u32(d); event.x = f32(d); event.y = f32(d); break; } case EVENT.IMAGE_SCALE: { event.image_id = u32(d); event.corner = u32(d); event.x = f32(d); event.y = f32(d); break; } case EVENT.UNDO: case EVENT.REDO: { break; } case EVENT.ERASER: { event.stroke_id = u32(d); break; } default: { console.error('fuck', event.type); console.trace(); process.exit(1); } } return event; }