Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] expose symbol placement transition duration option
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Feb 4, 2019
1 parent b56a718 commit cc34eae
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,16 @@ public Bitmap getImage(@NonNull String id) {

/**
* <p>
* Set the transition duration for style changes.
* Set the transition options for style changes.
* </p>
* The default value for delay and duration is zero, so any changes take effect without animation.
* If not set, any changes take effect without animation, besides symbols,
* which will fade in/out with a default duration after symbol collision detection.
* <p>
* To disable symbols fade in/out animation,
* pass transition options with {@link TransitionOptions#enablePlacementTransitions} equal to false.
* <p>
* Both {@link TransitionOptions#duration} and {@link TransitionOptions#delay}
* will also change the behavior of the symbols fade in/out animation if the placement transition is enabled.
*
* @param transitionOptions the transition options
*/
Expand All @@ -388,9 +395,17 @@ public void setTransition(@NonNull TransitionOptions transitionOptions) {

/**
* <p>
* Get the transition for style changes.
* Get the transition options for style changes.
* </p>
* The default value for delay and transition is zero, so any changes take effect without animation.
* By default, any changes take effect without animation, besides symbols,
* which will fade in/out with a default duration after symbol collision detection.
* <p>
* To disable symbols fade in/out animation,
* pass transition options with {@link TransitionOptions#enablePlacementTransitions} equal to false
* into {@link #setTransition(TransitionOptions)}.
* <p>
* Both {@link TransitionOptions#duration} and {@link TransitionOptions#delay}
* will also change the behavior of the symbols fade in/out animation if the placement transition is enabled.
*
* @return TransitionOptions the transition options
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.mapbox.mapboxsdk.style.layers;

import android.support.annotation.Keep;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
* Resembles transition property from the style specification.
Expand All @@ -15,6 +13,8 @@ public class TransitionOptions {
private long duration;
@Keep
private long delay;
@Keep
private boolean enablePlacementTransitions;

/**
* Create a transition property based on duration and a delay.
Expand All @@ -23,8 +23,21 @@ public class TransitionOptions {
* @param delay the delay to start the transition
*/
public TransitionOptions(long duration, long delay) {
this(duration, delay, true);
}

/**
* Create a transition property.
*
* @param duration the duration of the transition
* @param delay the delay to start the transition
* @param enablePlacementTransitions the flag that describes whether the fade in/out symbol placement transition
* should be enabled. Defaults to true.
*/
public TransitionOptions(long duration, long delay, boolean enablePlacementTransitions) {
this.duration = duration;
this.delay = delay;
this.enablePlacementTransitions = enablePlacementTransitions;
}

/**
Expand All @@ -33,12 +46,30 @@ public TransitionOptions(long duration, long delay) {
* @param duration the duration of the transition
* @param delay the delay to start the transition
* @return a new transition property object
* @deprecated use {@link #fromTransitionOptions(long, long, boolean)} instead
*/
@Keep
@Deprecated
public static TransitionOptions fromTransitionOptions(long duration, long delay) {
// Invoked from JNI only
return new TransitionOptions(duration, delay);
}

/**
* Create a transition property.
*
* @param duration the duration of the transition
* @param delay the delay to start the transition
* @param enablePlacementTransitions the flag that describes whether the fade in/out symbol placement transition
* should be enabled. Defaults to true.
* @return a new transition property object
*/
@Keep
static TransitionOptions fromTransitionOptions(long duration, long delay, boolean enablePlacementTransitions) {
// Invoked from JNI only
return new TransitionOptions(duration, delay, enablePlacementTransitions);
}

/**
* Get the transition duration.
*
Expand All @@ -57,8 +88,17 @@ public long getDelay() {
return delay;
}

/**
* Get the flag that describes whether the fade in/out symbol placement transition should be enabled.
*
* @return true if the fade in/out symbol placement transition should be enabled, false otherwise
*/
public boolean isEnablePlacementTransitions() {
return enablePlacementTransitions;
}

@Override
public boolean equals(@Nullable Object o) {
public boolean equals(Object o) {
if (this == o) {
return true;
}
Expand All @@ -71,22 +111,26 @@ public boolean equals(@Nullable Object o) {
if (duration != that.duration) {
return false;
}
return delay == that.delay;
if (delay != that.delay) {
return false;
}
return enablePlacementTransitions == that.enablePlacementTransitions;
}

@Override
public int hashCode() {
int result = (int) (duration ^ (duration >>> 32));
result = 31 * result + (int) (delay ^ (delay >>> 32));
result = 31 * result + (enablePlacementTransitions ? 1 : 0);
return result;
}

@NonNull
@Override
public String toString() {
return "TransitionOptions{"
+ "duration=" + duration
+ ", delay=" + delay
+ ", enablePlacementTransitions=" + enablePlacementTransitions
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.mapbox.mapboxsdk.geometry.ProjectedMeters
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer
import com.mapbox.mapboxsdk.style.layers.TransitionOptions
import com.mapbox.mapboxsdk.testapp.utils.TestConstants
import junit.framework.Assert.assertEquals
import junit.framework.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -325,6 +325,16 @@ class NativeMapViewTest {
fun testTransitionOptions() {
val transitionOptions = TransitionOptions(500, 500)
nativeMapView.transitionOptions = transitionOptions
assertTrue(transitionOptions.isEnablePlacementTransitions)
assertEquals(transitionOptions, nativeMapView.transitionOptions)
}

@Test
@UiThreadTest
fun testTransitionOptions_disablePlacementTransitions() {
val transitionOptions = TransitionOptions(500, 500, false)
nativeMapView.transitionOptions = transitionOptions
assertFalse(transitionOptions.isEnablePlacementTransitions)
assertEquals(transitionOptions, nativeMapView.transitionOptions)
}

Expand Down
6 changes: 4 additions & 2 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,13 +682,15 @@ jni::Local<jni::Object<TransitionOptions>> NativeMapView::getTransitionOptions(J
const auto transitionOptions = map->getStyle().getTransitionOptions();
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(transitionOptions.duration.value_or(mbgl::Duration::zero())).count();
const auto delay = std::chrono::duration_cast<std::chrono::milliseconds>(transitionOptions.delay.value_or(mbgl::Duration::zero())).count();
return TransitionOptions::fromTransitionOptions(env, duration, delay);
const auto enablePlacementTransitions = (jboolean) transitionOptions.enablePlacementTransitions;
return TransitionOptions::fromTransitionOptions(env, duration, delay, enablePlacementTransitions);
}

void NativeMapView::setTransitionOptions(JNIEnv& env, const jni::Object<TransitionOptions>& options) {
const mbgl::style::TransitionOptions transitionOptions(
Duration(mbgl::Milliseconds(TransitionOptions::getDuration(env, options))),
Duration(mbgl::Milliseconds(TransitionOptions::getDelay(env, options)))
Duration(mbgl::Milliseconds(TransitionOptions::getDelay(env, options))),
TransitionOptions::isEnablePlacementTransitions(env, options)
);
map->getStyle().setTransitionOptions(transitionOptions);
}
Expand Down
3 changes: 2 additions & 1 deletion platform/android/src/style/conversion/transition_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace conversion {
Result<jni::Local<jni::Object<TransitionOptions>>> Converter<jni::Local<jni::Object<TransitionOptions>>, mbgl::style::TransitionOptions>::operator()(jni::JNIEnv& env, const mbgl::style::TransitionOptions& value) const {
return TransitionOptions::fromTransitionOptions(env,
std::chrono::duration_cast<std::chrono::milliseconds>(value.duration.value_or(mbgl::Duration::zero())).count(),
std::chrono::duration_cast<std::chrono::milliseconds>(value.delay.value_or(mbgl::Duration::zero())).count()
std::chrono::duration_cast<std::chrono::milliseconds>(value.delay.value_or(mbgl::Duration::zero())).count(),
(jboolean) value.enablePlacementTransitions
);
}

Expand Down
12 changes: 9 additions & 3 deletions platform/android/src/style/transition_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace mbgl {
namespace android {

jni::Local<jni::Object<TransitionOptions>> TransitionOptions::fromTransitionOptions(jni::JNIEnv& env, jlong duration, jlong delay) {
jni::Local<jni::Object<TransitionOptions>> TransitionOptions::fromTransitionOptions(jni::JNIEnv& env, jlong duration, jlong delay, jboolean enablePlacementTransitions) {
static auto& javaClass = jni::Class<TransitionOptions>::Singleton(env);
static auto method = javaClass.GetStaticMethod<jni::Object<TransitionOptions> (jlong, jlong)>(env, "fromTransitionOptions");
return javaClass.Call(env, method, duration, delay);
static auto method = javaClass.GetStaticMethod<jni::Object<TransitionOptions> (jlong, jlong, jboolean)>(env, "fromTransitionOptions");
return javaClass.Call(env, method, duration, delay, enablePlacementTransitions);
}

long TransitionOptions::getDuration(jni::JNIEnv& env, const jni::Object<TransitionOptions>& transitionOptions) {
Expand All @@ -21,6 +21,12 @@ long TransitionOptions::getDelay(jni::JNIEnv& env, const jni::Object<TransitionO
return transitionOptions.Get(env, field);
}

bool TransitionOptions::isEnablePlacementTransitions(jni::JNIEnv& env, const jni::Object<mbgl::android::TransitionOptions>& transitionOptions) {
static auto& javaClass = jni::Class<TransitionOptions>::Singleton(env);
static auto field = javaClass.GetField<jboolean >(env, "enablePlacementTransitions");
return transitionOptions.Get(env, field);
}

void TransitionOptions::registerNative(jni::JNIEnv& env) {
jni::Class<TransitionOptions>::Singleton(env);
}
Expand Down
4 changes: 3 additions & 1 deletion platform/android/src/style/transition_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class TransitionOptions : private mbgl::util::noncopyable {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/layers/TransitionOptions"; };

static jni::Local<jni::Object<TransitionOptions>> fromTransitionOptions(jni::JNIEnv&, jlong duration, jlong offset);
static jni::Local<jni::Object<TransitionOptions>> fromTransitionOptions(jni::JNIEnv&, jlong duration, jlong delay, jboolean enablePlacementTransitions);

static long getDuration(jni::JNIEnv&, const jni::Object<TransitionOptions>&);

static long getDelay(jni::JNIEnv&, const jni::Object<TransitionOptions>&);

static bool isEnablePlacementTransitions(jni::JNIEnv&, const jni::Object<TransitionOptions>&);

static void registerNative(jni::JNIEnv&);
};

Expand Down

0 comments on commit cc34eae

Please sign in to comment.