From 76924c731cacb9e9ce33c127c48e626daf686587 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 11 Jul 2021 18:59:37 -0500 Subject: [PATCH] bugfixes in lots of places --- core/math.onyx | 17 +++++++++++++- core/memory.onyx | 8 +++++++ .../immediate_mode/immediate_renderer.onyx | 1 + modules/js_events/js_events.js | 23 +++++++++++++++++++ modules/js_events/request_file.onyx | 5 ++-- modules/ui/components/radio.onyx | 6 +++-- modules/ui/components/slider.onyx | 5 ++-- modules/wasm_utils/types.onyx | 6 +++++ 8 files changed, 64 insertions(+), 7 deletions(-) diff --git a/core/math.onyx b/core/math.onyx index 7030b086..054738fe 100644 --- a/core/math.onyx +++ b/core/math.onyx @@ -295,4 +295,19 @@ rotate_right :: #match { wasm.rotr_i32, wasm.rotr_i64 } lerp :: (t: f32, a: $T, b: T) -> T { return ~~(~~a * (1 - t) + ~~b * t); -} \ No newline at end of file +} + +choose :: (n: $T, k: T) -> T { + assert(T == i32 || T == i64 || T == u32 || T == u64, "bad type for choose function"); + + ret := 1; + for i: (n - k + 1) .. (n + 1) { + ret *= i; + } + + for i: 1 .. (k + 1) { + ret /= i; + } + + return ret; +} diff --git a/core/memory.onyx b/core/memory.onyx index ec57fae9..2f0ae4ad 100644 --- a/core/memory.onyx +++ b/core/memory.onyx @@ -44,6 +44,14 @@ make_slice :: ($T: type_expr, count: i32, allocator := context.allocator) -> [] }; } +free_slice :: (sl: ^[] $T, allocator := context.allocator) { + if sl.data == null do return; + + raw_free(allocator, sl.data); + sl.data = null; + sl.count = 0; +} + align :: #match { (size: ^u64, align: u64) { if *size % align != 0 { diff --git a/modules/immediate_mode/immediate_renderer.onyx b/modules/immediate_mode/immediate_renderer.onyx index 7eff08ad..5b4b60f7 100644 --- a/modules/immediate_mode/immediate_renderer.onyx +++ b/modules/immediate_mode/immediate_renderer.onyx @@ -404,6 +404,7 @@ Immediate_Renderer :: struct { apply_transform :: (use ir: ^Immediate_Renderer, transform: Transform) { transform_apply(array.get_ptr(^world_transform_stack, -1), transform); + world_transform_dirty = true; } to_screen_coordinates :: (use ir: ^Immediate_Renderer, use v: Vector2) -> Vector2 { diff --git a/modules/js_events/js_events.js b/modules/js_events/js_events.js index 546bec22..a60a716b 100644 --- a/modules/js_events/js_events.js +++ b/modules/js_events/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/modules/js_events/request_file.onyx b/modules/js_events/request_file.onyx index f283838e..e199cba1 100644 --- a/modules/js_events/request_file.onyx +++ b/modules/js_events/request_file.onyx @@ -1,12 +1,13 @@ package js_events -#private_file next_request_id := 0; +// 0 is reserved for dropped files +#private_file next_request_id := 1; request_file :: (filepath: str) -> u32 { js_request_file :: (event_storage: ^EventStorage, event_size: u32, filepath: str, fileid: u32) -> void #foreign "js_events" "request_file" ---; - next_request_id += 1; fileid := next_request_id; + next_request_id += 1; js_request_file(^event_storage, sizeof Event, filepath, fileid); return fileid; diff --git a/modules/ui/components/radio.onyx b/modules/ui/components/radio.onyx index 3ca62cf1..f64c56dd 100644 --- a/modules/ui/components/radio.onyx +++ b/modules/ui/components/radio.onyx @@ -25,9 +25,11 @@ radio :: (use r: Rectangle, selected: ^$T, value: T, text: str, theme := ^defaul hash := get_site_hash(site, increment); animation_state := map.get(^animation_states, hash); + mx, my := get_mouse_position(); + if is_active_item(hash) { if mouse_state.left_button_just_up { - if is_hot_item(hash) && Rectangle.contains(r, mouse_state.x, mouse_state.y) { + if is_hot_item(hash) && Rectangle.contains(r, mx, my) { result = true; *selected = value; animation_state.click_time = 1.0f; @@ -42,7 +44,7 @@ radio :: (use r: Rectangle, selected: ^$T, value: T, text: str, theme := ^defaul } } - if Rectangle.contains(r, mouse_state.x, mouse_state.y) { + if Rectangle.contains(r, mx, my) { set_hot_item(hash); } diff --git a/modules/ui/components/slider.onyx b/modules/ui/components/slider.onyx index 7e48b943..403d706a 100644 --- a/modules/ui/components/slider.onyx +++ b/modules/ui/components/slider.onyx @@ -23,6 +23,7 @@ slider :: (use r: Rectangle, value: ^$T, min_value: T, max_value: T, theme := ^d hash := get_site_hash(site, increment); animation_state := map.get(^animation_states, hash); width, height := Rectangle.dimensions(r); + mx, my := get_mouse_position(); if is_hot_item(hash) { if mouse_state.left_button_down { @@ -30,7 +31,7 @@ slider :: (use r: Rectangle, value: ^$T, min_value: T, max_value: T, theme := ^d result = true; // Animate this? - x := mouse_state.x - x0; + x := mx - x0; if T == i32 || T == i64 || T == u32 || T == u64 { step_width := width / ~~math.abs(max_value - min_value); @@ -49,7 +50,7 @@ slider :: (use r: Rectangle, value: ^$T, min_value: T, max_value: T, theme := ^d } } - if Rectangle.contains(r, mouse_state.x, mouse_state.y) { + if Rectangle.contains(r, mx, my) { set_hot_item(hash); } diff --git a/modules/wasm_utils/types.onyx b/modules/wasm_utils/types.onyx index dfe66578..4bc34076 100644 --- a/modules/wasm_utils/types.onyx +++ b/modules/wasm_utils/types.onyx @@ -175,6 +175,12 @@ parse_sections :: (use bin: ^WasmBinary, allocator := context.allocator) -> Wasm return ws; } +free :: (use bin: ^WasmBinary) { + map.free(^sections); + map.free(^custom_section_locations); +} + + free_sections :: (use sections: ^WasmSections) { if type_section.data != null do raw_free(allocator, type_section.data); if import_section.data != null do raw_free(allocator, import_section.data); -- 2.25.1