bugfixes in lots of places
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 11 Jul 2021 23:59:37 +0000 (18:59 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 11 Jul 2021 23:59:37 +0000 (18:59 -0500)
core/math.onyx
core/memory.onyx
modules/immediate_mode/immediate_renderer.onyx
modules/js_events/js_events.js
modules/js_events/request_file.onyx
modules/ui/components/radio.onyx
modules/ui/components/slider.onyx
modules/wasm_utils/types.onyx

index 7030b08643a94fc0abb8a4a4fc8e4c8f7c89686b..054738fef5eb2788462340f83980c3cc4484c1b6 100644 (file)
@@ -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;
+}
index ec57fae940c2abf0cb2e560d62aa1264b6265bf8..2f0ae4ad352bb7785eeab3865bb48a9c2b3a47b7 100644 (file)
@@ -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 {
index 7eff08ad05b34acd43ab22f8ce5dcdfcb5573d53..5b4b60f7d39c9208b8017670cb72d396f775bf12 100644 (file)
@@ -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 {
index 546bec2225c05e1ffcacf2b1c86ddeb8605cb9b2..a60a716b8238d4edd8086d02e2d564012f9a8b85 100644 (file)
@@ -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;
index f283838e26b5cccc17df56f8a8c0f3e7b0ea2229..e199cba1c3cbcac6cfdbfcc088bd50634f880660 100644 (file)
@@ -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;
index 3ca62cf19fcf50863cbd907f434ba267b20c82d6..f64c56dd2c5c947409afe0e2c10467749d207bc9 100644 (file)
@@ -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);
     }
 
index 7e48b9438a62c2490aecbcd6a76cac12295f3342..403d706a2141e7ec178d51180d23a9a8286a4e38 100644 (file)
@@ -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);
     }
 
index dfe66578a52a24268d3c45354dd1e309752f8195..4bc34076559b5d6e5100a1b6508c9fb01263ae61 100644 (file)
@@ -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);