rig.cpp 5.66 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3

#include "rig.h"
#include "../scene/renderer.h"
TheNumbat's avatar
TheNumbat committed
4
#include "manager.h"
TheNumbat's avatar
TheNumbat committed
5
6
7

namespace Gui {

TheNumbat's avatar
TheNumbat committed
8
bool Rig::keydown(Widgets &widgets, Undo &undo, SDL_Keysym key) {
TheNumbat's avatar
TheNumbat committed
9

TheNumbat's avatar
TheNumbat committed
10
11
    if (!my_obj)
        return false;
TheNumbat's avatar
TheNumbat committed
12
13

#ifdef __APPLE__
TheNumbat's avatar
TheNumbat committed
14
    if (key.sym == SDLK_BACKSPACE && key.mod & KMOD_GUI) {
TheNumbat's avatar
TheNumbat committed
15
#else
TheNumbat's avatar
TheNumbat committed
16
    if (key.sym == SDLK_DELETE && selected) {
TheNumbat's avatar
TheNumbat committed
17
#endif
TheNumbat's avatar
TheNumbat committed
18
19
20
21
        undo.del_bone(my_obj->id(), selected);
        selected = nullptr;
        return true;
    }
TheNumbat's avatar
TheNumbat committed
22
23
24
    return false;
}

TheNumbat's avatar
TheNumbat committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void Rig::render(Scene_Maybe obj_opt, Widgets &widgets, Camera &cam) {

    if (!obj_opt.has_value())
        return;

    Scene_Item &item = obj_opt.value();
    if (!item.is<Scene_Object>())
        return;

    Scene_Object &obj = item.get<Scene_Object>();
    if (obj.opt.shape_type != PT::Shape_Type::none)
        return;

    if (my_obj != &obj) {
        my_obj = &obj;
        selected = nullptr;
    }
    if (my_obj->rig_dirty) {
        mesh_bvh.build(obj.mesh());
        my_obj->rig_dirty = false;
    }

    Mat4 view = cam.get_view();
TheNumbat's avatar
TheNumbat committed
48
    obj.render(view, false, false, false, false);
TheNumbat's avatar
TheNumbat committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    obj.armature.render(view, selected, root_selected, false);

    if (selected || root_selected) {

        widgets.active = Widget_Type::move;
        Vec3 pos;

        if (selected)
            pos = obj.armature.end_of(selected);
        else
            pos = obj.armature.base();

        float scale = std::min((cam.pos() - pos).norm() / 5.5f, 10.0f);
        widgets.render(view, pos, scale);
    }
TheNumbat's avatar
TheNumbat committed
64
65
}

TheNumbat's avatar
TheNumbat committed
66
67
68
69
70
void Rig::invalidate(Joint *j) {
    if (selected == j)
        selected = nullptr;
    if (new_joint == j)
        new_joint = nullptr;
TheNumbat's avatar
TheNumbat committed
71
72
}

TheNumbat's avatar
TheNumbat committed
73
74
75
76
77
78
void Rig::end_transform(Widgets &widgets, Undo &undo, Scene_Object &obj) {
    if (root_selected)
        undo.move_root(obj.id(), old_ext);
    else
        undo.move_bone(obj.id(), selected, old_ext);
    obj.set_skel_dirty();
TheNumbat's avatar
TheNumbat committed
79
80
}

TheNumbat's avatar
TheNumbat committed
81
82
83
84
85
86
87
88
89
void Rig::apply_transform(Widgets &widgets) {
    if (root_selected) {
        my_obj->armature.base() = widgets.apply_action(Pose::moved(old_pos)).pos;
        my_obj->set_skel_dirty();
    } else if (selected) {
        Vec3 new_pos = widgets.apply_action(Pose::moved(old_pos)).pos;
        selected->extent = new_pos - old_base;
        my_obj->set_skel_dirty();
    }
TheNumbat's avatar
TheNumbat committed
90
91
92
}

Vec3 Rig::selected_pos() {
TheNumbat's avatar
TheNumbat committed
93
94
95
96
97
98
    if (root_selected) {
        return my_obj->armature.base();
    } else {
        assert(selected);
        return my_obj->armature.end_of(selected);
    }
TheNumbat's avatar
TheNumbat committed
99
100
}

TheNumbat's avatar
TheNumbat committed
101
102
103
104
105
void Rig::select(Scene &scene, Widgets &widgets, Undo &undo, Scene_ID id, Vec3 cam, Vec2 spos,
                 Vec3 dir) {

    if (!my_obj)
        return;
TheNumbat's avatar
TheNumbat committed
106

TheNumbat's avatar
TheNumbat committed
107
    if (creating_bone) {
TheNumbat's avatar
TheNumbat committed
108

TheNumbat's avatar
TheNumbat committed
109
        undo.add_bone(my_obj->id(), new_joint);
TheNumbat's avatar
TheNumbat committed
110

TheNumbat's avatar
TheNumbat committed
111
112
        selected = new_joint;
        new_joint = nullptr;
TheNumbat's avatar
TheNumbat committed
113

TheNumbat's avatar
TheNumbat committed
114
115
        creating_bone = false;
        root_selected = false;
TheNumbat's avatar
TheNumbat committed
116

TheNumbat's avatar
TheNumbat committed
117
118
119
120
121
122
123
124
125
126
127
    } else if (widgets.want_drag()) {

        if (root_selected) {
            old_pos = my_obj->armature.base();
        } else {
            assert(selected);
            old_pos = my_obj->armature.end_of(selected);
            old_base = my_obj->armature.base_of(selected);
            old_ext = selected->extent;
        }
        widgets.start_drag(old_pos, cam, spos, dir);
TheNumbat's avatar
TheNumbat committed
128
129

    } else if (!id || id >= n_Widget_IDs) {
TheNumbat's avatar
TheNumbat committed
130
131
132

        selected = my_obj->armature.get_joint(id);
        root_selected = my_obj->armature.is_root_id(id);
TheNumbat's avatar
TheNumbat committed
133
134
135
136
    }
}

void Rig::clear() {
TheNumbat's avatar
TheNumbat committed
137
138
    my_obj = nullptr;
    selected = nullptr;
TheNumbat's avatar
TheNumbat committed
139
140
}

TheNumbat's avatar
TheNumbat committed
141
void Rig::clear_select() { selected = nullptr; }
TheNumbat's avatar
TheNumbat committed
142
143
144

void Rig::hover(Vec3 cam, Vec2 spos, Vec3 dir) {

TheNumbat's avatar
TheNumbat committed
145
146
147
148
    if (creating_bone) {

        assert(new_joint);
        assert(my_obj);
TheNumbat's avatar
TheNumbat committed
149

TheNumbat's avatar
TheNumbat committed
150
151
152
153
        Ray f(cam, dir);
        PT::Trace hit1 = mesh_bvh.hit(f);
        if (!hit1.hit)
            return;
TheNumbat's avatar
TheNumbat committed
154

TheNumbat's avatar
TheNumbat committed
155
156
        Ray s(hit1.position + dir * EPS_F, dir);
        PT::Trace hit2 = mesh_bvh.hit(s);
TheNumbat's avatar
TheNumbat committed
157

TheNumbat's avatar
TheNumbat committed
158
159
160
        Vec3 pos = hit1.position;
        if (hit2.hit)
            pos = 0.5f * (hit1.position + hit2.position);
TheNumbat's avatar
TheNumbat committed
161

TheNumbat's avatar
TheNumbat committed
162
163
164
        new_joint->extent = pos - old_base;
        my_obj->set_skel_dirty();
    }
TheNumbat's avatar
TheNumbat committed
165
166
}

TheNumbat's avatar
TheNumbat committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
Mode Rig::UIsidebar(Manager &manager, Undo &undo, Widgets &widgets, Scene_Maybe obj_opt) {

    if (!my_obj)
        return Mode::rig;

    if (!obj_opt.has_value())
        return Mode::rig;

    Scene_Item &item = obj_opt.value();
    if (!item.is<Scene_Object>())
        return Mode::rig;

    Scene_Object &obj = item.get<Scene_Object>();
    if (obj.opt.shape_type != PT::Shape_Type::none)
        return Mode::rig;

    if (my_obj != &obj) {
        my_obj = &obj;
        selected = nullptr;
    }
    if (my_obj->rig_dirty) {
        mesh_bvh.build(obj.mesh());
        my_obj->rig_dirty = false;
    }

    ImGui::Text("Edit Skeleton");

    if (creating_bone) {
        if (ImGui::Button("Cancel")) {
            creating_bone = false;
            my_obj->armature.erase(new_joint);
            new_joint = nullptr;
            my_obj->set_skel_dirty();
        }
    } else if (ImGui::Button("New Bone")) {

        creating_bone = true;

        if (!selected || root_selected) {
            new_joint = my_obj->armature.add_root(Vec3{0.0f});
            old_base = my_obj->armature.base();
        } else {
            new_joint = my_obj->armature.add_child(selected, Vec3{0.0f});
            old_base = my_obj->armature.end_of(selected);
        }
        my_obj->set_skel_dirty();
    }

    if (selected) {

        ImGui::Separator();
        ImGui::Text("Edit Bone");

        ImGui::DragFloat3("Extent", selected->extent.data, 0.1f);
        ImGui::DragFloat("Radius", &selected->radius, 0.05f, 0.0f,
                         std::numeric_limits<float>::max());

        ImGui::DragFloat3("Pose", selected->pose.data, 0.1f);

        if (ImGui::Button("Delete [del]")) {
            undo.del_bone(my_obj->id(), selected);
            selected = nullptr;
        }
    }
TheNumbat's avatar
TheNumbat committed
231
232
233
234

    return Mode::rig;
}

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