manager.cpp 33 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
        if (mode == Mode::model) {
            model.erase_selected(undo, scene.get(layout.selected()));
        } else if (mode != Mode::rig && mode != Mode::animate) {
TheNumbat's avatar
TheNumbat committed
59
60
61
62
            undo.del_obj(layout.selected());
            return true;
        }
    }
TheNumbat's avatar
TheNumbat committed
63

TheNumbat's avatar
TheNumbat committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    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
80

TheNumbat's avatar
TheNumbat committed
81
82
83
84
85
86
87
88
89
90
91
92
    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
93
            cam.look_at(Vec3{}, -cam.front() * cam.dist());
TheNumbat's avatar
TheNumbat committed
94
            return true;
TheNumbat's avatar
TheNumbat committed
95
        } else if (mode != Mode::model && layout.selected()) {
TheNumbat's avatar
TheNumbat committed
96
97
98
99
100
            frame(scene, cam);
            return true;
        }
    } break;
    }
TheNumbat's avatar
TheNumbat committed
101

TheNumbat's avatar
TheNumbat committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    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
117
118
}

TheNumbat's avatar
TheNumbat committed
119
120
121
122
123
124
125
126
127
128
129
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
130
131
}

TheNumbat's avatar
TheNumbat committed
132
void Manager::write_scene(Scene &scene) {
TheNumbat's avatar
TheNumbat committed
133

TheNumbat's avatar
TheNumbat committed
134
135
136
137
138
139
140
    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
141
142
}

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

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

TheNumbat's avatar
TheNumbat committed
147
148
149
150
151
152
153
154
155
156
157
158
    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
159
160
}

TheNumbat's avatar
TheNumbat committed
161
void Manager::load_image(Scene_Light &light) {
TheNumbat's avatar
TheNumbat committed
162

TheNumbat's avatar
TheNumbat committed
163
164
165
166
167
168
169
170
    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
171
172
}

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

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

TheNumbat's avatar
TheNumbat committed
179
    bool U = false;
TheNumbat's avatar
TheNumbat committed
180

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

TheNumbat's avatar
TheNumbat committed
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
    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
230

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

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

TheNumbat's avatar
TheNumbat committed
238
    Pose &pose = item.pose();
TheNumbat's avatar
TheNumbat committed
239

TheNumbat's avatar
TheNumbat committed
240
241
    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
242
            widgets.active = act;
TheNumbat's avatar
TheNumbat committed
243
        if (ImGui::IsItemActivated())
TheNumbat's avatar
TheNumbat committed
244
            old_pose = pose;
TheNumbat's avatar
TheNumbat committed
245
        if (ImGui::IsItemDeactivatedAfterEdit() && old_pose != pose)
TheNumbat's avatar
TheNumbat committed
246
247
248
            undo.update_pose(item.id(), old_pose);
    };

TheNumbat's avatar
TheNumbat committed
249
250
    if (!(item.is<Scene_Light>() && item.get<Scene_Light>().is_env()) &&
        ImGui::CollapsingHeader("Edit Pose")) {
TheNumbat's avatar
TheNumbat committed
251
252
253
254
255
256
257
258
259
260
        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
261
        if (Manager::wrap_button("Delete [del]")) {
TheNumbat's avatar
TheNumbat committed
262
263
264
265
266
267
            undo.del_obj(item.id());
        }

        ImGui::Unindent();
    }

TheNumbat's avatar
TheNumbat committed
268
    if (item.is<Scene_Object>()) {
TheNumbat's avatar
TheNumbat committed
269

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

TheNumbat's avatar
TheNumbat committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        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
289
            ImGui::Indent();
TheNumbat's avatar
TheNumbat committed
290
291
292
293
294
295
            if (obj.is_editable()) {
                if (ImGui::Button("Edit Mesh##button")) {
                    cur_mode = Mode::model;
                }
                ImGui::SameLine();
                if (ImGui::Button("Flip Normals")) {
TheNumbat's avatar
TheNumbat committed
296
                    obj.flip_normals();
TheNumbat's avatar
TheNumbat committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
                }
                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
315
316
            ImGui::Unindent();

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

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

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

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

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

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

TheNumbat's avatar
TheNumbat committed
346
    bool E = false, U = false;
TheNumbat's avatar
TheNumbat committed
347

TheNumbat's avatar
TheNumbat committed
348
349
350
351
352
353
354
355
    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
356

TheNumbat's avatar
TheNumbat committed
357
358
359
360
361
362
363
    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
364

TheNumbat's avatar
TheNumbat committed
365
366
367
    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
368

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

TheNumbat's avatar
TheNumbat committed
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
    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
415
416
417
}

bool Manager::wrap_button(std::string label) {
TheNumbat's avatar
TheNumbat committed
418
419
420
421
422
423
424
425
    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
426
427
};

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

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

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

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

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

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

TheNumbat's avatar
TheNumbat committed
447
448
449
    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
450

TheNumbat's avatar
TheNumbat committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    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;
        }
    }
TheNumbat's avatar
TheNumbat committed
468
469
470

    ImGui::Separator();
    ImGui::Text("Select an Object");
TheNumbat's avatar
TheNumbat committed
471

TheNumbat's avatar
TheNumbat committed
472
473
474
475
    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
476

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

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

TheNumbat's avatar
TheNumbat committed
482
483
484
485
486
487
488
489
        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
490

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

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

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

TheNumbat's avatar
TheNumbat committed
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
538
    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
539

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

TheNumbat's avatar
TheNumbat committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    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
559

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

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

TheNumbat's avatar
TheNumbat committed
564
    ImGui::SetNextWindowSizeConstraints({200.0f, 0.0f}, {FLT_MAX,FLT_MAX});
TheNumbat's avatar
TheNumbat committed
565
    ImGui::Begin("New Light", &new_light_window,
TheNumbat's avatar
TheNumbat committed
566
567
                    ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar |
                    ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
TheNumbat's avatar
TheNumbat committed
568

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

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

TheNumbat's avatar
TheNumbat committed
577
    ImGui::Text("Light Objects");
TheNumbat's avatar
TheNumbat committed
578

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

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

TheNumbat's avatar
TheNumbat committed
585
586
587
588
589
590
591
592
593
594
595
        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
596

TheNumbat's avatar
TheNumbat committed
597
598
599
600
601
602
603
604
605
606
607
    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
608

TheNumbat's avatar
TheNumbat committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    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
630

TheNumbat's avatar
TheNumbat committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
    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
649

TheNumbat's avatar
TheNumbat committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
    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
667

TheNumbat's avatar
TheNumbat committed
668
669
670
671
672
673
674
675
676
677
678
679
        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
680

TheNumbat's avatar
TheNumbat committed
681
682
683
684
685
686
687
688
689
690
691
        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
692

TheNumbat's avatar
TheNumbat committed
693
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
694
695
}

TheNumbat's avatar
TheNumbat committed
696
void Manager::UInew_obj(Undo &undo) {
TheNumbat's avatar
TheNumbat committed
697

TheNumbat's avatar
TheNumbat committed
698
    unsigned int idx = 0;
TheNumbat's avatar
TheNumbat committed
699

TheNumbat's avatar
TheNumbat committed
700
701
702
703
704
705
706
707
    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
708

TheNumbat's avatar
TheNumbat committed
709
    ImGui::SetNextWindowSizeConstraints({200.0f, 0.0f}, {FLT_MAX,FLT_MAX});
TheNumbat's avatar
TheNumbat committed
710
711
    ImGui::Begin("New Object", &new_obj_window,
                 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar |
TheNumbat's avatar
TheNumbat committed
712
                 ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
TheNumbat's avatar
TheNumbat committed
713

TheNumbat's avatar
TheNumbat committed
714
715
716
717
718
719
720
721
722
    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
723

TheNumbat's avatar
TheNumbat committed
724
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
725

TheNumbat's avatar
TheNumbat committed
726
727
728
729
730
731
732
733
734
    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
735

TheNumbat's avatar
TheNumbat committed
736
#if 0 // These have some problems converting to halfedge
TheNumbat's avatar
TheNumbat committed
737
738
739
740
741
742
743
744
745
746
747
748
749
750
    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
751

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

	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();
TheNumbat's avatar
TheNumbat committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782

    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();
    }
TheNumbat's avatar
TheNumbat committed
783
784
#endif

TheNumbat's avatar
TheNumbat committed
785
786
    ImGui::Separator();

TheNumbat's avatar
TheNumbat committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    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();
    }
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
801
802
803
}

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

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

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

TheNumbat's avatar
TheNumbat committed
819
    set_error(animate.pump_output(scene));
TheNumbat's avatar
TheNumbat committed
820
821
}

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

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

TheNumbat's avatar
TheNumbat committed
826
827
828
Animate &Manager::get_animate() { return animate; }

const Settings &Manager::get_settings() const { return settings; }
TheNumbat's avatar
TheNumbat committed
829
830
831

void Manager::UIsettings() {

TheNumbat's avatar
TheNumbat committed
832
833
    ImGui::Begin("Preferences", &settings_shown,
                 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings);
TheNumbat's avatar
TheNumbat committed
834

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

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

TheNumbat's avatar
TheNumbat committed
843
844
845
846
847
848
849
    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
850

TheNumbat's avatar
TheNumbat committed
851
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
852
853
854
}

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

void Manager::UIerror() {
TheNumbat's avatar
TheNumbat committed
865
866
867
868
869
870
871
872
873
874
875
876
877
    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
878
879
}

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

TheNumbat's avatar
TheNumbat committed
882
883
884
885
886
887
888
889
890
    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
891

TheNumbat's avatar
TheNumbat committed
892
893
    float menu_height = 0.0f;
    if (ImGui::BeginMainMenuBar()) {
TheNumbat's avatar
TheNumbat committed
894

TheNumbat's avatar
TheNumbat committed
895
        if (ImGui::BeginMenu("File")) {
TheNumbat's avatar
TheNumbat committed
896

TheNumbat's avatar
TheNumbat committed
897
898
899
900
901
902
903
904
            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
905

TheNumbat's avatar
TheNumbat committed
906
907
908
909
910
911
912
913
914
915
916
917
        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
918

TheNumbat's avatar
TheNumbat committed
919
920
921
922
923
        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
924

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

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

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

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

TheNumbat's avatar
TheNumbat committed
937
938
        if (mode_button(Gui::Mode::simulate, "Simulate"))
            mode = Gui::Mode::simulate;
TheNumbat's avatar
TheNumbat committed
939

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

TheNumbat's avatar
TheNumbat committed
942
943
944
        menu_height = ImGui::GetWindowSize().y;
        ImGui::EndMainMenuBar();
    }
TheNumbat's avatar
TheNumbat committed
945

TheNumbat's avatar
TheNumbat committed
946
    return menu_height;
TheNumbat's avatar
TheNumbat committed
947
948
949
950
}

void Manager::create_baseplane() {

TheNumbat's avatar
TheNumbat committed
951
952
953
954
955
956
957
958
959
960
    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
961
962
}

TheNumbat's avatar
TheNumbat committed
963
964
965
966
void Manager::end_drag(Undo &undo, Scene &scene) {

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

TheNumbat's avatar
TheNumbat committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
    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
995
996
}

TheNumbat's avatar
TheNumbat committed
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
1038
1039
1040
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
1041
1042
}

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

TheNumbat's avatar
TheNumbat committed
1045
    widgets.select(id);
TheNumbat's avatar
TheNumbat committed
1046

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

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

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

TheNumbat's avatar
TheNumbat committed
1062
1063
1064
1065
1066
1067
1068
1069
    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
1070

TheNumbat's avatar
TheNumbat committed
1071
    return widgets.is_dragging();
TheNumbat's avatar
TheNumbat committed
1072
1073
1074
}

void Manager::set_select(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
1075
1076
    clear_select();
    layout.set_selected(id);
TheNumbat's avatar
TheNumbat committed
1077
1078
1079
}

void Manager::clear_select() {
TheNumbat's avatar
TheNumbat committed
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
    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
1095
1096
}

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

TheNumbat's avatar
TheNumbat committed
1099
    Mat4 view = camera.get_view();
TheNumbat's avatar
TheNumbat committed
1100

TheNumbat's avatar
TheNumbat committed
1101
    animate.update(scene);
TheNumbat's avatar
TheNumbat committed
1102

TheNumbat's avatar
TheNumbat committed
1103
1104
1105
1106
1107
1108
1109
1110
1111
    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
1112

TheNumbat's avatar
TheNumbat committed
1113
1114
1115
1116
1117
            if (render) {
                item.render(view);
            }
        });
    }
TheNumbat's avatar
TheNumbat committed
1118

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

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

TheNumbat's avatar
TheNumbat committed
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
    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
1146
1147
1148
}

void Manager::hover(Vec2 pixel, Vec3 cam, Vec2 spos, Vec3 dir) {
TheNumbat's avatar
TheNumbat committed
1149
1150
1151
1152
1153
    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
1154
1155
}

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