Skip to content

Commit

Permalink
add arithmetic operation and group converters
Browse files Browse the repository at this point in the history
two new converters:
- arithmetic operation, that applies a basic arithmetic operation to an input
- group converter: this is a special converter that is used to apply a sequence of converters to a data bound property.

https://github.com/user-attachments/assets/50a0226c-343f-4252-923b-4f0c51ca6c8b

Diffs=
ad34dd4da add arithmetic operation and group converters (#7801)

Co-authored-by: hernan <hernan@rive.app>
  • Loading branch information
bodymovin and bodymovin committed Aug 19, 2024
1 parent 215de97 commit f0647c1
Show file tree
Hide file tree
Showing 23 changed files with 589 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f99c93181b2073db07b0c54f766648ce56c89df9
ad34dd4dae54aa071ca80c457e375c015b9497a8
8 changes: 8 additions & 0 deletions dev/defs/data_bind/converters/data_converter_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "DataConverterGroup",
"key": {
"int": 499,
"string": "dataconvertergroup"
},
"extends": "data_bind/converters/data_converter.json"
}
39 changes: 39 additions & 0 deletions dev/defs/data_bind/converters/data_converter_group_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "DataConverterGroupItem",
"key": {
"int": 498,
"string": "dataconvertergroupitem"
},
"properties": {
"order": {
"type": "FractionalIndex",
"initialValue": "FractionalIndex.invalid",
"key": {
"int": 678,
"string": "order"
},
"runtime": false
},
"converterId": {
"type": "Id",
"typeRuntime": "uint",
"initialValue": "Core.missingId",
"initialValueRuntime": "-1",
"key": {
"int": 679,
"string": "converterid"
},
"description": "Id of the converter"
},
"groupId": {
"type": "Id",
"initialValue": "Core.missingId",
"key": {
"int": 680,
"string": "groupid"
},
"description": "Id of the group this item belongs to",
"runtime": false
}
}
}
28 changes: 28 additions & 0 deletions dev/defs/data_bind/converters/data_converter_operation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "DataConverterOperation",
"key": {
"int": 500,
"string": "dataconverteroperation"
},
"extends": "data_bind/converters/data_converter.json",
"properties": {
"value": {
"type": "double",
"initialValue": "1",
"key": {
"int": 681,
"string": "value"
},
"description": "Number to multiply the input to"
},
"operationType": {
"type": "uint",
"initialValue": "0",
"key": {
"int": 682,
"string": "operationtype"
},
"description": "The operation tu use for the input and value"
}
}
}
16 changes: 16 additions & 0 deletions include/rive/animation/arithmetic_operation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _RIVE_ARITHMETIC_OPERATION_HPP_
#define _RIVE_ARITHMETIC_OPERATION_HPP_

namespace rive
{
enum class ArithmeticOperation : int
{
add = 0,
subtract = 1,
multiply = 2,
divide = 3,
modulo = 4,
};
} // namespace rive

#endif
28 changes: 28 additions & 0 deletions include/rive/data_bind/converters/data_converter_group.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef _RIVE_DATA_CONVERTER_GROUP_HPP_
#define _RIVE_DATA_CONVERTER_GROUP_HPP_
#include "rive/generated/data_bind/converters/data_converter_group_base.hpp"
#include "rive/data_bind/converters/data_converter_group_item.hpp"
#include <stdio.h>
namespace rive
{
class DataConverterGroup : public DataConverterGroupBase
{
public:
DataValue* convert(DataValue* value) override;
DataValue* reverseConvert(DataValue* value) override;
void addItem(DataConverterGroupItem* item);
DataType outputType() override
{
if (m_items.size() > 0)
{
return m_items.back()->converter()->outputType();
};
return Super::outputType();
}

private:
std::vector<DataConverterGroupItem*> m_items;
};
} // namespace rive

#endif
20 changes: 20 additions & 0 deletions include/rive/data_bind/converters/data_converter_group_item.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _RIVE_DATA_CONVERTER_GROUP_ITEM_HPP_
#define _RIVE_DATA_CONVERTER_GROUP_ITEM_HPP_
#include "rive/generated/data_bind/converters/data_converter_group_item_base.hpp"
#include "rive/data_bind/converters/data_converter.hpp"
#include <stdio.h>
namespace rive
{
class DataConverterGroupItem : public DataConverterGroupItemBase
{
public:
StatusCode import(ImportStack& importStack) override;
DataConverter* converter() const { return m_dataConverter; };
void converter(DataConverter* value) { m_dataConverter = value; };

protected:
DataConverter* m_dataConverter;
};
} // namespace rive

#endif
18 changes: 18 additions & 0 deletions include/rive/data_bind/converters/data_converter_operation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _RIVE_DATA_CONVERTER_OPERATION_HPP_
#define _RIVE_DATA_CONVERTER_OPERATION_HPP_
#include "rive/generated/data_bind/converters/data_converter_operation_base.hpp"
#include "rive/animation/arithmetic_operation.hpp"
#include <stdio.h>
namespace rive
{
class DataConverterOperation : public DataConverterOperationBase
{
public:
DataValue* convert(DataValue* value) override;
DataValue* reverseConvert(DataValue* value) override;
DataType outputType() override { return DataType::number; };
ArithmeticOperation op() const { return (ArithmeticOperation)operationType(); }
};
} // namespace rive

#endif
33 changes: 33 additions & 0 deletions include/rive/generated/core_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
#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_group.hpp"
#include "rive/data_bind/converters/data_converter_group_item.hpp"
#include "rive/data_bind/converters/data_converter_operation.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"
Expand Down Expand Up @@ -472,8 +475,14 @@ class CoreRegistry
return new BindablePropertyBoolean();
case DataBindBase::typeKey:
return new DataBind();
case DataConverterGroupItemBase::typeKey:
return new DataConverterGroupItem();
case DataConverterGroupBase::typeKey:
return new DataConverterGroup();
case DataConverterRounderBase::typeKey:
return new DataConverterRounder();
case DataConverterOperationBase::typeKey:
return new DataConverterOperation();
case DataConverterToStringBase::typeKey:
return new DataConverterToString();
case DataBindContextBase::typeKey:
Expand Down Expand Up @@ -1047,9 +1056,15 @@ class CoreRegistry
case DataBindBase::converterIdPropertyKey:
object->as<DataBindBase>()->converterId(value);
break;
case DataConverterGroupItemBase::converterIdPropertyKey:
object->as<DataConverterGroupItemBase>()->converterId(value);
break;
case DataConverterRounderBase::decimalsPropertyKey:
object->as<DataConverterRounderBase>()->decimals(value);
break;
case DataConverterOperationBase::operationTypePropertyKey:
object->as<DataConverterOperationBase>()->operationType(value);
break;
case BindablePropertyEnumBase::propertyValuePropertyKey:
object->as<BindablePropertyEnumBase>()->propertyValue(value);
break;
Expand Down Expand Up @@ -1589,6 +1604,9 @@ class CoreRegistry
case JoystickBase::heightPropertyKey:
object->as<JoystickBase>()->height(value);
break;
case DataConverterOperationBase::valuePropertyKey:
object->as<DataConverterOperationBase>()->value(value);
break;
case BindablePropertyNumberBase::propertyValuePropertyKey:
object->as<BindablePropertyNumberBase>()->propertyValue(value);
break;
Expand Down Expand Up @@ -2085,8 +2103,12 @@ class CoreRegistry
return object->as<DataBindBase>()->flags();
case DataBindBase::converterIdPropertyKey:
return object->as<DataBindBase>()->converterId();
case DataConverterGroupItemBase::converterIdPropertyKey:
return object->as<DataConverterGroupItemBase>()->converterId();
case DataConverterRounderBase::decimalsPropertyKey:
return object->as<DataConverterRounderBase>()->decimals();
case DataConverterOperationBase::operationTypePropertyKey:
return object->as<DataConverterOperationBase>()->operationType();
case BindablePropertyEnumBase::propertyValuePropertyKey:
return object->as<BindablePropertyEnumBase>()->propertyValue();
case NestedArtboardLeafBase::fitPropertyKey:
Expand Down Expand Up @@ -2456,6 +2478,8 @@ class CoreRegistry
return object->as<JoystickBase>()->width();
case JoystickBase::heightPropertyKey:
return object->as<JoystickBase>()->height();
case DataConverterOperationBase::valuePropertyKey:
return object->as<DataConverterOperationBase>()->value();
case BindablePropertyNumberBase::propertyValuePropertyKey:
return object->as<BindablePropertyNumberBase>()->propertyValue();
case NestedArtboardLeafBase::alignmentXPropertyKey:
Expand Down Expand Up @@ -2722,7 +2746,9 @@ class CoreRegistry
case DataBindBase::propertyKeyPropertyKey:
case DataBindBase::flagsPropertyKey:
case DataBindBase::converterIdPropertyKey:
case DataConverterGroupItemBase::converterIdPropertyKey:
case DataConverterRounderBase::decimalsPropertyKey:
case DataConverterOperationBase::operationTypePropertyKey:
case BindablePropertyEnumBase::propertyValuePropertyKey:
case NestedArtboardLeafBase::fitPropertyKey:
case WeightBase::valuesPropertyKey:
Expand Down Expand Up @@ -2901,6 +2927,7 @@ class CoreRegistry
case JoystickBase::originYPropertyKey:
case JoystickBase::widthPropertyKey:
case JoystickBase::heightPropertyKey:
case DataConverterOperationBase::valuePropertyKey:
case BindablePropertyNumberBase::propertyValuePropertyKey:
case NestedArtboardLeafBase::alignmentXPropertyKey:
case NestedArtboardLeafBase::alignmentYPropertyKey:
Expand Down Expand Up @@ -3308,8 +3335,12 @@ class CoreRegistry
return object->is<DataBindBase>();
case DataBindBase::converterIdPropertyKey:
return object->is<DataBindBase>();
case DataConverterGroupItemBase::converterIdPropertyKey:
return object->is<DataConverterGroupItemBase>();
case DataConverterRounderBase::decimalsPropertyKey:
return object->is<DataConverterRounderBase>();
case DataConverterOperationBase::operationTypePropertyKey:
return object->is<DataConverterOperationBase>();
case BindablePropertyEnumBase::propertyValuePropertyKey:
return object->is<BindablePropertyEnumBase>();
case NestedArtboardLeafBase::fitPropertyKey:
Expand Down Expand Up @@ -3658,6 +3689,8 @@ class CoreRegistry
return object->is<JoystickBase>();
case JoystickBase::heightPropertyKey:
return object->is<JoystickBase>();
case DataConverterOperationBase::valuePropertyKey:
return object->is<DataConverterOperationBase>();
case BindablePropertyNumberBase::propertyValuePropertyKey:
return object->is<BindablePropertyNumberBase>();
case NestedArtboardLeafBase::alignmentXPropertyKey:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef _RIVE_DATA_CONVERTER_GROUP_BASE_HPP_
#define _RIVE_DATA_CONVERTER_GROUP_BASE_HPP_
#include "rive/data_bind/converters/data_converter.hpp"
namespace rive
{
class DataConverterGroupBase : public DataConverter
{
protected:
typedef DataConverter Super;

public:
static const uint16_t typeKey = 499;

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

uint16_t coreType() const override { return typeKey; }

Core* clone() const override;

protected:
};
} // namespace rive

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef _RIVE_DATA_CONVERTER_GROUP_ITEM_BASE_HPP_
#define _RIVE_DATA_CONVERTER_GROUP_ITEM_BASE_HPP_
#include "rive/core.hpp"
#include "rive/core/field_types/core_uint_type.hpp"
namespace rive
{
class DataConverterGroupItemBase : public Core
{
protected:
typedef Core Super;

public:
static const uint16_t typeKey = 498;

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

uint16_t coreType() const override { return typeKey; }

static const uint16_t converterIdPropertyKey = 679;

private:
uint32_t m_ConverterId = -1;

public:
inline uint32_t converterId() const { return m_ConverterId; }
void converterId(uint32_t value)
{
if (m_ConverterId == value)
{
return;
}
m_ConverterId = value;
converterIdChanged();
}

Core* clone() const override;
void copy(const DataConverterGroupItemBase& object) { m_ConverterId = object.m_ConverterId; }

bool deserialize(uint16_t propertyKey, BinaryReader& reader) override
{
switch (propertyKey)
{
case converterIdPropertyKey:
m_ConverterId = CoreUintType::deserialize(reader);
return true;
}
return false;
}

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

#endif
Loading

0 comments on commit f0647c1

Please sign in to comment.