[android] #5610 - Runtime style api - part 2

This commit is contained in:
Ivo van Dongen
2016-07-26 17:02:52 -04:00
parent ed821c06d9
commit 10b2cc60a7
73 changed files with 6974 additions and 297 deletions

View File

@@ -22,18 +22,11 @@ global.camelizeWithLeadingLowercase = function (str) {
});
}
global.snakeCaseUpper = function (str) {
global.snakeCaseUpper = function snakeCaseUpper(str) {
return str.replace(/-/g, "_").toUpperCase();
}
global.propertyType = function propertyType(property) {
//TODO: Doe we want these exceptions?
// if (/-translate-anchor$/.test(property.name)) {
// return 'TranslateAnchorType';
// }
// if (/-(rotation|pitch)-alignment$/.test(property.name)) {
// return 'AlignmentType';
// }
switch (property.type) {
case 'boolean':
return 'Boolean';
@@ -42,9 +35,9 @@ global.propertyType = function propertyType(property) {
case 'string':
return 'String';
case 'enum':
return `String`;
return 'String';
case 'color':
return `String`;
return 'String';
case 'array':
return `${propertyType({type:property.value})}[]`;
default:
@@ -52,6 +45,53 @@ global.propertyType = function propertyType(property) {
}
}
global.propertyJNIType = function propertyJNIType(property) {
switch (property.type) {
case 'boolean':
return 'jboolean';
case 'jfloat':
return 'Float';
case 'String':
return 'String';
case 'enum':
return 'String';
case 'color':
return 'String';
case 'array':
return `jarray<${propertyType({type:property.value})}[]>`;
default:
return 'jobject*';
}
}
global.propertyNativeType = function (property) {
if (/-translate-anchor$/.test(property.name)) {
return 'TranslateAnchorType';
}
if (/-(rotation|pitch)-alignment$/.test(property.name)) {
return 'AlignmentType';
}
switch (property.type) {
case 'boolean':
return 'bool';
case 'number':
return 'float';
case 'string':
return 'std::string';
case 'enum':
return `${camelize(property.name)}Type`;
case 'color':
return `Color`;
case 'array':
if (property.length) {
return `std::array<${propertyType({type: property.value})}, ${property.length}>`;
} else {
return `std::vector<${propertyType({type: property.value})}>`;
}
default: throw new Error(`unknown type for ${property.name}`)
}
}
global.propertyTypeAnnotation = function propertyTypeAnnotation(property) {
switch (property.type) {
case 'enum':
@@ -61,6 +101,42 @@ global.propertyTypeAnnotation = function propertyTypeAnnotation(property) {
}
};
global.defaultValueJava = function(property) {
if(property.name.endsWith("-pattern")) {
return '"pedestrian-polygon"';
}
if(property.name.endsWith("-font")) {
return 'new String[]{"Open Sans Regular", "Arial Unicode MS Regular"}';
}
switch (property.type) {
case 'boolean':
return 'true';
case 'number':
return '0.3f';
case 'string':
return '"' + property['default'] + '"';
case 'enum':
return snakeCaseUpper(property.name) + "_" + snakeCaseUpper(property.values[0]);
case 'color':
return '"rgba(0, 0, 0, 1)"';
case 'array':
switch (property.value) {
case 'string':
return '[' + property['default'] + "]";
case 'number':
var result ='new Float[]{';
for (var i = 0; i < property.length; i++) {
result += "0f";
if (i +1 != property.length) {
result += ",";
}
}
return result + "}";
}
default: throw new Error(`unknown type for ${property.name}`)
}
}
//Process Layers
const layers = spec.layer.type.values.map((type) => {
const layoutProperties = Object.keys(spec[`layout_${type}`]).reduce((memo, name) => {
@@ -81,24 +157,27 @@ const layers = spec.layer.type.values.map((type) => {
type: type,
layoutProperties: layoutProperties,
paintProperties: paintProperties,
properties: layoutProperties.concat(paintProperties)
};
});
const layerHpp = ejs.compile(fs.readFileSync('platform/android/scripts/layer.hpp.ejs', 'utf8'), {strict: true});
const layerCpp = ejs.compile(fs.readFileSync('platform/android/scripts/layer.cpp.ejs', 'utf8'), {strict: true});
const layerJava = ejs.compile(fs.readFileSync('platform/android/scripts/layer.java.ejs', 'utf8'), {strict: true});
const layerHpp = ejs.compile(fs.readFileSync('platform/android/src/style/layers/layer.hpp.ejs', 'utf8'), {strict: true});
const layerCpp = ejs.compile(fs.readFileSync('platform/android/src/style/layers/layer.cpp.ejs', 'utf8'), {strict: true});
const layerJava = ejs.compile(fs.readFileSync('platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/layer.java.ejs', 'utf8'), {strict: true});
const layerJavaUnitTests = ejs.compile(fs.readFileSync('platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/style/layer.junit.ejs', 'utf8'), {strict: true});
for (const layer of layers) {
fs.writeFileSync(`platform/android/src/style/layers/${layer.type}_layer.hpp`, layerHpp(layer));
fs.writeFileSync(`platform/android/src/style/layers/${layer.type}_layer.cpp`, layerCpp(layer));
fs.writeFileSync(`platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/${camelize(layer.type)}Layer.java`, layerJava(layer));
fs.writeFileSync(`platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/style/${camelize(layer.type)}LayerTest.java`, layerJavaUnitTests(layer));
}
//Process all layer properties
const layoutProperties = _(layers).map('layoutProperties').flatten().value();
const paintProperties = _(layers).map('paintProperties').flatten().value();
const propertiesTemplate = ejs.compile(fs.readFileSync('platform/android/scripts/layer_property_factory.java.ejs', 'utf8'), {strict: true});
const propertiesTemplate = ejs.compile(fs.readFileSync('platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs', 'utf8'), {strict: true});
fs.writeFileSync(
`platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java`,
propertiesTemplate({layoutProperties: layoutProperties, paintProperties: paintProperties})
@@ -106,9 +185,22 @@ fs.writeFileSync(
//Create types for the enum properties
const enumProperties = _(layoutProperties).union(paintProperties).filter({'type': 'enum'}).value();
const enumPropertyTemplate = ejs.compile(fs.readFileSync('platform/android/scripts/layer_property.java.ejs', 'utf8'), {strict: true});
const enumPropertyJavaTemplate = ejs.compile(fs.readFileSync('platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property.java.ejs', 'utf8'), {strict: true});
fs.writeFileSync(
`platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Property.java`,
enumPropertyTemplate({properties: enumProperties})
enumPropertyJavaTemplate({properties: enumProperties})
);
//De-dup types before generating cpp headers
const enumPropertiesDeDup = _(enumProperties).uniq(global.propertyNativeType).value();
const enumPropertyHppTypeStringValueTemplate = ejs.compile(fs.readFileSync('platform/android/src/style/conversion/types_string_values.hpp.ejs', 'utf8'), {strict: true});
fs.writeFileSync(
`platform/android/src/style/conversion/types_string_values.hpp`,
enumPropertyHppTypeStringValueTemplate({properties: enumPropertiesDeDup})
);
const enumPropertyHppTypeTemplate = ejs.compile(fs.readFileSync('platform/android/src/style/conversion/types.hpp.ejs', 'utf8'), {strict: true});
fs.writeFileSync(
`platform/android/src/style/conversion/types.hpp`,
enumPropertyHppTypeTemplate({properties: enumPropertiesDeDup})
);

View File

@@ -1,64 +0,0 @@
<%
const type = locals.type;
-%>
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`.
#include "<%- type %>_layer.hpp"
#include <string>
//XXX
#include <mbgl/platform/log.hpp>
namespace mbgl {
namespace android {
<% if (type === 'background') { -%>
<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(jni::JNIEnv& env, jni::String layerId)
: Layer(env, std::make_unique<mbgl::style::<%- camelize(type) %>Layer>(jni::Make<std::string>(env, layerId))) {
<% } else { -%>
<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(jni::JNIEnv& env, jni::String layerId, jni::String sourceId)
: Layer(env, std::make_unique<mbgl::style::<%- camelize(type) %>Layer>(jni::Make<std::string>(env, layerId), jni::Make<std::string>(env, sourceId))) {
<% } -%>
mbgl::Log::Debug(mbgl::Event::JNI, "<%- camelize(type) %>Layer constructed, owning reference");
}
<%- camelize(type) %>Layer::<%- camelize(type) %>Layer(mbgl::Map& map, mbgl::style::<%- camelize(type) %>Layer& coreLayer)
: Layer(map, coreLayer) {
mbgl::Log::Debug(mbgl::Event::JNI, "<%- camelize(type) %>Layer Non-owning reference constructor");
}
<%- camelize(type) %>Layer::~<%- camelize(type) %>Layer() = default;
jni::Class<<%- camelize(type) %>Layer> <%- camelize(type) %>Layer::javaClass;
jni::jobject* <%- camelize(type) %>Layer::createJavaPeer(jni::JNIEnv& env) {
static auto constructor = <%- camelize(type) %>Layer::javaClass.template GetConstructor<jni::jlong>(env);
return <%- camelize(type) %>Layer::javaClass.New(env, constructor, reinterpret_cast<jni::jlong>(this));
}
void <%- camelize(type) %>Layer::registerNative(jni::JNIEnv& env) {
mbgl::Log::Debug(mbgl::Event::JNI, "Registering native background layer");
//Lookup the class
<%- camelize(type) %>Layer::javaClass = *jni::Class<<%- camelize(type) %>Layer>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
//Register the peer
jni::RegisterNativePeer<<%- camelize(type) %>Layer>(
env, <%- camelize(type) %>Layer::javaClass, "nativePtr",
<% if (type === 'background') { -%>
std::make_unique<<%- camelize(type) %>Layer, JNIEnv&, jni::String>,
<% } else { -%>
std::make_unique<<%- camelize(type) %>Layer, JNIEnv&, jni::String, jni::String>,
<% } -%>
"initialize",
"finalize"
);
}
} // namespace android
} // namespace mbgl

View File

@@ -1,38 +0,0 @@
<%
const type = locals.type;
-%>
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`.
#pragma once
#include "layer.hpp"
#include <mbgl/style/layers/<%- type %>_layer.hpp>
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
class <%- camelize(type) %>Layer : public Layer {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/layers/<%- camelize(type) %>Layer"; };
static jni::Class<<%- camelize(type) %>Layer> javaClass;
static void registerNative(jni::JNIEnv&);
<% if (type === 'background') { -%>
<%- camelize(type) %>Layer(jni::JNIEnv&, jni::String);
<% } else { -%>
<%- camelize(type) %>Layer(jni::JNIEnv&, jni::String, jni::String);
<% } -%>
<%- camelize(type) %>Layer(mbgl::Map&, mbgl::style::<%- camelize(type) %>Layer&);
~<%- camelize(type) %>Layer();
jni::jobject* createJavaPeer(jni::JNIEnv&);
};
} // namespace android
} // namespace mbgl

View File

@@ -1,44 +0,0 @@
<%
const type = locals.type;
-%>
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`.
package com.mapbox.mapboxsdk.style.layers;
/**
* <%- camelize(type) %> Layer
*/
public class <%- camelize(type) %>Layer extends Layer {
public <%- camelize(type) %>Layer(long nativePtr) {
super(nativePtr);
}
<% if (type === 'background') { -%>
public <%- camelize(type) %>Layer(String layerId) {
initialize(layerId);
}
protected native void initialize(String layerId);
<% } else { -%>
public <%- camelize(type) %>Layer(String layerId, String sourceId) {
initialize(layerId, sourceId);
}
protected native void initialize(String layerId, String sourceId);
public void setSourceLayer(String sourceLayer) {
nativeSetSourceLayer(sourceLayer);
}
<% } -%>
<% if (type !== 'background' && type !== 'raster') { -%>
public void setFilter(Filter.Statement filter) {
this.setFilter(filter.toArray());
}
public void setFilter(Object[] filter) {
nativeSetFilter(filter);
}
<% } -%>
}

View File

@@ -1,42 +0,0 @@
<%
const properties = locals.properties;
-%>
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`.
package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.StringDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Paint/Layout properties for Layer
*/
public abstract class Property<T> {
<% for (const property of properties) { -%>
//<%- property.name %>
<% for (const value of property.values) { -%>
public static final String <%- snakeCaseUpper(property.name) %>_<%- snakeCaseUpper(value) %> = "<%- value %>";
<% } -%>
@StringDef({
<% for (const value of property.values) { -%>
<%- snakeCaseUpper(property.name) %>_<%- snakeCaseUpper(value) %>,
<% } -%>
})
@Retention(RetentionPolicy.SOURCE)
public @interface <%- snakeCaseUpper(property.name) %> {}
<% } -%>
//Class definition
public final String name;
public final T value;
/* package */ Property(String name, T value) {
this.name = name;
this.value = value;
}
}

View File

@@ -1,45 +0,0 @@
<%
const paintProperties = locals.paintProperties;
const layoutProperties = locals.layoutProperties;
-%>
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`.
package com.mapbox.mapboxsdk.style.layers;
import android.annotation.SuppressLint;
import android.support.annotation.ColorInt;
/**
* Constructs paint/layout properties for Layers
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#layers>Layer style documentation</a>
*
*/
public class PropertyFactory {
public static Property<String> visibility(Boolean visible) {
return new LayoutProperty<>("visibility", visible? "visible": "none");
}
<% for (const property of paintProperties) { -%>
<% if (property.type == 'color') { -%>
public static Property<String> <%- camelizeWithLeadingLowercase(property.name) %>(@ColorInt int value) {
return new PaintProperty<>("<%- property.name %>", colorToRgbaString(value));
}
<% } -%>
public static Property<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %>(<%- propertyTypeAnnotation(property) %><%- iff(() => propertyTypeAnnotation(property), " ") %><%- propertyType(property) %> value) {
return new PaintProperty<>("<%- property.name %>", value);
}
<% } -%>
<% for (const property of layoutProperties) { -%>
public static Property<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %>(<%- propertyTypeAnnotation(property) %><%- iff(() => propertyTypeAnnotation(property), " ") %><%- propertyType(property) %> value) {
return new LayoutProperty<>("<%- property.name %>", value);
}
<% } -%>
@SuppressLint("DefaultLocale")
static String colorToRgbaString(@ColorInt int value) {
return String.format("rgba(%d, %d, %d, %d)", (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, (value >> 24) & 0xFF);
}
}