remove concept of settings object from library

This commit is contained in:
Konstantin Käfer
2014-04-30 13:47:03 +02:00
parent e95ece8333
commit 9bbdba9eab
20 changed files with 198 additions and 265 deletions

View File

@@ -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;

View File

@@ -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;
};

View File

@@ -1,17 +1,14 @@
#include "settings.hpp"
#include "rapidjson/prettywriter.h"
#include "rapidjson/filestream.h"
#include "rapidjson/document.h"
#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()
{
}
Settings_JSON::Settings_JSON() { load(); }
void Settings_JSON::load()
{
void Settings_JSON::load() {
FILE *settingsFile = fopen("/tmp/llmr-native.json", "r");
if (settingsFile != NULL) {
rapidjson::FileStream is(settingsFile);
@@ -20,36 +17,30 @@ void Settings_JSON::load()
if (document.IsArray()) {
longitude = document[rapidjson::SizeType(0)].GetDouble();
latitude = document[1].GetDouble();
scale = document[2].GetDouble();
zoom = document[2].GetDouble();
angle = document[3].GetDouble();
debug = document[4].GetBool();
}
}
}
void Settings_JSON::persist()
{
}
void Settings_JSON::sync()
{
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(scale);
writer.Double(zoom);
writer.Double(angle);
writer.Bool(debug);
writer.EndArray();
}
void Settings_JSON::clear()
{
void Settings_JSON::clear() {
longitude = 0;
latitude = 0;
scale = 0;
zoom = 0;
angle = 0;
debug = false;
}

24
common/settings_json.hpp Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -1,44 +1,41 @@
#import <Foundation/Foundation.h>
#include "settings.hpp"
#include "settings_nsuserdefaults.hpp"
using namespace llmr;
Settings_MacOSX::Settings_MacOSX()
Settings_NSUserDefaults::Settings_NSUserDefaults()
{
[[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"longitude" : @(longitude),
@"latitude" : @(latitude),
@"scale" : @(scale),
@"zoom" : @(zoom),
@"angle" : @(angle),
@"debug" : @(debug) }];
load();
}
void Settings_MacOSX::load()
void Settings_NSUserDefaults::load()
{
NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
longitude = [settings[@"longitude"] doubleValue];
latitude = [settings[@"latitude"] doubleValue];
scale = [settings[@"scale"] doubleValue];
zoom = [settings[@"zoom"] doubleValue];
angle = [settings[@"angle"] doubleValue];
debug = [settings[@"debug"] boolValue];
}
void Settings_MacOSX::persist()
void Settings_NSUserDefaults::save()
{
[[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{ @"longitude" : @(longitude),
@"latitude" : @(latitude),
@"scale" : @(scale),
@"zoom" : @(zoom),
@"angle" : @(angle),
@"debug" : @(debug) }];
}
void Settings_MacOSX::sync()
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
void Settings_MacOSX::clear()
void Settings_NSUserDefaults::clear()
{
[NSUserDefaults resetStandardUserDefaults];
}

View File

@@ -3,6 +3,5 @@
#include "map/map.hpp"
#include "map/view.hpp"
#include "map/settings.hpp"
#endif

View File

@@ -1,27 +0,0 @@
#ifndef LLMR_MAP_SETTINGS
#define LLMR_MAP_SETTINGS
#include <llmr/util/noncopyable.hpp>
namespace llmr {
class Settings : private util::noncopyable {
public:
virtual void load() {}; // load into memory
virtual void persist() {}; // persist in memory (then call sync() if platform-appropriate)
virtual void sync() {}; // save to disk
virtual void clear() {}; // clear from memory and disk
virtual ~Settings() {}
public:
double longitude = 0;
double latitude = 0;
double scale = 0;
double angle = 0;
bool debug = false;
};
}
#endif

View File

@@ -3,8 +3,14 @@
namespace llmr {
class Map;
class View {
public:
virtual void initialize(Map *map) {
this->map = map;
}
// Called from the render (=GL) thread. Signals that the context should
// swap the front and the back buffer.
virtual void swap() = 0;
@@ -13,6 +19,9 @@ public:
// thread. This is typically just called once at the beginning of the
// renderer setup since the render thread doesn't switch the contexts.
virtual void make_active() = 0;
protected:
llmr::Map *map = nullptr;
};
}

View File

@@ -1,22 +0,0 @@
//
// MBXSettings.hpp
// llmr
//
// Created by Justin R. Miller on 1/27/14.
//
//
#import <llmr/map/settings.hpp>
namespace llmr
{
class Settings_iOS : public Settings
{
public:
Settings_iOS();
virtual void load();
virtual void persist();
virtual void sync();
virtual void clear();
};
}

View File

@@ -1,52 +0,0 @@
//
// MBXSettings.cpp
// llmr
//
// Created by Justin R. Miller on 1/27/14.
//
//
#import "MBXSettings.h"
#import <Foundation/Foundation.h>
using namespace llmr;
Settings_iOS::Settings_iOS()
{
[[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"longitude" : @(longitude),
@"latitude" : @(latitude),
@"scale" : @(scale),
@"angle" : @(angle),
@"debug" : @(debug) }];
}
void Settings_iOS::load()
{
NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
longitude = [settings[@"longitude"] doubleValue];
latitude = [settings[@"latitude"] doubleValue];
scale = [settings[@"scale"] doubleValue];
angle = [settings[@"angle"] doubleValue];
debug = [settings[@"debug"] boolValue];
}
void Settings_iOS::persist()
{
[[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{ @"longitude" : @(longitude),
@"latitude" : @(latitude),
@"scale" : @(scale),
@"angle" : @(angle),
@"debug" : @(debug) }];
}
void Settings_iOS::sync()
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
void Settings_iOS::clear()
{
[NSUserDefaults resetStandardUserDefaults];
}

View File

@@ -1,7 +1,6 @@
#import "MBXViewController.h"
#import "MBXSettings.h"
#import "../common/settings_nsuserdefaults.hpp"
#import "../common/foundation_request.h"
#import <OpenGLES/EAGL.h>
@@ -30,7 +29,7 @@ class View;
llmr::Map *map = nullptr;
View *viewObj = nullptr;
llmr::Settings_iOS *settings = nullptr;
llmr::Settings_NSUserDefaults *settings = nullptr;
MBXViewController *viewController = nullptr;
#pragma mark - Setup
@@ -68,12 +67,14 @@ MBXViewController *viewController = nullptr;
- (void)setupMap
{
settings = new llmr::Settings_iOS();
settings->load();
viewObj = new View(self);
map = new llmr::Map(*viewObj);
// Load settings
settings = new llmr::Settings_NSUserDefaults();
map->setLonLatZoom(settings->longitude, settings->latitude, settings->zoom);
map->setAngle(settings->angle);
map->setDebug(settings->debug);
}
@@ -219,6 +220,20 @@ MBXViewController *viewController = nullptr;
- (void)dealloc
{
if (settings)
{
// Save settings
if (map)
{
map->getLonLatZoom(settings->longitude, settings->latitude, settings->zoom);
settings->angle = map->getAngle();
settings->debug = map->getDebug();
settings->save();
}
delete settings;
settings = nullptr;
}
if (map)
{
delete map;
@@ -231,13 +246,6 @@ MBXViewController *viewController = nullptr;
viewObj = nullptr;
}
if (settings)
{
settings->sync();
delete settings;
settings = nullptr;
}
if ([[EAGLContext currentContext] isEqual:self.context])
{
[EAGLContext setCurrentContext:nil];

View File

@@ -11,8 +11,10 @@
"sources": [
"./main.m",
"./MBXAppDelegate.m",
"./MBXSettings.mm",
"./MBXViewController.mm",
"../common/settings_nsuserdefaults.hpp",
"../common/settings_nsuserdefaults.mm",
"../common/foundation_request.h",
"../common/foundation_request.mm",
],
'product_extension': 'app',

View File

@@ -10,8 +10,8 @@
'type': 'executable',
'sources': [
'./main.cpp',
'./settings.cpp',
'./settings.hpp',
'../common/settings_json.cpp',
'../common/settings_json.hpp',
'../common/glfw_view.hpp',
'../common/glfw_view.cpp',
'../common/curl_request.cpp',

View File

@@ -4,15 +4,15 @@
#include <signal.h>
#include <getopt.h>
#include "settings.hpp"
#include "../common/settings_json.hpp"
#include "../common/glfw_view.hpp"
MapView *mapView = nullptr;
GLFWView *view = nullptr;
void quit_handler(int) {
if (mapView) {
if (view) {
fprintf(stderr, "waiting for quit...\n");
glfwSetWindowShouldClose(mapView->window, true);
glfwSetWindowShouldClose(view->window, true);
glfwPostEmptyEvent();
} else {
exit(0);
@@ -40,13 +40,22 @@ int main(int argc, char *argv[]) {
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
// main loop
view = new GLFWView();
llmr::Map map(*view);
// Load settings
llmr::Settings_JSON settings;
mapView = new MapView(settings, fullscreen_flag);
mapView->init();
int ret = mapView->run();
mapView->settings.sync();
delete mapView;
map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
map.setAngle(settings.angle);
map.setDebug(settings.debug);
int ret = view->run();
// Save settings
map.getLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
settings.angle = map.getAngle();
settings.debug = map.getDebug();
settings.save();
return ret;
}

View File

@@ -1,19 +0,0 @@
#ifndef LLMR_JSON_SETTINGS
#define LLMR_JSON_SETTINGS
#include <llmr/map/settings.hpp>
namespace llmr {
class Settings_JSON : public Settings {
public:
Settings_JSON();
virtual void load();
virtual void persist();
virtual void sync();
virtual void clear();
};
}
#endif

View File

@@ -10,8 +10,8 @@
'type': 'executable',
'sources': [
'./main.mm',
'./settings.mm',
'./settings.hpp',
'../common/settings_nsuserdefaults.hpp',
'../common/settings_nsuserdefaults.mm',
'../common/glfw_view.hpp',
'../common/glfw_view.cpp',
'../common/foundation_request.h',

View File

@@ -1,14 +1,25 @@
// #import <Foundation/Foundation.h>
// #import <AppKit/AppKit.h>
#include "settings.hpp"
#include "../common/settings_nsuserdefaults.hpp"
#include "../common/glfw_view.hpp"
int main() {
llmr::Settings_MacOSX settings;
MapView view(settings);
view.init();
GLFWView view;
llmr::Map map(view);
// Load settings
llmr::Settings_NSUserDefaults settings;
map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
map.setAngle(settings.angle);
map.setDebug(settings.debug);
// fprintf(stderr, "lon: %f, lat: %f, zoom: %f, angle: %f, debug: %d\n", settings.l)
int ret = view.run();
settings.sync();
// Save settings
map.getLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
settings.angle = map.getAngle();
settings.debug = map.getDebug();
settings.save();
return ret;
}

View File

@@ -1,19 +0,0 @@
#ifndef LLMR_MACOSX_SETTINGS
#define LLMR_MACOSX_SETTINGS
#include <llmr/map/settings.hpp>
namespace llmr {
class Settings_MacOSX : public Settings {
public:
Settings_MacOSX();
virtual void load();
virtual void persist();
virtual void sync();
virtual void clear();
};
}
#endif

View File

@@ -20,6 +20,8 @@ Map::Map(View& view)
painter(*this),
loop(uv_loop_new()) {
view.initialize(this);
// Make sure that we're doing an initial drawing in all cases.
is_clean.clear();
is_rendered.clear();