-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_components.h
More file actions
412 lines (358 loc) · 10.5 KB
/
ui_components.h
File metadata and controls
412 lines (358 loc) · 10.5 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
#ifndef RAYTRACINGWEEKEND_UI_COMPONENTS_H
#define RAYTRACINGWEEKEND_UI_COMPONENTS_H
#include <memory>
#include <imgui.h>
#include <misc/cpp/imgui_stdlib.h>
#include "misc.h"
#include "scene.h"
#include "hittable.h"
#include "primitives/textures/tex_color.h"
// Name slots
inline bool name_slot(hittable& object, scene& _scene)
{
std::string name_buf = object.name; // copy
if (ImGui::InputText("##name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.objects,
[&object, &name_buf](const std::shared_ptr<hittable>& sp) {
return sp.get() != &object && sp->name == name_buf; // Compare underlying raw pointers
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
object.name = name_buf;
return true;
}
}
return false;
}
inline bool name_slot(material& mat, scene& _scene)
{
std::string name_buf = mat.name; // copy
if (ImGui::InputText("##name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.materials,
[&mat, &name_buf](const std::shared_ptr<material>& sp) {
return sp.get() != &mat && sp->name == name_buf; // Compare underlying raw pointers
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
mat.name = name_buf;
return true;
}
}
return false;
}
inline bool name_slot(texture& tex, scene& _scene)
{
std::string name_buf = tex.name;
if (ImGui::InputText("##name", &name_buf))
{
if (name_buf.empty() ||
std::ranges::any_of(_scene.textures,
[&tex, &name_buf](const std::shared_ptr<texture>& sp) {
return sp.get() != &tex && sp->name == name_buf;
}))
{
// Duplicate
ImGui::SetTooltip("It must be a unique, non-empty name.");
} else
{
tex.name = name_buf;
return true;
}
}
return false;
}
// Reference slots
inline bool hittable_slot(const char* label, std::shared_ptr<hittable>& hittable_ref, hittable& self_exclude, scene& _scene)
{
bool changed = false;
bool isnull = hittable_ref == nullptr;
if (ImGui::BeginCombo(label, isnull ? "" : hittable_ref->name.c_str()))
{
// Searchable
static ImGuiTextFilter filter;
if (ImGui::IsWindowAppearing())
{
ImGui::SetKeyboardFocusHere();
filter.Clear();
}
for (auto & i_obj : _scene.objects)
{
if (i_obj.get() == &self_exclude)
continue; // prevent self-referencing. Other circular references may still happen though
const bool is_selected = !isnull && (hittable_ref.get() == i_obj.get());
if (filter.PassFilter(i_obj->name.c_str()))
{
if (ImGui::Selectable((i_obj->name + " (" + i_obj->get_human_type() + ")").c_str(), is_selected))
{
hittable_ref = std::shared_ptr(i_obj);
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_GEO", ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoPreviewTooltip))
{
std::shared_ptr<hittable> ref = *static_cast<std::shared_ptr<hittable>*>(payload->Data);
if (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;
hittable_ref = std::shared_ptr(ref);
}
}
}
ImGui::EndDragDropTarget();
}
return changed;
}
// material
inline bool material_slot(const char* label, std::shared_ptr<material>& material_ref, std::shared_ptr<material>& self_exclude, scene& _scene)
{
bool changed = false;
bool isnull = material_ref == nullptr;
if (ImGui::BeginCombo(label, isnull ? "" : material_ref->name.c_str()))
{
// Searchable
static ImGuiTextFilter filter;
if (ImGui::IsWindowAppearing())
{
ImGui::SetKeyboardFocusHere();
filter.Clear();
}
for (auto & i_mat : _scene.materials)
{
if (self_exclude != nullptr && i_mat.get() == self_exclude.get()) // self exclude is ignored when in hittable mode(self_exclude = null)
continue;
const bool is_selected = !isnull && (material_ref.get() == i_mat.get());
if (filter.PassFilter(i_mat->name.c_str()))
{
if (ImGui::Selectable((i_mat->name + " (" + i_mat->get_human_type() + ")").c_str(), is_selected))
{
material_ref = std::shared_ptr(i_mat);
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_MAT", ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoPreviewTooltip))
{
std::shared_ptr<material> ref = *static_cast<std::shared_ptr<material>*>(payload->Data);
if (self_exclude != nullptr && ref.get() == self_exclude.get())
{
// 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;
material_ref = std::shared_ptr(ref);
}
}
}
ImGui::EndDragDropTarget();
}
return changed;
}
inline bool material_slot(const char* label, std::shared_ptr<material>& material_ref, scene& _scene)
{
shared_ptr<material> null_mat = nullptr;
return material_slot(label, material_ref, null_mat, _scene);
}
// texture
bool texture_slot(const char* label, std::shared_ptr<texture>& texture_ref, texture* self_exclude, scene& _scene);
inline bool texture_slot(const char* label, std::shared_ptr<texture>& texture_ref, scene& _scene)
{
return texture_slot(label, texture_ref, nullptr, _scene);
}
// CREATION
class hittable_type_combo
{
public:
hittable_type_combo() : selection(cube) {} // cube as default
hittable_type selection;
void combo()
{
if (ImGui::BeginCombo("Object type", hittable_get_human_type(selection).c_str()))
{
for (auto& type : types)
{
const bool is_selected = type.first == selection;
if (ImGui::Selectable(hittable_get_human_type(type.first).c_str(), is_selected))
{
selection = type.first;
}
if (is_selected)
ImGui::SetItemDefaultFocus(); // init focus to this~!
}
ImGui::EndCombo();
}
}
const char* get_description()
{
for (auto& type : types)
{
if (type.first == selection)
return type.second;
}
return "Unknown object. Proceed with caution.";
}
std::string name;
std::shared_ptr<hittable> object_ref;
std::shared_ptr<material> material_ref;
void reset_props()
{
name = "";
object_ref = nullptr;
material_ref = nullptr;
}
bool create_prompt(scene& _scene);
private:
const std::pair<hittable_type, const char*> types[8] = {
std::pair(cube, "(Shape) A world-aligned cube."), // Requires mat, name
std::pair(sphere, "(Shape) A perfectly smooth sphere."), // Requires mat, name
std::pair(quad, "(Shape) An arbitrary flat 3D equilateral."), // Requires mat, name
std::pair(disk, "(Shape) A circular variant of a quad."), // Requires mat, name
std::pair(mover, "(Modifier) Moves an existing object."), // Requires obj, name
std::pair(rotator, "(Modifier) Rotates an existing object."), // Requires obj, name
std::pair(volume, "(Modifier) Volumetrics in the shape of an object."), // Requires obj, mat, name
std::pair(list, "(Modifier) A list of objects merged together for easier management.") // Requires name
};
};
class material_type_combo
{
public:
material_type_combo() : selection(Diffuse) {} // cube as default
material_type selection;
void combo()
{
if (ImGui::BeginCombo("Material type", material_get_human_type(selection).c_str()))
{
for (auto& type : types)
{
const bool is_selected = type.first == selection;
if (ImGui::Selectable(material_get_human_type(type.first).c_str(), is_selected))
{
selection = type.first;
}
if (is_selected)
ImGui::SetItemDefaultFocus(); // init focus to this~!
}
ImGui::EndCombo();
}
}
const char* get_description()
{
for (auto& type : types)
{
if (type.first == selection)
return type.second;
}
return "Unknown material. Proceed with caution.";
}
std::string name;
std::shared_ptr<texture> tex_ref;
void reset_props()
{
name = "";
tex_ref = nullptr;
}
bool create_prompt(scene& _scene);
private:
const std::pair<material_type, const char*> types[6] = {
std::pair(Diffuse, "A perfectly matte material."), // name, tex
std::pair(Metallic, "A tintable metallic material with variable roughness."), // name, tex
std::pair(Translucent, "A perfectly refractive material."), // name, tex
std::pair(Emissive, "A glowing material that emits light."), // name, tex
std::pair(Volumetric, "Simulates volumetrics like fog. To be used with the volumetric modifier."), // name, tex
std::pair(Debug_Normal, "Displays the normal of an object via color. For debugging.") // NAME ONLY
};
};
class texture_type_combo
{
public:
texture_type_combo() : selection(Color) {} // cube as default
texture_type selection;
void combo()
{
if (ImGui::BeginCombo("Texture type", texture_get_human_type(selection).c_str()))
{
for (auto& type : types)
{
const bool is_selected = type.first == selection;
if (ImGui::Selectable(texture_get_human_type(type.first).c_str(), is_selected))
{
selection = type.first;
}
if (is_selected)
ImGui::SetItemDefaultFocus(); // init focus to this~!
}
ImGui::EndCombo();
}
}
const char* get_description()
{
for (auto& type : types)
{
if (type.first == selection)
return type.second;
}
return "Unknown texture. Proceed with caution.";
}
std::string name;
color color_ref;
std::shared_ptr<texture> tex_ref_a;
std::shared_ptr<texture> tex_ref_b;
std::string path;
void reset_props()
{
name = "";
color_ref = color::zero;
tex_ref_a = nullptr;
tex_ref_b = nullptr;
path = "";
}
bool create_prompt(scene& _scene);
private:
const std::pair<texture_type, const char*> types[5] = {
std::pair(Color, "Texture of a solid color."),
std::pair(Image, "Texture from an external image file."),
std::pair(Checker, "A Procedural checkerboard pattern."),
std::pair(Perlin, "Procedural perlin noise."),
std::pair(UV, "Visualizes the UVs of an object. For debugging.")
};
};
#endif //RAYTRACINGWEEKEND_UI_COMPONENTS_H