Skip to content

Commit

Permalink
For now, don't store FeatureView as a component.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Oct 31, 2024
1 parent ca715bd commit 1b5984e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
16 changes: 9 additions & 7 deletions src/apps/rocky_demo/Demo_Geocoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ auto Demo_Geocoder = [](Application& app)
static char input_buf[256];
static LabelStyle label_style_point;
static LabelStyle label_style_area;
static FeatureView feature_view;

if (status.failed())
{
Expand Down Expand Up @@ -65,11 +66,12 @@ auto Demo_Geocoder = [](Application& app)
label.visible = false;

// Outline for location boundary:
auto& feature_view = app.entities.emplace<FeatureView>(entity);
//feature_view = {};
//auto& feature_view = app.entities.emplace<FeatureView>(entity);
feature_view.styles.line = LineStyle();
feature_view.styles.line->color = vsg::vec4{ 1, 1, 0, 1 };
feature_view.styles.line->depth_offset = 9000.0f; //meters
feature_view.active = false;
feature_view.setVisible(false, app.entities);

// Transform to place the entity:
auto& xform = app.entities.emplace<Transform>(entity);
Expand Down Expand Up @@ -132,13 +134,13 @@ auto Demo_Geocoder = [](Application& app)
manip->setViewpoint(vp, std::chrono::seconds(2));
}

auto&& [icon, label, feature_view] = app.entities.get<Icon, Label, FeatureView>(entity);
auto&& [icon, label] = app.entities.get<Icon, Label>(entity);

// show the placemark:
if (feature.geometry.type == Geometry::Type::Points)
{
icon.visible = true;
feature_view.visible = false;
feature_view.setVisible(false, app.entities);
label.style = label_style_point;
}
else
Expand All @@ -151,7 +153,7 @@ auto Demo_Geocoder = [](Application& app)
feature_view.clear(app.entities);
feature_view.features = { copy_of_feature };
feature_view.generate(app.entities, app.mapNode->worldSRS(), app.runtime());
feature_view.active = true;
feature_view.setVisible(true, app.entities);
icon.visible = false;
label.style = label_style_area;
}
Expand All @@ -177,10 +179,10 @@ auto Demo_Geocoder = [](Application& app)

app.onNextUpdate([&]()
{
auto&& [icon, label, feature_view] = app.entities.get<Icon, Label, FeatureView>(entity);
auto&& [icon, label] = app.entities.get<Icon, Label>(entity);
icon.visible = false;
label.visible = false;
feature_view.visible = false;
feature_view.setVisible(false, app.entities);
});
}
}
Expand Down
27 changes: 13 additions & 14 deletions src/apps/rocky_demo/Demo_LineFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ using namespace ROCKY_NAMESPACE;

auto Demo_LineFeatures = [](Application& app)
{

#ifdef ROCKY_HAS_GDAL
static entt::entity entity = entt::null;
static bool initialized = false;

struct LoadedFeatures {
Status status;
std::shared_ptr<rocky::OGRFeatureSource> fs;
};
static jobs::future<LoadedFeatures> data;
static FeatureView feature_view;

if (entity == entt::null)
if (!initialized)
{
initialized = true;

if (data.empty())
{
data = jobs::dispatch([](auto& cancelable)
Expand All @@ -41,9 +43,6 @@ auto Demo_LineFeatures = [](Application& app)
else if (data.available() && data->status.ok())
{
// create a feature view and add features to it:
entity = app.entities.create();
FeatureView& feature_view = app.entities.emplace<FeatureView>(entity);

auto iter = data->fs->iterate(app.instance.io());
while (iter.hasMore())
{
Expand Down Expand Up @@ -82,18 +81,18 @@ auto Demo_LineFeatures = [](Application& app)

else if (ImGuiLTable::Begin("Line features"))
{
auto& component = app.entities.get<FeatureView>(entity);

ImGuiLTable::Checkbox("Visible", &component.active);
bool visible = feature_view.visible();
if (ImGuiLTable::Checkbox("Visible", &visible))
feature_view.setVisible(visible, app.entities);

if (component.styles.line.has_value())
if (feature_view.styles.line.has_value())
{
float* col = (float*)&component.styles.line->color;
float* col = (float*)&feature_view.styles.line->color;
if (ImGuiLTable::ColorEdit3("Color", col))
component.dirtyStyles(app.entities);
feature_view.dirtyStyles(app.entities);

if (ImGuiLTable::SliderFloat("Depth offset", &component.styles.line->depth_offset, 0.0f, 20000.0f, "%.0f"))
component.dirtyStyles(app.entities);
if (ImGuiLTable::SliderFloat("Depth offset", &feature_view.styles.line->depth_offset, 0.0f, 20000.0f, "%.0f"))
feature_view.dirtyStyles(app.entities);
}

ImGuiLTable::End();
Expand Down
17 changes: 9 additions & 8 deletions src/apps/rocky_demo/Demo_PolygonFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ using namespace ROCKY_NAMESPACE;
auto Demo_PolygonFeatures = [](Application& app)
{
#ifdef ROCKY_HAS_GDAL
static entt::entity entity = entt::null;
static bool initialized = false;

struct LoadedFeatures {
Status status;
std::shared_ptr<rocky::OGRFeatureSource> fs;
};
static jobs::future<LoadedFeatures> data;
static FeatureView feature_view;

if (entity == entt::null)
if (!initialized)
{
initialized = true;

if (data.empty())
{
data = jobs::dispatch([](auto& cancelable)
Expand All @@ -40,10 +43,7 @@ auto Demo_PolygonFeatures = [](Application& app)
}
else if (data.available() && data->status.ok())
{
// create a feature view and add features to it:
entity = app.entities.create();
FeatureView& feature_view = app.entities.emplace<FeatureView>(entity);

// create a feature view and add features to it
if (data->fs->featureCount() > 0)
feature_view.features.reserve(data->fs->featureCount());

Expand Down Expand Up @@ -81,8 +81,9 @@ auto Demo_PolygonFeatures = [](Application& app)

else if (ImGuiLTable::Begin("Polygon features"))
{
auto& component = app.entities.get<FeatureView>(entity);
ImGuiLTable::Checkbox("Visible", &component.active);
bool visible = feature_view.visible();
if (ImGuiLTable::Checkbox("Visible", &visible))
feature_view.setVisible(visible, app.entities);

ImGuiLTable::End();
}
Expand Down
17 changes: 14 additions & 3 deletions src/rocky/vsg/FeatureView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ FeatureView::clear(entt::registry& registry)
}
}

void
FeatureView::setVisible(bool value, entt::registry& registry)
{
if (_entity != entt::null)
{
auto [line, mesh] = registry.try_get<Line, Mesh>(_entity);
if (line)
line->visible = value;
if (mesh)
mesh->visible = value;
}
_visible = value;
}

void
FeatureView::generate(entt::registry& registry, const SRS& geom_srs, Runtime& runtime, bool keep_features)
{
Expand All @@ -340,21 +354,18 @@ FeatureView::generate(entt::registry& registry, const SRS& geom_srs, Runtime& ru
{
auto& geom = registry.get_or_emplace<Line>(_entity);
compile_feature_to_lines(feature, styles, geom_srs, geom);
geom.visible_ptr = &visible;
}
else if (feature.geometry.type == Geometry::Type::Polygon)
{
auto& geom = registry.get_or_emplace<Mesh>(_entity);
compile_polygon_feature_with_weemesh(feature, feature.geometry, styles, geom_srs, geom);
geom.visible_ptr = &visible;
}
else if (feature.geometry.type == Geometry::Type::MultiPolygon)
{
auto& geom = registry.get_or_emplace<Mesh>(_entity);
for (auto& part : feature.geometry.parts)
{
compile_polygon_feature_with_weemesh(feature, part, styles, geom_srs, geom);
geom.visible_ptr = &visible;
}
}
else
Expand Down
8 changes: 5 additions & 3 deletions src/rocky/vsg/FeatureView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ROCKY_NAMESPACE
* FeatureView is an attachment that compiles a collection of Feature objects
* for visualization.
*/
class ROCKY_EXPORT FeatureView : public ECS::VisibleComponent
class ROCKY_EXPORT FeatureView
{
public:
//! Collection of features to view
Expand All @@ -53,8 +53,9 @@ namespace ROCKY_NAMESPACE
//! Deletes any geometries previously created by generate()
void clear(entt::registry& registry);

//! Whether to render this component
bool active = true;
//! Visibility toggle
void setVisible(bool value, entt::registry& registry);
bool visible() const { return _visible; }

//! Call if you change the stylesheet after generating.
void dirtyStyles(entt::registry& entities);
Expand All @@ -71,5 +72,6 @@ namespace ROCKY_NAMESPACE

private:
entt::entity _entity = entt::null;
bool _visible = true;
};
}

0 comments on commit 1b5984e

Please sign in to comment.