Skip to content

Commit

Permalink
add two data converters
Browse files Browse the repository at this point in the history
this PR adds the UI to create converters in the editor, and includes the first two:
- a rounder that accepts a property for rounding
- a toString converter that can convert for now numbers and enum. It could be extended to colors and booleans in the future.

https://github.com/user-attachments/assets/2946d8ea-0fdf-4039-83d4-79b5fdfc6169

https://github.com/user-attachments/assets/ec60b523-ecb2-4453-9280-c99822706b2e

Diffs=
2c0927fc5 add two data converters (#7742)

Co-authored-by: hernan <hernan@rive.app>
  • Loading branch information
bodymovin and bodymovin committed Aug 9, 2024
1 parent b583801 commit 9e26b76
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8c7ac03294339ae2ad856b541c730dc16cec018d
2c0927fc5e9a5c420305a81446ac41eb5b896b2c
20 changes: 20 additions & 0 deletions dev/defs/data_bind/converters/data_converter_rounder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "DataConverterRounder",
"key": {
"int": 489,
"string": "dataconverterrounder"
},
"extends": "data_bind/converters/data_converter.json",
"properties": {
"decimals": {
"type": "uint",
"typeRuntime": "uint",
"initialValue": "0",
"key": {
"int": 669,
"string": "decimals"
},
"description": "Number of decimals to round to"
}
}
}
8 changes: 8 additions & 0 deletions dev/defs/data_bind/converters/data_converter_to_string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "DataConverterToString",
"key": {
"int": 490,
"string": "dataconvertertostring"
},
"extends": "data_bind/converters/data_converter.json"
}
6 changes: 4 additions & 2 deletions dev/defs/node.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"string": "xArtboard"
}
]
}
},
"bindable": true
},
"y": {
"type": "double",
Expand All @@ -38,7 +39,8 @@
"string": "yArtboard"
}
]
}
},
"bindable": true
},
"styleValue": {
"type": "uint",
Expand Down
16 changes: 16 additions & 0 deletions include/rive/data_bind/converters/data_converter_rounder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _RIVE_DATA_CONVERTER_ROUND_HPP_
#define _RIVE_DATA_CONVERTER_ROUND_HPP_
#include "rive/generated/data_bind/converters/data_converter_rounder_base.hpp"
#include "rive/data_bind/data_values/data_value.hpp"
#include <stdio.h>
namespace rive
{
class DataConverterRounder : public DataConverterRounderBase
{
public:
DataValue* convert(DataValue* value) override;
DataType outputType() override { return DataType::number; };
};
} // namespace rive

#endif
15 changes: 15 additions & 0 deletions include/rive/data_bind/converters/data_converter_to_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _RIVE_DATA_CONVERTER_TO_STRING_HPP_
#define _RIVE_DATA_CONVERTER_TO_STRING_HPP_
#include "rive/generated/data_bind/converters/data_converter_to_string_base.hpp"
#include <stdio.h>
namespace rive
{
class DataConverterToString : public DataConverterToStringBase
{
public:
DataValue* convert(DataValue* value) override;
DataType outputType() override { return DataType::string; };
};
} // namespace rive

#endif
14 changes: 14 additions & 0 deletions include/rive/generated/core_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
#include "rive/data_bind/bindable_property_number.hpp"
#include "rive/data_bind/bindable_property_string.hpp"
#include "rive/data_bind/converters/data_converter.hpp"
#include "rive/data_bind/converters/data_converter_rounder.hpp"
#include "rive/data_bind/converters/data_converter_to_string.hpp"
#include "rive/data_bind/data_bind.hpp"
#include "rive/data_bind/data_bind_context.hpp"
#include "rive/draw_rules.hpp"
Expand Down Expand Up @@ -451,6 +453,10 @@ class CoreRegistry
return new BindablePropertyBoolean();
case DataBindBase::typeKey:
return new DataBind();
case DataConverterRounderBase::typeKey:
return new DataConverterRounder();
case DataConverterToStringBase::typeKey:
return new DataConverterToString();
case DataBindContextBase::typeKey:
return new DataBindContext();
case BindablePropertyStringBase::typeKey:
Expand Down Expand Up @@ -1010,6 +1016,9 @@ class CoreRegistry
case DataBindBase::converterIdPropertyKey:
object->as<DataBindBase>()->converterId(value);
break;
case DataConverterRounderBase::decimalsPropertyKey:
object->as<DataConverterRounderBase>()->decimals(value);
break;
case BindablePropertyEnumBase::propertyValuePropertyKey:
object->as<BindablePropertyEnumBase>()->propertyValue(value);
break;
Expand Down Expand Up @@ -2034,6 +2043,8 @@ class CoreRegistry
return object->as<DataBindBase>()->flags();
case DataBindBase::converterIdPropertyKey:
return object->as<DataBindBase>()->converterId();
case DataConverterRounderBase::decimalsPropertyKey:
return object->as<DataConverterRounderBase>()->decimals();
case BindablePropertyEnumBase::propertyValuePropertyKey:
return object->as<BindablePropertyEnumBase>()->propertyValue();
case NestedArtboardLeafBase::fitPropertyKey:
Expand Down Expand Up @@ -2663,6 +2674,7 @@ class CoreRegistry
case DataBindBase::propertyKeyPropertyKey:
case DataBindBase::flagsPropertyKey:
case DataBindBase::converterIdPropertyKey:
case DataConverterRounderBase::decimalsPropertyKey:
case BindablePropertyEnumBase::propertyValuePropertyKey:
case NestedArtboardLeafBase::fitPropertyKey:
case WeightBase::valuesPropertyKey:
Expand Down Expand Up @@ -3239,6 +3251,8 @@ class CoreRegistry
return object->is<DataBindBase>();
case DataBindBase::converterIdPropertyKey:
return object->is<DataBindBase>();
case DataConverterRounderBase::decimalsPropertyKey:
return object->is<DataConverterRounderBase>();
case BindablePropertyEnumBase::propertyValuePropertyKey:
return object->is<BindablePropertyEnumBase>();
case NestedArtboardLeafBase::fitPropertyKey:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef _RIVE_DATA_CONVERTER_ROUNDER_BASE_HPP_
#define _RIVE_DATA_CONVERTER_ROUNDER_BASE_HPP_
#include "rive/core/field_types/core_uint_type.hpp"
#include "rive/data_bind/converters/data_converter.hpp"
namespace rive
{
class DataConverterRounderBase : public DataConverter
{
protected:
typedef DataConverter Super;

public:
static const uint16_t typeKey = 489;

/// Helper to quickly determine if a core object extends another without RTTI
/// at runtime.
bool isTypeOf(uint16_t typeKey) const override
{
switch (typeKey)
{
case DataConverterRounderBase::typeKey:
case DataConverterBase::typeKey:
return true;
default:
return false;
}
}

uint16_t coreType() const override { return typeKey; }

static const uint16_t decimalsPropertyKey = 669;

private:
uint32_t m_Decimals = 0;

public:
inline uint32_t decimals() const { return m_Decimals; }
void decimals(uint32_t value)
{
if (m_Decimals == value)
{
return;
}
m_Decimals = value;
decimalsChanged();
}

Core* clone() const override;
void copy(const DataConverterRounderBase& object)
{
m_Decimals = object.m_Decimals;
DataConverter::copy(object);
}

bool deserialize(uint16_t propertyKey, BinaryReader& reader) override
{
switch (propertyKey)
{
case decimalsPropertyKey:
m_Decimals = CoreUintType::deserialize(reader);
return true;
}
return DataConverter::deserialize(propertyKey, reader);
}

protected:
virtual void decimalsChanged() {}
};
} // namespace rive

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef _RIVE_DATA_CONVERTER_TO_STRING_BASE_HPP_
#define _RIVE_DATA_CONVERTER_TO_STRING_BASE_HPP_
#include "rive/data_bind/converters/data_converter.hpp"
namespace rive
{
class DataConverterToStringBase : public DataConverter
{
protected:
typedef DataConverter Super;

public:
static const uint16_t typeKey = 490;

/// Helper to quickly determine if a core object extends another without RTTI
/// at runtime.
bool isTypeOf(uint16_t typeKey) const override
{
switch (typeKey)
{
case DataConverterToStringBase::typeKey:
case DataConverterBase::typeKey:
return true;
default:
return false;
}
}

uint16_t coreType() const override { return typeKey; }

Core* clone() const override;

protected:
};
} // namespace rive

#endif
19 changes: 19 additions & 0 deletions src/data_bind/converters/data_converter_rounder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "rive/math/math_types.hpp"
#include "rive/data_bind/converters/data_converter_rounder.hpp"
#include "rive/data_bind/data_values/data_value_number.hpp"

using namespace rive;

DataValue* DataConverterRounder::convert(DataValue* input)
{
auto output = new DataValueNumber();
if (input->is<DataValueNumber>())
{
float value = input->as<DataValueNumber>()->value();
auto numberOfPlaces = decimals();
// TODO: @hernan review this way of rounding
float rounder = pow(10.0f, (float)numberOfPlaces);
output->value(std::round(value * rounder) / rounder);
}
return output;
}
36 changes: 36 additions & 0 deletions src/data_bind/converters/data_converter_to_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "rive/math/math_types.hpp"
#include "rive/data_bind/converters/data_converter_to_string.hpp"
#include "rive/data_bind/data_values/data_value_number.hpp"
#include "rive/data_bind/data_values/data_value_enum.hpp"
#include "rive/data_bind/data_values/data_value_string.hpp"

using namespace rive;

DataValue* DataConverterToString::convert(DataValue* input)
{
auto output = new DataValueString();
if (input->is<DataValueNumber>())
{
float value = input->as<DataValueNumber>()->value();
std::string str = std::to_string(value);
if (str.find('.') != std::string::npos)
{
// Remove trailing zeroes
str = str.substr(0, str.find_last_not_of('0') + 1);
// If the decimal point is now the last character, remove that as well
if (str.find('.') == str.size() - 1)
{
str = str.substr(0, str.size() - 1);
}
}
output->value(str);
}
else if (input->is<DataValueEnum>())
{
auto dataEnum = input->as<DataValueEnum>()->dataEnum();
auto index = input->as<DataValueEnum>()->value();
auto enumValue = dataEnum->value(index);
output->value(enumValue);
}
return output;
}
11 changes: 11 additions & 0 deletions src/generated/data_bind/converters/data_converter_rounder_base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "rive/generated/data_bind/converters/data_converter_rounder_base.hpp"
#include "rive/data_bind/converters/data_converter_rounder.hpp"

using namespace rive;

Core* DataConverterRounderBase::clone() const
{
auto cloned = new DataConverterRounder();
cloned->copy(*this);
return cloned;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "rive/generated/data_bind/converters/data_converter_to_string_base.hpp"
#include "rive/data_bind/converters/data_converter_to_string.hpp"

using namespace rive;

Core* DataConverterToStringBase::clone() const
{
auto cloned = new DataConverterToString();
cloned->copy(*this);
return cloned;
}

0 comments on commit 9e26b76

Please sign in to comment.