more refactoring and fallback values

This commit is contained in:
Konstantin Käfer
2014-06-27 16:59:42 +02:00
parent d75f0075f3
commit 0e9e297275
9 changed files with 228 additions and 128 deletions

View File

@@ -0,0 +1,41 @@
#ifndef LLMR_STYLE_APPLIED_CLASS_PROPERTIES
#define LLMR_STYLE_APPLIED_CLASS_PROPERTIES
#include <llmr/style/property_value.hpp>
#include <llmr/style/class_dictionary.hpp>
#include <llmr/util/time.hpp>
#include <list>
namespace llmr {
class AppliedClassProperty {
public:
AppliedClassProperty(ClassID class_id, timestamp begin, timestamp end, const PropertyValue &value);
public:
ClassID name;
timestamp begin;
timestamp end;
PropertyValue value;
};
class AppliedClassProperties {
public:
std::list<AppliedClassProperty> properties;
public:
// Returns thie ID of the most recent
ClassID mostRecent() const {
return properties.size() ? properties.back().name : ClassID::Fallback;
}
void add(ClassID class_id, timestamp begin, timestamp end, const PropertyValue &value) {
properties.emplace_back(class_id, begin, end, value);
}
};
}
#endif

View File

@@ -1,78 +1,13 @@
#ifndef LLMR_STYLE_CLASS_PROPERTIES
#define LLMR_STYLE_CLASS_PROPERTIES
#include <llmr/util/variant.hpp>
#include <llmr/style/function_properties.hpp>
#include <llmr/style/types.hpp>
#include <llmr/style/property_key.hpp>
#include <llmr/style/property_value.hpp>
#include <map>
namespace llmr {
enum class ClassPropertyKey {
FillEnabled,
FillAntialias,
FillOpacity,
FillColor,
FillOutlineColor,
FillTranslate, // for transitions only
FillTranslateX,
FillTranslateY,
FillTranslateAnchor,
FillImage,
LineEnabled,
LineOpacity,
LineColor,
LineTranslate, // for transitions only
LineTranslateX,
LineTranslateY,
LineTranslateAnchor,
LineWidth,
LineOffset,
LineBlur,
LineDashArray, // for transitions only
LineDashLand,
LineDashGap,
LineImage,
IconEnabled,
IconOpacity,
IconRotate,
IconRotateAnchor,
TextEnabled,
TextOpacity,
TextSize,
TextColor,
TextHaloColor,
TextHaloWidth,
TextHaloBlur,
CompositeEnabled,
CompositeOpacity,
RasterSpin,
RasterBrightnessLow,
RasterBrightnessHigh,
RasterSaturation,
RasterContrast,
RasterFade,
BackgroundColor
};
typedef util::variant<
FunctionProperty,
TranslateAnchorType,
RotateAnchorType,
Color,
std::string,
bool
> ClassPropertyValue;
struct ClassPropertyTransition {
uint16_t duration = 0;
uint16_t delay = 0;
@@ -93,7 +28,7 @@ public:
transitions.emplace(::std::forward<Args>(args)...);
}
inline const ClassPropertyTransition &getTransition(ClassPropertyKey key, const ClassPropertyTransition &defaultTransition) const {
inline const ClassPropertyTransition &getTransition(PropertyKey key, const ClassPropertyTransition &defaultTransition) const {
auto it = transitions.find(key);
if (it == transitions.end()) {
return defaultTransition;
@@ -103,16 +38,16 @@ public:
}
// Route-through iterable interface so that you can iterate on the object as is.
inline std::map<ClassPropertyKey, ClassPropertyValue>::const_iterator begin() const {
inline std::map<PropertyKey, PropertyValue>::const_iterator begin() const {
return properties.begin();
}
inline std::map<ClassPropertyKey, ClassPropertyValue>::const_iterator end() const {
inline std::map<PropertyKey, PropertyValue>::const_iterator end() const {
return properties.end();
}
public:
std::map<ClassPropertyKey, ClassPropertyValue> properties;
std::map<ClassPropertyKey, ClassPropertyTransition> transitions;
std::map<PropertyKey, PropertyValue> properties;
std::map<PropertyKey, ClassPropertyTransition> transitions;
};
}

View File

@@ -0,0 +1,29 @@
#ifndef LLMR_STYLE_PROPERTY_FALLBACK
#define LLMR_STYLE_PROPERTY_FALLBACK
#include <llmr/style/property_key.hpp>
#include <llmr/style/property_value.hpp>
#include <map>
namespace llmr {
class PropertyFallbackValue {
public:
static const PropertyValue &Get(PropertyKey key) {
auto it = properties.find(key);
if (it != properties.end()) {
return it->second;
} else {
return defaultProperty;
}
}
private:
static const std::map<PropertyKey, PropertyValue> properties;
static const PropertyValue defaultProperty;
};
}
#endif

View File

@@ -0,0 +1,61 @@
#ifndef LLMR_STYLE_PROPERTY_KEY
#define LLMR_STYLE_PROPERTY_KEY
namespace llmr {
enum class PropertyKey {
FillEnabled,
FillAntialias,
FillOpacity,
FillColor,
FillOutlineColor,
FillTranslate, // for transitions only
FillTranslateX,
FillTranslateY,
FillTranslateAnchor,
FillImage,
LineEnabled,
LineOpacity,
LineColor,
LineTranslate, // for transitions only
LineTranslateX,
LineTranslateY,
LineTranslateAnchor,
LineWidth,
LineOffset,
LineBlur,
LineDashArray, // for transitions only
LineDashLand,
LineDashGap,
LineImage,
IconEnabled,
IconOpacity,
IconRotate,
IconRotateAnchor,
TextEnabled,
TextOpacity,
TextSize,
TextColor,
TextHaloColor,
TextHaloWidth,
TextHaloBlur,
CompositeEnabled,
CompositeOpacity,
RasterSpin,
RasterBrightnessLow,
RasterBrightnessHigh,
RasterSaturation,
RasterContrast,
RasterFade,
BackgroundColor
};
}
#endif

View File

@@ -0,0 +1,21 @@
#ifndef LLMR_STYLE_PROPERTY_VALUE
#define LLMR_STYLE_PROPERTY_VALUE
#include <llmr/util/variant.hpp>
#include <llmr/style/function_properties.hpp>
#include <llmr/style/types.hpp>
namespace llmr {
typedef util::variant<
FunctionProperty,
TranslateAnchorType,
RotateAnchorType,
Color,
std::string,
bool
> PropertyValue;
}
#endif

View File

@@ -52,9 +52,9 @@ private:
// Parses optional properties into style class properties.
template <typename T>
bool parseStyleProperty(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value);
bool parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value);
template <typename T>
bool parseStyleProperty(const char *property_name, const std::vector<ClassPropertyKey> &keys, ClassProperties &klass, JSVal value);
bool parseStyleProperty(const char *property_name, const std::vector<PropertyKey> &keys, ClassProperties &klass, JSVal value);
FilterExpression parseFilter(JSVal, FilterExpression::Operator op);

View File

@@ -0,0 +1,13 @@
#include <llmr/style/property_fallback.hpp>
namespace llmr {
const std::map<PropertyKey, PropertyValue> PropertyFallbackValue::properties = {
{ PropertyKey::LineEnabled, true },
{ PropertyKey::LineOpacity, 1.0f },
{ PropertyKey::LineColor, Color({{ 0, 0, 0, 1 }}) },
};
const PropertyValue PropertyFallbackValue::defaultProperty;
}

View File

@@ -59,10 +59,10 @@ StyleProperties resetClassProperties(StyleLayer &layer) {
}
template <typename T>
void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &target) {
void applyProperty(const ClassProperties &properties, PropertyKey key, T &target) {
auto it = properties.properties.find(key);
if (it != properties.properties.end()) {
const ClassPropertyValue &value = it->second;
const PropertyValue &value = it->second;
if (value.is<T>()) {
target = value.get<T>();
}
@@ -70,10 +70,10 @@ void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &t
}
template <typename T>
void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &target, float z) {
void applyProperty(const ClassProperties &properties, PropertyKey key, T &target, float z) {
auto it = properties.properties.find(key);
if (it != properties.properties.end()) {
const ClassPropertyValue &value = it->second;
const PropertyValue &value = it->second;
if (value.is<FunctionProperty>()) {
target = value.get<FunctionProperty>().evaluate<T>(z);
} else if (value.is<bool>()) {
@@ -85,59 +85,59 @@ void applyProperty(const ClassProperties &properties, ClassPropertyKey key, T &t
void applyClassProperties(StyleProperties &style, const ClassProperties &properties, float z) {
if (style.is<FillProperties>()) {
FillProperties &fill = style.get<FillProperties>();
applyProperty(properties, ClassPropertyKey::FillEnabled, fill.enabled, z);
applyProperty(properties, ClassPropertyKey::FillAntialias, fill.antialias, z);
applyProperty(properties, ClassPropertyKey::FillOpacity, fill.opacity, z);
applyProperty(properties, ClassPropertyKey::FillColor, fill.fill_color);
applyProperty(properties, ClassPropertyKey::FillOutlineColor, fill.stroke_color);
applyProperty(properties, ClassPropertyKey::FillTranslateX, fill.translate[0], z);
applyProperty(properties, ClassPropertyKey::FillTranslateY, fill.translate[1], z);
applyProperty(properties, ClassPropertyKey::FillTranslateAnchor, fill.translateAnchor);
applyProperty(properties, ClassPropertyKey::FillImage, fill.image);
applyProperty(properties, PropertyKey::FillEnabled, fill.enabled, z);
applyProperty(properties, PropertyKey::FillAntialias, fill.antialias, z);
applyProperty(properties, PropertyKey::FillOpacity, fill.opacity, z);
applyProperty(properties, PropertyKey::FillColor, fill.fill_color);
applyProperty(properties, PropertyKey::FillOutlineColor, fill.stroke_color);
applyProperty(properties, PropertyKey::FillTranslateX, fill.translate[0], z);
applyProperty(properties, PropertyKey::FillTranslateY, fill.translate[1], z);
applyProperty(properties, PropertyKey::FillTranslateAnchor, fill.translateAnchor);
applyProperty(properties, PropertyKey::FillImage, fill.image);
} else if (style.is<LineProperties>()) {
LineProperties &line = style.get<LineProperties>();
applyProperty(properties, ClassPropertyKey::LineEnabled, line.enabled, z);
applyProperty(properties, ClassPropertyKey::LineOpacity, line.opacity, z);
applyProperty(properties, ClassPropertyKey::LineColor, line.color);
applyProperty(properties, ClassPropertyKey::LineTranslateX, line.translate[0], z);
applyProperty(properties, ClassPropertyKey::LineTranslateY, line.translate[1], z);
applyProperty(properties, ClassPropertyKey::LineTranslateAnchor, line.translateAnchor);
applyProperty(properties, ClassPropertyKey::LineWidth, line.width, z);
applyProperty(properties, ClassPropertyKey::LineOffset, line.offset, z);
applyProperty(properties, ClassPropertyKey::LineBlur, line.blur, z);
applyProperty(properties, ClassPropertyKey::LineDashLand, line.dash_array[0], z);
applyProperty(properties, ClassPropertyKey::LineDashGap, line.dash_array[1], z);
applyProperty(properties, ClassPropertyKey::LineImage, line.image);
applyProperty(properties, PropertyKey::LineEnabled, line.enabled, z);
applyProperty(properties, PropertyKey::LineOpacity, line.opacity, z);
applyProperty(properties, PropertyKey::LineColor, line.color);
applyProperty(properties, PropertyKey::LineTranslateX, line.translate[0], z);
applyProperty(properties, PropertyKey::LineTranslateY, line.translate[1], z);
applyProperty(properties, PropertyKey::LineTranslateAnchor, line.translateAnchor);
applyProperty(properties, PropertyKey::LineWidth, line.width, z);
applyProperty(properties, PropertyKey::LineOffset, line.offset, z);
applyProperty(properties, PropertyKey::LineBlur, line.blur, z);
applyProperty(properties, PropertyKey::LineDashLand, line.dash_array[0], z);
applyProperty(properties, PropertyKey::LineDashGap, line.dash_array[1], z);
applyProperty(properties, PropertyKey::LineImage, line.image);
} else if (style.is<IconProperties>()) {
IconProperties &icon = style.get<IconProperties>();
applyProperty(properties, ClassPropertyKey::IconEnabled, icon.enabled, z);
applyProperty(properties, ClassPropertyKey::IconOpacity, icon.opacity, z);
applyProperty(properties, ClassPropertyKey::IconRotate, icon.rotate, z);
applyProperty(properties, ClassPropertyKey::IconRotateAnchor, icon.rotate_anchor);
applyProperty(properties, PropertyKey::IconEnabled, icon.enabled, z);
applyProperty(properties, PropertyKey::IconOpacity, icon.opacity, z);
applyProperty(properties, PropertyKey::IconRotate, icon.rotate, z);
applyProperty(properties, PropertyKey::IconRotateAnchor, icon.rotate_anchor);
} else if (style.is<TextProperties>()) {
TextProperties &text = style.get<TextProperties>();
applyProperty(properties, ClassPropertyKey::TextEnabled, text.enabled, z);
applyProperty(properties, ClassPropertyKey::TextOpacity, text.opacity, z);
applyProperty(properties, ClassPropertyKey::TextSize, text.size, z);
applyProperty(properties, ClassPropertyKey::TextColor, text.color);
applyProperty(properties, ClassPropertyKey::TextHaloColor, text.halo_color);
applyProperty(properties, ClassPropertyKey::TextHaloWidth, text.halo_width, z);
applyProperty(properties, ClassPropertyKey::TextHaloBlur, text.halo_blur, z);
applyProperty(properties, PropertyKey::TextEnabled, text.enabled, z);
applyProperty(properties, PropertyKey::TextOpacity, text.opacity, z);
applyProperty(properties, PropertyKey::TextSize, text.size, z);
applyProperty(properties, PropertyKey::TextColor, text.color);
applyProperty(properties, PropertyKey::TextHaloColor, text.halo_color);
applyProperty(properties, PropertyKey::TextHaloWidth, text.halo_width, z);
applyProperty(properties, PropertyKey::TextHaloBlur, text.halo_blur, z);
} else if (style.is<CompositeProperties>()) {
CompositeProperties &composite = style.get<CompositeProperties>();
applyProperty(properties, ClassPropertyKey::CompositeEnabled, composite.enabled, z);
applyProperty(properties, ClassPropertyKey::CompositeOpacity, composite.opacity, z);
applyProperty(properties, PropertyKey::CompositeEnabled, composite.enabled, z);
applyProperty(properties, PropertyKey::CompositeOpacity, composite.opacity, z);
} else if (style.is<RasterProperties>()) {
RasterProperties &raster = style.get<RasterProperties>();
applyProperty(properties, ClassPropertyKey::RasterSpin, raster.spin, z);
applyProperty(properties, ClassPropertyKey::RasterBrightnessLow, raster.brightness[0], z);
applyProperty(properties, ClassPropertyKey::RasterBrightnessHigh, raster.brightness[1], z);
applyProperty(properties, ClassPropertyKey::RasterSaturation, raster.saturation, z);
applyProperty(properties, ClassPropertyKey::RasterContrast, raster.contrast, z);
applyProperty(properties, ClassPropertyKey::RasterFade, raster.fade, z);
applyProperty(properties, PropertyKey::RasterSpin, raster.spin, z);
applyProperty(properties, PropertyKey::RasterBrightnessLow, raster.brightness[0], z);
applyProperty(properties, PropertyKey::RasterBrightnessHigh, raster.brightness[1], z);
applyProperty(properties, PropertyKey::RasterSaturation, raster.saturation, z);
applyProperty(properties, PropertyKey::RasterContrast, raster.contrast, z);
applyProperty(properties, PropertyKey::RasterFade, raster.fade, z);
} else if (style.is<BackgroundProperties>()) {
BackgroundProperties &background = style.get<BackgroundProperties>();
applyProperty(properties, ClassPropertyKey::BackgroundColor, background.color);
applyProperty(properties, PropertyKey::BackgroundColor, background.color);
}
}

View File

@@ -172,7 +172,7 @@ void StyleParser::parseSources(JSVal value) {
#pragma mark - Parse Style Properties
template<> bool StyleParser::parseStyleProperty<bool>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template<> bool StyleParser::parseStyleProperty<bool>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -188,7 +188,7 @@ template<> bool StyleParser::parseStyleProperty<bool>(const char *property_name,
}
template<> bool StyleParser::parseStyleProperty<std::string>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template<> bool StyleParser::parseStyleProperty<std::string>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -203,7 +203,7 @@ template<> bool StyleParser::parseStyleProperty<std::string>(const char *propert
return true;
}
template<> bool StyleParser::parseStyleProperty<TranslateAnchorType>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template<> bool StyleParser::parseStyleProperty<TranslateAnchorType>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -219,7 +219,7 @@ template<> bool StyleParser::parseStyleProperty<TranslateAnchorType>(const char
}
template<> bool StyleParser::parseStyleProperty<RotateAnchorType>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template<> bool StyleParser::parseStyleProperty<RotateAnchorType>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -234,7 +234,7 @@ template<> bool StyleParser::parseStyleProperty<RotateAnchorType>(const char *pr
return true;
}
template<> bool StyleParser::parseStyleProperty<Color>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template<> bool StyleParser::parseStyleProperty<Color>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -257,7 +257,7 @@ template<> bool StyleParser::parseStyleProperty<Color>(const char *property_name
return true;
}
template <> bool StyleParser::parseStyleProperty<ClassPropertyTransition>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template <> bool StyleParser::parseStyleProperty<ClassPropertyTransition>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -305,7 +305,7 @@ FunctionProperty::fn parseFunctionType(JSVal type) {
}
}
template<> bool StyleParser::parseStyleProperty<FunctionProperty>(const char *property_name, ClassPropertyKey key, ClassProperties &klass, JSVal value) {
template<> bool StyleParser::parseStyleProperty<FunctionProperty>(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) {
if (!value.HasMember(property_name)) {
return false;
}
@@ -358,7 +358,7 @@ template<> bool StyleParser::parseStyleProperty<FunctionProperty>(const char *pr
}
template <typename T>
bool StyleParser::parseStyleProperty(const char *property_name, const std::vector<ClassPropertyKey> &keys, ClassProperties &klass, JSVal value) {
bool StyleParser::parseStyleProperty(const char *property_name, const std::vector<PropertyKey> &keys, ClassProperties &klass, JSVal value) {
if (value.HasMember(property_name)) {
JSVal rvalue = replaceConstant(value[property_name]);
if (!rvalue.IsArray()) {
@@ -478,7 +478,7 @@ void StyleParser::parseStyles(JSVal value, std::shared_ptr<StyleLayer> &layer) {
}
void StyleParser::parseStyle(JSVal value, ClassProperties &klass) {
using Key = ClassPropertyKey;
using Key = PropertyKey;
parseStyleProperty<FunctionProperty>("fill-enabled", Key::FillEnabled, klass, value);
parseStyleProperty<ClassPropertyTransition>("fill-enabled-transition", Key::FillEnabled, klass, value);