Skip to content

Commit a30ca45

Browse files
committed
add ability to insert existing objects into a layer node
1 parent 0149805 commit a30ca45

File tree

5 files changed

+62
-35
lines changed

5 files changed

+62
-35
lines changed

tsd/apps/interactive/common/tsd_ui.cpp

+25-25
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,6 @@ 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-
5530
static void buildUI_parameter_contextMenu(Context &ctx, Parameter *p)
5631
{
5732
if (ImGui::BeginPopup("buildUI_parameter_contextMenu")) {
@@ -465,6 +440,31 @@ void buildUI_parameter(tsd::Parameter &p, tsd::Context &ctx, bool useTable)
465440
ImGui::PopID();
466441
}
467442

443+
size_t buildUI_objects_menulist(const Context &ctx, anari::DataType type)
444+
{
445+
size_t retval = INVALID_INDEX;
446+
447+
for (size_t i = 0; i < ctx.numberOfObjects(type); i++) {
448+
auto *obj = ctx.getObject(type, i);
449+
if (!obj)
450+
continue;
451+
452+
ImGui::PushID(i);
453+
454+
static std::string oTitle;
455+
oTitle = '[';
456+
oTitle += std::to_string(i);
457+
oTitle += ']';
458+
oTitle += obj->name();
459+
if (ImGui::MenuItem(oTitle.c_str()))
460+
retval = i;
461+
462+
ImGui::PopID();
463+
}
464+
465+
return retval;
466+
}
467+
468468
void addDefaultRendererParameters(Object &o)
469469
{
470470
o.addParameter("background")

tsd/apps/interactive/common/tsd_ui.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ void buildUI_object(tsd::Object &o,
1818
int level = 0);
1919
void buildUI_parameter(
2020
tsd::Parameter &p, tsd::Context &ctx, bool asTable = false);
21+
size_t buildUI_objects_menulist(const Context &ctx, anari::DataType type);
2122

2223
void addDefaultRendererParameters(Object &o);
2324
Object parseANARIObject(
2425
anari::Device d, ANARIDataType type, const char *subtype);
2526

26-
} // namespace tsd::ui
27+
} // namespace tsd::ui

tsd/apps/interactive/common/windows/ObjectTree.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,24 @@ void ObjectTree::buildUI_objectContextMenu()
198198
clearSelectedNode = true;
199199
}
200200

201+
ImGui::Separator();
202+
203+
if (ImGui::BeginMenu("existing")) {
204+
#define OBJECT_UI_MENU_ITEM(text, type) \
205+
if (ImGui::BeginMenu(text)) { \
206+
if (auto i = tsd::ui::buildUI_objects_menulist(ctx, type); \
207+
i != tsd::INVALID_INDEX) \
208+
ctx.insertChildObjectNode(menuNode, type, i); \
209+
ImGui::EndMenu(); \
210+
}
211+
OBJECT_UI_MENU_ITEM("surface", ANARI_SURFACE);
212+
OBJECT_UI_MENU_ITEM("volume", ANARI_VOLUME);
213+
OBJECT_UI_MENU_ITEM("light", ANARI_LIGHT);
214+
ImGui::EndMenu();
215+
}
216+
217+
ImGui::Separator();
218+
201219
if (ImGui::BeginMenu("procedural")) {
202220
if (ImGui::MenuItem("cylinders")) {
203221
generate_cylinders(ctx, menuNode);

tsd/src/tsd/core/Context.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,17 @@ LayerNodeRef Context::insertChildTransformNode(
261261
return inst;
262262
}
263263

264+
LayerNodeRef Context::insertChildObjectNode(LayerNodeRef parent,
265+
anari::DataType type,
266+
size_t idx,
267+
const char *name)
268+
{
269+
auto inst = parent->insert_last_child(tsd::utility::Any{type, idx});
270+
(*inst)->name = name;
271+
signalLayerChange();
272+
return inst;
273+
}
274+
264275
void Context::removeInstancedObject(LayerNodeRef obj)
265276
{
266277
if (obj->isRoot())

tsd/src/tsd/core/Context.hpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ struct Context
105105
template <typename T>
106106
LayerNodeRef insertChildObjectNode(
107107
LayerNodeRef parent, IndexedVectorRef<T> obj, const char *name = "");
108+
LayerNodeRef insertChildObjectNode(LayerNodeRef parent,
109+
anari::DataType type,
110+
size_t idx,
111+
const char *name = "");
108112

109113
// NOTE: convenience to create an object _and_ insert it into the tree
110114
template <typename T>
@@ -293,22 +297,15 @@ template <typename T>
293297
inline LayerNodeRef Context::insertChildObjectNode(
294298
LayerNodeRef parent, IndexedVectorRef<T> obj, const char *name)
295299
{
296-
auto inst =
297-
parent->insert_last_child(tsd::utility::Any{obj->type(), obj->index()});
298-
(*inst)->name = name;
299-
signalLayerChange();
300-
return inst;
300+
return insertChildObjectNode(parent, obj->type(), obj->index(), name);
301301
}
302302

303303
template <typename T>
304304
inline Context::AddedObject<T> Context::insertNewChildObjectNode(
305305
LayerNodeRef parent, Token subtype, const char *name)
306306
{
307307
auto obj = createObject<T>(subtype);
308-
auto inst =
309-
parent->insert_last_child(tsd::utility::Any{obj->type(), obj->index()});
310-
(*inst)->name = name;
311-
signalLayerChange();
308+
auto inst = insertChildObjectNode(parent, obj, name);
312309
return std::make_pair(inst, obj);
313310
}
314311

0 commit comments

Comments
 (0)