From c6aaf4d33af9a85b0b58c9cc6538ce269d4fbe7e Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 11 Jul 2021 18:58:35 -0500 Subject: [PATCH] small improvements and updates --- site/js/js_events.js | 23 ++++++++++++++++++++++ src/main.onyx | 47 +++++++++++++++++++------------------------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/site/js/js_events.js b/site/js/js_events.js index 546bec2..a60a716 100644 --- a/site/js/js_events.js +++ b/site/js/js_events.js @@ -106,6 +106,29 @@ window.ONYX_MODULES.push({ push_event_to_buffer(esp, event_size, 0x06, [ window.innerWidth, window.innerHeight ]); + document.getElementsByTagName("canvas")[0].addEventListener("drop", function (ev) { + ev.preventDefault(); + + // ROBUSTNESS: Currently, this only gives the first file, which for a lot of purposes, will be enough. + // But if multiple files are dropped, the application will only know about the first one. + ev.dataTransfer.items[0].getAsFile().arrayBuffer() + .then(response => { + // 0 is assumed to be reserved in request_file.onyx. + requested_file_data[0] = response; + push_event_to_buffer(esp, event_size, 0x08, [0x01, 0, response.byteLength]); + }) + .catch(error => { + push_event_to_buffer(esp, event_size, 0x08, [0x02, 0, 0]); + }); + + return false; + }); + + document.getElementsByTagName("canvas")[0].addEventListener("dragover", function(ev) { + ev.preventDefault(); + return false; + }); + document.oncontextmenu = (e) => { e.preventDefault = true; return false; diff --git a/src/main.onyx b/src/main.onyx index 7e32498..2aeedc1 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -6,13 +6,14 @@ use package core #private_file gfx :: package immediate_mode #private_file ui :: package ui #private_file config :: package config +#private_file wasm :: package wasm_utils search_buffer: string.String_Buffer; - background_tile_texture : gfx.Texture; - on_file_load_callbacks : map.Map(u32, (file_event: ^events.Event) -> void); +wasm_data : [] u8; + main :: (args: [] cstr) { init(); @@ -134,26 +135,6 @@ poll_events :: () -> bool { } case .FileRequest { - @Bug // When debugging I tried to do this - // - printf("{}\n", event.file); - // - // And it looked like it was printing correctly. However, it destroyed the data in the event, - // which made the below code fail. Without printing this, everything works just fine. This - // probably has to do with the "any" type for printf. - // - // Actually, I tried this: - // - // println(event.file.file_id); - // - // And it also breaks it... Hmm - // - // Another wrinkle: When I copied this case-body to a different function, it worked perfectly. - // Definitely something wrong with the stack allocation when in case statements. - // - // events.consume() was returning a pointer to a local variable on the stack.... UGH - // Some kind of shadowing warning would be nice - if f := map.get(^on_file_load_callbacks, event.file.file_id); f != null_proc { f(event); @@ -161,6 +142,22 @@ poll_events :: () -> bool { printf("Warning: No callback set for file id {}.\n", event.file.file_id); } } + + case .FileDropped { + printf("File with size {} and status {} was dropped.\n", event.file.size, event.file.status); + + if wasm_data.count > 0 do memory.free_slice(^wasm_data); + + wasm_data = memory.make_slice(u8, event.file.size); + events.get_requested_file_data(event.file.file_id, wasm_data); + + wasm_module := wasm.load(wasm_data); + defer wasm.free(^wasm_module); + + for ^entry: wasm_module.sections.entries { + printf("Section: {}\nOffset: {}\n", entry.key, entry.value); + } + } } } @@ -171,7 +168,7 @@ update :: (dt: f32) { t += dt; if ui.is_key_just_down(~~#char "L") { - color_file := events.request_file("/res/colors_light.json"); + color_file := events.request_file("/res/colors_dark.json"); map.put(^on_file_load_callbacks, color_file, load_colors); } } @@ -275,10 +272,6 @@ load_colors :: (event: ^events.Event) { defer if color_data.count > 0 do cfree(color_data.data); events.get_requested_file_data(event.file.file_id, color_data); - @DynamicLoading // Currently, this file is just baked into the binary. When a mechanism - // is added to easily ask JS to make a fetch request for a file, that can be used here. - // colors_json := #file_contents "res/colors_light.json"; - arena := alloc.arena.make(context.allocator, 4096); defer alloc.arena.free(^arena); colors := json.decode(color_data, alloc.arena.make_allocator(^arena)); -- 2.25.1