window_js.odin 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. js.add_window_event_listener(.Resize, nil, js_window_event_resize, true)
  44. update_canvas_size(s.canvas_id)
  45. } else {
  46. set_window_size(window_width, window_height)
  47. }
  48. }
  49. js_window_event_resize :: proc(e: js.Event) {
  50. update_canvas_size(s.canvas_id)
  51. }
  52. update_canvas_size :: proc(canvas_id: HTML_Canvas_ID) {
  53. rect := js.get_bounding_client_rect(canvas_id)
  54. dpi := js.device_pixel_ratio()
  55. width := f64(rect.width) * dpi
  56. height := f64(rect.height) * dpi
  57. js.set_element_key_f64(canvas_id, "width", width)
  58. js.set_element_key_f64(canvas_id, "height", height)
  59. s.width = int(width)
  60. s.height = int(height)
  61. append(&s.events, Window_Event_Resize {
  62. width = int(width),
  63. height = int(height),
  64. })
  65. }
  66. js_shutdown :: proc() {
  67. }
  68. js_window_handle :: proc() -> Window_Handle {
  69. return Window_Handle(&s.canvas_id)
  70. }
  71. js_process_events :: proc() {
  72. }
  73. js_get_events :: proc() -> []Window_Event {
  74. return s.events[:]
  75. }
  76. js_get_width :: proc() -> int {
  77. return s.width
  78. }
  79. js_get_height :: proc() -> int {
  80. return s.height
  81. }
  82. js_clear_events :: proc() {
  83. runtime.clear(&s.events)
  84. }
  85. js_set_position :: proc(x: int, y: int) {
  86. log.error("set_position not implemented in JS")
  87. }
  88. js_set_size :: proc(w, h: int) {
  89. dpi := js.device_pixel_ratio()
  90. width := f64(w) * dpi
  91. height := f64(h) * dpi
  92. js.set_element_key_f64(s.canvas_id, "width", width)
  93. js.set_element_key_f64(s.canvas_id, "height", height)
  94. }
  95. js_get_window_scale :: proc() -> f32 {
  96. return f32(js.device_pixel_ratio())
  97. }
  98. js_set_flags :: proc(flags: Window_Flags) {
  99. }
  100. js_is_gamepad_active :: proc(gamepad: int) -> bool {
  101. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  102. return false
  103. }
  104. return false
  105. }
  106. js_get_gamepad_axis :: proc(gamepad: int, axis: Gamepad_Axis) -> f32 {
  107. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  108. return 0
  109. }
  110. return 0
  111. }
  112. js_set_gamepad_vibration :: proc(gamepad: int, left: f32, right: f32) {
  113. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  114. return
  115. }
  116. }
  117. js_set_internal_state :: proc(state: rawptr) {
  118. assert(state != nil)
  119. s = (^JS_State)(state)
  120. }
  121. JS_State :: struct {
  122. allocator: runtime.Allocator,
  123. canvas_id: HTML_Canvas_ID,
  124. width: int,
  125. height: int,
  126. events: [dynamic]Window_Event,
  127. }
  128. s: ^JS_State
  129. @(private="package")
  130. HTML_Canvas_ID :: string