Files
MapLibreParticleLab/platform/glfw/glfw_view.hpp
Bart Louwers 2a8ebc4906 core: rename mbgl directories to mln (#4511)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-22 20:50:14 +02:00

176 lines
5.1 KiB
C++

#pragma once
#include <mln/map/map.hpp>
#include <mln/map/map_snapshotter.hpp>
#include <mln/util/geometry.hpp>
#include <mln/util/run_loop.hpp>
#include <mln/util/timer.hpp>
#include <utility>
#include <optional>
#if (defined(MLN_RENDER_BACKEND_OPENGL) || defined(MLN_RENDER_BACKEND_VULKAN) || \
defined(MLN_RENDER_BACKEND_WEBGPU)) && \
!defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
#define ENABLE_LOCATION_INDICATOR
#endif
#ifdef ENABLE_LOCATION_INDICATOR
#include <mln/style/layers/location_indicator_layer.hpp>
#endif
struct GLFWwindow;
class GLFWBackend;
class GLFWRendererFrontend;
class SnapshotObserver;
namespace mln {
namespace gfx {
class RendererBackend;
} // namespace gfx
} // namespace mln
class GLFWView : public mln::MapObserver {
public:
GLFWView(bool fullscreen,
bool benchmark,
const mln::ResourceOptions &resourceOptions,
const mln::ClientOptions &clientOptions);
~GLFWView() override;
float getPixelRatio() const;
void setMap(mln::Map *);
void setRenderFrontend(GLFWRendererFrontend *);
mln::gfx::RendererBackend &getRendererBackend();
void setTestDirectory(std::string dir) { testDirectory = std::move(dir); };
// Callback called when the user presses the key mapped to style change.
// The expected action is to set a new style, different to the current one.
void setChangeStyleCallback(std::function<void()> callback);
void setPauseResumeCallback(std::function<void()> callback) { pauseResumeCallback = std::move(callback); };
void setOnlineStatusCallback(std::function<void()> callback) { onlineStatusCallback = std::move(callback); }
void setResetCacheCallback(std::function<void()> callback) { resetDatabaseCallback = std::move(callback); };
void setShouldClose();
void setWindowTitle(const std::string &);
void run();
void invalidate();
mln::Size getSize() const;
// mln::MapObserver implementation
void onDidFinishLoadingStyle() override;
void onWillStartRenderingFrame() override;
protected:
// mln::Backend implementation
private:
// Window callbacks
static void onKey(GLFWwindow *window, int key, int scancode, int action, int mods);
static void onScroll(GLFWwindow *window, double xoffset, double yoffset);
static void onWindowResize(GLFWwindow *window, int width, int height);
static void onFramebufferResize(GLFWwindow *window, int width, int height);
static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers);
static void onMouseMove(GLFWwindow *window, double x, double y);
static void onWindowFocus(GLFWwindow *window, int focused);
static void onWindowRefresh(GLFWwindow *window);
// Internal
void report(float duration);
void render();
mln::Color makeRandomColor() const;
mln::Point<double> makeRandomPoint() const;
static std::unique_ptr<mln::style::Image> makeImage(const std::string &id, int width, int height, float pixelRatio);
void nextOrientation();
void addRandomPointAnnotations(int count);
void addRandomLineAnnotations(int count);
void addRandomShapeAnnotations(int count);
void addRandomCustomPointAnnotations(int count);
void addAnimatedAnnotation();
void updateFreeCameraDemo();
void updateAnimatedAnnotations();
void toggleCustomSource();
void toggleLocationIndicatorLayer();
void cycleDebugOptions();
void clearAnnotations();
void popAnnotation();
void toggleCustomDrawableStyle();
void makeSnapshot(bool withOverlay = false);
mln::AnnotationIDs annotationIDs;
std::vector<std::string> spriteIDs;
mln::AnnotationIDs animatedAnnotationIDs;
std::vector<double> animatedAnnotationAddedTimes;
private:
void toggle3DExtrusions(bool visible);
mln::Map *map = nullptr;
GLFWRendererFrontend *rendererFrontend = nullptr;
std::unique_ptr<GLFWBackend> backend;
std::string testDirectory = ".";
double freeCameraDemoPhase = -1;
mln::TimePoint freeCameraDemoStartTime;
bool fullscreen = false;
const bool benchmark = false;
bool tracking = false;
bool rotating = false;
bool pitching = false;
bool show3DExtrusions = false;
// Frame timer
int frames = 0;
float frameTime = 0;
double lastReported = 0;
int width = 1024;
int height = 768;
float pixelRatio;
double lastX = 0, lastY = 0;
double lastClick = -1;
std::function<void()> changeStyleCallback;
std::function<void()> pauseResumeCallback;
std::function<void()> onlineStatusCallback;
std::function<void()> resetDatabaseCallback;
std::function<void(mln::Map *)> animateRouteCallback;
mln::util::RunLoop runLoop;
mln::util::Timer frameTick;
GLFWwindow *window = nullptr;
bool dirty = false;
std::optional<std::string> featureID;
std::unique_ptr<mln::MapSnapshotter> snapshotter;
std::unique_ptr<SnapshotObserver> snapshotterObserver;
mln::ResourceOptions mapResourceOptions;
mln::ClientOptions mapClientOptions;
#ifdef ENABLE_LOCATION_INDICATOR
bool puckFollowsCameraCenter = false;
mln::style::LocationIndicatorLayer *puck = nullptr;
#endif
};