Skip to content

Commit

Permalink
Viewpoints: update and add XML printout to GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Oct 18, 2023
1 parent 75e6dda commit 6351eff
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 143 deletions.
14 changes: 7 additions & 7 deletions src/osgEarth/EarthManipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ _min_distance ( 1.0 ),
_max_distance ( DBL_MAX ),
_tether_mode ( TETHER_CENTER ),
_arc_viewpoints ( true ),
_auto_vp_duration ( false ),
_min_vp_duration_s ( 3.0 ),
_max_vp_duration_s ( 8.0 ),
_auto_vp_duration ( true ),
_min_vp_duration_s ( 1.0 ),
_max_vp_duration_s ( 3.0 ),
_orthoTracksPerspective ( true ),
_terrainAvoidanceEnabled ( true ),
_terrainAvoidanceMinDistance ( 1.0 ),
Expand Down Expand Up @@ -539,8 +539,8 @@ EarthManipulator::Settings::setAutoViewpointDurationEnabled( bool value )
void
EarthManipulator::Settings::setAutoViewpointDurationLimits( double minSeconds, double maxSeconds )
{
_min_vp_duration_s = osg::clampAbove( minSeconds, 0.0 );
_max_vp_duration_s = osg::clampAbove( maxSeconds, _min_vp_duration_s );
_min_vp_duration_s = std::max( minSeconds, 0.0 );
_max_vp_duration_s = std::max( maxSeconds, _min_vp_duration_s );
dirty();
}

Expand Down Expand Up @@ -1036,7 +1036,7 @@ EarthManipulator::setViewpoint(const Viewpoint& vp, double duration_seconds)
_setVP1->focalPoint() = _setVP0->focalPoint().get();
}

_setVPDuration.set( osg::maximum(duration_seconds, 0.0), Units::SECONDS );
_setVPDuration.set( std::max(duration_seconds, 0.0), Units::SECONDS );

OE_DEBUG << LC << "setViewpoint:\n"
<< " from " << _setVP0->toString() << "\n"
Expand Down Expand Up @@ -1168,7 +1168,7 @@ EarthManipulator::setViewpointFrame(double time_s)
// Remaining time is the full duration minus the time since initiation:
double elapsed = time_s - _setVPStartTime->as(Units::SECONDS);
double duration = _setVPDuration.as(Units::SECONDS);
double t = osg::minimum(1.0, duration > 0.0 ? elapsed/duration : 1.0);
double t = std::min(1.0, duration > 0.0 ? elapsed/duration : 1.0);
double tp = t;

if ( _setVPArcHeight > 0.0 )
Expand Down
26 changes: 26 additions & 0 deletions src/osgEarth/ImGui/ImGui
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ namespace ImGuiLTable
return ImGui::SliderFloat(s.c_str(), v, v_min, v_max, format, flags);
}

static bool SliderDouble(const char* label, double* v, double v_min, double v_max, const char* format = nullptr)
{
ImGui::TableNextColumn();
ImGui::Text(label);
ImGui::TableNextColumn();
ImGui::SetNextItemWidth(-1);
std::string s("##" + std::string(label));
float temp = *v;
bool ok = ImGui::SliderFloat(s.c_str(), &temp, (float)v_min, (float)v_max, format);
if (ok) *v = (double)temp;
return ok;
}

static bool SliderDouble(const char* label, double* v, double v_min, double v_max, const char* format, ImGuiSliderFlags flags)
{
ImGui::TableNextColumn();
ImGui::Text(label);
ImGui::TableNextColumn();
ImGui::SetNextItemWidth(-1);
std::string s("##" + std::string(label));
float temp = *v;
bool ok = ImGui::SliderFloat(s.c_str(), &temp, (float)v_min, (float)v_max, format, flags);
if (ok) *v = (double)temp;
return ok;
}

static bool SliderInt(const char* label, int* v, int v_min, int v_max)
{
ImGui::TableNextColumn();
Expand Down
86 changes: 53 additions & 33 deletions src/osgEarth/ImGui/ViewpointsGUI
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef OSGEARTH_IMGUI_VIEWPOINTS_GUI
#define OSGEARTH_IMGUI_VIEWPOINTS_GUI

#include "ImGui"
#include <osgEarth/MapNode>
#include <osgEarth/Viewpoint>
#include <osgEarth/XmlUtils>
Expand Down Expand Up @@ -61,10 +62,11 @@ namespace osgEarth {
{
_duration = i.value("time", 1.0f);
auto key = 0;
for(auto& j : i.children("viewpoint"))
for (auto& j : i.children("viewpoint"))
{
_viewpoints.push_back(Viewpoint(j));

#if 0 // works, but conflicts with the ViewpointsExtension :)
if (key < 9)
{
EventRouter::get(view(ri)).onKeyPress(
Expand All @@ -76,59 +78,77 @@ namespace osgEarth {
});
key++;
}
#endif
}
}
}

_initialized = true;
}

ImGui::Begin(name(), visible());
if (ImGui::Begin(name(), visible()))
{
OE_SOFT_ASSERT_AND_RETURN(view(ri), void());
auto manip = dynamic_cast<EarthManipulator*>(view(ri)->getCameraManipulator());

if (!manip)
ImGui::TextColored(ImVec4(1,.9,.9,1), "(Viewpoints not active)");

if (_viewpoints.empty())
ImGui::TextColored(ImVec4(1, .9, .9, 1), "No viewpoints");

for (auto& vp : _viewpoints)
if (manip)
{
ImGui::PushID(&vp);
bool selected = false;
std::string name = vp.name().get();
if (name.empty())
{
name = "<no name>";
}
if (manip)
if (_viewpoints.empty())
ImGui::TextColored(ImVec4(1, .9, .9, 1), "No viewpoints");

for (auto& vp : _viewpoints)
{
ImGui::Selectable(name.c_str(), &selected);
if (selected)
manip->setViewpoint(vp, _duration);
ImGui::PushID(&vp);
bool selected = false;
std::string name = vp.name().get();
if (name.empty())
{
name = "<no name>";
}
if (manip)
{
ImGui::Selectable(name.c_str(), &selected);
if (selected)
manip->setViewpoint(vp, _duration);
}
else
{
ImGui::TextColored(ImVec4(.5, .5, .5, 1), name.c_str());
}
ImGui::PopID();
ImGui::Separator();
}
else

ImGui::Separator();

ImGuiLTable::Begin("viewpoints-settings");
{
ImGui::TextColored(ImVec4(.5,.5,.5,1), name.c_str());
double xmin, xmax;
manip->getSettings()->getAutoViewpointDurationLimits(xmin, xmax);
if (ImGuiLTable::SliderDouble("Min fly time (s)", &xmin, 0.0, 4.0, "%.1lf"))
manip->getSettings()->setAutoViewpointDurationLimits(xmin, xmax);
if (ImGuiLTable::SliderDouble("Max fly time (s)", &xmax, xmin, 10.0, "%.1lf"))
manip->getSettings()->setAutoViewpointDurationLimits(xmin, xmax);
}
ImGui::PopID();
ImGui::Separator();
}
ImGuiLTable::End();

if (ImGui::Button("Print current viewpoint as XML"))
{
auto manip = dynamic_cast<EarthManipulator*>(view(ri)->getCameraManipulator());
if (manip)
static bool show_xml = false;
ImGui::Checkbox("XML dump", &show_xml);
if (show_xml)
{
std::stringstream buf;
Util::XmlDocument xml(manip->getViewpoint().getConfig());
xml.store(std::cout);
std::cout << std::endl;
xml.store(buf);
std::string xml_str = buf.str();
xml_str = xml_str.substr(xml_str.find("<viewpoint>"));
ImGui::InputTextMultiline("##vp_xml", (char*)xml_str.c_str(), xml_str.size(), ImVec2(-1, -1), ImGuiInputTextFlags_ReadOnly);
}
}
else
{
ImGui::TextColored(ImVec4(1, .9, .9, 1), "(Viewpoints not active)");
}
ImGui::End();
}
ImGui::End();
}
};
}
Expand Down
Loading

0 comments on commit 6351eff

Please sign in to comment.