Skip to content

Commit

Permalink
Add search form
Browse files Browse the repository at this point in the history
  • Loading branch information
raefaldhia authored and Raefaldhi Amartya Junior committed Dec 30, 2016
1 parent 98f30e5 commit 5b74f58
Show file tree
Hide file tree
Showing 116 changed files with 20,001 additions and 88 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ SRCS = \
src/MapsData.cpp \
src/MapsView.cpp \
src/MapsWindow.cpp \
src/SearchForm.cpp \
src/VirtualScroller.cpp \
vendor/tinyxml/tinystr.cpp \
vendor/tinyxml/tinyxml.cpp \
vendor/tinyxml/tinyxmlerror.cpp \
vendor/tinyxml/tinyxmlparser.cpp \


# Specify the resource definition files to use. Full or relative paths can be
Expand Down Expand Up @@ -81,6 +86,10 @@ RSRCS = \

#%}

#%}

#%}

# Specify libraries to link against.
# There are two acceptable forms of library specifications:
# - if your library follows the naming pattern of libXXX.so or libXXX.a,
Expand Down Expand Up @@ -110,7 +119,7 @@ SYSTEM_INCLUDE_PATHS =
# Additional paths paths to look for local headers. These use the form
# #include "header". Directories that contain the files in SRCS are
# automatically included.
LOCAL_INCLUDE_PATHS = src/
LOCAL_INCLUDE_PATHS = src/ vendor/

# Specify the level of optimization that you want. Specify either NONE (O0),
# SOME (O1), FULL (O2), or leave blank (for the default optimization level).
Expand Down
2 changes: 1 addition & 1 deletion src/Maps.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Maps.h"

Maps::Maps() : BApplication("application/x-vnd.Haiku-Maps") {
Maps::Maps() : BApplication("application/x-vnd.Haiku-Maps") {
mapsWindow = new MapsWindow();
mapsWindow->Show();
}
Expand Down
111 changes: 56 additions & 55 deletions src/MapsData.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,94 @@
#include "MapsData.h"

#include <vector>

#include <Messenger.h>
#include <UrlRequest.h>
#include <UrlProtocolRoster.h>
#include <UrlProtocolListener.h>

static BString baseUrl("https://api.mapbox.com/styles/v1/mapbox/streets-v8/static/%f,%f,%f,%f,%f/%dx%d?access_token=pk.eyJ1IjoicmFlZmFsZGhpYSIsImEiOiJjaXdnN3J0YTkwMTV1MnVraXgzNGowbTBuIn0.9RYCJF1sfuUD86QRuBItYw&attribution=false&logo=false");

static float longitude = 0.0f;
static float latitude = 0.0f;
static float zoom = 0.0f;
static float bearing = 0.0f;
static float pitch = 0.0f;
static int width = 400;
static int height = 400;

static float scale = 1.5f;

static std::vector<BHandler*> handler;
static BMallocIO* data = NULL;

class MapsData_Listener : public BUrlProtocolListener {
class MapsData_Listener : public BUrlProtocolListener, protected MapsData {
public:
MapsData_Listener() {
if (data != NULL) {
delete data;
data = NULL;
if (MapsData::data != NULL) {
delete MapsData::data;
MapsData::data = NULL;
}
data = new BMallocIO();
MapsData::data = new BMallocIO();
}
virtual void DataReceived(BUrlRequest* caller, const char* _data, off_t position, ssize_t size) {
data->WriteAt(position, _data, size);
MapsData::data->WriteAt(position, _data, size);
}
virtual void RequestCompleted(BUrlRequest* caller, bool success) {
if (success) {
for (std::vector<BHandler*>::iterator it = handler.begin(); it != handler.end(); it++) {
for (std::vector<BHandler*>::iterator it = MapsData::handler.begin(); it != MapsData::handler.end(); it++) {
BMessenger messenger(*it);
messenger.SendMessage(new BMessage(MAPDATA_UPDATE));
}
}
}
};

static BUrlRequest* request = NULL;
static MapsData_Listener* listener = NULL;
static thread_id thread;
// Default scale between BPoint and the maps tile
// Change this value if you have more accurate value
#define DEFAULT_BPOINT_SCALE 1.5f

BMallocIO* MapsData::data = NULL;
std::vector<BHandler*> MapsData::handler;

MapsVector MapsData::mapsVector;

thread_id MapsData::thread;
BUrlRequest* MapsData::request = NULL;
MapsData_Listener* MapsData::listener = NULL;

BString MapsData::baseUrl("https://api.mapbox.com/styles/v1/mapbox/streets-v8/static/%f,%f,%f,%f,%f/%dx%d?access_token=pk.eyJ1IjoicmFlZmFsZGhpYSIsImEiOiJjaXdnN3J0YTkwMTV1MnVraXgzNGowbTBuIn0.9RYCJF1sfuUD86QRuBItYw&attribution=false&logo=false");

MapsData::MapsData() {

}

void MapsData::SetLongitude(float lon) {
if (lon < -180.0) {
longitude = 360.0 + lon;
MapsData::~MapsData() {

}

void MapsData::Initialize(int width, int height) {
mapsVector.width = width;
mapsVector.height = height;

mapsVector.scale = DEFAULT_BPOINT_SCALE;

mapsVector.longitude = 0.0f;
mapsVector.latitude = 0.0f;
mapsVector.zoom = 0.0f;
mapsVector.bearing = 0.0f;
mapsVector.pitch = 0.0f;
}

void MapsData::SetLongitude(float longitude) {
if (longitude < -180.0) {
mapsVector.longitude = 360.0 + longitude;
}
else if (lon > 180.0) {
longitude = -360.0 + lon;
else if (longitude > 180.0) {
mapsVector.longitude = -360.0 + longitude;
}
else {
longitude = lon;
mapsVector.longitude = longitude;
}
}

void MapsData::SetLatitude(float lat) {
if (lat < -90.0) {
latitude = -90.0;
void MapsData::SetLatitude(float latitude) {
if (latitude < -90.0) {
mapsVector.latitude = -90.0;
}
else if (lat > 90.0) {
latitude = 90.0;
else if (latitude > 90.0) {
mapsVector.latitude = 90.0;
}
else {
latitude = lat;
mapsVector.latitude = latitude;
}
}

void MapsData::SetZoom(float _zoom) {
zoom = _zoom;
scale = 1.5 * pow(2.0, zoom);
void MapsData::SetZoom(float zoom) {
mapsVector.zoom = zoom;
mapsVector.scale = DEFAULT_BPOINT_SCALE * pow(2.0, mapsVector.zoom);
}

void MapsData::AddHandler(BHandler* handle) {
Expand All @@ -98,7 +108,7 @@ void MapsData::Retrieve() {
listener = NULL;
}
BString dataUrl;
dataUrl.SetToFormat(baseUrl.String(), longitude, latitude, zoom, bearing, pitch, width, height);
dataUrl.SetToFormat(baseUrl.String(), mapsVector.longitude, mapsVector.latitude, mapsVector.zoom, mapsVector.bearing, mapsVector.pitch, mapsVector.width, mapsVector.height);

listener = new MapsData_Listener();
request = BUrlProtocolRoster::MakeRequest(BUrl(dataUrl.String()), listener);
Expand All @@ -110,15 +120,6 @@ BMallocIO* MapsData::Get() {
return data;
}

Vector2 MapsData::GetCoords() {
Vector2 coords;

coords.lon = longitude;
coords.lat = latitude;

return coords;
MapsVector MapsData::GetVector() {
return mapsVector;
}

float MapsData::GetScale() {
return scale;
}
42 changes: 35 additions & 7 deletions src/MapsData.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
#ifndef MAPSDATA_H
#define MAPSDATA_H

#include <vector>

#include <Handler.h>
#include <UrlRequest.h>

class MapsData_Listener;

enum {
MAPDATA_UPDATE = 'mdud'
};

struct Vector2 {
float lon;
float lat;
struct MapsVector {
int width;
int height;

float scale;

float longitude;
float latitude;
float zoom;
float bearing;
float pitch;
};

class MapsData {
private:
protected:
MapsData();
~MapsData();

static BMallocIO* data;
static std::vector<BHandler*> handler;
public:
static void Initialize(int, int);
static void Retrieve();

static void SetLongitude(float);
static void SetLatitude(float);
static void SetZoom(float);
static void Retrieve();

static BMallocIO* Get();
static void AddHandler(BHandler*);

static float GetScale();
static Vector2 GetCoords();
static MapsVector GetVector();
private:
static MapsVector mapsVector;

static thread_id thread;
static BUrlRequest* request;
static MapsData_Listener* listener;

static BString baseUrl;
};

#endif // MAPSDATA_H
41 changes: 22 additions & 19 deletions src/MapsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
#include <Messenger.h>
#include <TranslationUtils.h>

#include <ScrollView.h>
#include <ListView.h>
#include <LayoutBuilder.h>
#include "MapsData.h"

MapsView::MapsView(float _longitude, float _latitude, float _zoom,
float _bearing, float _pitch, int _width, int _height)
MapsView::MapsView()
: BView("mapsView", B_WILL_DRAW) {
bitmap = NULL;

virtualScroller = new VirtualScroller(this);

MapsData::SetLongitude(_longitude);
MapsData::SetLatitude(_latitude);

MapsData::AddHandler(this);
MapsData::Retrieve();
}

MapsView::~MapsView() {

}

void MapsView::AddHandler(BHandler* handle) {
handler.push_back(handle);
}

void MapsView::Draw(BRect updateRect){
if (bitmap != NULL) {
SetViewBitmap(bitmap);
Expand All @@ -36,26 +40,30 @@ void MapsView::MouseDown(BPoint where) {
if (mouseClicked == B_PRIMARY_MOUSE_BUTTON) {
IsMouseDown = true;
pastPoint = where;

for (std::vector<BHandler*>::iterator it = handler.begin(); it != handler.end(); it++) {
BMessenger messenger(*it);
messenger.SendMessage(new BMessage(M_MAPSVIEW_ON_FOCUS));
}
}
}

void MapsView::MouseUp(BPoint where) {
if (IsMouseDown) {
IsMouseDown = false;

Vector2 coords = MapsData::GetCoords();

MapsVector mapsVector = MapsData::GetVector();
if (pastPoint.x < where.x) {
MapsData::SetLongitude(coords.lon - ((where.x - pastPoint.x) / MapsData::GetScale()));
MapsData::SetLongitude(mapsVector.longitude - ((where.x - pastPoint.x) / mapsVector.scale));
}
else if (pastPoint.x > where.x) {
MapsData::SetLongitude(coords.lon + ((pastPoint.x - where.x) / MapsData::GetScale()));
MapsData::SetLongitude(mapsVector.longitude + ((pastPoint.x - where.x) / mapsVector.scale));
}
if (pastPoint.y < where.y) {
MapsData::SetLatitude(coords.lat + ((where.y - pastPoint.y) / MapsData::GetScale()));
MapsData::SetLatitude(mapsVector.latitude + ((where.y - pastPoint.y) / mapsVector.scale));
}
else if (pastPoint.y > where.y) {
MapsData::SetLatitude(coords.lat - ((pastPoint.y - where.y) / MapsData::GetScale()));
MapsData::SetLatitude(mapsVector.latitude - ((pastPoint.y - where.y) / mapsVector.scale));
}
MapsData::Retrieve();
}
Expand All @@ -66,18 +74,13 @@ void MapsView::MessageReceived(BMessage* message) {
case MAPDATA_UPDATE: {
bitmap = BTranslationUtils::GetBitmap(MapsData::Get());
Invalidate();

break;
}
}break;
case VIRTUAL_SCROLLER: {
MapsData::SetZoom(20 - (message->GetFloat("value", 0.0) / 3));
MapsData::Retrieve();

break;
}
}break;
default: {
BView::MessageReceived(message);
break;
}
}break;
}
}
Loading

0 comments on commit 5b74f58

Please sign in to comment.