refs #342 & #346: per-map change callbacks

This commit is contained in:
Justin R. Miller
2014-07-16 18:18:04 -07:00
parent 3adad61b82
commit a902446e7f
10 changed files with 64 additions and 63 deletions

View File

@@ -1,7 +1,5 @@
#include "glfw_view.hpp"
#include <llmr/util/time.hpp>
GLFWView::GLFWView(bool fullscreen) : fullscreen(fullscreen) {
#ifdef NVIDIA
glDiscardFramebufferEXT = (PFNGLDISCARDFRAMEBUFFEREXTPROC)glfwGetProcAddress("glDiscardFramebufferEXT");
@@ -196,6 +194,10 @@ void GLFWView::swap() {
glfwPostEmptyEvent();
}
void GLFWView::notify_map_change(llmr::MapChange change, llmr::timestamp delay) {
// no-op
}
void GLFWView::fps() {
static int frames = 0;
static double time_elapsed = 0;
@@ -283,9 +285,5 @@ void show_color_debug_image(std::string name, const char *data, size_t logical_w
}
#endif
void notify_map_change(MapChange change, timestamp delay) {
// no-op
}
}
}

View File

@@ -2,6 +2,7 @@
#define LLMR_COMMON_GLFW_VIEW
#include <llmr/llmr.hpp>
#include <llmr/util/time.hpp>
#ifdef NVIDIA
#define GLFW_INCLUDE_ES2
@@ -18,6 +19,7 @@ public:
void initialize(llmr::Map *map);
void swap();
void make_active();
void notify_map_change(llmr::MapChange change, llmr::timestamp delay = 0);
static void key(GLFWwindow *window, int key, int scancode, int action, int mods);
static void scroll(GLFWwindow *window, double xoffset, double yoffset);

View File

@@ -1,17 +1,5 @@
#include "headless_view.hpp"
#include <llmr/util/time.hpp>
#include <llmr/util/timer.hpp>
#include <llmr/platform/platform.hpp>
namespace llmr {
namespace platform {
void notify_map_change(MapChange change, timestamp delay) {
// no-op
}
}
HeadlessView::HeadlessView() {
#if LLMR_USE_CGL
@@ -161,6 +149,10 @@ HeadlessView::~HeadlessView() {
#endif
}
void HeadlessView::notify_map_change(llmr::MapChange change, llmr::timestamp delay) {
// no-op
}
void HeadlessView::make_active() {
#if LLMR_USE_CGL
CGLError error = CGLSetCurrentContext(gl_context);

View File

@@ -10,6 +10,7 @@
#include <llmr/map/view.hpp>
#include <llmr/platform/gl.hpp>
#include <llmr/util/time.hpp>
namespace llmr {
@@ -20,6 +21,7 @@ public:
void resize(int width, int height);
void notify_map_change(MapChange change, timestamp delay = 0);
void make_active();
void swap();
unsigned int root_fbo();

View File

@@ -5,6 +5,7 @@
#include <llmr/util/noncopyable.hpp>
#include <llmr/util/uv.hpp>
#include "view.hpp"
#include "transform_state.hpp"
#include <forward_list>
@@ -15,7 +16,7 @@ struct box;
class Transform : private util::noncopyable {
public:
Transform();
Transform(View &view);
// Map view
// Note: width * ratio does not necessarily equal fb_width
@@ -74,6 +75,8 @@ private:
void constrain(double& scale, double& y) const;
private:
View &view;
mutable uv::rwlock mtx;
// This reflects the current state of the transform, representing the actual position of the

View File

@@ -1,10 +1,25 @@
#ifndef LLMR_MAP_VIEW
#define LLMR_MAP_VIEW
#include <llmr/util/time.hpp>
namespace llmr {
class Map;
enum MapChange : uint8_t {
MapChangeRegionWillChange = 0,
MapChangeRegionWillChangeAnimated = 1,
MapChangeRegionDidChange = 2,
MapChangeRegionDidChangeAnimated = 3,
MapChangeWillStartLoadingMap = 4,
MapChangeDidFinishLoadingMap = 5,
MapChangeDidFailLoadingMap = 6,
MapChangeWillStartRenderingMap = 7,
MapChangeDidFinishRenderingMap = 8,
MapChangeDidFinishRenderingMapFullyRendered = 9
};
class View {
public:
virtual void initialize(Map *map) {
@@ -26,6 +41,11 @@ public:
return 0;
}
// Notifies a watcher of map x/y/scale/rotation changes.
// Must only be called from the same thread that caused the change.
// Must not be called from the render thread.
virtual void notify_map_change(MapChange change, timestamp delay = 0) = 0;
protected:
llmr::Map *map = nullptr;
};

View File

@@ -1,8 +1,6 @@
#ifndef LLMR_PLATFORM_PLATFORM
#define LLMR_PLATFORM_PLATFORM
#include <llmr/util/time.hpp>
#include <memory>
#include <functional>
#include <string>
@@ -26,19 +24,6 @@ struct Response {
std::function<void(Response *)> callback;
};
enum MapChange : uint8_t {
MapChangeRegionWillChange = 0,
MapChangeRegionWillChangeAnimated = 1,
MapChangeRegionDidChange = 2,
MapChangeRegionDidChangeAnimated = 3,
MapChangeWillStartLoadingMap = 4,
MapChangeDidFinishLoadingMap = 5,
MapChangeDidFailLoadingMap = 6,
MapChangeWillStartRenderingMap = 7,
MapChangeDidFinishRenderingMap = 8,
MapChangeDidFinishRenderingMapFullyRendered = 9
};
// Makes an HTTP request of a URL, preferrably on a background thread, and calls a function with the
// results in the original thread (which runs the libuv loop).
// If the loop pointer is NULL, the callback function will be called on an arbitrary thread.
@@ -50,9 +35,6 @@ std::shared_ptr<Request> request_http(const std::string &url,
// Cancels an HTTP request.
void cancel_request_http(const std::shared_ptr<Request> &req);
// Notifies a watcher of map changes.
void notify_map_change(MapChange key, timestamp delay = 0);
// Shows an alpha image with the specified dimensions in a named window.
void show_debug_image(std::string name, const char *data, size_t width, size_t height);

View File

@@ -22,7 +22,7 @@ using namespace llmr;
Map::Map(View& view)
: view(view),
transform(),
transform(view),
style(std::make_shared<Style>()),
glyphAtlas(std::make_shared<GlyphAtlas>(1024, 1024)),
spriteAtlas(std::make_shared<SpriteAtlas>(512, 512)),

View File

@@ -14,7 +14,9 @@ const double R2D = 180.0 / M_PI;
const double M2PI = 2 * M_PI;
const double MIN_ROTATE_SCALE = 8;
Transform::Transform() {
Transform::Transform(View &view)
: view(view) {
setScale(current.scale);
setAngle(current.angle);
}
@@ -28,7 +30,7 @@ bool Transform::resize(const uint16_t w, const uint16_t h, const float ratio,
if (final.width != w || final.height != h || final.pixelRatio != ratio ||
final.framebuffer[0] != fb_w || final.framebuffer[1] != fb_h) {
platform::notify_map_change(platform::MapChangeRegionWillChange);
view.notify_map_change(MapChangeRegionWillChange);
current.width = final.width = w;
current.height = final.height = h;
@@ -38,7 +40,7 @@ bool Transform::resize(const uint16_t w, const uint16_t h, const float ratio,
if (!canRotate() && current.angle) _setAngle(0);
constrain(current.scale, current.y);
platform::notify_map_change(platform::MapChangeRegionDidChange);
view.notify_map_change(MapChangeRegionDidChange);
return true;
} else {
@@ -57,9 +59,9 @@ void Transform::moveBy(const double dx, const double dy, const timestamp duratio
void Transform::_moveBy(const double dx, const double dy, const timestamp duration) {
// This is only called internally, so we don't need a lock here.
platform::notify_map_change(duration ?
platform::MapChangeRegionWillChangeAnimated :
platform::MapChangeRegionWillChange);
view.notify_map_change(duration ?
MapChangeRegionWillChangeAnimated :
MapChangeRegionWillChange);
final.x = current.x + std::cos(current.angle) * dx + std::sin(current.angle) * dy;
final.y = current.y + std::cos(current.angle) * dy + std::sin(-current.angle) * dx;
@@ -84,10 +86,10 @@ void Transform::_moveBy(const double dx, const double dy, const timestamp durati
std::make_shared<util::ease_transition<double>>(current.y, final.y, current.y, start, duration));
}
platform::notify_map_change(duration ?
platform::MapChangeRegionDidChangeAnimated :
platform::MapChangeRegionDidChange,
duration);
view.notify_map_change(duration ?
MapChangeRegionDidChangeAnimated :
MapChangeRegionDidChange,
duration);
}
void Transform::setLonLat(const double lon, const double lat, const timestamp duration) {
@@ -276,9 +278,9 @@ void Transform::_setScaleXY(const double new_scale, const double xn, const doubl
const timestamp duration) {
// This is only called internally, so we don't need a lock here.
platform::notify_map_change(duration ?
platform::MapChangeRegionWillChangeAnimated :
platform::MapChangeRegionWillChange);
view.notify_map_change(duration ?
MapChangeRegionWillChangeAnimated :
MapChangeRegionWillChange);
final.scale = new_scale;
final.x = xn;
@@ -309,10 +311,10 @@ void Transform::_setScaleXY(const double new_scale, const double xn, const doubl
Bc = s / 360;
Cc = s / (2 * M_PI);
platform::notify_map_change(duration ?
platform::MapChangeRegionDidChangeAnimated :
platform::MapChangeRegionDidChange,
duration);
view.notify_map_change(duration ?
MapChangeRegionDidChangeAnimated :
MapChangeRegionDidChange,
duration);
}
#pragma mark - Constraints
@@ -388,9 +390,9 @@ void Transform::setAngle(const double new_angle, const double cx, const double c
void Transform::_setAngle(double new_angle, const timestamp duration) {
// This is only called internally, so we don't need a lock here.
platform::notify_map_change(duration ?
platform::MapChangeRegionWillChangeAnimated :
platform::MapChangeRegionWillChange);
view.notify_map_change(duration ?
MapChangeRegionWillChangeAnimated :
MapChangeRegionWillChange);
while (new_angle > M_PI)
new_angle -= M2PI;
@@ -410,10 +412,10 @@ void Transform::_setAngle(double new_angle, const timestamp duration) {
current.angle, final.angle, current.angle, start, duration));
}
platform::notify_map_change(duration ?
platform::MapChangeRegionDidChangeAnimated :
platform::MapChangeRegionDidChange,
duration);
view.notify_map_change(duration ?
MapChangeRegionDidChangeAnimated :
MapChangeRegionDidChange,
duration);
}
double Transform::getAngle() const {