manager.cpp 33.3 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
    if (id == layout.selected()) {
        layout.clear_select();
        model.unset_mesh();
TheNumbat's avatar
TheNumbat committed
41
42
        rig.clear();
        animate.clear();
TheNumbat's avatar
TheNumbat committed
43
    }
TheNumbat's avatar
TheNumbat committed
44
45
}

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

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

#ifdef __APPLE__
TheNumbat's avatar
TheNumbat committed
52
53
    Uint16 mod = KMOD_GUI;
    if (key.sym == SDLK_BACKSPACE && key.mod & KMOD_GUI) {
TheNumbat's avatar
TheNumbat committed
54
#else
TheNumbat's avatar
TheNumbat committed
55
56
    Uint16 mod = KMOD_CTRL;
    if (key.sym == SDLK_DELETE && layout.selected()) {
TheNumbat's avatar
TheNumbat committed
57
#endif
TheNumbat's avatar
TheNumbat committed
58
59
60
        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
61
62
63
64
            undo.del_obj(layout.selected());
            return true;
        }
    }
TheNumbat's avatar
TheNumbat committed
65

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

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

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

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

TheNumbat's avatar
TheNumbat committed
134
135
136
137
138
139
static bool postfix(const std::string &path, const std::string &type) {
    if (path.length() >= type.length())
        return path.compare(path.length() - type.length(), type.length(), type) == 0;
    return false;
}

TheNumbat's avatar
TheNumbat committed
140
void Manager::write_scene(Scene &scene) {
TheNumbat's avatar
TheNumbat committed
141

TheNumbat's avatar
TheNumbat committed
142
143
144
    char *path = nullptr;
    NFD_SaveDialog("dae", nullptr, &path);
    if (path) {
TheNumbat's avatar
TheNumbat committed
145
146
147
148
149
        std::string spath(path);
        if (!postfix(path, ".dae")) {
            spath += ".dae";
        }
        std::string error = scene.write(spath, render.get_cam(), animate);
TheNumbat's avatar
TheNumbat committed
150
151
152
        set_error(error);
        free(path);
    }
TheNumbat's avatar
TheNumbat committed
153
154
}

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

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

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

TheNumbat's avatar
TheNumbat committed
173
void Manager::load_image(Scene_Light &light) {
TheNumbat's avatar
TheNumbat committed
174

TheNumbat's avatar
TheNumbat committed
175
176
177
178
179
180
181
182
    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
183
184
}

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

TheNumbat's avatar
TheNumbat committed
187
188
189
    static Material::Options old_opt;
    Material::Options start_opt = material.opt;
    Material::Options &opt = material.opt;
TheNumbat's avatar
TheNumbat committed
190

TheNumbat's avatar
TheNumbat committed
191
    bool U = false;
TheNumbat's avatar
TheNumbat committed
192

TheNumbat's avatar
TheNumbat committed
193
194
195
196
197
198
    auto activate = [&]() {
        if (ImGui::IsItemDeactivated() && old_opt != opt)
            U = true;
        else if (ImGui::IsItemActivated())
            old_opt = start_opt;
    };
TheNumbat's avatar
TheNumbat committed
199

TheNumbat's avatar
TheNumbat committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    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
242

TheNumbat's avatar
TheNumbat committed
243
244
245
    if (U) {
        undo.update_material(obj_id, old_opt);
    }
TheNumbat's avatar
TheNumbat committed
246
247
}

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

TheNumbat's avatar
TheNumbat committed
250
    Pose &pose = item.pose();
TheNumbat's avatar
TheNumbat committed
251

TheNumbat's avatar
TheNumbat committed
252
253
    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
254
            widgets.active = act;
TheNumbat's avatar
TheNumbat committed
255
        if (ImGui::IsItemActivated())
TheNumbat's avatar
TheNumbat committed
256
            old_pose = pose;
TheNumbat's avatar
TheNumbat committed
257
        if (ImGui::IsItemDeactivatedAfterEdit() && old_pose != pose)
TheNumbat's avatar
TheNumbat committed
258
259
260
            undo.update_pose(item.id(), old_pose);
    };

TheNumbat's avatar
TheNumbat committed
261
262
    if (!(item.is<Scene_Light>() && item.get<Scene_Light>().is_env()) &&
        ImGui::CollapsingHeader("Edit Pose")) {
TheNumbat's avatar
TheNumbat committed
263
264
265
266
267
268
269
270
271
272
        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
273
        if (Manager::wrap_button("Delete [del]")) {
TheNumbat's avatar
TheNumbat committed
274
275
276
277
278
279
            undo.del_obj(item.id());
        }

        ImGui::Unindent();
    }

TheNumbat's avatar
TheNumbat committed
280
    if (item.is<Scene_Object>()) {
TheNumbat's avatar
TheNumbat committed
281

TheNumbat's avatar
TheNumbat committed
282
283
        Scene_Object &obj = item.get<Scene_Object>();
        static Scene_Object::Options old_opt;
TheNumbat's avatar
TheNumbat committed
284
285
        Scene_Object::Options start_opt = obj.opt;

TheNumbat's avatar
TheNumbat committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
        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
301
            ImGui::Indent();
TheNumbat's avatar
TheNumbat committed
302
303
304
305
306
307
            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
308
                    obj.flip_normals();
TheNumbat's avatar
TheNumbat committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
                }
                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
327
328
            ImGui::Unindent();

TheNumbat's avatar
TheNumbat committed
329
330
331
332
            if (E)
                obj.set_mesh_dirty();
            if (U)
                undo.update_object(obj.id(), old_opt);
TheNumbat's avatar
TheNumbat committed
333
        }
TheNumbat's avatar
TheNumbat committed
334
        if (ImGui::CollapsingHeader("Edit Material")) {
TheNumbat's avatar
TheNumbat committed
335
336
337
338
339
            ImGui::Indent();
            material_edit_gui(undo, obj.id(), obj.material);
            ImGui::Unindent();
        }

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

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

TheNumbat's avatar
TheNumbat committed
344
        if (ImGui::CollapsingHeader("Edit Light")) {
TheNumbat's avatar
TheNumbat committed
345
346
347
348
349
            ImGui::Indent();
            light_edit_gui(undo, light);
            ImGui::Unindent();
        }
    }
TheNumbat's avatar
TheNumbat committed
350
    return cur_mode;
TheNumbat's avatar
TheNumbat committed
351
352
}

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

TheNumbat's avatar
TheNumbat committed
355
356
    static Scene_Light::Options old_opt;
    Scene_Light::Options start_opt = light.opt;
TheNumbat's avatar
TheNumbat committed
357

TheNumbat's avatar
TheNumbat committed
358
    bool E = false, U = false;
TheNumbat's avatar
TheNumbat committed
359

TheNumbat's avatar
TheNumbat committed
360
361
362
363
364
365
366
367
    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
368

TheNumbat's avatar
TheNumbat committed
369
370
371
372
373
374
375
    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
376

TheNumbat's avatar
TheNumbat committed
377
378
379
    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
380

TheNumbat's avatar
TheNumbat committed
381
382
383
384
        ImGui::DragFloat("Intensity", &light.opt.intensity, 0.1f, 0.0f,
                         std::numeric_limits<float>::max(), "%.2f");
        activate();
    }
TheNumbat's avatar
TheNumbat committed
385

TheNumbat's avatar
TheNumbat committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    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
427
428
429
}

bool Manager::wrap_button(std::string label) {
TheNumbat's avatar
TheNumbat committed
430
431
432
433
434
435
436
437
    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
438
439
};

TheNumbat's avatar
TheNumbat committed
440
441
442
void Manager::frame(Scene &scene, Camera &cam) {
    if (!layout.selected())
        return;
TheNumbat's avatar
TheNumbat committed
443

TheNumbat's avatar
TheNumbat committed
444
445
446
    Vec3 center = layout.selected_pos(scene);
    Vec3 dir = cam.front() * cam.dist();
    cam.look_at(center, center - dir);
TheNumbat's avatar
TheNumbat committed
447
448
}

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

TheNumbat's avatar
TheNumbat committed
451
452
453
    const ImGuiWindowFlags flags =
        ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing;
    static float anim_height = 0.0f;
TheNumbat's avatar
TheNumbat committed
454

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

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

TheNumbat's avatar
TheNumbat committed
459
460
461
    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
462

TheNumbat's avatar
TheNumbat committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
    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
480
481
482

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

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

TheNumbat's avatar
TheNumbat committed
489
        ImGui::PushID(obj.id());
TheNumbat's avatar
TheNumbat committed
490

TheNumbat's avatar
TheNumbat committed
491
492
        auto [name, cap] = obj.name();
        ImGui::InputText("##name", name, cap);
TheNumbat's avatar
TheNumbat committed
493

TheNumbat's avatar
TheNumbat committed
494
495
496
497
498
499
500
501
        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
502

TheNumbat's avatar
TheNumbat committed
503
504
505
        ImGui::PopID();
    });
    if (!scene.empty()) {
TheNumbat's avatar
TheNumbat committed
506

TheNumbat's avatar
TheNumbat committed
507
508
509
510
        if (mode != Mode::model && mode != Mode::rig && layout.selected() &&
            ImGui::Button("Center Object [f]")) {
            frame(scene, cam);
        }
TheNumbat's avatar
TheNumbat committed
511

TheNumbat's avatar
TheNumbat committed
512
513
        ImGui::Separator();
    }
TheNumbat's avatar
TheNumbat committed
514

TheNumbat's avatar
TheNumbat committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    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
551

TheNumbat's avatar
TheNumbat committed
552
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
553

TheNumbat's avatar
TheNumbat committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    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
571

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

TheNumbat's avatar
TheNumbat committed
574
    unsigned int idx = 0;
TheNumbat's avatar
TheNumbat committed
575

TheNumbat's avatar
TheNumbat committed
576
    ImGui::SetNextWindowSizeConstraints({200.0f, 0.0f}, {FLT_MAX,FLT_MAX});
TheNumbat's avatar
TheNumbat committed
577
    ImGui::Begin("New Light", &new_light_window,
TheNumbat's avatar
TheNumbat committed
578
579
                    ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar |
                    ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
TheNumbat's avatar
TheNumbat committed
580

TheNumbat's avatar
TheNumbat committed
581
582
    static Spectrum color = Spectrum(1.0f);
    static float intensity = 1.0f;
TheNumbat's avatar
TheNumbat committed
583

TheNumbat's avatar
TheNumbat committed
584
585
586
587
    ImGui::Text("Radiance");
    ImGui::ColorPicker3("Spectrum", color.data);
    ImGui::InputFloat("Intensity", &intensity);
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
588

TheNumbat's avatar
TheNumbat committed
589
    ImGui::Text("Light Objects");
TheNumbat's avatar
TheNumbat committed
590

TheNumbat's avatar
TheNumbat committed
591
592
593
    if (ImGui::CollapsingHeader("Directional Light")) {
        ImGui::PushID(idx++);
        static Vec3 direction = Vec3(0.0f, -1.0f, 0.0f);
TheNumbat's avatar
TheNumbat committed
594

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

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

TheNumbat's avatar
TheNumbat committed
609
610
611
612
613
614
615
616
617
618
619
    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
620

TheNumbat's avatar
TheNumbat committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    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
642

TheNumbat's avatar
TheNumbat committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    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
661

TheNumbat's avatar
TheNumbat committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
    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
679

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

TheNumbat's avatar
TheNumbat committed
693
694
695
696
697
698
699
700
701
702
703
        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
704

TheNumbat's avatar
TheNumbat committed
705
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
706
707
}

TheNumbat's avatar
TheNumbat committed
708
void Manager::UInew_obj(Undo &undo) {
TheNumbat's avatar
TheNumbat committed
709

TheNumbat's avatar
TheNumbat committed
710
    unsigned int idx = 0;
TheNumbat's avatar
TheNumbat committed
711

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

TheNumbat's avatar
TheNumbat committed
721
    ImGui::SetNextWindowSizeConstraints({200.0f, 0.0f}, {FLT_MAX,FLT_MAX});
TheNumbat's avatar
TheNumbat committed
722
723
    ImGui::Begin("New Object", &new_obj_window,
                 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar |
TheNumbat's avatar
TheNumbat committed
724
                 ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
TheNumbat's avatar
TheNumbat committed
725

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

TheNumbat's avatar
TheNumbat committed
736
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
737

TheNumbat's avatar
TheNumbat committed
738
739
740
741
742
743
744
745
746
    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
747

TheNumbat's avatar
TheNumbat committed
748
#if 0 // These have some problems converting to halfedge
TheNumbat's avatar
TheNumbat committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
    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
763

TheNumbat's avatar
TheNumbat committed
764
    ImGui::Separator();
TheNumbat's avatar
TheNumbat committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780

	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
781
782
783
784
785
786
787
788
789
790
791
792
793
794

    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
795
796
#endif

TheNumbat's avatar
TheNumbat committed
797
798
    ImGui::Separator();

TheNumbat's avatar
TheNumbat committed
799
800
801
802
803
804
805
806
807
808
809
810
811
812
    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
813
814
815
}

void Manager::set_error(std::string msg) {
TheNumbat's avatar
TheNumbat committed
816
817
818
819
    if (msg.empty())
        return;
    error_msg = msg;
    error_shown = true;
TheNumbat's avatar
TheNumbat committed
820
821
}

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

TheNumbat's avatar
TheNumbat committed
824
825
826
827
828
829
    float height = UImenu(scene, undo);
    UIsidebar(scene, undo, height, cam);
    UIerror();
    UIstudent();
    if (settings_shown)
        UIsettings();
TheNumbat's avatar
TheNumbat committed
830

TheNumbat's avatar
TheNumbat committed
831
    set_error(animate.pump_output(scene));
TheNumbat's avatar
TheNumbat committed
832
833
}

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

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

TheNumbat's avatar
TheNumbat committed
838
839
840
Animate &Manager::get_animate() { return animate; }

const Settings &Manager::get_settings() const { return settings; }
TheNumbat's avatar
TheNumbat committed
841
842
843

void Manager::UIsettings() {

TheNumbat's avatar
TheNumbat committed
844
845
    ImGui::Begin("Preferences", &settings_shown,
                 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings);
TheNumbat's avatar
TheNumbat committed
846

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

TheNumbat's avatar
TheNumbat committed
849
850
851
852
853
    int max = GL::max_msaa();
    if (settings.samples < 1)
        settings.samples = 1;
    if (settings.samples > max)
        settings.samples = max;
TheNumbat's avatar
TheNumbat committed
854

TheNumbat's avatar
TheNumbat committed
855
856
857
858
859
860
861
    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
862

TheNumbat's avatar
TheNumbat committed
863
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
864
865
866
}

void Manager::UIstudent() {
TheNumbat's avatar
TheNumbat committed
867
868
869
    if (!debug_shown)
        return;
    ImGui::Begin("Debug Data", &debug_shown, ImGuiWindowFlags_NoSavedSettings);
TheNumbat's avatar
TheNumbat committed
870
#ifndef SCOTTY3D_BUILD_REF
TheNumbat's avatar
TheNumbat committed
871
    student_debug_ui();
TheNumbat's avatar
TheNumbat committed
872
#endif
TheNumbat's avatar
TheNumbat committed
873
    ImGui::End();
TheNumbat's avatar
TheNumbat committed
874
875
876
}

void Manager::UIerror() {
TheNumbat's avatar
TheNumbat committed
877
878
879
880
881
882
883
884
885
886
887
888
889
    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
890
891
}

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

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

TheNumbat's avatar
TheNumbat committed
904
905
    float menu_height = 0.0f;
    if (ImGui::BeginMainMenuBar()) {
TheNumbat's avatar
TheNumbat committed
906

TheNumbat's avatar
TheNumbat committed
907
        if (ImGui::BeginMenu("File")) {
TheNumbat's avatar
TheNumbat committed
908

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

TheNumbat's avatar
TheNumbat committed
918
919
920
921
922
923
924
925
926
927
928
929
        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
930

TheNumbat's avatar
TheNumbat committed
931
932
933
934
935
        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
936

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

TheNumbat's avatar
TheNumbat committed
940
941
        if (mode_button(Gui::Mode::render, "Render"))
            mode = Gui::Mode::render;
TheNumbat's avatar
TheNumbat committed
942

TheNumbat's avatar
TheNumbat committed
943
944
        if (mode_button(Gui::Mode::rig, "Rig"))
            mode = Gui::Mode::rig;
TheNumbat's avatar
TheNumbat committed
945

TheNumbat's avatar
TheNumbat committed
946
947
        if (mode_button(Gui::Mode::animate, "Animate"))
            mode = Gui::Mode::animate;
TheNumbat's avatar
TheNumbat committed
948

TheNumbat's avatar
TheNumbat committed
949
950
        if (mode_button(Gui::Mode::simulate, "Simulate"))
            mode = Gui::Mode::simulate;
TheNumbat's avatar
TheNumbat committed
951

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

TheNumbat's avatar
TheNumbat committed
954
955
956
        menu_height = ImGui::GetWindowSize().y;
        ImGui::EndMainMenuBar();
    }
TheNumbat's avatar
TheNumbat committed
957

TheNumbat's avatar
TheNumbat committed
958
    return menu_height;
TheNumbat's avatar
TheNumbat committed
959
960
961
962
}

void Manager::create_baseplane() {

TheNumbat's avatar
TheNumbat committed
963
964
965
966
967
968
969
970
971
972
    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
973
974
}

TheNumbat's avatar
TheNumbat committed
975
976
977
978
void Manager::end_drag(Undo &undo, Scene &scene) {

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

TheNumbat's avatar
TheNumbat committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
    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
1007
1008
}

TheNumbat's avatar
TheNumbat committed
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
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
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
1053
1054
}

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

TheNumbat's avatar
TheNumbat committed
1057
    widgets.select(id);
TheNumbat's avatar
TheNumbat committed
1058

TheNumbat's avatar
TheNumbat committed
1059
1060
1061
1062
1063
    switch (mode) {
    case Mode::render:
    case Mode::layout: {
        layout.select(scene, widgets, id, cam, spos, dir);
    } break;
TheNumbat's avatar
TheNumbat committed
1064

TheNumbat's avatar
TheNumbat committed
1065
1066
1067
1068
    case Mode::model: {
        std::string err = model.select(widgets, id, cam, spos, dir);
        set_error(err);
    } break;
TheNumbat's avatar
TheNumbat committed
1069

TheNumbat's avatar
TheNumbat committed
1070
1071
1072
    case Mode::rig: {
        rig.select(scene, widgets, undo, id, cam, spos, dir);
    } break;
TheNumbat's avatar
TheNumbat committed
1073

TheNumbat's avatar
TheNumbat committed
1074
1075
1076
1077
1078
1079
1080
1081
    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
1082

TheNumbat's avatar
TheNumbat committed
1083
    return widgets.is_dragging();
TheNumbat's avatar
TheNumbat committed
1084
1085
1086
}

void Manager::set_select(Scene_ID id) {
TheNumbat's avatar
TheNumbat committed
1087
1088
    clear_select();
    layout.set_selected(id);
TheNumbat's avatar
TheNumbat committed
1089
1090
1091
}

void Manager::clear_select() {
TheNumbat's avatar
TheNumbat committed
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
    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
1107
1108
}

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

TheNumbat's avatar
TheNumbat committed
1111
    Mat4 view = camera.get_view();
TheNumbat's avatar
TheNumbat committed
1112

TheNumbat's avatar
TheNumbat committed
1113
    animate.update(scene);
TheNumbat's avatar
TheNumbat committed
1114

TheNumbat's avatar
TheNumbat committed
1115
1116
1117
1118
1119
1120
1121
1122
1123
    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
1124

TheNumbat's avatar
TheNumbat committed
1125
1126
1127
1128
1129
            if (render) {
                item.render(view);
            }
        });
    }
TheNumbat's avatar
TheNumbat committed
1130

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

TheNumbat's avatar
TheNumbat committed
1133
1134
1135
1136
1137
    auto selected = scene.get(layout.selected());
    switch (mode) {
    case Mode::layout: {
        layout.render(selected, widgets, camera);
    } break;
TheNumbat's avatar
TheNumbat committed
1138

TheNumbat's avatar
TheNumbat committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
    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
1158
1159
1160
}

void Manager::hover(Vec2 pixel, Vec3 cam, Vec2 spos, Vec3 dir) {
TheNumbat's avatar
TheNumbat committed
1161
1162
1163
1164
1165
    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
1166
1167
}

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