-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
13 changed files
with
262 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
8c7ac03294339ae2ad856b541c730dc16cec018d | ||
2c0927fc5e9a5c420305a81446ac41eb5b896b2c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
include/rive/data_bind/converters/data_converter_rounder.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
include/rive/data_bind/converters/data_converter_to_string.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
include/rive/generated/data_bind/converters/data_converter_rounder_base.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
36 changes: 36 additions & 0 deletions
36
include/rive/generated/data_bind/converters/data_converter_to_string_base.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/generated/data_bind/converters/data_converter_rounder_base.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/generated/data_bind/converters/data_converter_to_string_base.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |