summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2018-11-13 18:29:25 +0100
committerŁukasz Paczos <lukasz.paczos@mapbox.com>2018-12-04 17:22:38 +0100
commit794b851f02b98c5f1652edc141b1a6feb459d59b (patch)
tree66fda491ea0a100d3a45092ce2840d590fc7fefa
parent1e7c7e048991a33167d5fb40f6c9ea2a44885104 (diff)
downloadqtlocation-mapboxgl-794b851f02b98c5f1652edc141b1a6feb459d59b.tar.gz
[android] expose Formatted text-field setter
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java10
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java11
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs5
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/Formatted.java19
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/FormattedSection.java9
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java30
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java5
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/layer.junit.ejs4
-rwxr-xr-xplatform/android/scripts/generate-style-code.js2
-rw-r--r--src/mbgl/style/expression/formatted.cpp60
10 files changed, 129 insertions, 26 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java
index e370a366f5..72591196d4 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java
@@ -2,10 +2,11 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.Keep;
import android.support.annotation.NonNull;
-
import android.support.annotation.Nullable;
+
import com.google.gson.JsonElement;
import com.mapbox.mapboxsdk.style.expressions.Expression;
+import com.mapbox.mapboxsdk.style.types.Formatted;
import com.mapbox.mapboxsdk.utils.ThreadUtils;
/**
@@ -137,9 +138,12 @@ public abstract class Layer {
@Nullable
private Object convertValue(@Nullable Object value) {
- if (value != null && value instanceof Expression) {
+ if (value instanceof Expression) {
return ((Expression) value).toArray();
+ } else if (value instanceof Formatted) {
+ return ((Formatted) value).toArray();
+ } else {
+ return value;
}
- return value;
}
} \ No newline at end of file
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java
index 51edca3002..3d8b921a79 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java
@@ -5,6 +5,7 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import com.mapbox.mapboxsdk.style.expressions.Expression;
+import com.mapbox.mapboxsdk.style.types.Formatted;
import static com.mapbox.mapboxsdk.utils.ColorUtils.colorToRgbaString;
/**
@@ -2140,6 +2141,16 @@ public class PropertyFactory {
* @param value a Formatted value
* @return property wrapper around Formatted
*/
+ public static PropertyValue<Formatted> textField(Formatted value) {
+ return new LayoutPropertyValue<>("text-field", value);
+ }
+
+ /**
+ * Value to use for a text label. If a plain `string` is provided, it will be treated as a `formatted` with default/inherited formatting options.
+ *
+ * @param value a Formatted value
+ * @return property wrapper around Formatted
+ */
public static PropertyValue<Expression> textField(Expression value) {
return new LayoutPropertyValue<>("text-field", value);
}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs
index 7474e7ebb8..f4b171489a 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs
@@ -9,6 +9,7 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;
import com.mapbox.mapboxsdk.style.expressions.Expression;
+import com.mapbox.mapboxsdk.style.types.Formatted;
import static com.mapbox.mapboxsdk.utils.ColorUtils.colorToRgbaString;
/**
@@ -73,7 +74,8 @@ public class PropertyFactory {
public static PropertyValue<String> <%- camelizeWithLeadingLowercase(property.name) %>(String value) {
return new LayoutPropertyValue<>("<%- property.name %>", value);
}
-<% } else {-%>
+
+<% } -%>
/**
* <%- propertyFactoryMethodDoc(property) %>
*
@@ -83,7 +85,6 @@ public class PropertyFactory {
public static PropertyValue<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %>(<%- propertyTypeAnnotation(property) %><%- iff(() => propertyTypeAnnotation(property), " ") %><%- propertyType(property) %> value) {
return new LayoutPropertyValue<>("<%- property.name %>", value);
}
-<% } -%>
/**
* <%- propertyFactoryMethodDoc(property) %>
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/Formatted.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/Formatted.java
index 9be08dcebd..fb2d361bfc 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/Formatted.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/Formatted.java
@@ -17,18 +17,9 @@ public class Formatted {
/**
* Create a new formatted text.
*
- * @param formattedSection section with formatting options
- */
- public Formatted(FormattedSection formattedSection) {
- this(new FormattedSection[] {formattedSection});
- }
-
- /**
- * Create a new formatted text.
- *
* @param formattedSections sections with formatting options
*/
- public Formatted(FormattedSection[] formattedSections) {
+ public Formatted(FormattedSection... formattedSections) {
this.formattedSections = formattedSections;
}
@@ -41,6 +32,14 @@ public class Formatted {
return formattedSections;
}
+ public Object[] toArray() {
+ Object[] sections = new Object[formattedSections.length];
+ for (int i = 0; i < formattedSections.length; i++) {
+ sections[i] = formattedSections[i].toArray();
+ }
+ return sections;
+ }
+
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/FormattedSection.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/FormattedSection.java
index 7d0caec992..c96a104afd 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/FormattedSection.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/FormattedSection.java
@@ -5,6 +5,8 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
/**
* A component of the {@link Formatted}.
@@ -117,4 +119,11 @@ public class FormattedSection {
result = 31 * result + Arrays.hashCode(fontStack);
return result;
}
+
+ Object[] toArray() {
+ Map<String, Object> params = new HashMap<>();
+ params.put("font-scale", fontScale);
+ params.put("text-font", fontStack);
+ return new Object[] {text, params};
+ }
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java
index 093fbf2662..7f8691d46b 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java
@@ -406,10 +406,9 @@ public class ExpressionTest extends BaseActivityTest {
assertNull(layer.getTextField().getExpression());
assertEquals(new Formatted(
- new FormattedSection[] {
- new FormattedSection("test", 1.5, new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}),
- new FormattedSection("\ntest2", 2.0),
- }
+ new FormattedSection("test", 1.5,
+ new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}),
+ new FormattedSection("\ntest2", 2.0)
), layer.getTextField().getValue());
});
}
@@ -493,6 +492,29 @@ public class ExpressionTest extends BaseActivityTest {
});
}
+ @Test
+ public void testTextFieldFormattedArgument() {
+ validateTestSetup();
+ invoke(mapboxMap, (uiController, mapboxMap) -> {
+ LatLng latLng = new LatLng(51, 17);
+ mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())));
+ SymbolLayer layer = new SymbolLayer("layer", "source");
+ mapboxMap.addLayer(layer);
+
+ Formatted formatted = new Formatted(
+ new FormattedSection("test", 1.5),
+ new FormattedSection("\ntest", 0.5, new String[] {"Arial Unicode MS Regular", "DIN Offc Pro Regular"})
+ );
+ layer.setProperties(textField(formatted));
+ waitForLayer(uiController, mapboxMap, latLng);
+ assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
+ .isEmpty());
+
+ assertNull(layer.getTextField().getExpression());
+ assertEquals(formatted, layer.getTextField().getValue());
+ });
+ }
+
private static final long WAIT_TIMEOUT = 5000;
private static final long WAIT_DELAY = 150;
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java
index 9d12ae9401..f7be4f7b10 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java
@@ -521,11 +521,14 @@ public class SymbolLayerTest extends BaseActivityTest {
assertNull(layer.getTextField().getValue());
// Set and Get
- Formatted propertyValue = new Formatted(new FormattedSection[]{new FormattedSection("default")});
+ Formatted propertyValue = new Formatted(new FormattedSection("default"));
layer.setProperties(textField("default"));
assertEquals(layer.getTextField().getValue(), propertyValue);
+ layer.setProperties(textField(propertyValue));
+ assertEquals(layer.getTextField().getValue(), propertyValue);
+
layer.setProperties(textField("{token}"));
assertEquals(layer.getTextField().getExpression(), format(Expression.formatEntry(Expression.toString(Expression.get("token")))));
});
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/layer.junit.ejs b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/layer.junit.ejs
index 2b3c09a34b..2471ca8a98 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/layer.junit.ejs
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/layer.junit.ejs
@@ -175,10 +175,10 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>("default"));
assertEquals(layer.get<%- camelize(property.name) %>().getValue(), propertyValue);
-<% } else {-%>
+
+<% } -%>
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>(propertyValue));
assertEquals(layer.get<%- camelize(property.name) %>().getValue(), propertyValue);
-<% } -%>
<% if (property.tokens) { -%>
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>("{token}"));
diff --git a/platform/android/scripts/generate-style-code.js b/platform/android/scripts/generate-style-code.js
index da29c0363c..56e7511362 100755
--- a/platform/android/scripts/generate-style-code.js
+++ b/platform/android/scripts/generate-style-code.js
@@ -187,7 +187,7 @@ global.defaultValueJava = function(property) {
case 'number':
return '0.3f';
case 'formatted':
- return 'new Formatted(new FormattedSection[]{new FormattedSection("default")})'
+ return 'new Formatted(new FormattedSection("default"))'
case 'string':
return '"' + property['default'] + '"';
case 'enum':
diff --git a/src/mbgl/style/expression/formatted.cpp b/src/mbgl/style/expression/formatted.cpp
index 6eb106dfec..8232d0c698 100644
--- a/src/mbgl/style/expression/formatted.cpp
+++ b/src/mbgl/style/expression/formatted.cpp
@@ -44,13 +44,67 @@ namespace conversion {
using namespace mbgl::style::expression;
-optional<Formatted> Converter<Formatted>::operator()(const Convertible& value, Error&) const {
+optional<Formatted> Converter<Formatted>::operator()(const Convertible& value, Error& error) const {
using namespace mbgl::style::expression;
- auto result = toString(value);
- if (result) {
+ if (isArray(value)) {
+ std::vector<FormattedSection> sections;
+ for (std::size_t i = 0; i < arrayLength(value); ++i) {
+ Convertible section = arrayMember(value, i);
+ std::size_t sectionLength = arrayLength(section);
+ if (sectionLength < 1) {
+ error.message = "Section has to contain a text and optional parameters.";
+ return nullopt;
+ }
+
+ optional<std::string> sectionText = toString(arrayMember(section, 0));
+ if (!sectionText) {
+ error.message = "Section has to contain a text.";
+ return nullopt;
+ }
+
+ optional<double> fontScale;
+ optional<FontStack> textFont;
+ if (sectionLength > 1) {
+ Convertible sectionParams = arrayMember(section, 1);
+ if (!isObject(sectionParams)) {
+ error.message = "Parameters have to be enclosed in an object.";
+ return nullopt;
+ }
+
+ optional<Convertible> fontScaleMember = objectMember(sectionParams, "font-scale");
+ if (fontScaleMember) {
+ fontScale = toDouble(*fontScaleMember);
+ }
+
+ optional<Convertible> textFontMember = objectMember(sectionParams, "text-font");
+ if (textFontMember) {
+ if (isArray(*textFontMember)) {
+ std::vector<std::string> fontsVector;
+ for (std::size_t j = 0; j < arrayLength(*textFontMember); ++j) {
+ auto font = toString(arrayMember(*textFontMember, j));
+ if (font) {
+ fontsVector.push_back(*font);
+ } else {
+ error.message = "Font has to be a string.";
+ return nullopt;
+ }
+ }
+ textFont = fontsVector;
+ } else {
+ error.message = "Font stack has to be an array.";
+ return nullopt;
+ }
+ }
+ }
+
+ sections.push_back(FormattedSection(*sectionText, fontScale, textFont));
+ }
+ return Formatted(sections);
+ } else if (optional<std::string> result = toString(value)) {
return Formatted(result->c_str());
} else {
+ error.message = "Formatted must be plain string or array type.";
return nullopt;
}
}