imoguizmo 0.4.0

Interactive orientation gizmo
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use glam::{Mat4, Vec2, Vec3, Vec4};
use imgui::{DrawListMut, ImColor32, MouseButton, Ui};
use std::cmp::Ordering;
use std::f32;
use std::sync::LazyLock;

pub mod internal {
    use std::cell::RefCell;

    #[derive(Debug, Clone, Copy)]
    pub struct RectConfig {
        pub m_x: f32,
        pub m_y: f32,
        pub m_size: f32,
    }

    impl Default for RectConfig {
        fn default() -> Self {
            Self {
                m_x: 0.0,
                m_y: 0.0,
                m_size: 100.0,
            }
        }
    }

    thread_local! {
        pub static RECT: RefCell<RectConfig> = RefCell::new(RectConfig::default());
    }

    #[derive(Debug, Clone, Copy)]
    pub struct DragState {
        pub active: bool,
        pub last_mouse: [f32; 2],
        pub yaw: f32,
        pub pitch: f32,
    }

    impl Default for DragState {
        fn default() -> Self {
            Self {
                active: false,
                last_mouse: [0.0, 0.0],
                yaw: 0.0,
                pitch: 0.0,
            }
        }
    }

    thread_local! {
        pub static DRAG_STATE: RefCell<DragState> = RefCell::new(DragState::default());
    }
}

#[inline]
pub fn check_inside_circle(center: Vec2, radius: f32, point: Vec2) -> bool {
    let dx = point.x - center.x;
    let dy = point.y - center.y;
    dx * dx + dy * dy <= radius * radius
}

#[allow(clippy::too_many_arguments)]
#[inline]
fn draw_positive_line(
    ui: &Ui,
    draw_list: &mut DrawListMut,
    center: Vec2,
    axis: Vec2,
    color: ImColor32,
    radius: f32,
    thickness: f32,
    text: &str,
    selected: bool,
) {
    let line_end = Vec2 {
        x: center.x + axis.x,
        y: center.y + axis.y,
    };
    draw_list.add_line(center, line_end, color).thickness(thickness).build();
    draw_list.add_circle(line_end, radius, color).filled(true).build();

    // text size from ImGui
    let label_size = imgui::Ui::calc_text_size(ui, text);
    let text_pos = Vec2 {
        x: (line_end.x - 0.5 * label_size[0]).floor(),
        y: (line_end.y - 0.5 * label_size[1]).floor(),
    };
    if selected {
        draw_list.add_circle(line_end, radius, ImColor32::WHITE).build();
        draw_list.add_text(text_pos, ImColor32::WHITE, text);
    } else {
        draw_list.add_text(text_pos, ImColor32::BLACK, text);
    }
}

#[inline]
fn draw_negative_line(
    draw_list: &mut DrawListMut,
    center: Vec2,
    axis: Vec2,
    color: ImColor32,
    radius: f32,
    selected: bool,
) {
    let line_end = Vec2 {
        x: center.x - axis.x,
        y: center.y - axis.y,
    };
    draw_list.add_circle(line_end, radius, color).filled(true).build();
    if selected {
        draw_list.add_circle(line_end, radius, ImColor32::WHITE).build();
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Config {
    pub line_thickness_scale: f32,
    pub axis_length_scale: f32,
    pub positive_radius_scale: f32,
    pub negative_radius_scale: f32,
    pub hover_circle_radius_scale: f32,
    pub x_circle_front_color: ImColor32,
    pub x_circle_back_color: ImColor32,
    pub y_circle_front_color: ImColor32,
    pub y_circle_back_color: ImColor32,
    pub z_circle_front_color: ImColor32,
    pub z_circle_back_color: ImColor32,
    pub hover_circle_color: ImColor32,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            line_thickness_scale: 0.017,
            axis_length_scale: 0.33,
            positive_radius_scale: 0.075,
            negative_radius_scale: 0.05,
            hover_circle_radius_scale: 0.88,
            x_circle_front_color: ImColor32::from_rgba(255, 54, 83, 255),
            x_circle_back_color: ImColor32::from_rgba(154, 57, 71, 255),
            y_circle_front_color: ImColor32::from_rgba(138, 219, 0, 255),
            y_circle_back_color: ImColor32::from_rgba(98, 138, 34, 255),
            z_circle_front_color: ImColor32::from_rgba(44, 143, 255, 255),
            z_circle_back_color: ImColor32::from_rgba(52, 100, 154, 255),
            hover_circle_color: ImColor32::from_rgba(100, 100, 100, 130),
        }
    }
}

pub static CONFIG: LazyLock<Config> = LazyLock::new(Config::default);

// Set the square rect where the gizmo will be drawn (top-left x,y and size)
pub fn set_rect(x: f32, y: f32, size: f32) {
    internal::RECT.with(|r| {
        let mut r = r.borrow_mut();
        r.m_x = x;
        r.m_y = y;
        r.m_size = size;
    });
}

// Positions an invisible window for the gizmo.
pub fn begin_frame<R, F: FnOnce() -> R>(ui: &Ui, background: bool, f: F) -> Option<R> {
    use imgui::{Condition, WindowFlags};

    let (pos, size) = internal::RECT.with(|r| {
        let r = *r.borrow();
        ([r.m_x, r.m_y], r.m_size)
    });

    let mut window = ui.window("imoguizmo");
    window = window
        .position(pos, Condition::Always)
        .size([size, size], Condition::Always)
        .flags(
            WindowFlags::NO_DECORATION
                | WindowFlags::NO_INPUTS
                | WindowFlags::NO_SAVED_SETTINGS
                | WindowFlags::NO_FOCUS_ON_APPEARING
                | WindowFlags::NO_BRING_TO_FRONT_ON_FOCUS
                | if !background {
                    WindowFlags::NO_BACKGROUND
                } else {
                    WindowFlags::empty()
                },
        );

    window.build(f)
}

pub fn draw_gizmo(
    ui: &Ui,
    view_matrix: mint::ColumnMatrix4<f32>,
    projection_matrix: mint::ColumnMatrix4<f32>,
    pivot_distance: f32,
) -> Option<mint::ColumnMatrix4<f32>> {
    let mut draw_list = ui.get_window_draw_list();

    let (center, size, hsize) = internal::RECT.with(|r| {
        let r = *r.borrow();
        let h = r.m_size * 0.5;
        (
            Vec2 {
                x: r.m_x + h,
                y: r.m_y + h,
            },
            r.m_size,
            h,
        )
    });

    let mut view_projection: Mat4 = Mat4::from(projection_matrix) * Mat4::from(view_matrix);

    // Non-square aspect ratio correction (scale X components of x/z axes)
    {
        let p: Mat4 = Mat4::from(projection_matrix);
        let aspect_ratio = p.y_axis.y / p.x_axis.x;
        view_projection.x_axis.x *= aspect_ratio;
        view_projection.z_axis.x *= aspect_ratio;
    }

    let axis_length = size * CONFIG.axis_length_scale;
    let x_axis = view_projection * Vec4::new(axis_length, 0.0, 0.0, 0.0);
    let y_axis = view_projection * Vec4::new(0.0, axis_length, 0.0, 0.0);
    let z_axis = view_projection * Vec4::new(0.0, 0.0, axis_length, 0.0);

    let interactive = pivot_distance > 0.0;
    let io = ui.io();
    let mouse_pos = Vec2 {
        x: io.mouse_pos[0],
        y: io.mouse_pos[1],
    };

    let hover_circle_radius = hsize * CONFIG.hover_circle_radius_scale;
    if CONFIG.hover_circle_color != ImColor32::BLACK
        && interactive
        && check_inside_circle(center, hover_circle_radius, mouse_pos)
    {
        draw_list
            .add_circle(center, hover_circle_radius, CONFIG.hover_circle_color)
            .filled(true)
            .build();
    }

    let positive_radius = size * CONFIG.positive_radius_scale;
    let negative_radius = size * CONFIG.negative_radius_scale;
    let x_positive_closer = 0.0 >= x_axis.w;
    let y_positive_closer = 0.0 >= y_axis.w;
    let z_positive_closer = 0.0 >= z_axis.w;

    let mut pairs: Vec<(i32, f32)> = vec![
        (0, x_axis.w),
        (1, y_axis.w),
        (2, z_axis.w),
        (3, -x_axis.w),
        (4, -y_axis.w),
        (5, -z_axis.w),
    ];
    pairs.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));

    let mut selection: i32 = -1;
    if interactive {
        for &(idx, _) in pairs.iter().rev() {
            let hit = match idx {
                0 => check_inside_circle(
                    Vec2 {
                        x: center.x + x_axis.x,
                        y: center.y - x_axis.y,
                    },
                    positive_radius,
                    mouse_pos,
                ),
                1 => check_inside_circle(
                    Vec2 {
                        x: center.x + y_axis.x,
                        y: center.y - y_axis.y,
                    },
                    positive_radius,
                    mouse_pos,
                ),
                2 => check_inside_circle(
                    Vec2 {
                        x: center.x + z_axis.x,
                        y: center.y - z_axis.y,
                    },
                    positive_radius,
                    mouse_pos,
                ),
                3 => check_inside_circle(
                    Vec2 {
                        x: center.x - x_axis.x,
                        y: center.y + x_axis.y,
                    },
                    negative_radius,
                    mouse_pos,
                ),
                4 => check_inside_circle(
                    Vec2 {
                        x: center.x - y_axis.x,
                        y: center.y + y_axis.y,
                    },
                    negative_radius,
                    mouse_pos,
                ),
                5 => check_inside_circle(
                    Vec2 {
                        x: center.x - z_axis.x,
                        y: center.y + z_axis.y,
                    },
                    negative_radius,
                    mouse_pos,
                ),
                _ => false,
            };
            if hit {
                selection = idx;
                break;
            }
        }
    }

    let line_thickness = size * CONFIG.line_thickness_scale;
    for &(fst, _) in &pairs {
        match fst {
            0 => draw_positive_line(
                ui,
                &mut draw_list,
                center,
                Vec2 {
                    x: x_axis.x,
                    y: -x_axis.y,
                },
                if x_positive_closer {
                    CONFIG.x_circle_front_color
                } else {
                    CONFIG.x_circle_back_color
                },
                positive_radius,
                line_thickness,
                "X",
                selection == 0,
            ),
            1 => draw_positive_line(
                ui,
                &mut draw_list,
                center,
                Vec2 {
                    x: y_axis.x,
                    y: -y_axis.y,
                },
                if y_positive_closer {
                    CONFIG.y_circle_front_color
                } else {
                    CONFIG.y_circle_back_color
                },
                positive_radius,
                line_thickness,
                "Y",
                selection == 1,
            ),
            2 => draw_positive_line(
                ui,
                &mut draw_list,
                center,
                Vec2 {
                    x: z_axis.x,
                    y: -z_axis.y,
                },
                if z_positive_closer {
                    CONFIG.z_circle_front_color
                } else {
                    CONFIG.z_circle_back_color
                },
                positive_radius,
                line_thickness,
                "Z",
                selection == 2,
            ),
            3 => draw_negative_line(
                &mut draw_list,
                center,
                Vec2 {
                    x: x_axis.x,
                    y: -x_axis.y,
                },
                if !x_positive_closer {
                    CONFIG.x_circle_front_color
                } else {
                    CONFIG.x_circle_back_color
                },
                negative_radius,
                selection == 3,
            ),
            4 => draw_negative_line(
                &mut draw_list,
                center,
                Vec2 {
                    x: y_axis.x,
                    y: -y_axis.y,
                },
                if !y_positive_closer {
                    CONFIG.y_circle_front_color
                } else {
                    CONFIG.y_circle_back_color
                },
                negative_radius,
                selection == 4,
            ),
            5 => draw_negative_line(
                &mut draw_list,
                center,
                Vec2 {
                    x: z_axis.x,
                    y: -z_axis.y,
                },
                if !z_positive_closer {
                    CONFIG.z_circle_front_color
                } else {
                    CONFIG.z_circle_back_color
                },
                negative_radius,
                selection == 5,
            ),
            _ => {}
        }
    }

    if selection != -1 && ui.is_mouse_clicked(MouseButton::Left) {
        let model: Mat4 = Mat4::from(view_matrix).inverse();

        let pos = Vec3::new(model.w_axis.x, model.w_axis.y, model.w_axis.z);
        let z_axis_model = Vec3::new(model.z_axis.x, model.z_axis.y, model.z_axis.z);
        let pivot_pos = pos - (z_axis_model * pivot_distance);

        let ups = [
            Vec3::new(0.0, 1.0, 0.0),
            Vec3::new(0.0, 0.0, 1.0),
            Vec3::new(0.0, 1.0, 0.0),
            Vec3::new(0.0, 1.0, 0.0),
            Vec3::new(0.0, 0.0, -1.0),
            Vec3::new(0.0, 1.0, 0.0),
        ];

        let new_view = match selection {
            0 => Mat4::look_at_lh(pivot_pos + Vec3::new(pivot_distance, 0.0, 0.0), pivot_pos, ups[0]),
            1 => Mat4::look_at_lh(pivot_pos + Vec3::new(0.0, pivot_distance, 0.0), pivot_pos, ups[1]),
            2 => Mat4::look_at_lh(pivot_pos + Vec3::new(0.0, 0.0, pivot_distance), pivot_pos, ups[2]),
            3 => Mat4::look_at_lh(pivot_pos - Vec3::new(pivot_distance, 0.0, 0.0), pivot_pos, ups[3]),
            4 => Mat4::look_at_lh(pivot_pos - Vec3::new(0.0, pivot_distance, 0.0), pivot_pos, ups[4]),
            5 => Mat4::look_at_lh(pivot_pos - Vec3::new(0.0, 0.0, pivot_distance), pivot_pos, ups[5]),
            _ => return None,
        };

        return Some(mint::ColumnMatrix4::from(new_view));
    }

    let mut view_out: Option<mint::ColumnMatrix4<f32>> = None;

    let hover_inside = check_inside_circle(center, hover_circle_radius, mouse_pos);
    internal::DRAG_STATE.with(|state| {
        let mut s = state.borrow_mut();

        if ui.is_mouse_dragging(MouseButton::Left) {
            if !s.active && hover_inside {
                // Start drag
                s.active = true;
                s.last_mouse = [mouse_pos.x, mouse_pos.y];

                // let inv_view = Mat4::from(view_matrix).inverse();
                // let forward = -inv_view.z_axis.truncate();
                // s.yaw = forward.z.atan2(forward.x);
                // s.pitch = forward.y.asin();
            } else if s.active {
                // Drag in progress
                let dx = mouse_pos.x - s.last_mouse[0];
                let dy = mouse_pos.y - s.last_mouse[1];
                s.last_mouse = [mouse_pos.x, mouse_pos.y];

                s.yaw -= dx * 0.05;
                s.pitch = (s.pitch - dy * 0.05)
                    .clamp(-std::f32::consts::FRAC_PI_2 + 0.01, std::f32::consts::FRAC_PI_2 - 0.01);

                // Build new forward vector
                let cos_pitch = s.pitch.cos();
                let forward = Vec3::new(s.yaw.cos() * cos_pitch, s.pitch.sin(), s.yaw.sin() * cos_pitch);

                // Rotate view matrix: convert to glam, apply yaw/pitch
                let view_glam: Mat4 = Mat4::from(view_matrix);

                // inverse to get camera position & orientation
                let inv_view = view_glam.inverse();
                let cam_pos = Vec3::new(inv_view.w_axis.x, inv_view.w_axis.y, inv_view.w_axis.z);

                let up = Vec3::Y;
                let new_view = Mat4::look_at_lh(cam_pos, cam_pos + forward, up);

                view_out = Some(mint::ColumnMatrix4::from(new_view));
            }
        } else {
            s.active = false;
        }
    });

    if view_out.is_some() {
        return view_out;
    }

    None
}