Skip to content

Commit

Permalink
add bindable properties for state machines
Browse files Browse the repository at this point in the history
This PR sets the fundamental pieces for PRs that will be coming afterward to support data binding state machines.
The most important parts are:
- bindable properties that will be used as binding points for conditions, listeners, and blend animations
- data bind objects don't extend from Component anymore to decouple them from artboards
- because of that, now data bind objects are written in the context of their bound object so there is a unified way for setting data bind targets

The rest is the usual boilerplate code from core objects and some inheritance changes

Diffs=
b5f342002 add bindable properties for state machines (#7640)

Co-authored-by: hernan <[email protected]>
  • Loading branch information
bodymovin and bodymovin committed Jul 22, 2024
1 parent 5fb0ffa commit 2b9cfe1
Show file tree
Hide file tree
Showing 65 changed files with 1,025 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f55ebd34f3938bfe1e36ebf83bbc92c0e5d34a62
b5f342002be71c61608ad6c37835349eb4e7c719
20 changes: 20 additions & 0 deletions dev/defs/data_bind/bindable_property.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "BindableProperty",
"key": {
"int": 9,
"string": "bindableproperty"
},
"abstract": true,
"properties": {
"dataBindId": {
"type": "Id",
"initialValue": "Core.missingId",
"key": {
"int": 633,
"string": "databindid"
},
"description": "Id of the data bind binded to this property",
"runtime": false
}
}
}
19 changes: 19 additions & 0 deletions dev/defs/data_bind/bindable_property_boolean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "BindablePropertyBoolean",
"key": {
"int": 472,
"string": "bindablepropertyboolean"
},
"extends": "data_bind/bindable_property.json",
"properties": {
"propertyValue": {
"type": "bool",
"initialValue": "false",
"key": {
"int": 634,
"string": "propertyvalue"
},
"description": "A property of type bool that can be used for data binding."
}
}
}
19 changes: 19 additions & 0 deletions dev/defs/data_bind/bindable_property_color.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "BindablePropertyColor",
"key": {
"int": 475,
"string": "bindablepropertycolor"
},
"extends": "data_bind/bindable_property.json",
"properties": {
"propertyValue": {
"type": "Color",
"initialValue": "0xFF1D1D1D",
"key": {
"int": 638,
"string": "propertyvalue"
},
"description": "A property of type int that can be used for data binding."
}
}
}
21 changes: 21 additions & 0 deletions dev/defs/data_bind/bindable_property_enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "BindablePropertyEnum",
"key": {
"int": 474,
"string": "bindablepropertyenum"
},
"extends": "data_bind/bindable_property.json",
"properties": {
"propertyValue": {
"type": "Id",
"typeRuntime": "uint",
"initialValue": "Core.missingId",
"initialValueRuntime": "-1",
"key": {
"int": 637,
"string": "propertyvalue"
},
"description": "The id of the enum that can be used for data binding."
}
}
}
19 changes: 19 additions & 0 deletions dev/defs/data_bind/bindable_property_number.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "BindablePropertyNumber",
"key": {
"int": 473,
"string": "bindablepropertynumber"
},
"extends": "data_bind/bindable_property.json",
"properties": {
"propertyValue": {
"type": "double",
"initialValue": "0",
"key": {
"int": 636,
"string": "propertyvalue"
},
"description": "A property of type double that can be used for data binding."
}
}
}
19 changes: 19 additions & 0 deletions dev/defs/data_bind/bindable_property_string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "BindablePropertyString",
"key": {
"int": 471,
"string": "bindablepropertystring"
},
"extends": "data_bind/bindable_property.json",
"properties": {
"propertyValue": {
"type": "String",
"initialValue": "''",
"key": {
"int": 635,
"string": "propertyvalue"
},
"description": "A property of type String that can be used for data binding."
}
}
}
6 changes: 2 additions & 4 deletions dev/defs/data_bind/data_bind.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
"int": 446,
"string": "databind"
},
"extends": "component.json",
"properties": {
"targetId": {
"type": "Id",
"typeRuntime": "uint",
"initialValue": "Core.missingId",
"initialValueRuntime": "-1",
"key": {
"int": 585,
"string": "targetid"
},
"description": "Identifier used to track the object that is targetted."
"description": "Identifier used to track the object that is targetted.",
"runtime": false
},
"propertyKey": {
"type": "uint",
Expand Down
2 changes: 2 additions & 0 deletions include/rive/animation/nested_state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class NestedStateMachine : public NestedStateMachineBase
size_t inputCount() { return m_nestedInputs.size(); }
NestedInput* input(size_t index);
NestedInput* input(std::string name);
void dataContextFromInstance(ViewModelInstance* viewModelInstance);
void dataContext(DataContext* dataContext);
};
} // namespace rive

Expand Down
5 changes: 5 additions & 0 deletions include/rive/animation/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class StateMachineLayer;
class StateMachineInput;
class StateMachineListener;
class StateMachineImporter;
class DataBind;
class StateMachine : public StateMachineBase
{
friend class StateMachineImporter;
Expand All @@ -18,10 +19,12 @@ class StateMachine : public StateMachineBase
std::vector<std::unique_ptr<StateMachineLayer>> m_Layers;
std::vector<std::unique_ptr<StateMachineInput>> m_Inputs;
std::vector<std::unique_ptr<StateMachineListener>> m_Listeners;
std::vector<std::unique_ptr<DataBind>> m_dataBinds;

void addLayer(std::unique_ptr<StateMachineLayer>);
void addInput(std::unique_ptr<StateMachineInput>);
void addListener(std::unique_ptr<StateMachineListener>);
void addDataBind(std::unique_ptr<DataBind>);

public:
StateMachine();
Expand All @@ -32,11 +35,13 @@ class StateMachine : public StateMachineBase
size_t layerCount() const { return m_Layers.size(); }
size_t inputCount() const { return m_Inputs.size(); }
size_t listenerCount() const { return m_Listeners.size(); }
size_t dataBindCount() const { return m_dataBinds.size(); }

const StateMachineInput* input(std::string name) const;
const StateMachineInput* input(size_t index) const;
const StateMachineLayer* layer(std::string name) const;
const StateMachineLayer* layer(size_t index) const;
const DataBind* dataBind(size_t index) const;
const StateMachineListener* listener(size_t index) const;

StatusCode onAddedDirty(CoreContext* context) override;
Expand Down
9 changes: 9 additions & 0 deletions include/rive/animation/state_machine_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <stddef.h>
#include <vector>
#include <unordered_map>
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/animation/state_instance.hpp"
#include "rive/animation/state_transition.hpp"
Expand Down Expand Up @@ -31,6 +32,8 @@ class NestedEventNotifier;
class Event;
class KeyedProperty;
class EventReport;
class DataBind;
class BindableProperty;

#ifdef WITH_RIVE_TOOLS
class StateMachineInstance;
Expand All @@ -56,6 +59,7 @@ class StateMachineInstance : public Scene, public NestedEventNotifier, public Ne
double randomValue();
StateTransition* findRandomTransition(StateInstance* stateFromInstance, bool ignoreTriggers);
StateTransition* findAllowedTransition(StateInstance* stateFromInstance, bool ignoreTriggers);
DataContext* m_DataContext;

public:
StateMachineInstance(const StateMachine* machine, ArtboardInstance* instance);
Expand All @@ -78,6 +82,8 @@ class StateMachineInstance : public Scene, public NestedEventNotifier, public Ne
SMIBool* getBool(const std::string& name) const override;
SMINumber* getNumber(const std::string& name) const override;
SMITrigger* getTrigger(const std::string& name) const override;
void dataContextFromInstance(ViewModelInstance* viewModelInstance) override;
void dataContext(DataContext* dataContext);

size_t currentAnimationCount() const;
const LinearAnimationInstance* currentAnimationByIndex(size_t index) const;
Expand Down Expand Up @@ -128,6 +134,7 @@ class StateMachineInstance : public Scene, public NestedEventNotifier, public Ne
/// Gets a reported event at an index < reportedEventCount().
const EventReport reportedEventAt(std::size_t index) const;
bool playsAudio() override { return true; }
BindableProperty* bindablePropertyInstance(BindableProperty* bindableProperty);

private:
std::vector<EventReport> m_reportedEvents;
Expand All @@ -139,6 +146,8 @@ class StateMachineInstance : public Scene, public NestedEventNotifier, public Ne
std::vector<std::unique_ptr<HitComponent>> m_hitComponents;
StateMachineInstance* m_parentStateMachineInstance = nullptr;
NestedArtboard* m_parentNestedArtboard = nullptr;
std::vector<DataBind*> m_dataBinds;
std::unordered_map<BindableProperty*, BindableProperty*> m_bindablePropertyInstances;
#ifdef WITH_RIVE_TOOLS
public:
void onInputChanged(InputChanged callback) { m_inputChangedCallback = callback; }
Expand Down
16 changes: 13 additions & 3 deletions include/rive/artboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ class Artboard : public ArtboardBase, public CoreContext, public ShapePaintConta
DataContext* parent,
bool isRoot);
void dataContextFromInstance(ViewModelInstance* viewModelInstance);
void populateDataBinds(std::vector<Component*>* dataBinds);
void buildDataBindDependencies(std::vector<Component*>* dataBinds);
void sortDataBinds(std::vector<Component*> dataBinds);
void addDataBind(DataBind* dataBind);
void populateDataBinds(std::vector<DataBind*>* dataBinds);
void sortDataBinds(std::vector<DataBind*> dataBinds);
bool hasAudio() const;

template <typename T = Component> T* find(const std::string& name)
Expand Down Expand Up @@ -284,6 +284,16 @@ class Artboard : public ArtboardBase, public CoreContext, public ShapePaintConta
{
auto object = *itr;
cloneObjects.push_back(object == nullptr ? nullptr : object->clone());
// For each object, clone its data bind objects and target their clones
for (auto dataBind : m_DataBinds)
{
if (dataBind->target() == object)
{
auto dataBindClone = static_cast<DataBind*>(dataBind->clone());
dataBindClone->target(cloneObjects.back());
artboardClone->m_DataBinds.push_back(dataBindClone);
}
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions include/rive/data_bind/bindable_property.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _RIVE_BINDABLE_PROPERTY_HPP_
#define _RIVE_BINDABLE_PROPERTY_HPP_
#include "rive/generated/data_bind/bindable_property_base.hpp"
#include "rive/data_bind/data_bind.hpp"
#include <stdio.h>
namespace rive
{
class BindableProperty : public BindablePropertyBase
{
public:
void dataBind(DataBind* value) { m_dataBind = value; };
DataBind* dataBind() { return m_dataBind; };

private:
DataBind* m_dataBind;
};
} // namespace rive

#endif
13 changes: 13 additions & 0 deletions include/rive/data_bind/bindable_property_boolean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RIVE_BINDABLE_PROPERTY_BOOLEAN_HPP_
#define _RIVE_BINDABLE_PROPERTY_BOOLEAN_HPP_
#include "rive/generated/data_bind/bindable_property_boolean_base.hpp"
#include <stdio.h>
namespace rive
{
class BindablePropertyBoolean : public BindablePropertyBooleanBase
{
public:
};
} // namespace rive

#endif
13 changes: 13 additions & 0 deletions include/rive/data_bind/bindable_property_color.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RIVE_BINDABLE_PROPERTY_COLOR_HPP_
#define _RIVE_BINDABLE_PROPERTY_COLOR_HPP_
#include "rive/generated/data_bind/bindable_property_color_base.hpp"
#include <stdio.h>
namespace rive
{
class BindablePropertyColor : public BindablePropertyColorBase
{
public:
};
} // namespace rive

#endif
13 changes: 13 additions & 0 deletions include/rive/data_bind/bindable_property_enum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RIVE_BINDABLE_PROPERTY_ENUM_HPP_
#define _RIVE_BINDABLE_PROPERTY_ENUM_HPP_
#include "rive/generated/data_bind/bindable_property_enum_base.hpp"
#include <stdio.h>
namespace rive
{
class BindablePropertyEnum : public BindablePropertyEnumBase
{
public:
};
} // namespace rive

#endif
13 changes: 13 additions & 0 deletions include/rive/data_bind/bindable_property_number.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RIVE_BINDABLE_PROPERTY_NUMBER_HPP_
#define _RIVE_BINDABLE_PROPERTY_NUMBER_HPP_
#include "rive/generated/data_bind/bindable_property_number_base.hpp"
#include <stdio.h>
namespace rive
{
class BindablePropertyNumber : public BindablePropertyNumberBase
{
public:
};
} // namespace rive

#endif
13 changes: 13 additions & 0 deletions include/rive/data_bind/bindable_property_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RIVE_BINDABLE_PROPERTY_STRING_HPP_
#define _RIVE_BINDABLE_PROPERTY_STRING_HPP_
#include "rive/generated/data_bind/bindable_property_string_base.hpp"
#include <stdio.h>
namespace rive
{
class BindablePropertyString : public BindablePropertyStringBase
{
public:
};
} // namespace rive

#endif
6 changes: 3 additions & 3 deletions include/rive/data_bind/context/context_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class DataBindContextValue
public:
DataBindContextValue(){};
virtual ~DataBindContextValue(){};
virtual void applyToSource(Component* component, uint32_t propertyKey){};
virtual void apply(Component* component, uint32_t propertyKey){};
virtual void update(Component* component){};
virtual void applyToSource(Core* component, uint32_t propertyKey){};
virtual void apply(Core* component, uint32_t propertyKey){};
virtual void update(Core* component){};
};
} // namespace rive

Expand Down
4 changes: 2 additions & 2 deletions include/rive/data_bind/context/context_value_boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class DataBindContextValueBoolean : public DataBindContextValue

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

private:
bool m_Value;
Expand Down
Loading

0 comments on commit 2b9cfe1

Please sign in to comment.