1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#include "debugdraw.hpp"
#include "editor.hpp"
#include "model.hpp"
#include "scene.hpp"
#include "ui.hpp"
#include "world.hpp"
static struct {
UI* ui;
UI::Toolbar* toolbar;
UI::Button* mat_btn, * ent_btn;
UI::Modal* mat_win, * ent_win;
UI::Label* mat_name;
UI::Slider* metalness_input;
UI::Slider* roughness_input;
UI::Slider* ao_input;
UI::Label* ent_handle, * ent_index, * ent_version, * ent_mask;
World* world;
Entity_Id selected;
Model_Instance* selected_inst;
int selected_mesh;
} editor;
static int mat_win_handler(UI::Element* e, const UI::Message& m) {
(void)e;
if (m.type == UI::Message::Type::destroy) {
editor.mat_btn->enable();
editor.mat_win = 0;
}
return 0;
}
static void setup_mat_edit() {
if (editor.selected_inst) {
Mesh* meshes = editor.selected_inst->m->get_meshes();
Mesh& msh = meshes[editor.selected_mesh];
Material* mat = msh.material;
editor.metalness_input->enable();
editor.metalness_input->input->set_val(mat->metalness);
editor.roughness_input->enable();
editor.roughness_input->input->set_val(mat->roughness);
editor.ao_input->enable();
editor.ao_input->input->set_val(mat->ao);
editor.mat_name->set_text(mat->name);
} else {
editor.metalness_input->disable();
editor.roughness_input->disable();
editor.ao_input->disable();
editor.mat_name->set_text("<no material>");
}
}
void setup_ent_debug() {
if (editor.selected) {
int ind = entity_index(editor.selected);
editor.ent_handle->set_hex(editor.selected);
editor.ent_version->set_int(entity_version(editor.selected));
editor.ent_index->set_int(ind);
editor.ent_mask->set_hex(editor.world->masks[ind]);
} else {
editor.ent_handle->set_text("<null>");
editor.ent_version->set_text("<null>");
editor.ent_index->set_text("<null>");
editor.ent_mask->set_text("<null>");
}
}
static int mat_btn_handler(UI::Element* e, const UI::Message& m) {
(void)e;
if (m.type == UI::Message::Type::click) {
editor.mat_win = editor.ui->create_element<UI::Modal>(
editor.ui->root,
"Material Editor"
);
int rows[] = { 200, 250, 0 };
editor.mat_win->handler = mat_win_handler;
editor.mat_btn->disable();
editor.mat_name = editor.ui->create_element<UI::Label>(
editor.mat_win->contents,
""
);
auto container = editor.ui->create_element<UI::Table>(
editor.mat_win->contents,
rows
);
#define bind_mat_float(el, name, point) \
editor.ui->create_element<UI::Label>( \
container, \
name \
); \
el = editor.ui->create_element<UI::Slider>( \
container, \
0.0f, \
1.0f \
); \
el->input->handler = []( \
UI::Element* e, \
const UI::Message& m \
) { \
if ( \
m.type == UI::Message::Type::input_changed && \
editor.selected_inst \
) { \
UI::Float_Input* in = (UI::Float_Input*)e; \
Mesh* meshes = editor.selected_inst->m->get_meshes(); \
Mesh& mesh = meshes[editor.selected_mesh]; \
Material* mat = mesh.material; \
mat->point = in->val; \
} \
return 0; \
};
bind_mat_float(
editor.metalness_input,
"Metalness",
metalness
);
bind_mat_float(
editor.roughness_input,
"Roughness",
roughness
);
bind_mat_float(
editor.ao_input,
"Ambient Occlusion",
ao
);
#undef bind_mat_float
setup_mat_edit();
}
return 0;
}
static int ent_win_handler(UI::Element* e, const UI::Message& m) {
(void)e;
if (m.type == UI::Message::Type::destroy) {
editor.ent_btn->enable();
editor.ent_win = 0;
}
return 0;
}
static int ent_btn_handler(UI::Element* e, const UI::Message& m) {
if (m.type == UI::Message::Type::click) {
auto ui = editor.ui;
editor.ent_win = ui->create_element<UI::Modal>(
editor.ui->root,
"Entity Debugger"
);
editor.ent_win->handler = ent_win_handler;
editor.ent_btn->disable();
int rows[] = { 100, 50, 0 };
auto tab = ui->create_element<UI::Table>(
editor.ent_win->contents,
rows
);
ui->create_element<UI::Label>(tab, "Handle");
editor.ent_handle = ui->create_element<UI::Label>(tab, "");
ui->create_element<UI::Label>(tab, "Index");
editor.ent_index = ui->create_element<UI::Label>(tab, "");
ui->create_element<UI::Label>(tab, "Version");
editor.ent_version = ui->create_element<UI::Label>(tab, "");
ui->create_element<UI::Label>(tab, "Mask");
editor.ent_mask = ui->create_element<UI::Label>(tab, "");
setup_ent_debug();
}
return 0;
}
void init_editor(UI* ui, World* world) {
editor.ui = ui;
editor.toolbar = ui->create_element<UI::Toolbar>(ui->root);
editor.mat_btn = ui->create_element<UI::Button>(
editor.toolbar,
"Edit Material"
);
editor.mat_btn->handler = mat_btn_handler;
editor.ent_btn = ui->create_element<UI::Button>(
editor.toolbar,
"Entity Debugger"
);
editor.ent_btn->handler = ent_btn_handler;
editor.mat_win = 0;
editor.ent_win = 0;
editor.world = world;
}
void deinit_editor() {
}
void editor_on_select(Entity_Id e, int m) {
if (!editor.ui->hovered) {
editor.selected = e;
if (e && editor.world->has<C_Model>(e)) {
auto& cm = editor.world->get<C_Model>(e);
editor.selected_inst = cm.i;
editor.selected_mesh = m;
} else {
editor.selected_inst = 0;
editor.selected_mesh = -1;
}
if (editor.mat_win) {
setup_mat_edit();
}
if (editor.ent_win) {
setup_ent_debug();
}
}
}
void editor_draw(Line_Renderer& lr) {
if (editor.selected_inst) {
lr.add_box(
editor.selected_inst->bounds[editor.selected_mesh]
);
}
}
|