Skip to content

Commit

Permalink
Add click invocation for vector layers, add coord to screen position …
Browse files Browse the repository at this point in the history
…conversion
  • Loading branch information
maurhofer-ubique authored and zimmermannubique committed Feb 1, 2024
1 parent 0725022 commit 52b50c3
Show file tree
Hide file tree
Showing 33 changed files with 224 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ class GLThread constructor(
width = w
height = h
sizeChanged = true
requestRender()
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions bridging/android/jni/map/NativeMapCamera2dInterface.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions bridging/ios/MCMapCamera2dInterface+Private.mm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bridging/ios/MCMapCamera2dInterface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions bridging/ios/MCTiled2dMapVectorLayerInterface+Private.mm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bridging/ios/MCTiled2dMapVectorLayerInterface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions djinni/map/core.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ map_camera_2d_interface = interface +c {
remove_listener(listener: map_camera_2d_listener_interface);

coord_from_screen_position(pos_screen: vec_2_f): coord;
screen_pos_from_coord(coord: coord): vec_2_f;
map_units_from_pixels(distancePx: f64): f64;

set_rotation_enabled(enabled: bool);
Expand Down
2 changes: 2 additions & 0 deletions djinni/map/layers/tiled/vector/tiled_vector_layer.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ sourceUrlParams: optional<map<string, string>>

reload_data_source(source_name: string);
reload_local_data_source(source_name: string, geo_json: string);

perform_click(coord: coord);
}

vector_layer_feature_info_value = record {
Expand Down
2 changes: 2 additions & 0 deletions shared/public/MapCamera2dInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class MapCamera2dInterface {

virtual ::Coord coordFromScreenPosition(const ::Vec2F & posScreen) = 0;

virtual ::Vec2F screenPosFromCoord(const ::Coord & coord) = 0;

virtual double mapUnitsFromPixels(double distancePx) = 0;

virtual void setRotationEnabled(bool enabled) = 0;
Expand Down
2 changes: 2 additions & 0 deletions shared/public/Tiled2dMapVectorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class Tiled2dMapVectorLayer

std::optional<int32_t> getMaxZoomLevelIdentifier() override;

void performClick(const Coord &coord) override;

// Touch Interface
bool onTouchDown(const Vec2F &posScreen) override;

Expand Down
3 changes: 3 additions & 0 deletions shared/public/Tiled2dMapVectorLayerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "Coord.h"
#include "FontLoaderInterface.h"
#include "LayerInterface.h"
#include "LoaderInterface.h"
Expand Down Expand Up @@ -65,4 +66,6 @@ class Tiled2dMapVectorLayerInterface {
virtual void reloadDataSource(const std::string & sourceName) = 0;

virtual void reloadLocalDataSource(const std::string & sourceName, const std::string & geoJson) = 0;

virtual void performClick(const ::Coord & coord) = 0;
};
2 changes: 2 additions & 0 deletions shared/public/Tiled2dMapVectorTile.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Tiled2dMapVectorTile : public ActorObject,

void setSelectionDelegate(const std::weak_ptr<Tiled2dMapVectorLayerSelectionCallbackInterface> &selectionDelegate);

virtual bool performClick(const Coord &coord) = 0;

protected:
const std::weak_ptr<MapInterface> mapInterface;
const Tiled2dMapVersionedTileInfo tileInfo;
Expand Down
30 changes: 30 additions & 0 deletions shared/src/map/camera/MapCamera2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Vec2D.h"
#include "Vec2DHelper.h"
#include "Vec2FHelper.h"
#include "Logger.h"

#define DEFAULT_ANIM_LENGTH 300
#define ROTATION_THRESHOLD 20
Expand Down Expand Up @@ -733,6 +734,35 @@ Coord MapCamera2d::coordFromScreenPosition(const ::Vec2F &posScreen) {
return Coord(centerPosition.systemIdentifier, centerPosition.x + adjXDiff, centerPosition.y - adjYDiff, centerPosition.z);
}

::Vec2F MapCamera2d::screenPosFromCoord(const Coord &coord) {
const auto mapInterface = this->mapInterface;
const auto conversionHelper = mapInterface ? mapInterface->getCoordinateConverterHelper() : nullptr;
const auto renderingContext = mapInterface ? mapInterface->getRenderingContext() : nullptr;
if (!conversionHelper || !renderingContext) {
return Vec2F(0.0, 0.0);
}

const auto mapCoord = conversionHelper->convert(mapCoordinateSystem.identifier, coord);

double angRad = -angle * M_PI / 180.0;
double sinAng = std::sin(angRad);
double cosAng = std::cos(angRad);

double coordXDiffToCenter = mapCoord.x - centerPosition.x;
double coordYDiffToCenter = mapCoord.y - centerPosition.y;

double screenXDiffToCenter = coordXDiffToCenter * cosAng - coordYDiffToCenter * sinAng;
double screenYDiffToCenter = coordXDiffToCenter * sinAng + coordYDiffToCenter * cosAng;

const Vec2I sizeViewport = renderingContext->getViewportSize();

double zoomFactor = screenPixelAsRealMeterFactor * zoom;
double posScreenX = (screenXDiffToCenter / zoomFactor) + ((double)sizeViewport.x / 2.0);
double posScreenY = ((double)sizeViewport.y / 2.0) - (screenYDiffToCenter / zoomFactor);

return Vec2F(posScreenX, posScreenY);
}

double MapCamera2d::mapUnitsFromPixels(double distancePx) { return distancePx * screenPixelAsRealMeterFactor * zoom; }

double MapCamera2d::getScalingFactor() { return mapUnitsFromPixels(1.0); }
Expand Down
3 changes: 3 additions & 0 deletions shared/src/map/camera/MapCamera2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "MapCoordinateSystem.h"
#include "SimpleTouchInterface.h"
#include "Vec2I.h"
#include "Vec2F.h"
#include <mutex>
#include <optional>
#include <set>
Expand Down Expand Up @@ -120,6 +121,8 @@ class MapCamera2d : public MapCamera2dInterface,

virtual ::Coord coordFromScreenPosition(const ::Vec2F &posScreen) override;

Vec2F screenPosFromCoord(const Coord &coord) override;

virtual double mapUnitsFromPixels(double distancePx) override;

virtual double getScalingFactor() override;
Expand Down
12 changes: 12 additions & 0 deletions shared/src/map/layers/tiled/vector/Tiled2dMapVectorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,18 @@ bool Tiled2dMapVectorLayer::onClickConfirmed(const Vec2F &posScreen) {
return false;
}

void Tiled2dMapVectorLayer::performClick(const Coord &coord) {
const auto mapInterface = this->mapInterface;
const auto camera = mapInterface ? mapInterface->getCamera() : nullptr;

if (!camera) {
return;
}

const auto screenPos = camera->screenPosFromCoord(coord);
onClickConfirmed(screenPos);
}

bool Tiled2dMapVectorLayer::onDoubleClick(const Vec2F &posScreen) {
return interactionManager->onDoubleClick(posScreen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Tiled2dMapVectorSourceDataManager : public ActorObject {

virtual void clearTouch() = 0;

virtual bool performClick(const std::unordered_set<std::string> &layers, const Coord &coord) = 0;

virtual void setSprites(std::shared_ptr<SpriteData> spriteData, std::shared_ptr<TextureHolderInterface> spriteTexture) {}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@ Tiled2dMapVectorSourceTileDataManager::onClickConfirmed(const std::unordered_set
return false;
}

bool Tiled2dMapVectorSourceTileDataManager::performClick(const std::unordered_set<std::string> &layers, const Coord &coord) {
if (interactableLayers.empty()) {
return false;
}
for (const auto &[tileInfo, subTiles]: tiles) {
const auto tileState = tileStateMap.find(tileInfo);
if (tileState == tileStateMap.end() || tileState->second != TileState::VISIBLE) {
continue;
}
for (auto rIter = subTiles.rbegin(); rIter != subTiles.rend(); rIter++) {
if (interactableLayers.count(std::get<1>(*rIter)) == 0 || layers.count(std::get<1>(*rIter)) == 0) {
continue;
}
bool hit = std::get<2>(*rIter).syncAccess([coord](auto tile) {
return tile->performClick(coord);
});
if (hit) {
return true;
}
}
}
return false;
}


bool Tiled2dMapVectorSourceTileDataManager::onDoubleClick(const std::unordered_set<std::string> &layers, const Vec2F &posScreen) {
if (interactableLayers.empty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
#include "SpriteData.h"

class Tiled2dMapVectorLayer;

class Tiled2dMapVectorTile;

class Tiled2dMapVectorSourceTileDataManager : public Tiled2dMapVectorLayerTileCallbackInterface,
public Tiled2dMapVectorSourceDataManager,
public Tiled2dMapVectorSourceDataManager,
public std::enable_shared_from_this<Tiled2dMapVectorSourceTileDataManager> {
public:
using Tiled2dMapVectorSourceDataManager::Tiled2dMapVectorSourceDataManager;
Expand Down Expand Up @@ -52,6 +53,8 @@ class Tiled2dMapVectorSourceTileDataManager : public Tiled2dMapVectorLayerTileCa

void clearTouch() override;

bool performClick(const std::unordered_set<std::string> &layers, const Coord &coord) override;

void setSprites(std::shared_ptr<SpriteData> spriteData, std::shared_ptr<TextureHolderInterface> spriteTexture) override;

virtual void reloadLayerContent(const std::vector<std::tuple<std::shared_ptr<VectorLayerDescription>, int32_t>> &descriptionLayerIndexPairs) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ void Tiled2dMapVectorInteractionManager::clearTouch() {
}
}

void Tiled2dMapVectorInteractionManager::performClick(const Coord &coord) {
const auto lambda = [&coord](std::unordered_set<std::string> &layers, auto &manager) -> bool {
if (auto strongManager = manager.lock()) {
return strongManager->performClick(layers, coord);
}
return false;
};
callInReverseOrder(lambda);
}

template<typename F>
bool Tiled2dMapVectorInteractionManager::callInReverseOrder(F&& managerLambda) {
if (!mapDescription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Tiled2dMapVectorInteractionManager : public TouchInterface {

void clearTouch() override;

void performClick(const Coord &coord);

private:
template<typename F>
inline bool callInReverseOrder(F&& managerLambda);
Expand Down
Loading

0 comments on commit 52b50c3

Please sign in to comment.