window_js.odin 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #+build js
  2. #+private file
  3. package karl2d
  4. @(private="package")
  5. WINDOW_INTERFACE_JS :: Window_Interface {
  6. state_size = js_state_size,
  7. init = js_init,
  8. shutdown = js_shutdown,
  9. window_handle = js_window_handle,
  10. process_events = js_process_events,
  11. get_events = js_get_events,
  12. get_width = js_get_width,
  13. get_height = js_get_height,
  14. clear_events = js_clear_events,
  15. set_position = js_set_position,
  16. set_size = js_set_size,
  17. get_window_scale = js_get_window_scale,
  18. set_flags = js_set_flags,
  19. is_gamepad_active = js_is_gamepad_active,
  20. get_gamepad_axis = js_get_gamepad_axis,
  21. set_gamepad_vibration = js_set_gamepad_vibration,
  22. set_internal_state = js_set_internal_state,
  23. }
  24. import "core:sys/wasm/js"
  25. import "base:runtime"
  26. import "core:log"
  27. import "core:fmt"
  28. js_state_size :: proc() -> int {
  29. return size_of(JS_State)
  30. }
  31. js_init :: proc(
  32. window_state: rawptr,
  33. window_width: int,
  34. window_height: int,
  35. window_title: string,
  36. flags: Window_Flags,
  37. allocator: runtime.Allocator,
  38. ) {
  39. s = (^JS_State)(window_state)
  40. s.allocator = allocator
  41. s.canvas_id = "webgl-canvas"
  42. // The browser window probably has some other size than what was sent in.
  43. if .Resizable in flags {
  44. add_window_event_listener(.Resize, js_window_event_resize)
  45. update_canvas_size(s.canvas_id)
  46. } else {
  47. js_set_size(window_width, window_height)
  48. }
  49. add_canvas_event_listener(.Mouse_Move, js_window_event_mouse_move)
  50. add_canvas_event_listener(.Mouse_Down, js_window_event_mouse_down)
  51. add_canvas_event_listener(.Mouse_Up, js_window_event_mouse_up)
  52. add_window_event_listener(.Key_Down, js_window_event_key_down)
  53. add_window_event_listener(.Key_Up, js_window_event_key_up)
  54. }
  55. add_window_event_listener :: proc(evt: js.Event_Kind, callback: proc(e: js.Event)) {
  56. js.add_window_event_listener(
  57. evt,
  58. nil,
  59. callback,
  60. true,
  61. )
  62. }
  63. add_canvas_event_listener :: proc(evt: js.Event_Kind, callback: proc(e: js.Event)) {
  64. js.add_event_listener(
  65. s.canvas_id,
  66. evt,
  67. nil,
  68. callback,
  69. true,
  70. )
  71. }
  72. js_window_event_key_down :: proc(e: js.Event) {
  73. if e.key.repeat {
  74. return
  75. }
  76. key := key_from_js_event(e)
  77. append(&s.events, Window_Event_Key_Went_Down {
  78. key = key,
  79. })
  80. }
  81. js_window_event_key_up :: proc(e: js.Event) {
  82. key := key_from_js_event(e)
  83. append(&s.events, Window_Event_Key_Went_Up {
  84. key = key,
  85. })
  86. }
  87. js_window_event_resize :: proc(e: js.Event) {
  88. update_canvas_size(s.canvas_id)
  89. }
  90. js_window_event_mouse_move :: proc(e: js.Event) {
  91. dpi := js.device_pixel_ratio()
  92. append(&s.events, Window_Event_Mouse_Move {
  93. position = {f32(e.mouse.client.x) * f32(dpi), f32(e.mouse.client.y) * f32(dpi)},
  94. })
  95. }
  96. js_window_event_mouse_down :: proc(e: js.Event) {
  97. append(&s.events, Window_Event_Mouse_Button_Went_Down {
  98. button = .Left,
  99. })
  100. }
  101. js_window_event_mouse_up :: proc(e: js.Event) {
  102. append(&s.events, Window_Event_Mouse_Button_Went_Up {
  103. button = .Left,
  104. })
  105. }
  106. update_canvas_size :: proc(canvas_id: HTML_Canvas_ID) {
  107. rect := js.get_bounding_client_rect("body")
  108. width := f64(rect.width)
  109. height := f64(rect.height)
  110. js.set_element_key_f64(canvas_id, "width", width)
  111. js.set_element_key_f64(canvas_id, "height", height)
  112. s.width = int(rect.width)
  113. s.height = int(rect.height)
  114. append(&s.events, Window_Event_Resize {
  115. width = int(width),
  116. height = int(height),
  117. })
  118. }
  119. js_shutdown :: proc() {
  120. }
  121. js_window_handle :: proc() -> Window_Handle {
  122. return Window_Handle(&s.canvas_id)
  123. }
  124. js_process_events :: proc() {
  125. //for gamepad_idx in 0..<4 {
  126. /*prev_state := s.gamepad_state[gamepad_idx]
  127. if js.get_gamepad_state(s.gamepad_state[gamepad_idx], &gs) && gs.connected {
  128. log.info(gs)
  129. }*/
  130. // }
  131. /*
  132. for gamepad in 0..<4 {
  133. gp_event: win32.XINPUT_KEYSTROKE
  134. for win32.XInputGetKeystroke(win32.XUSER(gamepad), 0, &gp_event) == .SUCCESS {
  135. button: Maybe(Gamepad_Button)
  136. #partial switch gp_event.VirtualKey {
  137. case .DPAD_UP: button = .Left_Face_Up
  138. case .DPAD_DOWN: button = .Left_Face_Down
  139. case .DPAD_LEFT: button = .Left_Face_Left
  140. case .DPAD_RIGHT: button = .Left_Face_Right
  141. case .Y: button = .Right_Face_Up
  142. case .A: button = .Right_Face_Down
  143. case .X: button = .Right_Face_Left
  144. case .B: button = .Right_Face_Right
  145. case .LSHOULDER: button = .Left_Shoulder
  146. case .LTRIGGER: button = .Left_Trigger
  147. case .RSHOULDER: button = .Right_Shoulder
  148. case .RTRIGGER: button = .Right_Trigger
  149. case .BACK: button = .Middle_Face_Left
  150. // Not sure you can get the "middle button" with XInput (the one that goe to dashboard)
  151. case .START: button = .Middle_Face_Right
  152. case .LTHUMB_PRESS: button = .Left_Stick_Press
  153. case .RTHUMB_PRESS: button = .Right_Stick_Press
  154. }
  155. b := button.? or_continue
  156. evt: Window_Event
  157. if .KEYDOWN in gp_event.Flags {
  158. evt = Window_Event_Gamepad_Button_Went_Down {
  159. gamepad = gamepad,
  160. button = b,
  161. }
  162. } else if .KEYUP in gp_event.Flags {
  163. evt = Window_Event_Gamepad_Button_Went_Up {
  164. gamepad = gamepad,
  165. button = b,
  166. }
  167. }
  168. if evt != nil {
  169. append(&s.events, evt)
  170. }
  171. */
  172. }
  173. js_get_events :: proc() -> []Window_Event {
  174. return s.events[:]
  175. }
  176. js_get_width :: proc() -> int {
  177. return s.width
  178. }
  179. js_get_height :: proc() -> int {
  180. return s.height
  181. }
  182. js_clear_events :: proc() {
  183. runtime.clear(&s.events)
  184. }
  185. js_set_position :: proc(x: int, y: int) {
  186. buf: [256]u8
  187. js.set_element_style(s.canvas_id, "margin-top", fmt.bprintf(buf[:], "%vpx", x))
  188. js.set_element_style(s.canvas_id, "margin-left", fmt.bprintf(buf[:], "%vpx", y))
  189. }
  190. js_set_size :: proc(w, h: int) {
  191. s.width = w
  192. s.height = h
  193. js.set_element_key_f64(s.canvas_id, "width", f64(w))
  194. js.set_element_key_f64(s.canvas_id, "height", f64(h))
  195. }
  196. js_get_window_scale :: proc() -> f32 {
  197. return f32(js.device_pixel_ratio())
  198. }
  199. js_set_flags :: proc(flags: Window_Flags) {
  200. }
  201. js_is_gamepad_active :: proc(gamepad: int) -> bool {
  202. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  203. return false
  204. }
  205. gs: js.Gamepad_State
  206. return js.get_gamepad_state(gamepad, &gs) && gs.connected
  207. }
  208. js_get_gamepad_axis :: proc(gamepad: int, axis: Gamepad_Axis) -> f32 {
  209. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  210. return 0
  211. }
  212. return 0
  213. }
  214. js_set_gamepad_vibration :: proc(gamepad: int, left: f32, right: f32) {
  215. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  216. return
  217. }
  218. }
  219. js_set_internal_state :: proc(state: rawptr) {
  220. assert(state != nil)
  221. s = (^JS_State)(state)
  222. }
  223. @(private="package")
  224. HTML_Canvas_ID :: string
  225. JS_State :: struct {
  226. allocator: runtime.Allocator,
  227. canvas_id: HTML_Canvas_ID,
  228. width: int,
  229. height: int,
  230. events: [dynamic]Window_Event,
  231. gamepad_state: [MAX_GAMEPADS]js.Gamepad_State,
  232. }
  233. s: ^JS_State
  234. key_from_js_event :: proc(e: js.Event) -> Keyboard_Key {
  235. switch e.key.code {
  236. case "Digit1": return .N1
  237. case "Digit2": return .N2
  238. case "Digit3": return .N3
  239. case "Digit4": return .N4
  240. case "Digit5": return .N5
  241. case "Digit6": return .N6
  242. case "Digit7": return .N7
  243. case "Digit8": return .N8
  244. case "Digit9": return .N9
  245. case "Digit0": return .N0
  246. case "KeyA": return .A
  247. case "KeyB": return .B
  248. case "KeyC": return .C
  249. case "KeyD": return .D
  250. case "KeyE": return .E
  251. case "KeyF": return .F
  252. case "KeyG": return .G
  253. case "KeyH": return .H
  254. case "KeyI": return .I
  255. case "KeyJ": return .J
  256. case "KeyK": return .K
  257. case "KeyL": return .L
  258. case "KeyM": return .M
  259. case "KeyN": return .N
  260. case "KeyO": return .O
  261. case "KeyP": return .P
  262. case "KeyQ": return .Q
  263. case "KeyR": return .R
  264. case "KeyS": return .S
  265. case "KeyT": return .T
  266. case "KeyU": return .U
  267. case "KeyV": return .V
  268. case "KeyW": return .W
  269. case "KeyX": return .X
  270. case "KeyY": return .Y
  271. case "KeyZ": return .Z
  272. case "Quote": return .Apostrophe
  273. case "Comma": return .Comma
  274. case "Minus": return .Minus
  275. case "Period": return .Period
  276. case "Slash": return .Slash
  277. case "Semicolon": return .Semicolon
  278. case "Equal": return .Equal
  279. case "BracketLeft": return .Left_Bracket
  280. case "Backslash": return .Backslash
  281. case "IntlBackslash": return .Backslash
  282. case "BracketRight": return .Right_Bracket
  283. case "Backquote": return .Backtick
  284. case "Space": return .Space
  285. case "Escape": return .Escape
  286. case "Enter": return .Enter
  287. case "Tab": return .Tab
  288. case "Backspace": return .Backspace
  289. case "Insert": return .Insert
  290. case "Delete": return .Delete
  291. case "ArrowRight": return .Right
  292. case "ArrowLeft": return .Left
  293. case "ArrowDown": return .Down
  294. case "ArrowUp": return .Up
  295. case "PageUp": return .Page_Up
  296. case "PageDown": return .Page_Down
  297. case "Home": return .Home
  298. case "End": return .End
  299. case "CapsLock": return .Caps_Lock
  300. case "ScrollLock": return .Scroll_Lock
  301. case "NumLock": return .Num_Lock
  302. case "PrintScreen": return .Print_Screen
  303. case "Pause": return .Pause
  304. case "F1": return .F1
  305. case "F2": return .F2
  306. case "F3": return .F3
  307. case "F4": return .F4
  308. case "F5": return .F5
  309. case "F6": return .F6
  310. case "F7": return .F7
  311. case "F8": return .F8
  312. case "F9": return .F9
  313. case "F10": return .F10
  314. case "F11": return .F11
  315. case "F12": return .F12
  316. case "ShiftLeft": return .Left_Shift
  317. case "ControlLeft": return .Left_Control
  318. case "AltLeft": return .Left_Alt
  319. case "MetaLeft": return .Left_Super
  320. case "ShiftRight": return .Right_Shift
  321. case "ControlRight": return .Right_Control
  322. case "AltRight": return .Right_Alt
  323. case "MetaRight": return .Right_Super
  324. case "ContextMenu": return .Menu
  325. case "Numpad0": return .NP_0
  326. case "Numpad1": return .NP_1
  327. case "Numpad2": return .NP_2
  328. case "Numpad3": return .NP_3
  329. case "Numpad4": return .NP_4
  330. case "Numpad5": return .NP_5
  331. case "Numpad6": return .NP_6
  332. case "Numpad7": return .NP_7
  333. case "Numpad8": return .NP_8
  334. case "Numpad9": return .NP_9
  335. case "NumpadDecimal": return .NP_Decimal
  336. case "NumpadDivide": return .NP_Divide
  337. case "NumpadMultiply": return .NP_Multiply
  338. case "NumpadSubtract": return .NP_Subtract
  339. case "NumpadAdd": return .NP_Add
  340. case "NumpadEnter": return .NP_Enter
  341. }
  342. log.errorf("Unhandled key code: %v", e.key.code)
  343. return .None
  344. }