}
}
+var requested_file_data = {};
+
window.ONYX_MODULES.push({
module_name: "js_events",
return false;
};
},
+
+ request_file(esp, event_size, filename_ptr, filename_len, fileid) {
+ esp /= 4;
+
+ var path_memory = new Uint8Array(ONYX_MEMORY.buffer, filename_ptr, filename_len);
+ var path = new TextDecoder("utf-8").decode(path_memory);
+ console.log(path);
+
+ fetch(path)
+ .then(response => response.arrayBuffer())
+ .then(array_buffer => {
+ requested_file_data[fileid] = array_buffer;
+
+ push_event_to_buffer(esp, event_size, 0x09, [ 0x01, fileid, array_buffer.byteLength ]);
+ })
+ .catch((error) => {
+ push_event_to_buffer(esp, event_size, 0x09, [ 0x02, fileid, 0 ]);
+ });
+ },
+
+ get_requested_file_data(fileid, bufferptr, bufferlen) {
+ var file_data = requested_file_data[fileid];
+ if (file_data == null) return 0;
+
+ if (bufferlen < file_data.byteLength) return 0;
+
+ let WASM_U8 = new Uint8Array(ONYX_MEMORY.buffer);
+ var u8_data = new Uint8Array(file_data);
+
+ WASM_U8.set(u8_data, bufferptr);
+
+ requested_file_data[fileid] = null;
+ delete requested_file_data[fileid];
+
+ return 1;
+ },
});
+
background_tile_texture : gfx.Texture;
+on_file_load_callbacks : map.Map(u32, (file_event: ^events.Event) -> void);
+
main :: (args: [] cstr) {
init();
gl.init("main_canvas");
events.init();
gfx.immediate_renderer_init();
- load_colors();
ui.init_ui();
+ map.init(^on_file_load_callbacks);
+
+ color_file := events.request_file("/res/colors_dark.json");
+ map.put(^on_file_load_callbacks, color_file, load_colors);
+
search_buffer = string.buffer_make(memory.make_slice(u8, 256));
gl.enable(gl.BLEND);
test_window.size = .{ 800, 800 };
test_window.border_width = 8.0f;
- test_window.border_color = config.Colors.primary;
- test_window.background_color = config.Colors.dark_background;
- test_window.active_color = config.Colors.background;
-
- load_colors :: () {
- json :: package json
-
- @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_dark.json";
-
- arena := alloc.arena.make(context.allocator, 4096);
- defer alloc.arena.free(^arena);
- colors := json.decode(colors_json, alloc.arena.make_allocator(^arena));
- defer json.free(colors);
-
- config.Colors.dark_background = decode_color(colors.root["dark_background"]);
- config.Colors.background = decode_color(colors.root["background"]);
- config.Colors.foreground = decode_color(colors.root["foreground"]);
-
- config.Colors.keyword = decode_color(colors.root["keyword"]);
- config.Colors.value = decode_color(colors.root["value"]);
- config.Colors.jumppoint = decode_color(colors.root["jumppoint"]);
-
- config.Colors.primary = decode_color(colors.root["primary"]);
- config.Colors.primary_light = decode_color(colors.root["primary_light"]);
- config.Colors.primary_dark = decode_color(colors.root["primary_dark"]);
- config.Colors.primary_text = decode_color(colors.root["primary_text"]);
-
- config.Colors.secondary = decode_color(colors.root["secondary"]);
- config.Colors.secondary_light = decode_color(colors.root["secondary_light"]);
- config.Colors.secondary_dark = decode_color(colors.root["secondary_dark"]);
- config.Colors.secondary_text = decode_color(colors.root["secondary_text"]);
-
- decode_color :: (v: ^json.Value) -> gfx.Color4 {
- return .{
- r = ~~v[0]->as_float(),
- g = ~~v[1]->as_float(),
- b = ~~v[2]->as_float(),
- };
- }
- }
}
gl.setSize(event.resize.width, event.resize.height);
gfx.set_window_size(event.resize.width, event.resize.height);
}
+
+ 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);
+
+ } else {
+ printf("Warning: No callback set for file id {}.\n", event.file.file_id);
+ }
+ }
}
}
update :: (dt: f32) {
t += dt;
+
+ if ui.is_key_just_down(~~#char "L") {
+ color_file := events.request_file("/res/colors_light.json");
+ map.put(^on_file_load_callbacks, color_file, load_colors);
+ }
}
t := 0.0f;
gfx.set_texture();
gfx.pop_matrix();
}
+
+
+@Relocate
+#private_file
+load_colors :: (event: ^events.Event) {
+ json :: package json
+
+ assert(event.kind == .FileRequest, "Bad event type");
+ assert(event.file.status == .Success, "Failed to load color file");
+
+ color_data := memory.make_slice(u8, event.file.size);
+ 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));
+ defer json.free(colors);
+
+ config.Colors.dark_background = decode_color(colors.root["dark_background"]);
+ config.Colors.background = decode_color(colors.root["background"]);
+ config.Colors.foreground = decode_color(colors.root["foreground"]);
+
+ config.Colors.keyword = decode_color(colors.root["keyword"]);
+ config.Colors.value = decode_color(colors.root["value"]);
+ config.Colors.jumppoint = decode_color(colors.root["jumppoint"]);
+
+ config.Colors.primary = decode_color(colors.root["primary"]);
+ config.Colors.primary_light = decode_color(colors.root["primary_light"]);
+ config.Colors.primary_dark = decode_color(colors.root["primary_dark"]);
+ config.Colors.primary_text = decode_color(colors.root["primary_text"]);
+
+ config.Colors.secondary = decode_color(colors.root["secondary"]);
+ config.Colors.secondary_light = decode_color(colors.root["secondary_light"]);
+ config.Colors.secondary_dark = decode_color(colors.root["secondary_dark"]);
+ config.Colors.secondary_text = decode_color(colors.root["secondary_text"]);
+
+ decode_color :: (v: ^json.Value) -> gfx.Color4 {
+ return .{
+ r = ~~v[0]->as_float(),
+ g = ~~v[1]->as_float(),
+ b = ~~v[2]->as_float(),
+ };
+ }
+
+ @Temporary
+ test_window.border_color = config.Colors.primary;
+ test_window.background_color = config.Colors.dark_background;
+ test_window.active_color = config.Colors.background;
+}