Browse Source

Add utilities for `Rect`s

gingerBill 3 years ago
parent
commit
d7681d5b06
2 changed files with 59 additions and 2 deletions
  1. 38 2
      vendor/wasm/js/dom.odin
  2. 21 0
      vendor/wasm/js/runtime.js

+ 38 - 2
vendor/wasm/js/dom.odin

@@ -6,11 +6,10 @@ foreign import dom_lib "odin_dom"
 @(default_calling_convention="contextless")
 foreign dom_lib {
 	get_element_value_f64    :: proc(id: string) -> f64 ---
-	get_element_min_max      :: proc(id: string) -> (min, max: f64) ---
 	set_element_value        :: proc(id: string, value: f64) ---
 }
 
-get_element_value_string :: proc(id: string, buf: []byte) -> string {
+get_element_value_string :: proc "contextless" (id: string, buf: []byte) -> string {
 	@(default_calling_convention="contextless")
 	foreign dom_lib {
 		@(link_name="get_element_value_string")
@@ -20,3 +19,40 @@ get_element_value_string :: proc(id: string, buf: []byte) -> string {
 	return string(buf[:n])
 
 }
+
+
+get_element_min_max :: proc "contextless" (id: string) -> (min, max: f64) {
+	@(default_calling_convention="contextless")
+	foreign dom_lib {
+		@(link_name="get_element_min_max")
+		_get_element_min_max :: proc(min_max: ^[2]f64, id: string) ---
+	}
+	min_max: [2]f64
+	_get_element_min_max(&min_max, id)
+	return min_max[0], min_max[1]
+}
+
+
+Rect :: struct {
+	x, y, width, height: f64,
+}
+
+get_bounding_client_rect :: proc "contextless" (id: string) -> (rect: Rect) {
+	@(default_calling_convention="contextless")
+	foreign dom_lib {
+		@(link_name="get_bounding_client_rect")
+		_get_bounding_client_rect :: proc(rect: ^Rect, id: string) ---
+	}
+	_get_bounding_client_rect(&rect, id)
+	return
+}
+
+get_window_rect :: proc "contextless" () -> (rect: Rect) {
+	@(default_calling_convention="contextless")
+	foreign dom_lib {
+		@(link_name="get_window_rect")
+		_get_window_rect :: proc(rect: ^Rect) ---
+	}
+	_get_window_rect(&rect)
+	return
+}

+ 21 - 0
vendor/wasm/js/runtime.js

@@ -1549,6 +1549,27 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
 					element.value = value;
 				}
 			},
+
+			get_bounding_client_rect: (rect_ptr, id_ptr, id_len) => {
+				let id = wasmMemoryInterface.loadString(id_ptr, id_len);
+				let element = document.getElementById(id);
+				if (element) {
+					let values = wasmMemoryInterface.loadF64Array(rect_ptr, 4);
+					let rect = element.getBoundingClientRect();
+					values[0] = rect.left;
+					values[1] = rect.top;
+					values[2] = rect.right  - rect.left;
+					values[3] = rect.bottom - rect.top;
+				}
+			},
+			get_window_rect: (rect_ptr) => {
+				let values = wasmMemoryInterface.loadF64Array(rect_ptr, 4);
+				values[0] = window.screenX;
+				values[1] = window.screenY;
+				values[2] = window.screen.width;
+				values[3] = window.screen.height;
+			},
+
 		},
 
 		"webgl": webglContext.getWebGL1Interface(),