Skip to content

Commit 2f72a23

Browse files
committed
Make compass clickable
1 parent 0417ecd commit 2f72a23

File tree

3 files changed

+148
-29
lines changed

3 files changed

+148
-29
lines changed

base/sources/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ function base_update() {
466466
}
467467

468468
base_ext_update();
469+
compass_update();
469470
}
470471

471472
function base_material_dropped() {

base/sources/compass.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

base/sources/render_path_base.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,7 @@ function render_path_base_get_super_sampling(): f32 {
6363
}
6464

6565
function render_path_base_draw_compass() {
66-
if (context_raw.show_compass) {
67-
let cam: camera_object_t = scene_camera;
68-
let compass: mesh_object_t = scene_get_child(".Compass").ext;
69-
70-
let _visible: bool = compass.base.visible;
71-
let _parent: object_t = compass.base.parent;
72-
let _loc: vec4_t = compass.base.transform.loc;
73-
let _rot: quat_t = compass.base.transform.rot;
74-
let crot: quat_t = cam.base.transform.rot;
75-
let ratio: f32 = app_w() / app_h();
76-
let _P: mat4_t = cam.p;
77-
cam.p = mat4_ortho(-8 * ratio, 8 * ratio, -8, 8, -2, 2);
78-
compass.base.visible = true;
79-
compass.base.parent = cam.base;
80-
compass.base.transform.loc = vec4_create(7.4 * ratio, 7.0, -1);
81-
compass.base.transform.rot = quat_create(-crot.x, -crot.y, -crot.z, crot.w);
82-
compass.base.transform.scale = vec4_create(0.4, 0.4, 0.4);
83-
transform_build_matrix(compass.base.transform);
84-
compass.frustum_culling = false;
85-
let empty: string[] = [];
86-
mesh_object_render(compass, "overlay", empty);
87-
88-
cam.p = _P;
89-
compass.base.visible = _visible;
90-
compass.base.parent = _parent;
91-
compass.base.transform.loc = _loc;
92-
compass.base.transform.rot = _rot;
93-
transform_build_matrix(compass.base.transform);
94-
}
66+
compass_render();
9567
}
9668

9769
function render_path_base_begin() {
@@ -515,6 +487,8 @@ function render_path_base_draw_gbuffer() {
515487
let hide: bool = operator_shortcut(map_get(config_keymap, "stencil_hide"), shortcut_type_t.DOWN) || keyboard_down("control");
516488
let is_decal: bool = base_is_decal_layer();
517489
if (is_decal && !hide) {
490+
line_draw_color = 0xff000000;
491+
line_draw_strength = 0.005;
518492
line_draw_render(context_raw.layer.decal_mat);
519493
}
520494
}

0 commit comments

Comments
 (0)