manager.cpp 32.8 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3
4
5
6
7
8
9
10
11

#include <imgui/imgui.h>
#include <nfd/nfd.h>

#include "manager.h"

#include "../geometry/util.h"
#include "../scene/renderer.h"

namespace Gui {

TheNumbat's avatar
TheNumbat committed
12
13
14
Manager::Manager(Scene &scene, Vec2 dim)
    : render(scene, dim), animate(dim), baseplane(1.0f), window_dim(dim) {
    create_baseplane();
TheNumbat's avatar
TheNumbat committed
15
16
17
}

void Manager::update_dim(Vec2 dim) {
TheNumbat's avatar
TheNumbat committed
18
19
20
    window_dim = dim;
    render.update_dim(dim);
    animate.update_dim(dim);
TheNumbat's avatar
TheNumbat committed
21
22
23
}

Vec3 Color::axis(Axis a) {
TheNumbat's avatar
TheNumbat committed
24
25
26
27
28
29
30
31
32
33
34
    switch (a) {
    case Axis::X:
        return red;
    case Axis::Y:
        return green;
    case Axis::Z:
        return blue;
    default:
        assert(false);
    }
    return Vec3();
TheNumbat's avatar
TheNumbat committed
35
36
37
}

void Manager::invalidate_obj(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
38
39
40
41
    if (id == layout.selected()) {
        layout.clear_select();
        model.unset_mesh();
    }
TheNumbat's avatar
TheNumbat committed
42
43
}

TheNumbat's avatar
TheNumbat committed
44
bool Manager::keydown(Undo &undo, SDL_Keysym key, Scene &scene, Camera &cam) {
TheNumbat's avatar
TheNumbat committed
45

TheNumbat's avatar
TheNumbat committed
46
47
    if (widgets.is_dragging())
        return false;
TheNumbat's avatar
TheNumbat committed
48
49

#ifdef __APPLE__
TheNumbat's avatar
TheNumbat committed
50
51
    Uint16 mod = KMOD_GUI;
    if (key.sym == SDLK_BACKSPACE && key.mod & KMOD_GUI) {
TheNumbat's avatar
TheNumbat committed
52
#else
TheNumbat's avatar
TheNumbat committed
53
54
    Uint16 mod = KMOD_CTRL;
    if (key.sym == SDLK_DELETE && layout.selected()) {
TheNumbat's avatar
TheNumbat committed
55
#endif
TheNumbat's avatar
TheNumbat committed
56
57
58
59
60
        if (mode != Mode::model && mode != Mode::rig && mode != Mode::animate) {
            undo.del_obj(layout.selected());
            return true;
        }
    }
TheNumbat's avatar
TheNumbat committed
61

TheNumbat's avatar
TheNumbat committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    if (key.mod & mod) {
        switch (key.sym) {
        case SDLK_d:
            debug_shown = true;
            return true;
        case SDLK_e:
            write_scene(scene);
            return true;
        case SDLK_o:
            load_scene(scene, undo, true);
            return true;
        case SDLK_s:
            save_scene(scene);
            return true;
        }
    }
TheNumbat's avatar
TheNumbat committed
78

TheNumbat's avatar
TheNumbat committed
79
80
81
82
83
84
85
86
87
88
89
90
    switch (key.sym) {
    case SDLK_m:
        widgets.active = Widget_Type::move;
        return true;
    case SDLK_r:
        widgets.active = Widget_Type::rotate;
        return true;
    case SDLK_s:
        widgets.active = Widget_Type::scale;
        return true;
    case SDLK_f: {
        if (mode == Mode::rig) {
TheNumbat's avatar
TheNumbat committed
91
            cam.look_at(Vec3{}, -cam.front() * cam.dist());
TheNumbat's avatar
TheNumbat committed
92
93
94
95
96
97
98
            return true;
        } else if (layout.selected()) {
            frame(scene, cam);
            return true;
        }
    } break;
    }
TheNumbat's avatar
TheNumbat committed
99

TheNumbat's avatar
TheNumbat committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    switch (mode) {
    case Mode::layout:
        return layout.keydown(widgets, key);
    case Mode::model:
        return model.keydown(widgets, key, cam);
    case Mode::render:
        return render.keydown(widgets, key);
    case Mode::rig:
        return rig.keydown(widgets, undo, key);
    case Mode::animate:
        return animate.keydown(widgets, undo, layout.selected(), key);
    case Mode::simulate:
        break;
    }
    return false;
TheNumbat's avatar
TheNumbat committed
115
116
}

TheNumbat's avatar
TheNumbat committed
117
118
119
120
121
122
123
124
125
126
127
void Manager::save_scene(Scene &scene) {
    if (save_file.empty()) {
        char *path = nullptr;
        NFD_SaveDialog("dae", nullptr, &path);
        if (path) {
            save_file = std::string(path);
            free(path);
        }
    }
    std::string error = scene.write(save_file, render.get_cam(), animate);
    set_error(error);
TheNumbat's avatar
TheNumbat committed
128
129
}

TheNumbat's avatar
TheNumbat committed
130
void Manager::write_scene(Scene &scene) {
TheNumbat's avatar
TheNumbat committed
131

TheNumbat's avatar
TheNumbat committed
132
133
134
135
136
137
138
    char *path = nullptr;
    NFD_SaveDialog("dae", nullptr, &path);
    if (path) {
        std::string error = scene.write(std::string(path), render.get_cam(), animate);
        set_error(error);
        free(path);
    }
TheNumbat's avatar
TheNumbat committed
139
140
}

TheNumbat's avatar
TheNumbat committed
141
void Manager::set_file(std::string save) { save_file = save; }
TheNumbat's avatar
TheNumbat committed
142

TheNumbat's avatar
TheNumbat committed
143
void Manager::load_scene(Scene &scene, Undo &undo, bool clear) {
TheNumbat's avatar
TheNumbat committed
144

TheNumbat's avatar
TheNumbat committed
145
146
147
148
149
150
151
152
153
154
155
156
    char *path = nullptr;
    NFD_OpenDialog(scene_file_types, nullptr, &path);
    if (path) {
        if (clear) {
            save_file = std::string(path);
            layout.clear_select();
            model.unset_mesh();
        }
        std::string error = scene.load(clear, undo, *this, std::string(path));
        set_error(error);
        free(path);
    }
TheNumbat's avatar
TheNumbat committed
157
158
}

TheNumbat's avatar
TheNumbat committed
159
void Manager::load_image(Scene_Light &light) {
TheNumbat's avatar
TheNumbat committed
160

TheNumbat's avatar
TheNumbat committed
161
162
163
164
165
166
167
168
    char *path = nullptr;
    NFD_OpenDialog(image_file_types, nullptr, &path);
    if (path) {
        std::string error = light.emissive_load(std::string(path));
        set_error(error);
        free(path);
    }
    light.dirty();
TheNumbat's avatar
TheNumbat committed
169
170
}

TheNumbat's avatar
TheNumbat committed
171
void Manager::material_edit_gui(Undo &undo, Scene_ID obj_id, Material &material) {
TheNumbat's avatar
TheNumbat committed
172

TheNumbat's avatar
TheNumbat committed
173
174
175
    static Material::Options old_opt;
    Material::Options start_opt = material.opt;
    Material::Options &opt = material.opt;
TheNumbat's avatar
TheNumbat committed
176

TheNumbat's avatar
TheNumbat committed
177
    bool U = false;
TheNumbat's avatar
TheNumbat committed
178

TheNumbat's avatar
TheNumbat committed
179
180
181
182
183
184
    auto activate = [&]() {
        if (ImGui::IsItemDeactivated() && old_opt != opt)
            U = true;
        else if (ImGui::IsItemActivated())
            old_opt = start_opt;
    };
TheNumbat's avatar
TheNumbat committed
185

TheNumbat's avatar
TheNumbat committed
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
    if (ImGui::Combo("Type", (int *)&opt.type, Material_Type_Names, (int)Material_Type::count)) {
        if (start_opt != opt) {
            old_opt = start_opt;
            U = true;
        }
    }

    switch (opt.type) {
    case Material_Type::lambertian: {
        ImGui::ColorEdit3("Albedo", opt.albedo.data);
        activate();
    } break;
    case Material_Type::mirror: {
        ImGui::ColorEdit3("Reflectance", opt.reflectance.data);
        activate();
    } break;
    case Material_Type::refract: {
        ImGui::ColorEdit3("Transmittance", opt.transmittance.data);
        activate();
        ImGui::DragFloat("Index of Refraction", &opt.ior, 0.1f, 0.0f,
                         std::numeric_limits<float>::max(), "%.2f");
        activate();
    } break;
    case Material_Type::glass: {
        ImGui::ColorEdit3("Reflectance", opt.reflectance.data);
        activate();
        ImGui::ColorEdit3("Transmittance", opt.transmittance.data);
        activate();
        ImGui::DragFloat("Index of Refraction", &opt.ior, 0.1f, 0.0f,
                         std::numeric_limits<float>::max(), "%.2f");
        activate();
    } break;
    case Material_Type::diffuse_light: {
        ImGui::ColorEdit3("Emissive", opt.emissive.data);
        activate();
        ImGui::DragFloat("Intensity", &opt.intensity, 0.1f, 0.0f, std::numeric_limits<float>::max(),
                         "%.2f");
        activate();
    } break;
    default:
        break;
    }
TheNumbat's avatar
TheNumbat committed
228

TheNumbat's avatar
TheNumbat committed
229
230
231
    if (U) {
        undo.update_material(obj_id, old_opt);
    }
TheNumbat's avatar
TheNumbat committed
232
233
}

TheNumbat's avatar
TheNumbat committed
234
Mode Manager::item_options(Undo &undo, Mode cur_mode, Scene_Item &item, Pose &old_pose) {
TheNumbat's avatar
TheNumbat committed
235

TheNumbat's avatar
TheNumbat committed
236
    Pose &pose = item.pose();
TheNumbat's avatar
TheNumbat committed
237

TheNumbat's avatar
TheNumbat committed
238
239
    auto sliders = [&](Widget_Type act, std::string label, Vec3 &data, float sens) {
        if (ImGui::DragFloat3(label.c_str(), data.data, sens))
TheNumbat's avatar
TheNumbat committed
240
            widgets.active = act;
TheNumbat's avatar
TheNumbat committed
241
        if (ImGui::IsItemActivated())
TheNumbat's avatar
TheNumbat committed
242
            old_pose = pose;
TheNumbat's avatar
TheNumbat committed
243
        if (ImGui::IsItemDeactivatedAfterEdit() && old_pose != pose)
TheNumbat's avatar
TheNumbat committed
244
245
246
            undo.update_pose(item.id(), old_pose);
    };

TheNumbat's avatar
TheNumbat committed
247
248
    if (!(item.is<Scene_Light>() && item.get<Scene_Light>().is_env()) &&
        ImGui::CollapsingHeader("Edit Pose")) {
TheNumbat's avatar
TheNumbat committed
249
250
251
252
253
254
255
256
257
258
        ImGui::Indent();

        pose.clamp_euler();
        sliders(Widget_Type::move, "Position", pose.pos, 0.1f);
        sliders(Widget_Type::rotate, "Rotation", pose.euler, 1.0f);
        sliders(Widget_Type::scale, "Scale", pose.scale, 0.03f);

        widgets.action_button(Widget_Type::move, "Move [m]", false);
        widgets.action_button(Widget_Type::rotate, "Rotate [r]");
        widgets.action_button(Widget_Type::scale, "Scale [s]");
TheNumbat's avatar
TheNumbat committed
259
        if (Manager::wrap_button("Delete [del]")) {
TheNumbat's avatar
TheNumbat committed
260
261
262
263
264
265
            undo.del_obj(item.id());
        }

        ImGui::Unindent();
    }

TheNumbat's avatar
TheNumbat committed
266
    if (item.is<Scene_Object>()) {
TheNumbat's avatar
TheNumbat committed
267

TheNumbat's avatar
TheNumbat committed
268
269
        Scene_Object &obj = item.get<Scene_Object>();
        static Scene_Object::Options old_opt;
TheNumbat's avatar
TheNumbat committed
270
271
        Scene_Object::Options start_opt = obj.opt;

TheNumbat's avatar
TheNumbat committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
        bool U = false, E = false;
        auto activate = [&]() {
            if (ImGui::IsItemActive())
                E = true;
            if (ImGui::IsItemDeactivated() && old_opt != obj.opt)
                U = true;
            else if (ImGui::IsItemActivated())
                old_opt = start_opt;
        };
        auto update = [&]() {
            obj.set_mesh_dirty();
            undo.update_object(obj.id(), start_opt);
        };

        if ((obj.is_editable() || obj.is_shape()) && ImGui::CollapsingHeader("Edit Mesh")) {
TheNumbat's avatar
TheNumbat committed
287
            ImGui::Indent();
TheNumbat's avatar
TheNumbat committed
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
            if (obj.is_editable()) {
                if (ImGui::Button("Edit Mesh##button")) {
                    cur_mode = Mode::model;
                }
                ImGui::SameLine();
                if (ImGui::Button("Flip Normals")) {
                    obj.get_mesh().flip();
                }
                if (ImGui::Checkbox("Smooth Normals", &obj.opt.smooth_normals)) {
                    update();
                }
                if (ImGui::Checkbox("Show Wireframe", &obj.opt.wireframe))
                    update();
            }
            if (ImGui::Combo("Use Implicit Shape", (int *)&obj.opt.shape_type, PT::Shape_Type_Names,
                             (int)PT::Shape_Type::count)) {
                if (obj.opt.shape_type == PT::Shape_Type::none)
                    obj.try_make_editable(start_opt.shape_type);
                update();
            }
            if (obj.opt.shape_type == PT::Shape_Type::sphere) {
                ImGui::DragFloat("Radius", &obj.opt.shape.get<PT::Sphere>().radius, 0.1f, 0.0f,
                                 std::numeric_limits<float>::max(), "%.2f");
                activate();
            }
TheNumbat's avatar
TheNumbat committed
313
314
            ImGui::Unindent();

TheNumbat's avatar
TheNumbat committed
315
316
317
318
            if (E)
                obj.set_mesh_dirty();
            if (U)
                undo.update_object(obj.id(), old_opt);
TheNumbat's avatar
TheNumbat committed
319
        }
TheNumbat's avatar
TheNumbat committed
320
        if (ImGui::CollapsingHeader("Edit Material")) {
TheNumbat's avatar
TheNumbat committed
321
322
323
324
325
            ImGui::Indent();
            material_edit_gui(undo, obj.id(), obj.material);
            ImGui::Unindent();
        }

TheNumbat's avatar
TheNumbat committed
326
    } else if (item.is<Scene_Light>()) {
TheNumbat's avatar
TheNumbat committed
327

TheNumbat's avatar
TheNumbat committed
328
        Scene_Light &light = item.get<Scene_Light>();
TheNumbat's avatar
TheNumbat committed
329

TheNumbat's avatar
TheNumbat committed
330
        if (ImGui::CollapsingHeader("Edit Light")) {
TheNumbat's avatar
TheNumbat committed
331
332
333
334
335
            ImGui::Indent();
            light_edit_gui(undo, light);
            ImGui::Unindent();
        }
    }
TheNumbat's avatar
TheNumbat committed
336
    return cur_mode;
TheNumbat's avatar
TheNumbat committed
337
338
}

TheNumbat's avatar
TheNumbat committed
339
void Manager::light_edit_gui(Undo &undo, Scene_Light &light) {
TheNumbat's avatar
TheNumbat committed
340

TheNumbat's avatar
TheNumbat committed
341
342
    static Scene_Light::Options old_opt;
    Scene_Light::Options start_opt = light.opt;
TheNumbat's avatar
TheNumbat committed
343

TheNumbat's avatar
TheNumbat committed
344
    bool E = false, U = false;
TheNumbat's avatar
TheNumbat committed
345

TheNumbat's avatar
TheNumbat committed
346
347
348
349
350
351
352
353
    auto activate = [&]() {
        if (ImGui::IsItemActive())
            E = true;
        if (ImGui::IsItemDeactivated() && old_opt != light.opt)
            U = true;
        else if (ImGui::IsItemActivated())
            old_opt = start_opt;
    };
TheNumbat's avatar
TheNumbat committed
354

TheNumbat's avatar
TheNumbat committed
355
356
357
358
359
360
361
    if (ImGui::Combo("Type", (int *)&light.opt.type, Light_Type_Names, (int)Light_Type::count)) {
        if (start_opt != light.opt) {
            old_opt = start_opt;
            U = true;
            E = true;
        }
    }
TheNumbat's avatar
TheNumbat committed
362

TheNumbat's avatar
TheNumbat committed
363
364
365
    if (!(light.opt.type == Light_Type::sphere && light.opt.has_emissive_map)) {
        ImGui::ColorEdit3("Spectrum", light.opt.spectrum.data);
        activate();
TheNumbat's avatar
TheNumbat committed
366

TheNumbat's avatar
TheNumbat committed
367
368
369
370
        ImGui::DragFloat("Intensity", &light.opt.intensity, 0.1f, 0.0f,
                         std::numeric_limits<float>::max(), "%.2f");
        activate();
    }
TheNumbat's avatar
TheNumbat committed
371

TheNumbat's avatar
TheNumbat committed
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
    switch (light.opt.type) {
    case Light_Type::sphere: {
        if (light.opt.has_emissive_map) {
            float x = ImGui::GetContentRegionAvail().x;
            ImGui::Image((ImTextureID)(long long)light.emissive_texture().get_id(), {x, x / 2.0f});
            if (ImGui::Button("Change Map")) {
                load_image(light);
            }
            ImGui::SameLine();
            if (ImGui::Button("Remove Map")) {
                light.emissive_clear();
                old_opt = start_opt;
                U = true;
            }
        } else {
            if (ImGui::Button("Use Texture Map")) {
                load_image(light);
                if (light.opt.has_emissive_map) {
                    old_opt = start_opt;
                    U = true;
                }
            }
        }
    } break;
    case Light_Type::spot: {
        ImGui::DragFloat2("Angle Cutoffs", light.opt.angle_bounds.data, 1.0f, 0.0f, 360.0f);
        activate();
    } break;
    case Light_Type::rectangle: {
        ImGui::DragFloat2("Size", light.opt.size.data, 0.1f, 0.0f,
                          std::numeric_limits<float>::max());
        activate();
    } break;
    default:
        break;
    }

    if (E)
        light.dirty();
    if (U)
        undo.update_light(light.id(), old_opt);
TheNumbat's avatar
TheNumbat committed
413
414
415
}

bool Manager::wrap_button(std::string label) {
TheNumbat's avatar
TheNumbat committed
416
417
418
419
420
421
422
423
    ImGuiStyle &style = ImGui::GetStyle();
    float available_w = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x;
    float last_w = ImGui::GetItemRectMax().x;
    float next_w = last_w + style.ItemSpacing.x + ImGui::CalcTextSize(label.c_str()).x +
                   style.FramePadding.x * 2;
    if (next_w < available_w)
        ImGui::SameLine();
    return ImGui::Button(label.c_str());
TheNumbat's avatar
TheNumbat committed
424
425
};

TheNumbat's avatar
TheNumbat committed
426
427
428
void Manager::frame(Scene &scene, Camera &cam) {
    if (!layout.selected())
        return;
TheNumbat's avatar
TheNumbat committed
429

TheNumbat's avatar
TheNumbat committed
430
431
432
    Vec3 center = layout.selected_pos(scene);
    Vec3 dir = cam.front() * cam.dist();
    cam.look_at(center, center - dir);
TheNumbat's avatar
TheNumbat committed
433
434
}

TheNumbat's avatar
TheNumbat committed
435
void Manager::UIsidebar(Scene &scene, Undo &undo, float menu_height, Camera &cam) {
TheNumbat's avatar
TheNumbat committed
436

TheNumbat's avatar
TheNumbat committed
437
438
439
    const ImGuiWindowFlags flags =
        ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing;
    static float anim_height = 0.0f;
TheNumbat's avatar
TheNumbat committed
440

TheNumbat's avatar
TheNumbat committed
441
    ImGui::SetNextWindowPos({0.0, menu_height});
TheNumbat's avatar
TheNumbat committed
442

TheNumbat's avatar
TheNumbat committed
443
    float h_cut = menu_height + (mode == Mode::animate ? anim_height : 0.0f);
TheNumbat's avatar
TheNumbat committed
444

TheNumbat's avatar
TheNumbat committed
445
446
447
    ImGui::SetNextWindowSizeConstraints({window_dim.x / 4.75f, window_dim.y - h_cut},
                                        {window_dim.x, window_dim.y - h_cut});
    ImGui::Begin("Menu", nullptr, flags);
TheNumbat's avatar
TheNumbat committed
448

TheNumbat's avatar
TheNumbat committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
    if (mode == Mode::layout) {
        ImGui::Text("Edit Scene");
        if (ImGui::Button("Import New Scene"))
            load_scene(scene, undo, true);
        if (wrap_button("Export Scene"))
            write_scene(scene);
        if (ImGui::Button("Import Objects"))
            load_scene(scene, undo, false);
        if (wrap_button("New Object")) {
            new_obj_window = true;
            new_obj_focus = true;
        }
        if (wrap_button("New Light")) {
            new_light_window = true;
            new_light_focus = true;
        }
    }
    if (!scene.empty()) {
        ImGui::Separator();
        ImGui::Text("Select an Object");
    }
TheNumbat's avatar
TheNumbat committed
470

TheNumbat's avatar
TheNumbat committed
471
472
473
474
    scene.for_items([&](Scene_Item &obj) {
        if ((mode == Mode::model || mode == Mode::rig) &&
            (!obj.is<Scene_Object>() || !obj.get<Scene_Object>().is_editable()))
            return;
TheNumbat's avatar
TheNumbat committed
475

TheNumbat's avatar
TheNumbat committed
476
        ImGui::PushID(obj.id());
TheNumbat's avatar
TheNumbat committed
477

TheNumbat's avatar
TheNumbat committed
478
479
        auto [name, cap] = obj.name();
        ImGui::InputText("##name", name, cap);
TheNumbat's avatar
TheNumbat committed
480

TheNumbat's avatar
TheNumbat committed
481
482
483
484
485
486
487
488
        bool is_selected = obj.id() == layout.selected();
        ImGui::SameLine();
        if (ImGui::Checkbox("##selected", &is_selected)) {
            if (is_selected)
                layout.set_selected(obj.id());
            else
                layout.clear_select();
        }
TheNumbat's avatar
TheNumbat committed
489

TheNumbat's avatar
TheNumbat committed
490
491
492
        ImGui::PopID();
    });
    if (!scene.empty()) {
TheNumbat's avatar
TheNumbat committed
493

TheNumbat's avatar
TheNumbat committed
494
495
496
497
        if (mode != Mode::model && mode != Mode::rig && layout.selected() &&
            ImGui::Button("Center Object [f]")) {
            frame(scene, cam);
        }
TheNumbat's avatar
TheNumbat committed
498

TheNumbat's avatar
TheNumbat committed
499
500
        ImGui::Separator();
    }
TheNumbat's avatar
TheNumbat committed
501

TheNumbat's avatar
TheNumbat committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    auto selected = scene.get(layout.selected());

    switch (mode) {
    case Mode::layout: {
        mode = layout.UIsidebar(*this, undo, widgets, selected);
    } break;

    case Mode::model: {
        std::string err = model.UIsidebar(undo, widgets, selected, cam);
        set_error(err);
    } break;

    case Mode::render: {
        mode = render.UIsidebar(*this, undo, scene, selected, cam);
    } break;

    case Mode::rig: {
        mode = rig.UIsidebar(*this, undo, widgets, selected);
    } break;

    case Mode::animate: {
        if (layout.UIsidebar(*this, undo, widgets, selected) == Mode::model)
            mode = Mode::model;
        animate.UIsidebar(*this, undo, selected, cam);
        ImGui::End();
        ImGui::SetNextWindowPos({0.0, window_dim.y}, ImGuiCond_Always, {0.0f, 1.0f});
        ImGui::SetNextWindowSize({window_dim.x, window_dim.y / 4.0f}, ImGuiCond_FirstUseEver);
        ImGui::SetNextWindowSizeConstraints({window_dim.x, window_dim.y / 4.0f}, window_dim);
        ImGui::Begin("Timeline", nullptr, flags);
        anim_height = ImGui::GetWindowHeight();
        animate.timeline(*this, undo, scene, selected, cam);
    } break;

    default:
        break;
    }
TheNumbat's avatar
TheNumbat committed
538

TheNumbat's avatar
TheNumbat committed
539
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
540

TheNumbat's avatar
TheNumbat committed
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
    if (mode == Mode::layout) {
        if (new_obj_focus) {
            ImGui::SetNextWindowFocus();
            new_obj_focus = false;
        }
        if (new_obj_window) {
            UInew_obj(undo);
        }
        if (new_light_focus) {
            ImGui::SetNextWindowFocus();
            new_light_focus = false;
        }
        if (new_light_window) {
            UInew_light(scene, undo);
        }
    }
}
TheNumbat's avatar
TheNumbat committed
558

TheNumbat's avatar
TheNumbat committed
559
void Manager::UInew_light(Scene &scene, Undo &undo) {
TheNumbat's avatar
TheNumbat committed
560

TheNumbat's avatar
TheNumbat committed
561
    unsigned int idx = 0;
TheNumbat's avatar
TheNumbat committed
562

TheNumbat's avatar
TheNumbat committed
563
564
565
    ImGui::Begin("New Light", &new_light_window,
                 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar |
                     ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
TheNumbat's avatar
TheNumbat committed
566

TheNumbat's avatar
TheNumbat committed
567
568
    static Spectrum color = Spectrum(1.0f);
    static float intensity = 1.0f;
TheNumbat's avatar
TheNumbat committed
569

TheNumbat's avatar
TheNumbat committed
570
571
572
573
    ImGui::Text("Radiance");
    ImGui::ColorPicker3("Spectrum", color.data);
    ImGui::InputFloat("Intensity", &intensity);
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
574

TheNumbat's avatar
TheNumbat committed
575
    ImGui::Text("Light Objects");
TheNumbat's avatar
TheNumbat committed
576

TheNumbat's avatar
TheNumbat committed
577
578
579
    if (ImGui::CollapsingHeader("Directional Light")) {
        ImGui::PushID(idx++);
        static Vec3 direction = Vec3(0.0f, -1.0f, 0.0f);
TheNumbat's avatar
TheNumbat committed
580

TheNumbat's avatar
TheNumbat committed
581
        ImGui::InputFloat3("Direction", direction.data, "%.2f");
TheNumbat's avatar
TheNumbat committed
582

TheNumbat's avatar
TheNumbat committed
583
584
585
586
587
588
589
590
591
592
593
        if (ImGui::Button("Add")) {
            Scene_Light light(Light_Type::directional, scene.reserve_id(), {});
            light.opt.spectrum = color;
            light.opt.intensity = intensity;
            light.pose = Pose::rotated(Mat4::rotate_to(direction.unit()).to_euler());
            light.dirty();
            undo.add_light(std::move(light));
            new_light_window = false;
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
594

TheNumbat's avatar
TheNumbat committed
595
596
597
598
599
600
601
602
603
604
605
    if (ImGui::CollapsingHeader("Point Light")) {
        ImGui::PushID(idx++);
        if (ImGui::Button("Add")) {
            Scene_Light light(Light_Type::point, scene.reserve_id(), {});
            light.opt.spectrum = color;
            light.opt.intensity = intensity;
            undo.add_light(std::move(light));
            new_light_window = false;
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
606

TheNumbat's avatar
TheNumbat committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    if (ImGui::CollapsingHeader("Spot Light")) {
        ImGui::PushID(idx++);
        static Vec3 direction = Vec3(0.0f, -1.0f, 0.0f);
        static Vec2 angles = Vec2(30.0f, 35.0f);

        ImGui::InputFloat3("Direction", direction.data, "%.2f");
        ImGui::InputFloat2("Angle Cutoffs", angles.data, "%.2f");
        angles = angles.range(0.0f, 360.0f);

        if (ImGui::Button("Add")) {
            Scene_Light light(Light_Type::spot, scene.reserve_id(), {});
            light.opt.spectrum = color;
            light.opt.intensity = intensity;
            light.pose = Pose::rotated(Mat4::rotate_to(direction.unit()).to_euler());
            light.opt.angle_bounds = angles;
            light.dirty();
            undo.add_light(std::move(light));
            new_light_window = false;
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
628

TheNumbat's avatar
TheNumbat committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
    if (ImGui::CollapsingHeader("Area Light (Rectangle)")) {
        ImGui::PushID(idx++);
        static Vec2 size = Vec2(1.0f);

        ImGui::InputFloat2("Size", size.data, "%.2f");
        size = clamp(size, Vec2(0.0f), size);

        if (ImGui::Button("Add")) {
            Scene_Light light(Light_Type::rectangle, scene.reserve_id(), {});
            light.opt.spectrum = color;
            light.opt.intensity = intensity;
            light.opt.size = size;
            light.dirty();
            undo.add_light(std::move(light));
            new_light_window = false;
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
647

TheNumbat's avatar
TheNumbat committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    if (!scene.has_env_light()) {
        ImGui::Separator();

        ImGui::Text("Environment Lights (up to one)");

        if (ImGui::CollapsingHeader("Hemisphere Light")) {
            ImGui::PushID(idx++);
            if (ImGui::Button("Add")) {
                Scene_Light light(Light_Type::hemisphere, scene.reserve_id(), {});
                light.opt.spectrum = color;
                light.opt.intensity = intensity;
                light.dirty();
                undo.add_light(std::move(light));
                new_light_window = false;
            }
            ImGui::PopID();
        }
TheNumbat's avatar
TheNumbat committed
665

TheNumbat's avatar
TheNumbat committed
666
667
668
669
670
671
672
673
674
675
676
677
        if (ImGui::CollapsingHeader("Sphere Light")) {
            ImGui::PushID(idx++);
            if (ImGui::Button("Add")) {
                Scene_Light light(Light_Type::sphere, scene.reserve_id(), {});
                light.opt.spectrum = color;
                light.opt.intensity = intensity;
                light.dirty();
                undo.add_light(std::move(light));
                new_light_window = false;
            }
            ImGui::PopID();
        }
TheNumbat's avatar
TheNumbat committed
678

TheNumbat's avatar
TheNumbat committed
679
680
681
682
683
684
685
686
687
688
689
        if (ImGui::CollapsingHeader("Environment Map")) {
            ImGui::PushID(idx++);
            if (ImGui::Button("Add")) {
                Scene_Light light(Light_Type::sphere, scene.reserve_id(), {});
                load_image(light);
                undo.add_light(std::move(light));
                new_light_window = false;
            }
            ImGui::PopID();
        }
    }
TheNumbat's avatar
TheNumbat committed
690

TheNumbat's avatar
TheNumbat committed
691
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
692
693
}

TheNumbat's avatar
TheNumbat committed
694
void Manager::UInew_obj(Undo &undo) {
TheNumbat's avatar
TheNumbat committed
695

TheNumbat's avatar
TheNumbat committed
696
    unsigned int idx = 0;
TheNumbat's avatar
TheNumbat committed
697

TheNumbat's avatar
TheNumbat committed
698
699
700
701
702
703
704
705
    auto add_mesh = [&, this](std::string n, GL::Mesh &&mesh, bool flip = false) {
        Halfedge_Mesh hm;
        hm.from_mesh(mesh);
        if (flip)
            hm.flip();
        undo.add_obj(std::move(hm), n);
        new_obj_window = false;
    };
TheNumbat's avatar
TheNumbat committed
706

TheNumbat's avatar
TheNumbat committed
707
708
709
    ImGui::Begin("New Object", &new_obj_window,
                 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar |
                     ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
TheNumbat's avatar
TheNumbat committed
710

TheNumbat's avatar
TheNumbat committed
711
712
713
714
715
716
717
718
719
    if (ImGui::CollapsingHeader("Cube")) {
        ImGui::PushID(idx++);
        static float R = 1.0f;
        ImGui::SliderFloat("Side Length", &R, 0.01f, 10.0f, "%.2f");
        if (ImGui::Button("Add")) {
            add_mesh("Cube", Util::cube_mesh(R / 2.0f), true);
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
720

TheNumbat's avatar
TheNumbat committed
721
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
722

TheNumbat's avatar
TheNumbat committed
723
724
725
726
727
728
729
730
731
    if (ImGui::CollapsingHeader("Square")) {
        ImGui::PushID(idx++);
        static float R = 1.0f;
        ImGui::SliderFloat("Side Length", &R, 0.01f, 10.0f, "%.2f");
        if (ImGui::Button("Add")) {
            add_mesh("Square", Util::square_mesh(R / 2.0f));
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
732

TheNumbat's avatar
TheNumbat committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
    ImGui::Separator();

    if (ImGui::CollapsingHeader("Cylinder")) {
        ImGui::PushID(idx++);
        static float R = 0.5f, H = 2.0f;
        static int S = 12;
        ImGui::SliderFloat("Radius", &R, 0.01f, 10.0f, "%.2f");
        ImGui::SliderFloat("Height", &H, 0.01f, 10.0f, "%.2f");
        ImGui::SliderInt("Sides", &S, 3, 100);
        if (ImGui::Button("Add")) {
            add_mesh("Cylinder", Util::cyl_mesh(R, H, S));
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
747

TheNumbat's avatar
TheNumbat committed
748
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767

#if 0 // The procedural torus has some problems converting to halfedge
	if(ImGui::CollapsingHeader("Torus")) {
		ImGui::PushID(idx++);
		static float IR = 0.8f, OR = 1.0f;
		static int SEG = 32, S = 16;
		ImGui::SliderFloat("Inner Radius", &IR, 0.01f, 10.0f, "%.2f");
		ImGui::SliderFloat("Outer Radius", &OR, 0.01f, 10.0f, "%.2f");
		ImGui::SliderInt("Segments", &SEG, 3, 100);
		ImGui::SliderInt("Sides", &S, 3, 100);
		if(ImGui::Button("Add")) {
			add_mesh("Torus", Util::torus_mesh(IR, OR, SEG, S));
		}
		ImGui::PopID();
	}

	ImGui::Separator();
#endif

TheNumbat's avatar
TheNumbat committed
768
769
770
771
772
773
774
775
776
777
778
779
780
    if (ImGui::CollapsingHeader("Sphere")) {
        ImGui::PushID(idx++);
        static float R = 1.0f;
        ImGui::SliderFloat("Radius", &R, 0.01f, 10.0f, "%.2f");
        if (ImGui::Button("Add")) {
            Scene_Object &obj = undo.add_obj(GL::Mesh(), "Sphere");
            obj.opt.shape_type = PT::Shape_Type::sphere;
            obj.opt.shape = PT::Shape(PT::Sphere(R));
            obj.set_mesh_dirty();
            new_obj_window = false;
        }
        ImGui::PopID();
    }
TheNumbat's avatar
TheNumbat committed
781

TheNumbat's avatar
TheNumbat committed
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
    ImGui::Separator();

    if (ImGui::CollapsingHeader("Cone")) {
        ImGui::PushID(idx++);
        static float BR = 1.0f, TR = 0.1f, H = 1.0f;
        static int S = 12;
        ImGui::SliderFloat("Bottom Radius", &BR, 0.01f, 10.0f, "%.2f");
        ImGui::SliderFloat("Top Radius", &TR, 0.01f, 10.0f, "%.2f");
        ImGui::SliderFloat("Height", &H, 0.01f, 10.0f, "%.2f");
        ImGui::SliderInt("Sides", &S, 3, 100);
        if (ImGui::Button("Add")) {
            add_mesh("Cone", Util::cone_mesh(BR, TR, H, S));
        }
        ImGui::PopID();
    }
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
798
799
800
}

void Manager::set_error(std::string msg) {
TheNumbat's avatar
TheNumbat committed
801
802
803
804
    if (msg.empty())
        return;
    error_msg = msg;
    error_shown = true;
TheNumbat's avatar
TheNumbat committed
805
806
}

TheNumbat's avatar
TheNumbat committed
807
void Manager::render_ui(Scene &scene, Undo &undo, Camera &cam) {
TheNumbat's avatar
TheNumbat committed
808

TheNumbat's avatar
TheNumbat committed
809
810
811
812
813
814
    float height = UImenu(scene, undo);
    UIsidebar(scene, undo, height, cam);
    UIerror();
    UIstudent();
    if (settings_shown)
        UIsettings();
TheNumbat's avatar
TheNumbat committed
815

TheNumbat's avatar
TheNumbat committed
816
    set_error(animate.pump_output(scene));
TheNumbat's avatar
TheNumbat committed
817
818
}

TheNumbat's avatar
TheNumbat committed
819
Rig &Manager::get_rig() { return rig; }
TheNumbat's avatar
TheNumbat committed
820

TheNumbat's avatar
TheNumbat committed
821
Render &Manager::get_render() { return render; }
TheNumbat's avatar
TheNumbat committed
822

TheNumbat's avatar
TheNumbat committed
823
824
825
Animate &Manager::get_animate() { return animate; }

const Settings &Manager::get_settings() const { return settings; }
TheNumbat's avatar
TheNumbat committed
826
827
828

void Manager::UIsettings() {

TheNumbat's avatar
TheNumbat committed
829
830
    ImGui::Begin("Preferences", &settings_shown,
                 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings);
TheNumbat's avatar
TheNumbat committed
831

TheNumbat's avatar
TheNumbat committed
832
    ImGui::InputInt("Multisampling", &settings.samples);
TheNumbat's avatar
TheNumbat committed
833

TheNumbat's avatar
TheNumbat committed
834
835
836
837
838
    int max = GL::max_msaa();
    if (settings.samples < 1)
        settings.samples = 1;
    if (settings.samples > max)
        settings.samples = max;
TheNumbat's avatar
TheNumbat committed
839

TheNumbat's avatar
TheNumbat committed
840
841
842
843
844
845
846
    if (ImGui::Button("Apply")) {
        Renderer::get().set_samples(settings.samples);
    }

    ImGui::Separator();
    ImGui::Text("GPU: %s", GL::renderer().c_str());
    ImGui::Text("OpenGL: %s", GL::version().c_str());
TheNumbat's avatar
TheNumbat committed
847

TheNumbat's avatar
TheNumbat committed
848
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
849
850
851
}

void Manager::UIstudent() {
TheNumbat's avatar
TheNumbat committed
852
853
854
    if (!debug_shown)
        return;
    ImGui::Begin("Debug Data", &debug_shown, ImGuiWindowFlags_NoSavedSettings);
TheNumbat's avatar
TheNumbat committed
855
#ifndef SCOTTY3D_BUILD_REF
TheNumbat's avatar
TheNumbat committed
856
    student_debug_ui();
TheNumbat's avatar
TheNumbat committed
857
#endif
TheNumbat's avatar
TheNumbat committed
858
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
859
860
861
}

void Manager::UIerror() {
TheNumbat's avatar
TheNumbat committed
862
863
864
865
866
867
868
869
870
871
872
873
874
    if (!error_shown)
        return;
    Vec2 center = window_dim / 2.0f;
    ImGui::SetNextWindowPos(Vec2{center.x, center.y}, 0, Vec2{0.5f, 0.5f});
    ImGui::Begin("Errors", &error_shown,
                 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings |
                     ImGuiWindowFlags_NoResize);
    if (!error_msg.empty())
        ImGui::Text("%s", error_msg.c_str());
    if (ImGui::Button("Close")) {
        error_shown = false;
    }
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
875
876
}

TheNumbat's avatar
TheNumbat committed
877
float Manager::UImenu(Scene &scene, Undo &undo) {
TheNumbat's avatar
TheNumbat committed
878

TheNumbat's avatar
TheNumbat committed
879
880
881
882
883
884
885
886
887
    auto mode_button = [this](Gui::Mode m, std::string name) -> bool {
        bool active = m == mode;
        if (active)
            ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_ButtonActive));
        bool clicked = ImGui::Button(name.c_str());
        if (active)
            ImGui::PopStyleColor();
        return clicked;
    };
TheNumbat's avatar
TheNumbat committed
888

TheNumbat's avatar
TheNumbat committed
889
890
    float menu_height = 0.0f;
    if (ImGui::BeginMainMenuBar()) {
TheNumbat's avatar
TheNumbat committed
891

TheNumbat's avatar
TheNumbat committed
892
        if (ImGui::BeginMenu("File")) {
TheNumbat's avatar
TheNumbat committed
893

TheNumbat's avatar
TheNumbat committed
894
895
896
897
898
899
900
901
            if (ImGui::MenuItem("Open Scene (Ctrl+o)"))
                load_scene(scene, undo, true);
            if (ImGui::MenuItem("Export Scene (Ctrl+e)"))
                write_scene(scene);
            if (ImGui::MenuItem("Save Scene (Ctrl+s)"))
                save_scene(scene);
            ImGui::EndMenu();
        }
TheNumbat's avatar
TheNumbat committed
902

TheNumbat's avatar
TheNumbat committed
903
904
905
906
907
908
909
910
911
912
913
914
        if (ImGui::BeginMenu("Edit")) {

            if (ImGui::MenuItem("Undo (Ctrl+z)"))
                undo.undo();
            if (ImGui::MenuItem("Redo (Ctrl+y)"))
                undo.redo();
            if (ImGui::MenuItem("Edit Debug Data (Ctrl+d)"))
                debug_shown = true;
            if (ImGui::MenuItem("Scotty3D Settings"))
                settings_shown = true;
            ImGui::EndMenu();
        }
TheNumbat's avatar
TheNumbat committed
915

TheNumbat's avatar
TheNumbat committed
916
917
918
919
920
        if (mode_button(Gui::Mode::layout, "Layout")) {
            mode = Gui::Mode::layout;
            if (widgets.active == Widget_Type::bevel)
                widgets.active = Widget_Type::move;
        }
TheNumbat's avatar
TheNumbat committed
921

TheNumbat's avatar
TheNumbat committed
922
923
        if (mode_button(Gui::Mode::model, "Model"))
            mode = Gui::Mode::model;
TheNumbat's avatar
TheNumbat committed
924

TheNumbat's avatar
TheNumbat committed
925
926
        if (mode_button(Gui::Mode::render, "Render"))
            mode = Gui::Mode::render;
TheNumbat's avatar
TheNumbat committed
927

TheNumbat's avatar
TheNumbat committed
928
929
        if (mode_button(Gui::Mode::rig, "Rig"))
            mode = Gui::Mode::rig;
TheNumbat's avatar
TheNumbat committed
930

TheNumbat's avatar
TheNumbat committed
931
932
        if (mode_button(Gui::Mode::animate, "Animate"))
            mode = Gui::Mode::animate;
TheNumbat's avatar
TheNumbat committed
933

TheNumbat's avatar
TheNumbat committed
934
935
        if (mode_button(Gui::Mode::simulate, "Simulate"))
            mode = Gui::Mode::simulate;
TheNumbat's avatar
TheNumbat committed
936

TheNumbat's avatar
TheNumbat committed
937
        ImGui::Text("FPS: %.0f", ImGui::GetIO().Framerate);
TheNumbat's avatar
TheNumbat committed
938

TheNumbat's avatar
TheNumbat committed
939
940
941
        menu_height = ImGui::GetWindowSize().y;
        ImGui::EndMainMenuBar();
    }
TheNumbat's avatar
TheNumbat committed
942

TheNumbat's avatar
TheNumbat committed
943
    return menu_height;
TheNumbat's avatar
TheNumbat committed
944
945
946
947
}

void Manager::create_baseplane() {

TheNumbat's avatar
TheNumbat committed
948
949
950
951
952
953
954
955
956
957
    const int R = 25;
    for (int i = -R; i <= R; i++) {
        if (i == 0) {
            baseplane.add(Vec3{-R, 0, i}, Vec3{R, 0, i}, Color::red);
            baseplane.add(Vec3{i, 0, -R}, Vec3{i, 0, R}, Color::blue);
            continue;
        }
        baseplane.add(Vec3{i, 0, -R}, Vec3{i, 0, R}, Color::baseplane);
        baseplane.add(Vec3{-R, 0, i}, Vec3{R, 0, i}, Color::baseplane);
    }
TheNumbat's avatar
TheNumbat committed
958
959
}

TheNumbat's avatar
TheNumbat committed
960
961
962
963
void Manager::end_drag(Undo &undo, Scene &scene) {

    if (!widgets.is_dragging())
        return;
TheNumbat's avatar
TheNumbat committed
964

TheNumbat's avatar
TheNumbat committed
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
    Scene_Item &obj = *scene.get(layout.selected());

    switch (mode) {
    case Mode::animate: {
        animate.end_transform(undo, obj);
    } break;

    case Mode::render:
    case Mode::layout: {
        layout.end_transform(undo, obj);
    } break;
    case Mode::model: {
        if (obj.is<Scene_Object>()) {
            std::string err = model.end_transform(widgets, undo, obj.get<Scene_Object>());
            set_error(err);
        }
    } break;
    case Mode::rig: {
        if (obj.is<Scene_Object>()) {
            rig.end_transform(widgets, undo, obj.get<Scene_Object>());
        }
    } break;
    default:
        break;
    }

    widgets.end_drag();
TheNumbat's avatar
TheNumbat committed
992
993
}

TheNumbat's avatar
TheNumbat committed
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
void Manager::drag_to(Scene &scene, Vec3 cam, Vec2 spos, Vec3 dir) {

    if (!widgets.is_dragging())
        return;
    Scene_Item &obj = *scene.get(layout.selected());

    Vec3 pos;
    switch (mode) {
    case Mode::animate: {
        pos = animate.selected_pos(obj);
    } break;
    case Mode::render:
    case Mode::layout: {
        pos = layout.selected_pos(scene);
    } break;
    case Mode::model: {
        pos = model.selected_pos();
    } break;
    case Mode::rig: {
        pos = rig.selected_pos();
    } break;
    default:
        break;
    }

    widgets.drag_to(pos, cam, spos, dir, mode == Mode::model);

    switch (mode) {
    case Mode::animate: {
        animate.apply_transform(widgets, obj);
    } break;
    case Mode::render:
    case Mode::layout: {
        layout.apply_transform(obj, widgets);
    } break;
    case Mode::model: {
        model.apply_transform(widgets);
    } break;
    case Mode::rig: {
        rig.apply_transform(widgets);
    } break;
    default:
        break;
    }
TheNumbat's avatar
TheNumbat committed
1038
1039
}

TheNumbat's avatar
TheNumbat committed
1040
bool Manager::select(Scene &scene, Undo &undo, Scene_ID id, Vec3 cam, Vec2 spos, Vec3 dir) {
TheNumbat's avatar
TheNumbat committed
1041

TheNumbat's avatar
TheNumbat committed
1042
    widgets.select(id);
TheNumbat's avatar
TheNumbat committed
1043

TheNumbat's avatar
TheNumbat committed
1044
1045
1046
1047
1048
    switch (mode) {
    case Mode::render:
    case Mode::layout: {
        layout.select(scene, widgets, id, cam, spos, dir);
    } break;
TheNumbat's avatar
TheNumbat committed
1049

TheNumbat's avatar
TheNumbat committed
1050
1051
1052
1053
    case Mode::model: {
        std::string err = model.select(widgets, id, cam, spos, dir);
        set_error(err);
    } break;
TheNumbat's avatar
TheNumbat committed
1054

TheNumbat's avatar
TheNumbat committed
1055
1056
1057
    case Mode::rig: {
        rig.select(scene, widgets, undo, id, cam, spos, dir);
    } break;
TheNumbat's avatar
TheNumbat committed
1058

TheNumbat's avatar
TheNumbat committed
1059
1060
1061
1062
1063
1064
1065
1066
    case Mode::animate: {
        if (animate.select(scene, widgets, layout.selected(), id, cam, spos, dir))
            layout.set_selected(id);
    } break;

    default:
        break;
    }
TheNumbat's avatar
TheNumbat committed
1067

TheNumbat's avatar
TheNumbat committed
1068
    return widgets.is_dragging();
TheNumbat's avatar
TheNumbat committed
1069
1070
1071
}

void Manager::set_select(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
1072
1073
    clear_select();
    layout.set_selected(id);
TheNumbat's avatar
TheNumbat committed
1074
1075
1076
}

void Manager::clear_select() {
TheNumbat's avatar
TheNumbat committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
    switch (mode) {
    case Mode::render:
    case Mode::animate:
    case Mode::layout:
        layout.clear_select();
        break;
    case Mode::rig:
        rig.clear_select();
        break;
    case Mode::model:
        model.clear_select();
        break;
    default:
        break;
    }
TheNumbat's avatar
TheNumbat committed
1092
1093
}

TheNumbat's avatar
TheNumbat committed
1094
void Manager::render_3d(Scene &scene, Camera &camera) {
TheNumbat's avatar
TheNumbat committed
1095

TheNumbat's avatar
TheNumbat committed
1096
    Mat4 view = camera.get_view();
TheNumbat's avatar
TheNumbat committed
1097

TheNumbat's avatar
TheNumbat committed
1098
    animate.update(scene);
TheNumbat's avatar
TheNumbat committed
1099

TheNumbat's avatar
TheNumbat committed
1100
1101
1102
1103
1104
1105
1106
1107
1108
    if (mode == Mode::layout || mode == Mode::render || mode == Mode::animate) {
        scene.for_items([&, this](Scene_Item &item) {
            bool render = item.id() != layout.selected();
            if (item.is<Scene_Light>()) {
                const Scene_Light &light = item.get<Scene_Light>();
                if (light.opt.type == Light_Type::sphere ||
                    light.opt.type == Light_Type::hemisphere)
                    render = true;
            }
TheNumbat's avatar
TheNumbat committed
1109

TheNumbat's avatar
TheNumbat committed
1110
1111
1112
1113
1114
            if (render) {
                item.render(view);
            }
        });
    }
TheNumbat's avatar
TheNumbat committed
1115

TheNumbat's avatar
TheNumbat committed
1116
    Renderer::get().lines(baseplane, view, Mat4::I, 1.0f);
TheNumbat's avatar
TheNumbat committed
1117

TheNumbat's avatar
TheNumbat committed
1118
1119
1120
1121
1122
    auto selected = scene.get(layout.selected());
    switch (mode) {
    case Mode::layout: {
        layout.render(selected, widgets, camera);
    } break;
TheNumbat's avatar
TheNumbat committed
1123

TheNumbat's avatar
TheNumbat committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
    case Mode::model: {
        model.render(selected, widgets, camera);
    } break;

    case Mode::render: {
        render.render(selected, widgets, camera);
    } break;

    case Mode::rig: {
        rig.render(selected, widgets, camera);
    } break;

    case Mode::animate: {
        animate.render(scene, selected, widgets, camera);
    } break;

    default:
        break;
    }
TheNumbat's avatar
TheNumbat committed
1143
1144
1145
}

void Manager::hover(Vec2 pixel, Vec3 cam, Vec2 spos, Vec3 dir) {
TheNumbat's avatar
TheNumbat committed
1146
1147
1148
1149
1150
    if (mode == Mode::model) {
        model.set_hover(Renderer::get().read_id(pixel));
    } else if (mode == Mode::rig) {
        rig.hover(cam, spos, dir);
    }
TheNumbat's avatar
TheNumbat committed
1151
1152
}

TheNumbat's avatar
TheNumbat committed
1153
} // namespace Gui