summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-04-30 13:47:03 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-04-30 13:47:03 +0200
commit9bbdba9eab28c2ba44002bada4b3bb1c0962bd90 (patch)
tree4a56a077b8597395dd0f38c5ab9cb0c87862afa1 /common
parente95ece83335b85ed063f8ea903acc71d5a2fb995 (diff)
downloadqtlocation-mapboxgl-9bbdba9eab28c2ba44002bada4b3bb1c0962bd90.tar.gz
remove concept of settings object from library
Diffstat (limited to 'common')
-rw-r--r--common/glfw_view.cpp97
-rw-r--r--common/glfw_view.hpp11
-rw-r--r--common/settings_json.cpp46
-rw-r--r--common/settings_json.hpp24
-rw-r--r--common/settings_nsuserdefaults.hpp24
-rw-r--r--common/settings_nsuserdefaults.mm41
6 files changed, 187 insertions, 56 deletions
diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp
index 321670b664..4bdac5f6b0 100644
--- a/common/glfw_view.cpp
+++ b/common/glfw_view.cpp
@@ -1,11 +1,12 @@
#include "glfw_view.hpp"
-MapView::MapView(llmr::Settings &settings, bool fullscreen)
- : fullscreen(fullscreen), settings(settings), map(*this) {}
+GLFWView::GLFWView(bool fullscreen) : fullscreen(fullscreen) {}
-MapView::~MapView() { glfwTerminate(); }
+GLFWView::~GLFWView() { glfwTerminate(); }
+
+void GLFWView::initialize(llmr::Map *map) {
+ View::initialize(map);
-void MapView::init() {
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize glfw\n");
exit(1);
@@ -44,8 +45,6 @@ void MapView::init() {
int fb_width, fb_height;
glfwGetFramebufferSize(window, &fb_width, &fb_height);
- settings.load();
-
resize(window, 0, 0);
glfwSetCursorPosCallback(window, mousemove);
@@ -58,8 +57,8 @@ void MapView::init() {
glfwMakeContextCurrent(nullptr);
}
-void MapView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) {
- MapView *mapView = (MapView *)glfwGetWindowUserPointer(window);
+void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) {
+ GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window);
if (action == GLFW_RELEASE) {
switch (key) {
@@ -67,26 +66,26 @@ void MapView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int
glfwSetWindowShouldClose(window, true);
break;
case GLFW_KEY_TAB:
- mapView->map.toggleDebug();
+ view->map->toggleDebug();
break;
case GLFW_KEY_X:
if (!mods)
- mapView->map.resetPosition();
+ view->map->resetPosition();
break;
case GLFW_KEY_R:
if (!mods)
- mapView->map.toggleRaster();
+ view->map->toggleRaster();
break;
case GLFW_KEY_N:
if (!mods)
- mapView->map.resetNorth();
+ view->map->resetNorth();
break;
}
}
}
-void MapView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) {
- MapView *mapView = (MapView *)glfwGetWindowUserPointer(window);
+void GLFWView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) {
+ GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window);
double delta = yoffset * 40;
bool is_wheel = delta != 0 && fmod(delta, 4.000244140625) == 0;
@@ -104,88 +103,88 @@ void MapView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) {
scale = 1.0 / scale;
}
- mapView->map.startScaling();
- mapView->map.scaleBy(scale, mapView->last_x, mapView->last_y);
+ view->map->startScaling();
+ view->map->scaleBy(scale, view->last_x, view->last_y);
}
-void MapView::resize(GLFWwindow *window, int, int) {
- MapView *mapView = (MapView *)glfwGetWindowUserPointer(window);
+void GLFWView::resize(GLFWwindow *window, int, int) {
+ GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window);
int width, height;
glfwGetWindowSize(window, &width, &height);
int fb_width, fb_height;
glfwGetFramebufferSize(window, &fb_width, &fb_height);
- mapView->map.resize(width, height, (float)fb_width / (float)width, fb_width, fb_height);
+ view->map->resize(width, height, (float)fb_width / (float)width, fb_width, fb_height);
}
-void MapView::mouseclick(GLFWwindow *window, int button, int action, int modifiers) {
- MapView *mapView = (MapView *)glfwGetWindowUserPointer(window);
+void GLFWView::mouseclick(GLFWwindow *window, int button, int action, int modifiers) {
+ GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window);
if (button == GLFW_MOUSE_BUTTON_RIGHT ||
(button == GLFW_MOUSE_BUTTON_LEFT && modifiers & GLFW_MOD_CONTROL)) {
- mapView->rotating = action == GLFW_PRESS;
- if (!mapView->rotating) {
- mapView->map.stopRotating();
+ view->rotating = action == GLFW_PRESS;
+ if (!view->rotating) {
+ view->map->stopRotating();
}
} else if (button == GLFW_MOUSE_BUTTON_LEFT) {
- mapView->tracking = action == GLFW_PRESS;
+ view->tracking = action == GLFW_PRESS;
if (action == GLFW_RELEASE) {
- mapView->map.stopPanning();
+ view->map->stopPanning();
double now = glfwGetTime();
- if (now - mapView->last_click < 0.4 /* ms */) {
- mapView->map.scaleBy(2.0, mapView->last_x, mapView->last_y, 0.5);
+ if (now - view->last_click < 0.4 /* ms */) {
+ view->map->scaleBy(2.0, view->last_x, view->last_y, 0.5);
}
- mapView->last_click = now;
+ view->last_click = now;
}
}
}
-void MapView::mousemove(GLFWwindow *window, double x, double y) {
- MapView *mapView = (MapView *)glfwGetWindowUserPointer(window);
- if (mapView->tracking) {
- double dx = x - mapView->last_x;
- double dy = y - mapView->last_y;
+void GLFWView::mousemove(GLFWwindow *window, double x, double y) {
+ GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window);
+ if (view->tracking) {
+ double dx = x - view->last_x;
+ double dy = y - view->last_y;
if (dx || dy) {
- mapView->map.startPanning();
- mapView->map.moveBy(dx, dy);
+ view->map->startPanning();
+ view->map->moveBy(dx, dy);
}
- } else if (mapView->rotating) {
- mapView->map.startRotating();
- mapView->map.rotateBy(mapView->last_x, mapView->last_y, x, y);
+ } else if (view->rotating) {
+ view->map->startRotating();
+ view->map->rotateBy(view->last_x, view->last_y, x, y);
}
- mapView->last_x = x;
- mapView->last_y = y;
+ view->last_x = x;
+ view->last_y = y;
}
-int MapView::run() {
- map.start();
+int GLFWView::run() {
+ map->start();
while (!glfwWindowShouldClose(window)) {
- if (map.needsSwap()) {
+ if (map->needsSwap()) {
glfwSwapBuffers(window);
- map.swapped();
+ map->swapped();
fps();
}
glfwWaitEvents();
}
- map.stop();
+ map->stop();
return 0;
}
-void MapView::make_active() {
+void GLFWView::make_active() {
glfwMakeContextCurrent(window);
}
-void MapView::swap() {
+void GLFWView::swap() {
glfwPostEmptyEvent();
}
-void MapView::fps() {
+void GLFWView::fps() {
static int frames = 0;
static double time_elapsed = 0;
diff --git a/common/glfw_view.hpp b/common/glfw_view.hpp
index 7c9f41811c..d8ea7a90ae 100644
--- a/common/glfw_view.hpp
+++ b/common/glfw_view.hpp
@@ -5,13 +5,12 @@
#include <GLFW/glfw3.h>
#include <uv.h>
-class MapView : public llmr::View {
+class GLFWView : public llmr::View {
public:
- MapView(llmr::Settings &settings, bool fullscreen = false);
- ~MapView();
-
- void init();
+ GLFWView(bool fullscreen = false);
+ ~GLFWView();
+ void initialize(llmr::Map *map);
void swap();
void make_active();
@@ -37,8 +36,6 @@ public:
double last_click = -1;
GLFWwindow *window = nullptr;
- llmr::Settings &settings;
- llmr::Map map;
uv_loop_t *loop = nullptr;
};
diff --git a/common/settings_json.cpp b/common/settings_json.cpp
new file mode 100644
index 0000000000..771d89e1a2
--- /dev/null
+++ b/common/settings_json.cpp
@@ -0,0 +1,46 @@
+#include "settings_json.hpp"
+#include <rapidjson/prettywriter.h>
+#include <rapidjson/filestream.h>
+#include <rapidjson/document.h>
+#include <stdio.h>
+
+using namespace llmr;
+
+Settings_JSON::Settings_JSON() { load(); }
+
+void Settings_JSON::load() {
+ FILE *settingsFile = fopen("/tmp/llmr-native.json", "r");
+ if (settingsFile != NULL) {
+ rapidjson::FileStream is(settingsFile);
+ rapidjson::Document document;
+ document.ParseStream<0>(is);
+ if (document.IsArray()) {
+ longitude = document[rapidjson::SizeType(0)].GetDouble();
+ latitude = document[1].GetDouble();
+ zoom = document[2].GetDouble();
+ angle = document[3].GetDouble();
+ debug = document[4].GetBool();
+ }
+ }
+}
+
+void Settings_JSON::save() {
+
+ rapidjson::FileStream s(fopen("/tmp/llmr-native.json", "w"));
+ rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
+ writer.StartArray();
+ writer.Double(longitude);
+ writer.Double(latitude);
+ writer.Double(zoom);
+ writer.Double(angle);
+ writer.Bool(debug);
+ writer.EndArray();
+}
+
+void Settings_JSON::clear() {
+ longitude = 0;
+ latitude = 0;
+ zoom = 0;
+ angle = 0;
+ debug = false;
+}
diff --git a/common/settings_json.hpp b/common/settings_json.hpp
new file mode 100644
index 0000000000..fa95254656
--- /dev/null
+++ b/common/settings_json.hpp
@@ -0,0 +1,24 @@
+#ifndef LLMR_JSON_SETTINGS
+#define LLMR_JSON_SETTINGS
+
+namespace llmr {
+
+class Settings_JSON {
+public:
+ Settings_JSON();
+ void load();
+ void save();
+ void clear();
+
+public:
+ double longitude = 0;
+ double latitude = 0;
+ double zoom = 0;
+ double angle = 0;
+
+ bool debug = false;
+};
+
+}
+
+#endif
diff --git a/common/settings_nsuserdefaults.hpp b/common/settings_nsuserdefaults.hpp
new file mode 100644
index 0000000000..575cc4a75f
--- /dev/null
+++ b/common/settings_nsuserdefaults.hpp
@@ -0,0 +1,24 @@
+#ifndef LLMR_COMMON_SETTINGS_NSUSERDEFAULTS
+#define LLMR_COMMON_SETTINGS_NSUSERDEFAULTS
+
+namespace llmr {
+
+class Settings_NSUserDefaults {
+public:
+ Settings_NSUserDefaults();
+ void load();
+ void save();
+ void clear();
+
+public:
+ double longitude = 0;
+ double latitude = 0;
+ double zoom = 0;
+ double angle = 0;
+
+ bool debug = false;
+};
+
+}
+
+#endif
diff --git a/common/settings_nsuserdefaults.mm b/common/settings_nsuserdefaults.mm
new file mode 100644
index 0000000000..783aa500d0
--- /dev/null
+++ b/common/settings_nsuserdefaults.mm
@@ -0,0 +1,41 @@
+#import <Foundation/Foundation.h>
+
+#include "settings_nsuserdefaults.hpp"
+
+using namespace llmr;
+
+Settings_NSUserDefaults::Settings_NSUserDefaults()
+{
+ [[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"longitude" : @(longitude),
+ @"latitude" : @(latitude),
+ @"zoom" : @(zoom),
+ @"angle" : @(angle),
+ @"debug" : @(debug) }];
+ load();
+}
+
+void Settings_NSUserDefaults::load()
+{
+ NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
+
+ longitude = [settings[@"longitude"] doubleValue];
+ latitude = [settings[@"latitude"] doubleValue];
+ zoom = [settings[@"zoom"] doubleValue];
+ angle = [settings[@"angle"] doubleValue];
+ debug = [settings[@"debug"] boolValue];
+}
+
+void Settings_NSUserDefaults::save()
+{
+ [[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{ @"longitude" : @(longitude),
+ @"latitude" : @(latitude),
+ @"zoom" : @(zoom),
+ @"angle" : @(angle),
+ @"debug" : @(debug) }];
+ [[NSUserDefaults standardUserDefaults] synchronize];
+}
+
+void Settings_NSUserDefaults::clear()
+{
+ [NSUserDefaults resetStandardUserDefaults];
+} \ No newline at end of file