diff --git a/client/aux.js b/client/aux.js index 1ff534b..73417ea 100644 --- a/client/aux.js +++ b/client/aux.js @@ -1,3 +1,61 @@ +function ui_offline() { + document.body.classList.add('offline'); + document.querySelector('.offline-toast').classList.remove('hidden'); +} + +function ui_online() { + document.body.classList.remove('offline'); + document.querySelector('.offline-toast').classList.add('hidden'); +} + +function event_size(event) { + let size = 1 + 3; // type + padding + + switch (event.type) { + case EVENT.PREDRAW: { + size += 4 * 2; + break; + } + + case EVENT.SET_COLOR: { + size += 4; + break; + } + + case EVENT.SET_WIDTH: { + size += 2; + break; + } + + case EVENT.STROKE: { + size += 4 + 2 + 2 + 4 + event.points.length * 4 * 2; // u32 stroke id + u16 (count) + u16 (width) + u32 (color + count * (f32, f32) points + break; + } + + case EVENT.UNDO: + case EVENT.REDO: { + break; + } + + case EVENT.IMAGE: + case EVENT.IMAGE_MOVE: { + size += 4 + 4 + 4; // file id + x + y + break; + } + + case EVENT.ERASER: { + size += 4; // stroke id + break; + } + + default: { + console.error('fuck'); + } + } + + return size; +} + function find_touch(touchlist, id) { for (const touch of touchlist) { if (touch.identifier === id) { diff --git a/client/client_recv.js b/client/client_recv.js index 91c4f02..7e6bcac 100644 --- a/client/client_recv.js +++ b/client/client_recv.js @@ -298,6 +298,7 @@ async function handle_message(state, context, d) { case MESSAGE.INIT: { state.me = des_u32(d); state.server_lsn = des_u32(d); + state.online = true; init_player_defaults(state, state.me); @@ -320,7 +321,7 @@ async function handle_message(state, context, d) { state.events.length = 0; - for (let i = 0; i < user_count; ++i) { + for (let i = 0; i < user_count; ++i) { const user_id = des_u32(d); const user_color = des_u32(d); const user_width = des_u16(d); @@ -340,9 +341,7 @@ async function handle_message(state, context, d) { do_draw = true; recompute_static_data(context); - send_ack(event_count); - sync_queue(state); break; diff --git a/client/client_send.js b/client/client_send.js index 1083da4..24e146e 100644 --- a/client/client_send.js +++ b/client/client_send.js @@ -192,6 +192,8 @@ function push_event(state, event) { // Queue an event and initialize repated sends until ACKed function queue_event(state, event, skip = false) { + if (!state.online) { return; } + push_event(state, event); if (skip) { @@ -206,7 +208,9 @@ function queue_event(state, event, skip = false) { } // Fire and forget. Doesn't do anything if we are offline -async function fire_event(event) { +async function fire_event(state, event) { + if (!state.online) { return; } + const s = serializer_create(1 + event_size(event)); ser_u8(s, MESSAGE.FIRE); diff --git a/client/default.css b/client/default.css index ecc94fd..741ecab 100644 --- a/client/default.css +++ b/client/default.css @@ -15,6 +15,14 @@ html, body { overflow: hidden; } +body .main { + height: 100%; +} + +body.offline .main { + filter: brightness(50%); +} + .dhide { display: none !important; } @@ -255,4 +263,33 @@ input[type=range]::-moz-range-track { left: 50%; top: 96px; transform: translate(-50%, -50%); -} \ No newline at end of file +} + +.offline-toast { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + z-index: 999; + background: rgba(0, 0, 0, 0.8); + display: flex; + align-items: center; + padding: 10px; + border-radius: var(--radius); + box-shadow: 0px 2px 3px 0px rgba(155, 150, 100, 0.2); + transition: transform .1s ease-in-out, opacity .1s; + font-size: 14px; + color: white; + font-weight: bold; + user-select: none; + transition: transform .1s ease-in-out, opacity .1s; +} + +.offline-toast.hidden { + transform: translate(-50%, -5px); + opacity: 0; +} + +body.offline * { + pointer-events: none; +} diff --git a/client/index.html b/client/index.html index 7cb14d4..6bd240a 100644 --- a/client/index.html +++ b/client/index.html @@ -3,59 +3,66 @@