Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array support for icon-padding #2845

Merged
merged 24 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7a2cc73
Add Padding type
random3940 Aug 8, 2024
2c3b555
Fix coersion, add databind render-test
random3940 Aug 16, 2024
a05a22b
Merge remote-tracking branch 'origin/main' into array-support-for-ico…
random3940 Aug 16, 2024
4497167
Use std::span instead of ctor overloads in Padding
random3940 Aug 16, 2024
ce2dc5c
Update expected images
random3940 Aug 23, 2024
31551d9
Merge remote-tracking branch 'origin/main' into array-support-for-ico…
random3940 Aug 26, 2024
adeac31
Fix interpolation, add interpolation/function tests
random3940 Aug 30, 2024
deceef9
impl stringify() properly
random3940 Sep 4, 2024
8859aa1
Merge remote-tracking branch 'origin/main' into array-support-for-ico…
random3940 Sep 4, 2024
dcb36bf
Fix auto-generated SymbolLayerTest.java
random3940 Sep 4, 2024
2c84b67
Clean-up TODOs & minor things
random3940 Sep 6, 2024
6f09b8a
Fix iOS build
random3940 Sep 6, 2024
4cbc8fa
Fix NSExpressions
random3940 Sep 11, 2024
9bd4c72
Merge remote-tracking branch 'origin/main' into array-support-for-ico…
random3940 Sep 16, 2024
ad58349
Clean-up & format
random3940 Sep 16, 2024
4b560f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2024
4533948
Re-generate Android SymbolLayerTest.java
random3940 Sep 18, 2024
6dc94d1
Fix mac/linux node builds
random3940 Sep 18, 2024
66beec8
Set different render-test results for desktop/mobile
random3940 Sep 19, 2024
3db610d
Address feedback
random3940 Sep 19, 2024
8b846c4
Merge branch 'main' into array-support-for-icon-padding
random3940 Sep 19, 2024
676be9f
Update android codegen to match clang-format
random3940 Sep 19, 2024
2eaf047
Add probes
louwers Sep 20, 2024
c75ace4
fix lint
louwers Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/logging.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/lru_cache.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/noncopyable.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/padding.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/platform.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/premultiply.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/projection.hpp
Expand Down Expand Up @@ -972,6 +973,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/util/mat4.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/mat4.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/math.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/padding.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/premultiply.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/quaternion.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/rapidjson.cpp
Expand Down
2 changes: 2 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ MLN_CORE_SOURCE = [
"src/mbgl/util/mat4.cpp",
"src/mbgl/util/mat4.hpp",
"src/mbgl/util/math.hpp",
"src/mbgl/util/padding.cpp",
"src/mbgl/util/premultiply.cpp",
"src/mbgl/util/quaternion.cpp",
"src/mbgl/util/quaternion.hpp",
Expand Down Expand Up @@ -835,6 +836,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/lru_cache.hpp",
"include/mbgl/util/monotonic_timer.hpp",
"include/mbgl/util/noncopyable.hpp",
"include/mbgl/util/padding.hpp",
"include/mbgl/util/platform.hpp",
"include/mbgl/util/premultiply.hpp",
"include/mbgl/util/projection.hpp",
Expand Down
6 changes: 6 additions & 0 deletions include/mbgl/style/conversion/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mbgl/style/types.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/padding.hpp>
#include <mbgl/util/string.hpp>

#include <array>
Expand Down Expand Up @@ -44,6 +45,11 @@ struct Converter<Color> {
std::optional<Color> operator()(const Convertible& value, Error& error) const;
};

template <>
struct Converter<Padding> {
std::optional<Padding> operator()(const Convertible& value, Error& error) const;
};

template <size_t N>
struct Converter<std::array<float, N>> {
std::optional<std::array<float, N>> operator()(const Convertible& value, Error& error) const;
Expand Down
19 changes: 13 additions & 6 deletions include/mbgl/style/conversion_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ struct ValueFactory<Color> {
static Value make(const Color& color) { return color.serialize(); }
};

template <>
struct ValueFactory<Padding> {
static Value make(const Padding& padding) { return padding.serialize(); }
};

template <typename T>
struct ValueFactory<T, typename std::enable_if_t<(!std::is_enum_v<T> && !is_linear_container<T>::value)>> {
static Value make(const T& arg) { return {arg}; }
Expand Down Expand Up @@ -361,12 +366,14 @@ Value makeValue(T&& arg) {

template <typename T>
StyleProperty makeStyleProperty(const PropertyValue<T>& value) {
return value.match([](const Undefined&) -> StyleProperty { return {}; },
[](const Color& c) -> StyleProperty { return {makeValue(c), StyleProperty::Kind::Expression}; },
[](const PropertyExpression<T>& fn) -> StyleProperty {
return {fn.getExpression().serialize(), StyleProperty::Kind::Expression};
},
[](const auto& t) -> StyleProperty { return {makeValue(t), StyleProperty::Kind::Constant}; });
return value.match(
[](const Undefined&) -> StyleProperty { return {}; },
[](const Color& c) -> StyleProperty { return {makeValue(c), StyleProperty::Kind::Expression}; },
[](const Padding& p) -> StyleProperty { return {makeValue(p), StyleProperty::Kind::Expression}; },
[](const PropertyExpression<T>& fn) -> StyleProperty {
return {fn.getExpression().serialize(), StyleProperty::Kind::Expression};
},
[](const auto& t) -> StyleProperty { return {makeValue(t), StyleProperty::Kind::Constant}; });
}

inline StyleProperty makeStyleProperty(const TransitionOptions& value) {
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/expression/dsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ std::unique_ptr<Expression> string(std::unique_ptr<Expression>, std::unique_ptr<
std::unique_ptr<Expression> boolean(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);

std::unique_ptr<Expression> toColor(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toPadding(std::unique_ptr<Expression> value, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toString(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toFormatted(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toImage(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
Expand Down
8 changes: 8 additions & 0 deletions include/mbgl/style/expression/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ struct ColorType {
bool operator==(const ColorType&) const { return true; }
};

struct PaddingType {
constexpr PaddingType() = default;
std::string getName() const { return "padding"; }
bool operator==(const PaddingType&) const { return true; }
};

struct ObjectType {
constexpr ObjectType() = default;
std::string getName() const { return "object"; }
Expand Down Expand Up @@ -85,6 +91,7 @@ constexpr NumberType Number;
constexpr StringType String;
constexpr BooleanType Boolean;
constexpr ColorType Color;
constexpr PaddingType Padding;
constexpr ValueType Value;
constexpr ObjectType Object;
constexpr CollatorType Collator;
Expand All @@ -99,6 +106,7 @@ using Type = variant<NullType,
BooleanType,
StringType,
ColorType,
PaddingType,
ObjectType,
ValueType,
mapbox::util::recursive_wrapper<Array>,
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/expression/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/padding.hpp>
#include <mbgl/util/variant.hpp>

#include <array>
Expand All @@ -30,6 +31,7 @@ using ValueBase = variant<NullValue,
Collator,
Formatted,
Image,
Padding,
mapbox::util::recursive_wrapper<std::vector<Value>>,
mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
struct Value : ValueBase {
Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/style/layers/symbol_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class SymbolLayer final : public Layer {
const PropertyValue<bool>& getIconOptional() const;
void setIconOptional(const PropertyValue<bool>&);

static PropertyValue<float> getDefaultIconPadding();
const PropertyValue<float>& getIconPadding() const;
void setIconPadding(const PropertyValue<float>&);
static PropertyValue<Padding> getDefaultIconPadding();
const PropertyValue<Padding>& getIconPadding() const;
void setIconPadding(const PropertyValue<Padding>&);

static PropertyValue<AlignmentType> getDefaultIconPitchAlignment();
const PropertyValue<AlignmentType>& getIconPitchAlignment() const;
Expand Down
18 changes: 18 additions & 0 deletions include/mbgl/util/interpolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ struct Interpolator<Color> {
}
};

template <>
struct Interpolator<Padding> {
public:
Padding operator()(const Padding& a, const Padding& b, const float t) const noexcept {
return {interpolate(a.top, b.top, t),
interpolate(a.right, b.right, t),
interpolate(a.bottom, b.bottom, t),
interpolate(a.left, b.left, t)};
}

Padding operator()(const Padding& a, const Padding& b, const double t) const noexcept {
return {interpolate(a.top, b.top, t),
interpolate(a.right, b.right, t),
interpolate(a.bottom, b.bottom, t),
interpolate(a.left, b.left, t)};
}
};

template <>
struct Interpolator<style::Rotation> {
public:
Expand Down
72 changes: 72 additions & 0 deletions include/mbgl/util/padding.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <mbgl/util/feature.hpp>
#include <array>
#include <span>

namespace mbgl {

// A set of four numbers representing padding around a box.
struct Padding {
// Empty/zero padding on all sides.
Padding() = default;

// Same padding on all sides.
Padding(float value)
: top(value),
right(value),
bottom(value),
left(value) {}

Padding(float t_, float r_, float b_, float l_)
: top(t_),
right(r_),
bottom(b_),
left(l_) {}

// Padding values are in CSS order: top, right, bottom, left.
Padding(const std::span<const float>& values) {
switch (values.size()) {
case 1:
top = right = bottom = left = values[0];
break;
case 2:
top = bottom = values[0];
right = left = values[1];
break;
case 3:
top = values[0];
left = right = values[1];
bottom = values[2];
break;
case 4:
top = values[0];
right = values[1];
bottom = values[2];
left = values[3];
break;
default:
throw std::invalid_argument("Padding must have between 1 and 4 elements");
}
}

float top = 0;
louwers marked this conversation as resolved.
Show resolved Hide resolved
float right = 0;
float bottom = 0;
float left = 0;

explicit operator bool() const { return top != 0 || right != 0 || bottom != 0 || left != 0; }

bool operator==(const Padding&) const = default;

std::array<float, 4> toArray() const;
random3940 marked this conversation as resolved.
Show resolved Hide resolved

// Used by ValueFactory<Padding>::make()
mbgl::Value serialize() const;
};

inline Padding operator*(const Padding& padding, float scale) {
return {padding.top * scale, padding.right * scale, padding.bottom * scale, padding.left * scale};
}

} // namespace mbgl
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions metrics/integration/render-tests/icon-padding/databind/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"version": 8,
"metadata": {
"test": {
"collisionDebug": true,
"width": 240,
"height": 100
}
},
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-60, 0] }
}, {
"type": "Feature",
"properties": { "padding": 5 },
"geometry": { "type": "Point", "coordinates": [-30, 0] }
}, {
"type": "Feature",
"properties": { "padding": [-20, -5] },
"geometry": { "type": "Point", "coordinates": [-0, 0] }
}, {
"type": "Feature",
"properties": { "padding": [2, 2, -10] },
"geometry": { "type": "Point", "coordinates": [30, 0] }
}, {
"type": "Feature",
"properties": { "padding": [2, 10, -20, 2] },
"geometry": { "type": "Point", "coordinates": [60, 0] }
}
]
}
}
},
"sprite": "local://sprites/2x",
"layers": [{
"id": "bg",
"type": "background",
"paint": {
"background-color": "#ccc"
}
}, {
"id": "symbolA",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": "icon",
"icon-padding": ["coalesce", ["get", "padding"], ["literal", [2]]]
}
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions metrics/integration/render-tests/icon-padding/default/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"version": 8,
"metadata": {
"test": {
"collisionDebug": true,
"width": 240,
"height": 100
}
},
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-60, 0] }
}, {
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-40, 0] }
}, {
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-20, 0] }
}
]
}
}
},
"sprite": "local://sprites/2x",
"layers": [{
"id": "bg",
"type": "background",
"paint": {
"background-color": "#ccc"
}
}, {
"id": "symbolA",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": "icon",
"icon-padding": [10]
}
}, {
"id": "symbolB",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": "icon",
"icon-padding": [2, 2, -20],
"icon-offset": [120, 0]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"network": [
[
"probeNetwork - default - end",
2,
2094
],
[
"probeNetwork - default - start",
0,
0
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"network": [
[
"probeNetwork - default - end",
2,
2094
],
[
"probeNetwork - default - start",
0,
0
]
]
}
Loading
Loading