-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_components.cpp
More file actions
455 lines (409 loc) · 12.3 KB
/
ui_components.cpp
File metadata and controls
455 lines (409 loc) · 12.3 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "ui_components.h"
#include "user_interface.h"
#include "transformers.h"
#include "volume_convex.h"
#include "im-file-dialog/ImFileDialog.h"
#include "primitives/geometry/geo_cube.h"
#include "primitives/geometry/geo_disk.h"
#include "primitives/geometry/geo_sphere.h"
#include "primitives/materials/mat_debug.h"
#include "primitives/materials/mat_emissive.h"
#include "primitives/materials/mat_metallic.h"
#include "primitives/materials/mat_translucent.h"
#include "primitives/textures/tex_checker.h"
#include "primitives/textures/tex_image.h"
#include "primitives/textures/tex_perlin.h"
#include "primitives/textures/tex_uv_debug.h"
bool texture_slot(const char* label, std::shared_ptr<texture>& texture_ref, texture* self_exclude,
scene& _scene)
{
bool changed = false;
bool isnull = texture_ref == nullptr;
if (ImGui::BeginCombo(label, isnull ? "" : texture_ref->name.c_str()))
{
// Searchable
static ImGuiTextFilter filter;
if (ImGui::IsWindowAppearing())
{
ImGui::SetKeyboardFocusHere();
filter.Clear();
}
for (auto & i_tex : _scene.textures)
{
if (self_exclude != nullptr && i_tex.get() == self_exclude) // self exclude is ignored when in hittable mode(self_exclude = null)
continue;
const bool is_selected = !isnull && (texture_ref.get() == i_tex.get());
if (filter.PassFilter(i_tex->name.c_str()))
{
if (ImGui::Selectable((i_tex->name + " (" + i_tex->get_human_type() + ")").c_str(), is_selected))
{
texture_ref = std::shared_ptr(i_tex);
changed = true;
}
}
}
ImGui::EndCombo();
}
ImGui::SetItemTooltip("Please don\'t make any circular references! Otherwise the program will hang.");
// Drag and drop
if (ImGui::BeginDragDropTarget())
{
// Hover
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("REF_TEX", ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoPreviewTooltip))
{
std::shared_ptr<texture> ref = *static_cast<std::shared_ptr<texture>*>(payload->Data);
if (self_exclude != nullptr && ref.get() == self_exclude)
{
// Is duplicate
ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
ImGui::SetTooltip("No circular references.");
} else
{
ImGui::SetTooltip(std::format("Drop to set as {0}", ref->name).c_str());
// Not Duplicate
if (payload->IsDelivery()) // Dropped
{
changed = true;
texture_ref = std::shared_ptr(ref);
}
}
}
ImGui::EndDragDropTarget();
}
// Convenience: Create color
ImGui::SameLine();
if (ImGui::Button(("Make color##"+std::string(label)).c_str()))
{
user_interface::popup_color_storage[0] = 0.0f;
user_interface::popup_color_storage[1] = 0.0f;
user_interface::popup_color_storage[2] = 0.0f;
user_interface::popup_string_storage = "";
ImGui::OpenPopup(("Make color texture##"+std::string(label)).c_str());
}
bool open_b = true;
if (ImGui::BeginPopupModal(("Make color texture##"+std::string(label)).c_str(), &open_b))
{
// Make color!
ImGui::Text("Use this to quickly create a solid color texture and apply it.");
// name
bool satisfied = true;
std::string name_buf = user_interface::popup_string_storage;
if (ImGui::InputText("Name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.textures,
[&name_buf](const std::shared_ptr<texture>& sp) {
return sp->name == name_buf; // Compare underlying raw pointers
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
user_interface::popup_string_storage = name_buf;
}
}
if (user_interface::popup_string_storage.empty())
{
ImGui::TextColored(ImVec4(1.0f,0.0f,0.0f,1.0f), "Name must not be empty.");
satisfied = false;
}
// color
ImGui::ColorEdit3("Color", user_interface::popup_color_storage);
ImGui::BeginDisabled(!satisfied);
if (ImGui::Button("Create"))
{
// Create texture
auto tex = make_shared<tex_color>(user_interface::popup_color_storage[0], user_interface::popup_color_storage[1], user_interface::popup_color_storage[2]);
tex->name = user_interface::popup_string_storage;
_scene.textures.push_back(tex);
texture_ref = shared_ptr(tex);
ImGui::CloseCurrentPopup();
changed = true;
}
ImGui::EndDisabled();
ImGui::EndPopup();
}
return changed;
}
bool hittable_type_combo::create_prompt(scene& _scene)
{
ImGui::TextColored(user_interface::color_mesh, hittable_get_human_type(selection).c_str());
bool satisified = true;
// name field
std::string name_buf = name;
if (ImGui::InputText("Name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.objects,
[&name_buf](const std::shared_ptr<hittable>& sp) {
return sp->name == name_buf; // Compare underlying raw pointers
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
name = name_buf;
}
}
if (name.empty())
{
ImGui::TextColored(ImVec4(1.0f,0.0f,0.0f,1.0f), "Name must not be empty.");
satisified = false;
}
if (selection == cube || selection == sphere || selection == quad || selection == disk || selection == volume)
{
material_slot("Material", material_ref, _scene);
if (material_ref == nullptr)
{
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "A material must be assigned.");
satisified = false;
}
}
if (selection == mover || selection == rotator || selection == volume)
{
hittable_slot("Target Object", object_ref, _scene.world, _scene); // scene.world as self-exclude, since it will never be used
if (object_ref == nullptr)
{
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "The target Object (Shape) must be designated.");
satisified = false;
}
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "More parameters can be configured in the properties panel.");
ImGui::BeginDisabled(!satisified);
if (ImGui::Button("Create"))
{
switch (selection)
{
case cube:
_scene.objects.push_back(make_shared<geo_cube>(name, material_ref));
break;
case disk:
_scene.objects.push_back(make_shared<geo_disk>(name, material_ref));
break;
case quad:
_scene.objects.push_back(make_shared<geo_quad>(name, material_ref));
break;
case sphere:
_scene.objects.push_back(make_shared<geo_sphere>(name, material_ref));
break;
case list:
_scene.objects.push_back(make_shared<hittable_list>(name));
break;
case volume:
_scene.objects.push_back(make_shared<volume_convex>(name, object_ref, material_ref));
break;
case mover:
_scene.objects.push_back(make_shared<trn_move>(name, object_ref));
break;
case rotator:
_scene.objects.push_back(make_shared<trn_rotate>(name, object_ref));
break;
default:
// NOT SUPPOSED TO HAPPEN!
ImGui::SetTooltip("ERROR! Creation of a illegal object type! Please make a bug report!");
return false;
}
ImGui::EndDisabled();
return true;
}
ImGui::EndDisabled();
return false;
}
bool material_type_combo::create_prompt(scene& _scene)
{
ImGui::TextColored(user_interface::color_mat, material_get_human_type(selection).c_str());
bool satisfied = true;
// name field
std::string name_buf = name;
if (ImGui::InputText("Name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.materials,
[&name_buf](const std::shared_ptr<material>& sp) {
return sp->name == name_buf; // Compare underlying raw pointers
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
name = name_buf;
}
}
if (name.empty())
{
ImGui::TextColored(ImVec4(1.0f,0.0f,0.0f,1.0f), "Name must not be empty.");
satisfied = false;
}
// only debug normal requires no texture ref
if (selection != Debug_Normal)
{
texture_slot("Texture", tex_ref, _scene);
if (tex_ref == nullptr)
satisfied = false;
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "More parameters can be configured in the properties panel.");
ImGui::BeginDisabled(!satisfied);
if (ImGui::Button("Create"))
{
switch (selection)
{
case Debug_Normal:
_scene.materials.push_back(make_shared<mat_debug_normal>(name));
break;
case Diffuse:
_scene.materials.push_back(make_shared<mat_diffuse>(name, tex_ref));
break;
case Emissive:
_scene.materials.push_back(make_shared<mat_emissive>(name, tex_ref));
break;
case Metallic:
_scene.materials.push_back(make_shared<mat_metallic>(name, tex_ref));
break;
case Translucent:
_scene.materials.push_back(make_shared<mat_translucent>(name, tex_ref));
break;
case Volumetric:
_scene.materials.push_back(make_shared<mat_volumetric>(name, tex_ref));
break;
default:
// NOT SUPPOSED TO HAPPEN!
ImGui::SetTooltip("ERROR! Creation of a illegal material type! Please make a bug report!");
return false;
}
ImGui::EndDisabled();
return true;
}
ImGui::EndDisabled();
return false;
}
bool texture_type_combo::create_prompt(scene& _scene)
{
ImGui::TextColored(user_interface::color_tex, texture_get_human_type(selection).c_str());
bool satisfied = true;
// name field
std::string name_buf = name;
if (ImGui::InputText("Name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.textures,
[&name_buf](const std::shared_ptr<texture>& sp) {
return sp->name == name_buf; // Compare underlying raw pointers
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
name = name_buf;
}
}
if (name.empty())
{
ImGui::TextColored(ImVec4(1.0f,0.0f,0.0f,1.0f), "Name must not be empty.");
satisfied = false;
}
// Properties
switch (selection)
{
case Color:
{
float* col_buf = color_ref.get_float();
if (ImGui::ColorEdit3("Color", col_buf))
color_ref.set_float(col_buf);
break;
}
case Image:
{
ImGui::BeginDisabled();
ImGui::InputText("File", &path);
ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button("Pick"))
ifd::FileDialog::Instance().Open("ImageOpenDialog", "Select an image",
"Image file (*.png;*.jpg;*.jpeg;*.bmp;*.tga){.png,.jpg,.jpeg,.bmp,.tga},.*");
ImGui::SetItemTooltip("Press to choose an image from your filesystem.");
if (path.empty())
{
ImGui::TextColored(ImVec4(1.0f,.0f,.0f,1.0f), "An image file must be picked.");
satisfied = false;
}
if (ifd::FileDialog::Instance().IsDone("ImageOpenDialog")) {
if (ifd::FileDialog::Instance().HasResult()) {
path = ifd::FileDialog::Instance().GetResult().string();
}
ifd::FileDialog::Instance().Close();
}
break;
}
case Perlin:
{
texture_slot("Color", tex_ref_a, _scene);
if (tex_ref_a == nullptr)
{
ImGui::TextColored(ImVec4(1.0f,.0f,.0f,1.0f), "Color texture must be assigned!");
satisfied = false;
}
break;
}
case UV:
{
ImGui::TextDisabled("No properties are needed to create this texture.");
break;
}
case Checker:
{
texture_slot("Texture A", tex_ref_a, _scene);
ImGui::SetItemTooltip("Texture of the even squares");
if (tex_ref_a == nullptr)
{
ImGui::TextColored(ImVec4(1.0f,.0f,.0f,1.0f), "Texture A must be assigned!");
satisfied = false;
}
texture_slot("Texture B", tex_ref_b, _scene);
ImGui::SetItemTooltip("Texture of the odd squares");
if (tex_ref_b == nullptr)
{
ImGui::TextColored(ImVec4(1.0f,.0f,.0f,1.0f), "Texture B must be assigned!");
satisfied = false;
}
break;
}
}
ImGui::Spacing();
ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "More parameters can be configured in the properties panel.");
ImGui::BeginDisabled(!satisfied);
if (ImGui::Button("Create"))
{
switch (selection)
{
case Color:
_scene.textures.push_back(make_shared<tex_color>(name, color_ref));
break;
case Image:
_scene.textures.push_back(make_shared<tex_image>(name, path.c_str()));
break;
case Perlin:
_scene.textures.push_back(make_shared<tex_perlin>(name, tex_ref_a));
break;
case UV:
_scene.textures.push_back(make_shared<tex_uv_debug>(name));
break;
case Checker:
_scene.textures.push_back(make_shared<tex_checker>(name, tex_ref_a, tex_ref_b));
break;
default:
// NOT SUPPOSED TO HAPPEN!
ImGui::SetTooltip("ERROR! Creation of a illegal texture type! Please make a bug report!");
return false;
}
ImGui::EndDisabled();
return true;
}
ImGui::EndDisabled();
return false;
}