Browse Source

Add more procedures for window related positions

gingerBill 3 years ago
parent
commit
43b350c590
2 changed files with 56 additions and 8 deletions
  1. 24 6
      vendor/wasm/js/dom.odin
  2. 32 2
      vendor/wasm/js/runtime.js

+ 24 - 6
vendor/wasm/js/dom.odin

@@ -6,7 +6,14 @@ foreign import dom_lib "odin_dom"
 @(default_calling_convention="contextless")
 foreign dom_lib {
 	get_element_value_f64    :: proc(id: string) -> f64 ---
-	set_element_value        :: proc(id: string, value: f64) ---
+	set_element_value_f64    :: proc(id: string, value: f64) ---
+
+	set_element_value_string :: proc(id: string, value: string) ---
+	get_element_value_string_length :: proc(id: string) -> int ---
+
+	device_pixel_ratio :: proc() -> f64 ---
+
+	window_set_scroll :: proc(x, y: f64) ---
 }
 
 get_element_value_string :: proc "contextless" (id: string, buf: []byte) -> string {
@@ -47,12 +54,23 @@ get_bounding_client_rect :: proc "contextless" (id: string) -> (rect: Rect) {
 	return
 }
 
-get_window_rect :: proc "contextless" () -> (rect: Rect) {
+window_get_rect :: proc "contextless" () -> (rect: Rect) {
+	@(default_calling_convention="contextless")
+	foreign dom_lib {
+		@(link_name="window_get_rect")
+		_window_get_rect :: proc(rect: ^Rect) ---
+	}
+	_window_get_rect(&rect)
+	return
+}
+
+window_get_scroll :: proc "contextless" () -> (x, y: f64) {
 	@(default_calling_convention="contextless")
 	foreign dom_lib {
-		@(link_name="get_window_rect")
-		_get_window_rect :: proc(rect: ^Rect) ---
+		@(link_name="window_get_scroll")
+		_window_get_scroll :: proc(scroll: ^[2]f64) ---
 	}
-	_get_window_rect(&rect)
+	scroll := [2]f64{x, y}
+	_window_get_scroll(&scroll)
 	return
-}
+}

+ 32 - 2
vendor/wasm/js/runtime.js

@@ -1533,6 +1533,14 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
 				}
 				return 0;
 			},
+			get_element_value_string_length: (id_ptr, id_len) => {
+				let id = wasmMemoryInterface.loadString(id_ptr, id_len);
+				let element = document.getElementById(id);
+				if (element) {
+					return element.value.length;
+				}
+				return 0;
+			},
 			get_element_min_max: (ptr_array2_f64, id_ptr, id_len) => {
 				let id = wasmMemoryInterface.loadString(id_ptr, id_len);
 				let element = document.getElementById(id);
@@ -1542,14 +1550,23 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
 					values[1] = element.max;
 				}
 			},
-			set_element_value: (id_ptr, id_len, value) => {
+			set_element_value_f64: (id_ptr, id_len, value) => {
+				let id = wasmMemoryInterface.loadString(id_ptr, id_len);
+				let element = document.getElementById(id);
+				if (element) {
+					element.value = value;
+				}
+			},
+			set_element_value_string: (id_ptr, id_len, value_ptr, value_id) => {
 				let id = wasmMemoryInterface.loadString(id_ptr, id_len);
+				let value = wasmMemoryInterface.loadString(value_ptr, value_len);
 				let element = document.getElementById(id);
 				if (element) {
 					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);
@@ -1562,7 +1579,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
 					values[3] = rect.bottom - rect.top;
 				}
 			},
-			get_window_rect: (rect_ptr) => {
+			window_get_rect: (rect_ptr) => {
 				let values = wasmMemoryInterface.loadF64Array(rect_ptr, 4);
 				values[0] = window.screenX;
 				values[1] = window.screenY;
@@ -1570,6 +1587,19 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
 				values[3] = window.screen.height;
 			},
 
+			window_get_scroll: (pos_ptr) => {
+				let values = wasmMemoryInterface.loadF64Array(pos_ptr, 2);
+				values[0] = window.scrollX;
+				values[1] = window.scrollY;
+			},
+			window_set_scroll: (x, y) => {
+				window.scroll(x, y);
+			},
+
+			device_pixel_ratio: () => {
+				return window.devicePixelRatio;
+			},
+
 		},
 
 		"webgl": webglContext.getWebGL1Interface(),