window_js.odin 9.8 KB

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