|
| 1 | + |
| 2 | +let _compass_hitbox_x: object_t; |
| 3 | +let _compass_hitbox_y: object_t; |
| 4 | +let _compass_hitbox_z: object_t; |
| 5 | +let _compass_hovered: object_t = null; |
| 6 | +let _compass_hovered_last: object_t = null; |
| 7 | + |
| 8 | +function compass_render() { |
| 9 | + if (!context_raw.show_compass) { |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + let cam: camera_object_t = scene_camera; |
| 14 | + let compass: mesh_object_t = scene_get_child(".Compass").ext; |
| 15 | + |
| 16 | + let _visible: bool = compass.base.visible; |
| 17 | + let _parent: object_t = compass.base.parent; |
| 18 | + let crot: quat_t = cam.base.transform.rot; |
| 19 | + let ratio: f32 = app_w() / app_h(); |
| 20 | + let _P: mat4_t = cam.p; |
| 21 | + cam.p = mat4_ortho(-8 * ratio, 8 * ratio, -8, 8, -2, 2); |
| 22 | + compass.base.visible = true; |
| 23 | + compass.base.parent = cam.base; |
| 24 | + compass.base.transform.loc = vec4_create(7.4 * ratio, 7.0, -1); |
| 25 | + compass.base.transform.rot = quat_create(-crot.x, -crot.y, -crot.z, crot.w); |
| 26 | + compass.base.transform.scale = vec4_create(0.4, 0.4, 0.4); |
| 27 | + transform_build_matrix(compass.base.transform); |
| 28 | + compass.frustum_culling = false; |
| 29 | + mesh_object_render(compass, "overlay", null); |
| 30 | + |
| 31 | + if (_compass_hovered != null) { |
| 32 | + line_draw_color = _compass_hovered == _compass_hitbox_x ? 0xffff0000 : |
| 33 | + _compass_hovered == _compass_hitbox_y ? 0xff00ff00 : |
| 34 | + 0xff0000ff; |
| 35 | + line_draw_strength = 0.1; |
| 36 | + shape_draw_sphere(_compass_hovered.transform.world); |
| 37 | + } |
| 38 | + |
| 39 | + cam.p = _P; |
| 40 | + compass.base.visible = _visible; |
| 41 | + compass.base.parent = _parent; |
| 42 | +} |
| 43 | + |
| 44 | +function compass_init_hitbox() { |
| 45 | + if (_compass_hitbox_x != null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + _compass_hitbox_x = object_create(); |
| 50 | + _compass_hitbox_y = object_create(); |
| 51 | + _compass_hitbox_z = object_create(); |
| 52 | + |
| 53 | + _compass_hitbox_x.transform.scale = vec4_create(0.15, 0.15, 0.15); |
| 54 | + _compass_hitbox_y.transform.scale = vec4_create(0.15, 0.15, 0.15); |
| 55 | + _compass_hitbox_z.transform.scale = vec4_create(0.15, 0.15, 0.15); |
| 56 | + |
| 57 | + _compass_hitbox_x.transform.loc = vec4_create(1.5, 0, 0); |
| 58 | + _compass_hitbox_y.transform.loc = vec4_create(0, 1.5, 0); |
| 59 | + _compass_hitbox_z.transform.loc = vec4_create(0, 0, 1.5); |
| 60 | + |
| 61 | + let compass: object_t = scene_get_child(".Compass"); |
| 62 | + object_set_parent(_compass_hitbox_x, compass); |
| 63 | + object_set_parent(_compass_hitbox_y, compass); |
| 64 | + object_set_parent(_compass_hitbox_z, compass); |
| 65 | +} |
| 66 | + |
| 67 | +function _compass_compare_quat(a: quat_t, b: quat_t): bool { |
| 68 | + return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; |
| 69 | +} |
| 70 | + |
| 71 | +function compass_update() { |
| 72 | + if (!context_raw.show_compass) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (_compass_hovered_last != _compass_hovered) { |
| 77 | + _compass_hovered_last = _compass_hovered; |
| 78 | + context_raw.ddirty = 2; |
| 79 | + } |
| 80 | + _compass_hovered = null; |
| 81 | + |
| 82 | + let x: f32 = mouse_view_x() / app_w(); |
| 83 | + let y: f32 = mouse_view_y() / app_h(); |
| 84 | + let hover: bool = x > 0.9 && x < 1.0 && y < 0.14 && y > 0.0; |
| 85 | + if (hover) { |
| 86 | + |
| 87 | + compass_init_hitbox(); |
| 88 | + |
| 89 | + let ratio: f32 = app_w() / app_h(); |
| 90 | + let _P: mat4_t = scene_camera.p; |
| 91 | + scene_camera.p = mat4_ortho(-8 * ratio, 8 * ratio, -8, 8, -2, 2); |
| 92 | + |
| 93 | + transform_build_matrix(_compass_hitbox_x.transform); |
| 94 | + transform_build_matrix(_compass_hitbox_y.transform); |
| 95 | + transform_build_matrix(_compass_hitbox_z.transform); |
| 96 | + |
| 97 | + let ts: transform_t[] = [ |
| 98 | + _compass_hitbox_x.transform, |
| 99 | + _compass_hitbox_y.transform, |
| 100 | + _compass_hitbox_z.transform |
| 101 | + ]; |
| 102 | + |
| 103 | + let t: transform_t = raycast_closest_box_intersect(ts, mouse_view_x(), mouse_view_y(), scene_camera); |
| 104 | + if (t != null) { |
| 105 | + let cq: quat_t = scene_camera.base.transform.rot; |
| 106 | + |
| 107 | + if (t == _compass_hitbox_x.transform) { |
| 108 | + if (mouse_started()) { |
| 109 | + // Flip between left / right |
| 110 | + _compass_compare_quat(quat_from_euler(math_pi() / 2, 0, math_pi() / 2), cq) ? |
| 111 | + // Left |
| 112 | + viewport_set_view(-1, 0, 0, math_pi() / 2, 0, -math_pi() / 2) : |
| 113 | + // Right |
| 114 | + viewport_set_view(1, 0, 0, math_pi() / 2, 0, math_pi() / 2); |
| 115 | + } |
| 116 | + _compass_hovered = _compass_hitbox_x; |
| 117 | + } |
| 118 | + |
| 119 | + else if (t == _compass_hitbox_y.transform) { |
| 120 | + if (mouse_started()) { |
| 121 | + _compass_compare_quat(quat_from_euler(math_pi() / 2, 0, math_pi()), cq) ? |
| 122 | + // Front |
| 123 | + viewport_set_view(0, -1, 0, math_pi() / 2, 0, 0) : |
| 124 | + // Back |
| 125 | + viewport_set_view(0, 1, 0, math_pi() / 2, 0, math_pi()); |
| 126 | + } |
| 127 | + _compass_hovered = _compass_hitbox_y; |
| 128 | + } |
| 129 | + |
| 130 | + else { |
| 131 | + if (mouse_started()) { |
| 132 | + _compass_compare_quat(quat_from_euler(0, 0, 0), cq) ? |
| 133 | + // Bottom |
| 134 | + viewport_set_view(0, 0, -1, math_pi(), 0, math_pi()) : |
| 135 | + // Top |
| 136 | + viewport_set_view(0, 0, 1, 0, 0, 0); |
| 137 | + } |
| 138 | + _compass_hovered = _compass_hitbox_z; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + scene_camera.p = _P; |
| 143 | + } |
| 144 | +} |
0 commit comments