Skip to content

Commit

Permalink
Add Android API for scene loading with scene updates
Browse files Browse the repository at this point in the history
- MapView provides an overloaded `getMapAsync` which accepts an ArrayList of SceneUpdates (new
Class).
- MapController::loadSceneFile provides an overloaded method to accept an ArrayList of SceneUpdates
- MapController::queueSceneUpdates provides an overloaded method to accept an ArrayList of
SceneUpdates
  • Loading branch information
tallytalwar committed Dec 18, 2016
1 parent de8d433 commit 89cc64f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
19 changes: 19 additions & 0 deletions android/tangram/jni/jniExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ extern "C" {
jniEnv->ReleaseStringUTFChars(path, cPath);
}

JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeLoadSceneWithUpdates(JNIEnv* jniEnv, jobject obj, jlong mapPtr, jstring path, jobjectArray jsceneUpdatePaths, jobjectArray jsceneUpdateValues) {
assert(mapPtr > 0);
size_t n_sceneUpdates = (jsceneUpdatePaths == NULL)? 0 : jniEnv->GetArrayLength(jsceneUpdatePaths);

std::vector<Tangram::SceneUpdate> sceneUpdates;
for (size_t i = 0; i < n_sceneUpdates; i++) {
jstring path = (jstring) (jniEnv->GetObjectArrayElement(jsceneUpdatePaths, i));
jstring value = (jstring) (jniEnv->GetObjectArrayElement(jsceneUpdateValues, i));
sceneUpdates.emplace_back(stringFromJString(jniEnv, path), stringFromJString(jniEnv, value));
jniEnv->DeleteLocalRef(path);
jniEnv->DeleteLocalRef(value);
}

auto map = reinterpret_cast<Tangram::Map*>(mapPtr);
const char* cPath = jniEnv->GetStringUTFChars(path, NULL);
map->loadScene(resolveScenePath(cPath).c_str(), false, sceneUpdates);
jniEnv->ReleaseStringUTFChars(path, cPath);
}

JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeResize(JNIEnv* jniEnv, jobject obj, jlong mapPtr, jint width, jint height) {
assert(mapPtr > 0);
auto map = reinterpret_cast<Tangram::Map*>(mapPtr);
Expand Down
34 changes: 34 additions & 0 deletions android/tangram/src/com/mapzen/tangram/MapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -228,6 +229,26 @@ public void loadSceneFile(String path) {
requestRender();
}

/**
* Load a new scene file
* @param path Location of the YAML scene file within the application assets
* @param sceneUpdates List of path:value pairs to be applied when loading this scene
*/
public void loadSceneFile(String path, ArrayList<SceneUpdate> sceneUpdates) {
String[] paths = new String[sceneUpdates.size()];
String[] values = new String[sceneUpdates.size()];
int index = 0;
for (SceneUpdate sceneUpdate : sceneUpdates) {
paths[index] = sceneUpdate.path;
values[index] = sceneUpdate.value;
index++;
}
scenePath = path;
checkPointer(mapPointer);
nativeLoadSceneWithUpdates(mapPointer, path, paths, values);
requestRender();
}

/**
* Set the {@link HttpHandler} for retrieving remote map resources; a default-constructed
* HttpHandler is suitable for most cases, but methods can be extended to modify resource URLs
Expand Down Expand Up @@ -746,6 +767,18 @@ public void queueSceneUpdate(String componentPath, String value) {
nativeQueueSceneUpdate(mapPointer, componentPath, value);
}

/**
* Enqueue a scene component update with its corresponding YAML node value
* @param componentPath The YAML component path delimited by a '.' (example "scene.animated")
* @param value A YAML valid string (example "{ property: true }" or "true")
*/
public void queueSceneUpdate(ArrayList<SceneUpdate> sceneUpdates) {
checkPointer(mapPointer);
for(SceneUpdate sceneUpdate : sceneUpdates) {
nativeQueueSceneUpdate(mapPointer, sceneUpdate.path, sceneUpdate.value);
}
}

/**
* Apply updates queued by queueSceneUpdate; this empties the current queue of updates
*/
Expand Down Expand Up @@ -862,6 +895,7 @@ boolean setMarkerDrawOrder(long markerId, int drawOrder) {
private synchronized native long nativeInit(MapController instance, AssetManager assetManager);
private synchronized native void nativeDispose(long mapPtr);
private synchronized native void nativeLoadScene(long mapPtr, String path);
private synchronized native void nativeLoadSceneWithUpdates(long mapPtr, String path, String[] sceneUpdatePaths, String[] sceneUpdateValues);
private synchronized native void nativeSetupGL(long mapPtr);
private synchronized native void nativeResize(long mapPtr, int width, int height);
private synchronized native boolean nativeUpdate(long mapPtr, float dt);
Expand Down
22 changes: 21 additions & 1 deletion android/tangram/src/com/mapzen/tangram/MapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.util.AttributeSet;
import android.widget.FrameLayout;

import java.util.ArrayList;

/**
* {@code MapView} is a View for displaying a Tangram map.
*/
Expand Down Expand Up @@ -55,6 +57,20 @@ public interface OnMapReadyCallback {
public void getMapAsync(@NonNull final OnMapReadyCallback callback,
@NonNull final String sceneFilePath) {

getMapAsync(callback, sceneFilePath, new ArrayList<SceneUpdate>());
}

/**
* Construct a {@code MapController} asynchronously; may only be called from the UI thread
* @param callback The object to receive the resulting MapController in a callback;
* the callback will be made on the UI thread
* @param sceneFilePath Location of the YAML scene file within the asset bundle
* @param sceneUpdates List of SceneUpdate to be applied when loading this scene
*/
public void getMapAsync(@NonNull final OnMapReadyCallback callback,
@NonNull final String sceneFilePath,
final ArrayList<SceneUpdate> sceneUpdates) {

disposeTask();

final MapController mapInstance = getMapInstance();
Expand All @@ -65,7 +81,11 @@ public void getMapAsync(@NonNull final OnMapReadyCallback callback,
@SuppressWarnings("WrongThread")
protected Boolean doInBackground(Void... params) {
mapInstance.init();
mapInstance.loadSceneFile(sceneFilePath);
if (sceneUpdates.size() > 0) {
mapInstance.loadSceneFile(sceneFilePath, sceneUpdates);
} else {
mapInstance.loadSceneFile(sceneFilePath);
}
return true;
}

Expand Down
24 changes: 24 additions & 0 deletions android/tangram/src/com/mapzen/tangram/SceneUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mapzen.tangram;

/**
* {@code SceneUpdate} Represents a DataStructure to specify a yaml path and the corresponding value for a Scene Update.
*/

public class SceneUpdate {

public String path;
public String value;

public SceneUpdate() { this("", ""); }

public SceneUpdate(String path, String value) {
set(path, value);
}

public SceneUpdate set(String p, String v) {
path = p;
value = v;
return this;
}

}
1 change: 1 addition & 0 deletions core/src/tangram.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ using LabelPickCallback = std::function<void(const LabelPickResult*)>;
struct SceneUpdate {
std::string path;
std::string value;
SceneUpdate(std::string p, std::string v) : path(p), value(v) {}
};

enum class EaseType : char {
Expand Down

0 comments on commit 89cc64f

Please sign in to comment.