diff --git a/android/tangram/jni/jniExports.cpp b/android/tangram/jni/jniExports.cpp index 1bbc732ef8..916a799aa4 100644 --- a/android/tangram/jni/jniExports.cpp +++ b/android/tangram/jni/jniExports.cpp @@ -110,29 +110,21 @@ 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 jcomponentPaths, jobjectArray jcomponentValues) { assert(mapPtr > 0); auto map = reinterpret_cast(mapPtr); const char* cPath = jniEnv->GetStringUTFChars(path, NULL); - map->loadScene(resolveScenePath(cPath).c_str()); - 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); + size_t n_sceneUpdates = (jcomponentPaths == NULL)? 0 : jniEnv->GetArrayLength(jcomponentPaths); std::vector sceneUpdates; for (size_t i = 0; i < n_sceneUpdates; i++) { - jstring path = (jstring) (jniEnv->GetObjectArrayElement(jsceneUpdatePaths, i)); - jstring value = (jstring) (jniEnv->GetObjectArrayElement(jsceneUpdateValues, i)); + jstring path = (jstring) (jniEnv->GetObjectArrayElement(jcomponentPaths, i)); + jstring value = (jstring) (jniEnv->GetObjectArrayElement(jcomponentValues, i)); sceneUpdates.emplace_back(stringFromJString(jniEnv, path), stringFromJString(jniEnv, value)); jniEnv->DeleteLocalRef(path); jniEnv->DeleteLocalRef(value); } - auto map = reinterpret_cast(mapPtr); - const char* cPath = jniEnv->GetStringUTFChars(path, NULL); map->loadScene(resolveScenePath(cPath).c_str(), false, sceneUpdates); jniEnv->ReleaseStringUTFChars(path, cPath); } @@ -475,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 jcomponentPaths, jobjectArray jcomponentValues) { + assert(mapPtr > 0); + size_t n_sceneUpdates = (jcomponentPaths == NULL)? 0 : jniEnv->GetArrayLength(jcomponentPaths); + + std::vector sceneUpdates; + for (size_t i = 0; i < n_sceneUpdates; i++) { + jstring path = (jstring) (jniEnv->GetObjectArrayElement(jcomponentPaths, i)); + jstring value = (jstring) (jniEnv->GetObjectArrayElement(jcomponentValues, i)); + sceneUpdates.emplace_back(stringFromJString(jniEnv, path), stringFromJString(jniEnv, value)); + jniEnv->DeleteLocalRef(path); + jniEnv->DeleteLocalRef(value); + } + + if (sceneUpdates.empty()) { return; } + + auto map = reinterpret_cast(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(mapPtr); diff --git a/android/tangram/src/com/mapzen/tangram/MapController.java b/android/tangram/src/com/mapzen/tangram/MapController.java index 5b44702c69..7c63415818 100644 --- a/android/tangram/src/com/mapzen/tangram/MapController.java +++ b/android/tangram/src/com/mapzen/tangram/MapController.java @@ -16,6 +16,7 @@ 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; @@ -225,27 +226,32 @@ static MapController getInstance(GLSurfaceView view) { public void loadSceneFile(String path) { scenePath = path; checkPointer(mapPointer); - nativeLoadScene(mapPointer, path); + nativeLoadScene(mapPointer, path, new String[0], new String[0]); 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 + * @param sceneUpdates List of {@code SceneUpdate} */ - public void loadSceneFile(String path, ArrayList sceneUpdates) { - String[] paths = new String[sceneUpdates.size()]; - String[] values = new String[sceneUpdates.size()]; + public void loadSceneFile(String path, List sceneUpdates) { + + if (sceneUpdates == null) { + // default to loading the scene file + loadSceneFile(path); + } + String[] componentPaths = new String[sceneUpdates.size()]; + String[] componentValues = new String[sceneUpdates.size()]; int index = 0; for (SceneUpdate sceneUpdate : sceneUpdates) { - paths[index] = sceneUpdate.path; - values[index] = sceneUpdate.value; + componentPaths[index] = sceneUpdate.path(); + componentValues[index] = sceneUpdate.value(); index++; } scenePath = path; checkPointer(mapPointer); - nativeLoadSceneWithUpdates(mapPointer, path, paths, values); + nativeLoadScene(mapPointer, path, componentPaths, componentValues); requestRender(); } @@ -759,24 +765,29 @@ 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(String componentPath, String value) { + public void queueSceneUpdate(SceneUpdate sceneUpdate) { checkPointer(mapPointer); - nativeQueueSceneUpdate(mapPointer, componentPath, value); + nativeQueueSceneUpdate(mapPointer, sceneUpdate.path(), sceneUpdate.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") + * @param sceneUpdates List of {@code SceneUpdate} */ - public void queueSceneUpdate(ArrayList sceneUpdates) { + public void queueSceneUpdate(List sceneUpdates) { checkPointer(mapPointer); + String[] componentPaths = new String[sceneUpdates.size()]; + String[] componentValues = new String[sceneUpdates.size()]; + + int index = 0; for(SceneUpdate sceneUpdate : sceneUpdates) { - nativeQueueSceneUpdate(mapPointer, sceneUpdate.path, sceneUpdate.value); + componentPaths[index] = sceneUpdate.path(); + componentValues[index] = sceneUpdate.value(); + index++; } + nativeQueueSceneUpdates(mapPointer, componentPaths, componentValues); } /** @@ -894,8 +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 nativeLoadSceneWithUpdates(long mapPtr, String path, String[] sceneUpdatePaths, String[] sceneUpdateValues); + private synchronized native void nativeLoadScene(long mapPtr, String path, String[] componentPaths, String[] componentValues); 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); @@ -924,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[] componentPaths, String[] componentValues); 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); diff --git a/android/tangram/src/com/mapzen/tangram/MapView.java b/android/tangram/src/com/mapzen/tangram/MapView.java index 8cbbb27f48..0cddfe0898 100644 --- a/android/tangram/src/com/mapzen/tangram/MapView.java +++ b/android/tangram/src/com/mapzen/tangram/MapView.java @@ -9,6 +9,7 @@ import android.widget.FrameLayout; import java.util.ArrayList; +import java.util.List; /** * {@code MapView} is a View for displaying a Tangram map. @@ -69,7 +70,7 @@ public void getMapAsync(@NonNull final OnMapReadyCallback callback, */ public void getMapAsync(@NonNull final OnMapReadyCallback callback, @NonNull final String sceneFilePath, - final ArrayList sceneUpdates) { + final List sceneUpdates) { disposeTask(); @@ -81,11 +82,7 @@ public void getMapAsync(@NonNull final OnMapReadyCallback callback, @SuppressWarnings("WrongThread") protected Boolean doInBackground(Void... params) { mapInstance.init(); - if (sceneUpdates.size() > 0) { - mapInstance.loadSceneFile(sceneFilePath, sceneUpdates); - } else { - mapInstance.loadSceneFile(sceneFilePath); - } + mapInstance.loadSceneFile(sceneFilePath, sceneUpdates); return true; } diff --git a/android/tangram/src/com/mapzen/tangram/SceneUpdate.java b/android/tangram/src/com/mapzen/tangram/SceneUpdate.java index e17db28032..f7204bd759 100644 --- a/android/tangram/src/com/mapzen/tangram/SceneUpdate.java +++ b/android/tangram/src/com/mapzen/tangram/SceneUpdate.java @@ -6,19 +6,20 @@ public class SceneUpdate { - public String path; - public String value; - - public SceneUpdate() { this("", ""); } + 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) { - set(path, value); + this.componentPath = path; + this.componentValue = value; } - public SceneUpdate set(String p, String v) { - path = p; - value = v; - return this; - } + public String path() { return componentPath; } + public String value() { return componentValue; } }