Skip to content

Commit 170ff69

Browse files
committed
add UI to alter parameter types on objects (primarily materials)
1 parent a8def7f commit 170ff69

File tree

1 file changed

+137
-33
lines changed

1 file changed

+137
-33
lines changed

tsd/apps/interactive/common/tsd_ui.cpp

+137-33
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,126 @@ static bool UI_stringList_callback(void *p, int index, const char **out_text)
2727
return true;
2828
}
2929

30+
static size_t buildUI_objects_menulist(const Context &ctx, anari::DataType type)
31+
{
32+
size_t retval = INVALID_INDEX;
33+
34+
for (size_t i = 0; i < ctx.numberOfObjects(type); i++) {
35+
auto *obj = ctx.getObject(type, i);
36+
if (!obj)
37+
continue;
38+
39+
ImGui::PushID(i);
40+
41+
static std::string oTitle;
42+
oTitle = '[';
43+
oTitle += std::to_string(i);
44+
oTitle += ']';
45+
oTitle += obj->name();
46+
if (ImGui::MenuItem(oTitle.c_str()))
47+
retval = i;
48+
49+
ImGui::PopID();
50+
}
51+
52+
return retval;
53+
}
54+
55+
static void buildUI_parameter_contextMenu(const Context &ctx, Parameter *p)
56+
{
57+
if (ImGui::BeginPopup("buildUI_parameter_contextMenu")) {
58+
if (ImGui::BeginMenu("set type")) {
59+
if (ImGui::BeginMenu("uniform")) {
60+
if (ImGui::BeginMenu("color")) {
61+
if (ImGui::MenuItem("float3") && p) {
62+
p->setValue(tsd::float3(1));
63+
p->setUsage(ParameterUsageHint::COLOR);
64+
}
65+
if (ImGui::MenuItem("float4") && p) {
66+
p->setValue(tsd::float4(1));
67+
p->setUsage(ParameterUsageHint::COLOR);
68+
}
69+
ImGui::EndMenu(); // "color"
70+
}
71+
72+
if (ImGui::BeginMenu("float")) {
73+
if (ImGui::MenuItem("float1") && p) {
74+
p->setValue(1.f);
75+
p->setUsage(ParameterUsageHint::NONE);
76+
}
77+
if (ImGui::MenuItem("float2") && p) {
78+
p->setValue(tsd::float2(1.f));
79+
p->setUsage(ParameterUsageHint::NONE);
80+
}
81+
if (ImGui::MenuItem("float3") && p) {
82+
p->setValue(tsd::float3(1.f));
83+
p->setUsage(ParameterUsageHint::NONE);
84+
}
85+
if (ImGui::MenuItem("float4") && p) {
86+
p->setValue(tsd::float4(1.f));
87+
p->setUsage(ParameterUsageHint::NONE);
88+
}
89+
ImGui::EndMenu(); // "float"
90+
}
91+
92+
if (ImGui::BeginMenu("int")) {
93+
if (ImGui::MenuItem("int1") && p) {
94+
p->setValue(0);
95+
p->setUsage(ParameterUsageHint::NONE);
96+
}
97+
if (ImGui::MenuItem("int2") && p) {
98+
p->setValue(tsd::int2(1));
99+
p->setUsage(ParameterUsageHint::NONE);
100+
}
101+
if (ImGui::MenuItem("int3") && p) {
102+
p->setValue(tsd::int3(1));
103+
p->setUsage(ParameterUsageHint::NONE);
104+
}
105+
if (ImGui::MenuItem("int4") && p) {
106+
p->setValue(tsd::int4(1));
107+
p->setUsage(ParameterUsageHint::NONE);
108+
}
109+
ImGui::EndMenu(); // "float"
110+
}
111+
112+
ImGui::EndMenu(); // "uniform"
113+
}
114+
115+
ImGui::Separator();
116+
117+
if (ImGui::MenuItem("attribute")) {
118+
p->setValue("attribute0");
119+
p->setStringValues(
120+
{"attribute0", "attribute1", "attribute2", "attribute3", "color"});
121+
p->setStringSelection(0);
122+
}
123+
124+
ImGui::Separator();
125+
126+
if (ImGui::BeginMenu("object")) {
127+
#define OBJECT_UI_MENU_ITEM(text, type) \
128+
if (ImGui::BeginMenu(text)) { \
129+
if (auto i = buildUI_objects_menulist(ctx, type); i != INVALID_INDEX && p) \
130+
p->setValue({type, i}); \
131+
ImGui::EndMenu(); \
132+
}
133+
134+
OBJECT_UI_MENU_ITEM("array", ANARI_ARRAY);
135+
OBJECT_UI_MENU_ITEM("geometry", ANARI_GEOMETRY);
136+
OBJECT_UI_MENU_ITEM("material", ANARI_MATERIAL);
137+
OBJECT_UI_MENU_ITEM("sampler", ANARI_SAMPLER);
138+
OBJECT_UI_MENU_ITEM("spatial field", ANARI_SPATIAL_FIELD);
139+
140+
ImGui::EndMenu(); // "object"
141+
}
142+
143+
ImGui::EndMenu(); // "set type"
144+
}
145+
146+
ImGui::EndPopup();
147+
}
148+
}
149+
30150
///////////////////////////////////////////////////////////////////////////////
31151

32152
void buildUI_object(tsd::Object &o,
@@ -108,6 +228,11 @@ void buildUI_object(tsd::Object &o,
108228

109229
ImGui::SameLine();
110230

231+
if (ImGui::Button("clear"))
232+
p.setValue({});
233+
234+
ImGui::SameLine();
235+
111236
ImGui::BeginDisabled(ctx.numberOfObjects(pVal.type()) == 0);
112237
if (ImGui::Button("select")) {
113238
typeForSelection = pVal.type();
@@ -187,9 +312,16 @@ void buildUI_parameter(
187312

188313
if (useTable) {
189314
ImGui::TableSetColumnIndex(0);
315+
190316
if (ImGui::Checkbox(name, &enabled))
191317
p.setEnabled(enabled);
192318
name = "";
319+
320+
const bool showContextMenu =
321+
ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right);
322+
if (showContextMenu)
323+
ImGui::OpenPopup("buildUI_parameter_contextMenu");
324+
193325
ImGui::TableSetColumnIndex(1);
194326
ImGui::PushItemWidth(-FLT_MIN); // Right-aligned
195327
}
@@ -267,42 +399,10 @@ void buildUI_parameter(
267399
p.setStringSelection(ss);
268400
}
269401
} else {
270-
#if 1
271402
if (useTable)
272403
ImGui::Text("\"%s\"", pVal.getString().c_str());
273404
else
274405
ImGui::BulletText("%s | '%s'", name, pVal.getString().c_str());
275-
#else
276-
constexpr int MAX_LENGTH = 2000;
277-
p.value.reserveString(MAX_LENGTH);
278-
279-
if (ImGui::Button("...")) {
280-
nfdchar_t *outPath = nullptr;
281-
nfdfilteritem_t filterItem[1] = {{"OBJ Files", "obj"}};
282-
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 1, nullptr);
283-
if (result == NFD_OKAY) {
284-
p.value = std::string(outPath).c_str();
285-
update = true;
286-
NFD_FreePath(outPath);
287-
} else {
288-
printf("NFD Error: %s\n", NFD_GetError());
289-
}
290-
}
291-
292-
ImGui::SameLine();
293-
294-
auto text_cb = [](ImGuiInputTextCallbackData *cbd) {
295-
auto &p = *(ui::Parameter *)cbd->UserData;
296-
p.value.resizeString(cbd->BufTextLen);
297-
return 0;
298-
};
299-
update |= ImGui::InputText(name,
300-
(char *)value,
301-
MAX_LENGTH,
302-
ImGuiInputTextFlags_CallbackEdit,
303-
text_cb,
304-
&p);
305-
#endif
306406
}
307407
} break;
308408
default:
@@ -318,7 +418,9 @@ void buildUI_parameter(
318418
if (ImGui::IsItemHovered()) {
319419
ImGui::BeginTooltip();
320420
if (isArray) {
321-
const auto &a = *ctx.getObject<Array>(pVal.getAsObjectIndex());
421+
const auto idx = pVal.getAsObjectIndex();
422+
const auto &a = *ctx.getObject<Array>(idx);
423+
ImGui::Text(" idx: [%zu]", idx);
322424
const auto t = a.type();
323425
if (t == ANARI_ARRAY3D)
324426
ImGui::Text(" size: %zu x %zu x %zu", a.dim(0), a.dim(1), a.dim(2));
@@ -339,6 +441,8 @@ void buildUI_parameter(
339441
if (update)
340442
p.setValue(pVal);
341443

444+
buildUI_parameter_contextMenu(ctx, &p);
445+
342446
ImGui::PopID();
343447
}
344448

0 commit comments

Comments
 (0)