Skip to content

Commit

Permalink
add data converter and data types for conversion
Browse files Browse the repository at this point in the history
First PR for converters support.
It adds the abstract data_converter class
And adds new data value objects that can be passed between converters to easily transform one type of data to another

https://github.com/user-attachments/assets/e8a0fdee-8845-4d0d-a894-c948c2b0a209

Diffs=
4bf7c7545 add data converter and data types for conversion (#7734)

Co-authored-by: hernan <[email protected]>
  • Loading branch information
bodymovin and bodymovin committed Aug 3, 2024
1 parent 4afdae6 commit 62025f9
Show file tree
Hide file tree
Showing 34 changed files with 684 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d9f5701ec10c0d0eecb0e3d2f5b0ee80499c746d
4bf7c75453f0fc3fec1519a9a3dfe7a4ed63a054
30 changes: 30 additions & 0 deletions dev/defs/data_bind/converters/data_converter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "DataConverter",
"key": {
"int": 488,
"string": "dataconverter"
},
"abstract": true,
"properties": {
"order": {
"type": "FractionalIndex",
"initialValue": "FractionalIndex.invalid",
"initialValueRuntime": "0",
"key": {
"int": 661,
"string": "order"
},
"description": "Order value for sorting data converters",
"runtime": false
},
"name": {
"type": "String",
"initialValue": "''",
"key": {
"int": 662,
"string": "name"
},
"description": "Non-unique identifier, used to give friendly names to data converters."
}
}
}
11 changes: 11 additions & 0 deletions dev/defs/data_bind/data_bind.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
"int": 587,
"string": "flags"
}
},
"converterId": {
"type": "Id",
"typeRuntime": "uint",
"initialValue": "Core.missingId",
"initialValueRuntime": "-1",
"key": {
"int": 660,
"string": "converterid"
},
"description": "Identifier used to link to a data converter."
}
}
}
1 change: 1 addition & 0 deletions include/rive/artboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class Artboard : public ArtboardBase, public CoreContext
{
auto dataBindClone = static_cast<DataBind*>(dataBind->clone());
dataBindClone->target(cloneObjects.back());
dataBindClone->converter(dataBind->converter());
artboardClone->m_DataBinds.push_back(dataBindClone);
}
}
Expand Down
37 changes: 32 additions & 5 deletions include/rive/data_bind/context/context_value.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
#ifndef _RIVE_DATA_BIND_CONTEXT_VALUE_HPP_
#define _RIVE_DATA_BIND_CONTEXT_VALUE_HPP_
#include "rive/refcnt.hpp"
#include "rive/viewmodel/viewmodel_instance_value.hpp"
#include "rive/data_bind/converters/data_converter.hpp"
#include <stdio.h>
namespace rive
{
class DataBindContextValue
{
protected:
ViewModelInstanceValue* m_Source;
ViewModelInstanceValue* m_source;
DataConverter* m_converter;
DataValue* m_dataValue;

public:
DataBindContextValue(){};
DataBindContextValue(ViewModelInstanceValue* source, DataConverter* converter);
virtual ~DataBindContextValue(){};
virtual void applyToSource(Core* component, uint32_t propertyKey){};
virtual void apply(Core* component, uint32_t propertyKey){};
virtual void applyToSource(Core* component, uint32_t propertyKey, bool isMainDirection);
virtual void apply(Core* component, uint32_t propertyKey, bool isMainDirection){};
virtual void update(Core* component){};
virtual DataValue* getTargetValue(Core* target, uint32_t propertyKey) { return nullptr; };
void updateSourceValue();
template <typename T = DataValue, typename U> U getDataValue(DataValue* input)
{
auto dataValue = m_converter != nullptr ? m_converter->convert(input) : input;
if (dataValue->is<T>())
{
return dataValue->as<T>()->value();
}
return (new T())->value();
};
template <typename T = DataValue, typename U> U getReverseDataValue(DataValue* input)
{
auto dataValue = m_converter != nullptr ? m_converter->reverseConvert(input) : input;
if (dataValue->is<T>())
{
return dataValue->as<T>()->value();
}
return (new T())->value();
};
template <typename T = DataValue, typename U>
U calculateValue(DataValue* input, bool isMainDirection)
{
return isMainDirection ? getDataValue<T, U>(input) : getReverseDataValue<T, U>(input);
};
};
} // namespace rive

Expand Down
9 changes: 3 additions & 6 deletions include/rive/data_bind/context/context_value_boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ class DataBindContextValueBoolean : public DataBindContextValue
{

public:
DataBindContextValueBoolean(ViewModelInstanceValue* value);
void apply(Core* component, uint32_t propertyKey) override;
virtual void applyToSource(Core* component, uint32_t propertyKey) override;

private:
bool m_Value;
DataBindContextValueBoolean(ViewModelInstanceValue* source, DataConverter* converter);
void apply(Core* component, uint32_t propertyKey, bool isMainDirection) override;
DataValue* getTargetValue(Core* target, uint32_t propertyKey) override;
};
} // namespace rive

Expand Down
9 changes: 3 additions & 6 deletions include/rive/data_bind/context/context_value_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ class DataBindContextValueColor : public DataBindContextValue
{

public:
DataBindContextValueColor(ViewModelInstanceValue* value);
void apply(Core* component, uint32_t propertyKey) override;
virtual void applyToSource(Core* component, uint32_t propertyKey) override;

private:
double m_Value;
DataBindContextValueColor(ViewModelInstanceValue* source, DataConverter* converter);
void apply(Core* component, uint32_t propertyKey, bool isMainDirection) override;
DataValue* getTargetValue(Core* target, uint32_t propertyKey) override;
};
} // namespace rive

Expand Down
9 changes: 3 additions & 6 deletions include/rive/data_bind/context/context_value_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ class DataBindContextValueEnum : public DataBindContextValue
{

public:
DataBindContextValueEnum(ViewModelInstanceValue* value);
void apply(Core* component, uint32_t propertyKey) override;
virtual void applyToSource(Core* component, uint32_t propertyKey) override;

private:
int m_Value;
DataBindContextValueEnum(ViewModelInstanceValue* source, DataConverter* converter);
void apply(Core* component, uint32_t propertyKey, bool isMainDirection) override;
DataValue* getTargetValue(Core* target, uint32_t propertyKey) override;
};
} // namespace rive

Expand Down
8 changes: 5 additions & 3 deletions include/rive/data_bind/context/context_value_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class DataBindContextValueList : public DataBindContextValue
{

public:
DataBindContextValueList(ViewModelInstanceValue* value);
void apply(Core* component, uint32_t propertyKey) override;
DataBindContextValueList(ViewModelInstanceValue* source, DataConverter* converter);
void apply(Core* component, uint32_t propertyKey, bool isMainDirection) override;
void update(Core* target) override;
virtual void applyToSource(Core* component, uint32_t propertyKey) override;
virtual void applyToSource(Core* component,
uint32_t propertyKey,
bool isMainDirection) override;

private:
std::vector<std::unique_ptr<DataBindContextValueListItem>> m_ListItemsCache;
Expand Down
9 changes: 3 additions & 6 deletions include/rive/data_bind/context/context_value_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ class DataBindContextValueNumber : public DataBindContextValue
{

public:
DataBindContextValueNumber(ViewModelInstanceValue* value);
void apply(Core* component, uint32_t propertyKey) override;
virtual void applyToSource(Core* component, uint32_t propertyKey) override;

private:
double m_Value;
DataBindContextValueNumber(ViewModelInstanceValue* source, DataConverter* converter);
void apply(Core* component, uint32_t propertyKey, bool isMainDirection) override;
DataValue* getTargetValue(Core* target, uint32_t propertyKey) override;
};
} // namespace rive

Expand Down
9 changes: 3 additions & 6 deletions include/rive/data_bind/context/context_value_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ class DataBindContextValueString : public DataBindContextValue
{

public:
DataBindContextValueString(ViewModelInstanceValue* value);
void apply(Core* component, uint32_t propertyKey) override;
virtual void applyToSource(Core* component, uint32_t propertyKey) override;

private:
std::string m_Value;
DataBindContextValueString(ViewModelInstanceValue* source, DataConverter* converter);
void apply(Core* component, uint32_t propertyKey, bool isMainDirection) override;
DataValue* getTargetValue(Core* target, uint32_t propertyKey) override;
};
} // namespace rive

Expand Down
18 changes: 18 additions & 0 deletions include/rive/data_bind/converters/data_converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _RIVE_DATA_CONVERTER_HPP_
#define _RIVE_DATA_CONVERTER_HPP_
#include "rive/generated/data_bind/converters/data_converter_base.hpp"
#include "rive/data_bind/data_values/data_value.hpp"
#include <stdio.h>
namespace rive
{
class DataConverter : public DataConverterBase
{
public:
virtual DataValue* convert(DataValue* value) { return value; };
virtual DataValue* reverseConvert(DataValue* value) { return value; };
virtual DataType outputType() { return DataType::none; };
StatusCode import(ImportStack& importStack) override;
};
} // namespace rive

#endif
6 changes: 6 additions & 0 deletions include/rive/data_bind/data_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "rive/viewmodel/viewmodel_instance_value.hpp"
#include "rive/data_bind/context/context_value.hpp"
#include "rive/data_bind/data_context.hpp"
#include "rive/data_bind/converters/data_converter.hpp"
#include "rive/data_bind/data_values/data_type.hpp"
#include <stdio.h>
namespace rive
{
Expand All @@ -21,12 +23,16 @@ class DataBind : public DataBindBase
ComponentDirt dirt() { return m_Dirt; };
void dirt(ComponentDirt value) { m_Dirt = value; };
bool addDirt(ComponentDirt value, bool recurse);
DataConverter* converter() const { return m_dataConverter; };
void converter(DataConverter* value) { m_dataConverter = value; };

protected:
ComponentDirt m_Dirt = ComponentDirt::Filthy;
Core* m_target;
ViewModelInstanceValue* m_Source;
std::unique_ptr<DataBindContextValue> m_ContextValue;
DataConverter* m_dataConverter;
DataType outputType();
};
} // namespace rive

Expand Down
30 changes: 30 additions & 0 deletions include/rive/data_bind/data_values/data_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _RIVE_DATA_TYPE_HPP_
#define _RIVE_DATA_TYPE_HPP_
namespace rive
{
/// Data types used for converters.
enum class DataType : unsigned int
{
/// None.
none = 0,

/// String.
string = 1,

/// Number.
number = 2,

/// Bool.
boolean = 3,

/// Color.
color = 4,

/// List.
list = 5,

/// Enum.
enumType = 6
};
} // namespace rive
#endif
21 changes: 21 additions & 0 deletions include/rive/data_bind/data_values/data_value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _RIVE_DATA_VALUE_HPP_
#define _RIVE_DATA_VALUE_HPP_
#include "rive/data_bind/data_values/data_type.hpp"

#include <stdio.h>
namespace rive
{
class DataValue
{
public:
virtual bool isTypeOf(DataType dataType) const { return false; }
template <typename T> inline bool is() const { return isTypeOf(T::typeKey); }
template <typename T> inline T* as()
{
assert(is<T>());
return static_cast<T*>(this);
}
};
} // namespace rive

#endif
23 changes: 23 additions & 0 deletions include/rive/data_bind/data_values/data_value_boolean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _RIVE_DATA_VALUE_BOOLEAN_HPP_
#define _RIVE_DATA_VALUE_BOOLEAN_HPP_
#include "rive/data_bind/data_values/data_value.hpp"

#include <stdio.h>
namespace rive
{
class DataValueBoolean : public DataValue
{
private:
bool m_value = false;

public:
DataValueBoolean(bool value) : m_value(value){};
DataValueBoolean(){};
static const DataType typeKey = DataType::boolean;
bool isTypeOf(DataType typeKey) const override { return typeKey == DataType::boolean; }
bool value() { return m_value; };
void value(bool value) { m_value = value; };
};
} // namespace rive

#endif
23 changes: 23 additions & 0 deletions include/rive/data_bind/data_values/data_value_color.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _RIVE_DATA_VALUE_COLOR_HPP_
#define _RIVE_DATA_VALUE_COLOR_HPP_
#include "rive/data_bind/data_values/data_value.hpp"

#include <stdio.h>
namespace rive
{
class DataValueColor : public DataValue
{
private:
int m_value = false;

public:
DataValueColor(int value) : m_value(value){};
DataValueColor(){};
static const DataType typeKey = DataType::color;
bool isTypeOf(DataType typeKey) const override { return typeKey == DataType::color; }
int value() { return m_value; };
void value(int value) { m_value = value; };
};
} // namespace rive

#endif
26 changes: 26 additions & 0 deletions include/rive/data_bind/data_values/data_value_enum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _RIVE_DATA_VALUE_ENUM_HPP_
#define _RIVE_DATA_VALUE_ENUM_HPP_
#include "rive/data_bind/data_values/data_value.hpp"
#include "rive/viewmodel/data_enum.hpp"

#include <iostream>
namespace rive
{
class DataValueEnum : public DataValue
{
private:
uint32_t m_value = 0;
DataEnum* m_dataEnum;

public:
DataValueEnum(uint32_t value, DataEnum* dataEnum) : m_value(value), m_dataEnum(dataEnum){};
DataValueEnum(){};
static const DataType typeKey = DataType::enumType;
bool isTypeOf(DataType typeKey) const override { return typeKey == DataType::enumType; };
uint32_t value() { return m_value; };
void value(uint32_t value) { m_value = value; };
DataEnum* dataEnum() { return m_dataEnum; };
void dataEnum(DataEnum* value) { m_dataEnum = value; };
};
} // namespace rive
#endif
Loading

0 comments on commit 62025f9

Please sign in to comment.