From 42ae9a6ce343efd4a3c7ead1603b39ef80cdd737 Mon Sep 17 00:00:00 2001 From: Jason Beverage Date: Fri, 31 Jan 2025 10:41:11 -0500 Subject: [PATCH] Added an auto update option to the NodeGraphGUI --- src/osgEarthImGui/NodeGraphGUI | 321 ++++++++++++++++++--------------- 1 file changed, 179 insertions(+), 142 deletions(-) diff --git a/src/osgEarthImGui/NodeGraphGUI b/src/osgEarthImGui/NodeGraphGUI index a771826b8c..208006dbf1 100644 --- a/src/osgEarthImGui/NodeGraphGUI +++ b/src/osgEarthImGui/NodeGraphGUI @@ -43,167 +43,204 @@ namespace osgEarth osg::observer_ptr< MapNode > _mapNode; osg::observer_ptr< ProceduralTiledModelLayer > _proceduralModelLayer; + bool _autoUpdate = true; public: NodeGraphGUI() : ImGuiPanel("NodeGraph") { } - void draw(osg::RenderInfo& ri) override + void DrawMenuBar() { - if (!isVisible()) - return; - - if (!findNodeOrHide(_mapNode, ri)) - return; - - if (!_proceduralModelLayer.valid()) + if (ImGui::BeginMenuBar()) { - _proceduralModelLayer = _mapNode->getMap()->getLayer(); - } - - if (ImGui::Begin(name(), visible(), ImGuiWindowFlags_MenuBar)) - { - if (ImGui::BeginMenuBar()) + if (ImGui::BeginMenu("Add Node")) { - if (ImGui::BeginMenu("Add Node")) + if (ImGui::MenuItem("Sphere")) { - if (ImGui::MenuItem("Sphere")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Transform")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Transform")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Float")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Float")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Join")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Join")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Simplify")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Simplify")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Random Points")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Random Points")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Points On Edge")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Points On Edge")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Gridded Points")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Gridded Points")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Place Nodes")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Place Nodes")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Features to Lines")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Features to Lines")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Random Value Per Feature")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Random Value Per Feature")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Random Color Per Feature")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Random Color Per Feature")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Current LOD")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Current LOD")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Comparison")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Comparison")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Clamp Points")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Clamp Points")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Filter Features")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Filter Features")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Feature Ouptut")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Feature Ouptut")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("OSM Highways Color")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("OSM Highways Color")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Polygons to Points")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Polygons to Points")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Image Mask")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Image Mask")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Image From Layer")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Image From Layer")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } #ifdef HAS_PFD - if (ImGui::MenuItem("Load Image")) - { - auto f = pfd::open_file("Image", pfd::path::home(), - { "All Files", "*" }, - pfd::opt::multiselect); + if (ImGui::MenuItem("Load Image")) + { + auto f = pfd::open_file("Image", pfd::path::home(), + { "All Files", "*" }, + pfd::opt::multiselect); - if (f.result().size() > 0) + if (f.result().size() > 0) + { + for (auto const& name : f.result()) { - for (auto const& name : f.result()) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared(name)); - } + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared(name)); } } + } #endif - if (ImGui::MenuItem("Offset")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Offset")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - if (ImGui::MenuItem("Join")) - { - _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); - } + if (ImGui::MenuItem("Join")) + { + _proceduralModelLayer->getNodeGraph()->operations.push_back(std::make_shared()); + } - ImGui::EndMenu(); - } + ImGui::EndMenu(); } - ImGui::EndMenuBar(); + } + ImGui::EndMenuBar(); + } + + void DrawToolbar() + { + if (ImGui::Checkbox("Auto Update", &_autoUpdate)) + { + // Go ahead and do an update if we are turning on auto update + if (_autoUpdate) + { + _proceduralModelLayer->dirtyNodeGraph(); + } + } + // Show an Update button to manually refresh the node graph if auto update is off. + if (!_autoUpdate) + { + ImGui::SameLine(); + if (ImGui::Button("Update")) + { + _proceduralModelLayer->dirtyNodeGraph(); + } + } + } + + void updateNodeGraph() + { + if (_autoUpdate) + { + _proceduralModelLayer->dirtyNodeGraph(); + } + } + + void draw(osg::RenderInfo& ri) override + { + if (!isVisible()) + return; + + if (!findNodeOrHide(_mapNode, ri)) + return; + + if (!_proceduralModelLayer.valid()) + { + _proceduralModelLayer = _mapNode->getMap()->getLayer(); + } + + if (ImGui::Begin(name(), visible(), ImGuiWindowFlags_MenuBar)) + { + DrawMenuBar(); + + DrawToolbar(); ImNodes::BeginNodeEditor(); @@ -224,7 +261,7 @@ namespace osgEarth if (ImGui::InputFloat("Value", &v)) { floatValue->setValue(v); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -237,7 +274,7 @@ namespace osgEarth if (ImGui::ColorEdit4("Color", (float*)color._v, ImGuiColorEditFlags_NoInputs)) { sphere->setColor(color); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -250,7 +287,8 @@ namespace osgEarth if (ImGui::InputFloat("Offset", &o)) { offset->setOffset(o); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); + } ImGui::PopItemWidth(); } @@ -264,14 +302,14 @@ namespace osgEarth if (ImGui::ColorEdit4("Color", (float*)color._v, ImGuiColorEditFlags_NoInputs)) { imageMask->setColor(color); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } float tolerance = imageMask->getTolerance(); if (ImGui::SliderFloat("Tolerance", &tolerance, 0.0f, 1.0f)) { imageMask->setTolerance(tolerance); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -288,7 +326,7 @@ namespace osgEarth if (ImGui::InputText("Layer", layerNameChar, IM_ARRAYSIZE(layerNameChar))) { imageFromLayer->setLayerName(std::string(layerNameChar)); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -305,7 +343,7 @@ namespace osgEarth if (ImGui::InputText("Layer", layerNameChar, IM_ARRAYSIZE(layerNameChar))) { join->setLayerName(std::string(layerNameChar)); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -339,7 +377,7 @@ namespace osgEarth if (dirty) { - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -378,7 +416,7 @@ namespace osgEarth { osg::Matrix newMatrix = osg::Matrixd::scale(scale) * osg::Matrix::rotate(rotation) * osg::Matrix::translate(translation); transformOp->setTransform(newMatrix); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -393,7 +431,7 @@ namespace osgEarth if (ImGui::InputInt("Count", &count)) { randomPoints->setCount(count);; - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -406,7 +444,7 @@ namespace osgEarth if (ImGui::InputInt("Count", &count)) { griddedPoints->setCount(count);; - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -419,7 +457,7 @@ namespace osgEarth if (ImGui::InputInt("Count", &count)) { pointsOnEdge->setCount(count);; - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -437,7 +475,7 @@ namespace osgEarth { c = (ComparisonOperator::Comparison)item_current; comparisonOperator->setComparison(c); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -451,7 +489,7 @@ namespace osgEarth { placeNodes->setMinScale(scale[0]); placeNodes->setMaxScale(scale[1]); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } float heading[] = { placeNodes->getMinHeading(), placeNodes->getMaxHeading() }; @@ -460,7 +498,7 @@ namespace osgEarth { placeNodes->setMinHeading(heading[0]); placeNodes->setMaxHeading(heading[1]); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -475,7 +513,7 @@ namespace osgEarth { randomValuePerFeature->setMinValue(range[0]); randomValuePerFeature->setMaxValue(range[1]); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -488,7 +526,7 @@ namespace osgEarth if (ImGui::Checkbox("Random", &random)) { polygonsToPoints->setRandom(random); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); @@ -504,7 +542,7 @@ namespace osgEarth if (ImGui::InputText("Attribute", attributeChar, IM_ARRAYSIZE(attributeChar))) { filterFeatures->setAttribute(std::string(attributeChar)); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } std::string value = filterFeatures->getValue(); @@ -513,7 +551,7 @@ namespace osgEarth if (ImGui::InputText("Value", valueChar, IM_ARRAYSIZE(valueChar))) { filterFeatures->setValue(std::string(valueChar)); - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } ImGui::PopItemWidth(); } @@ -600,7 +638,7 @@ namespace osgEarth } - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); } int link_id; @@ -613,8 +651,7 @@ namespace osgEarth if (itr->getId() == link_id) { o->getLinks().erase(itr); - - _proceduralModelLayer->dirtyNodeGraph(); + updateNodeGraph(); break; } }