Skip to content

Commit

Permalink
Merge pull request #1173 from tangrams/android-sceneLoadUpdates
Browse files Browse the repository at this point in the history
Add Android API for scene loading with scene updates
  • Loading branch information
tallytalwar authored Dec 22, 2016
2 parents c9749ea + 67eb847 commit 75f31f2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 10 deletions.
34 changes: 32 additions & 2 deletions android/tangram/jni/jniExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,22 @@ extern "C" {
delete map;
}

JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeLoadScene(JNIEnv* jniEnv, jobject obj, jlong mapPtr, jstring path) {
JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeLoadScene(JNIEnv* jniEnv, jobject obj, jlong mapPtr, jstring path, jobjectArray updateStrings) {
assert(mapPtr > 0);
auto map = reinterpret_cast<Tangram::Map*>(mapPtr);
const char* cPath = jniEnv->GetStringUTFChars(path, NULL);
map->loadScene(resolveScenePath(cPath).c_str());
size_t nUpdateStrings = (updateStrings == NULL) ? 0 : jniEnv->GetArrayLength(updateStrings);

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

map->loadScene(resolveScenePath(cPath).c_str(), false, sceneUpdates);
jniEnv->ReleaseStringUTFChars(path, cPath);
}

Expand Down Expand Up @@ -456,6 +467,25 @@ extern "C" {
jnienv->ReleaseStringUTFChars(value, cValue);
}

JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeQueueSceneUpdates(JNIEnv* jniEnv, jobject obj, jlong mapPtr, jobjectArray updateStrings) {
assert(mapPtr > 0);
size_t nUpdateStrings = (updateStrings == NULL)? 0 : jniEnv->GetArrayLength(updateStrings);

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

if (sceneUpdates.empty()) { return; }

auto map = reinterpret_cast<Tangram::Map*>(mapPtr);
map->queueSceneUpdate(sceneUpdates);
}

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.microedition.khronos.egl.EGLConfig;
Expand Down Expand Up @@ -222,9 +224,20 @@ static MapController getInstance(GLSurfaceView view) {
* @param path Location of the YAML scene file within the application assets
*/
public void loadSceneFile(String path) {
loadSceneFile(path, null);
}

/**
* Load a new scene file
* @param path Location of the YAML scene file within the application assets
* @param sceneUpdates List of {@code SceneUpdate}
*/
public void loadSceneFile(String path, List<SceneUpdate> sceneUpdates) {

String[] updateStrings = bundleSceneUpdates(sceneUpdates);
scenePath = path;
checkPointer(mapPointer);
nativeLoadScene(mapPointer, path);
nativeLoadScene(mapPointer, path, updateStrings);
requestRender();
}

Expand Down Expand Up @@ -738,12 +751,27 @@ public void setDebugFlag(DebugFlag flag, boolean on) {

/**
* 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")
* @param sceneUpdate A {@code SceneUpdate}
*/
public void queueSceneUpdate(SceneUpdate sceneUpdate) {
checkPointer(mapPointer);
if (sceneUpdate == null) {
throw new IllegalArgumentException("sceneUpdate can not be null in queueSceneUpdates");
}
nativeQueueSceneUpdate(mapPointer, sceneUpdate.getPath(), sceneUpdate.getValue());
}

/**
* Enqueue a scene component update with its corresponding YAML node value
* @param sceneUpdates List of {@code SceneUpdate}
*/
public void queueSceneUpdate(String componentPath, String value) {
public void queueSceneUpdate(List<SceneUpdate> sceneUpdates) {
checkPointer(mapPointer);
nativeQueueSceneUpdate(mapPointer, componentPath, value);
if (sceneUpdates == null || sceneUpdates.size() == 0) {
throw new IllegalArgumentException("sceneUpdates can not be null or empty in queueSceneUpdates");
}
String[] updateStrings = bundleSceneUpdates(sceneUpdates);
nativeQueueSceneUpdates(mapPointer, updateStrings);
}

/**
Expand Down Expand Up @@ -804,6 +832,22 @@ void checkId(long id) {
}
}

private String[] bundleSceneUpdates(List<SceneUpdate> sceneUpdates) {

String[] updateStrings = null;

if (sceneUpdates != null) {
updateStrings = new String[sceneUpdates.size() * 2];
int index = 0;
for (SceneUpdate sceneUpdate : sceneUpdates) {
updateStrings[index++] = sceneUpdate.getPath();
updateStrings[index++] = sceneUpdate.getValue();
}
}

return updateStrings;
}

boolean setMarkerStyling(long markerId, String styleStr) {
checkPointer(mapPointer);
checkId(markerId);
Expand Down Expand Up @@ -861,7 +905,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 nativeLoadScene(long mapPtr, String path, String[] updateStrings);
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 Expand Up @@ -890,7 +934,8 @@ boolean setMarkerDrawOrder(long markerId, int drawOrder) {
private synchronized native void nativeHandlePinchGesture(long mapPtr, float posX, float posY, float scale, float velocity);
private synchronized native void nativeHandleRotateGesture(long mapPtr, float posX, float posY, float rotation);
private synchronized native void nativeHandleShoveGesture(long mapPtr, float distance);
private synchronized native void nativeQueueSceneUpdate(long mapPtr, String componentPath, String value);
private synchronized native void nativeQueueSceneUpdate(long mapPtr, String componentPath, String componentValue);
private synchronized native void nativeQueueSceneUpdates(long mapPtr, String[] updateStrings);
private synchronized native void nativeApplySceneUpdates(long mapPtr);
private synchronized native void nativePickFeature(long mapPtr, float posX, float posY, FeaturePickListener listener);
private synchronized native void nativePickLabel(long mapPtr, float posX, float posY, LabelPickListener listener);
Expand Down
19 changes: 18 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,9 @@
import android.util.AttributeSet;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

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

getMapAsync(callback, sceneFilePath, null);
}

/**
* 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 List<SceneUpdate> sceneUpdates) {

disposeTask();

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

Expand Down
25 changes: 25 additions & 0 deletions android/tangram/src/com/mapzen/tangram/SceneUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 {

private String componentPath;
private String componentValue;

/**
* Add a point feature to this collection.
* @param path Series of yaml keys separated by a ".". Represents the scene path to be updated
* @param value A yaml string which will update the value at the specified path
*/
public SceneUpdate(String path, String value) {
this.componentPath = path;
this.componentValue = value;
}

public String getPath() { return componentPath; }
public String getValue() { return componentValue; }

}
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 75f31f2

Please sign in to comment.