Skip to content

Commit

Permalink
support randomizing transitions
Browse files Browse the repository at this point in the history
First version of randomization to get in UAT for validation.

Diffs=
edac19b06 support randomizing transitions (#7082)

Co-authored-by: hernan <hernan@rive.app>
  • Loading branch information
bodymovin and bodymovin committed Apr 19, 2024
1 parent f8e8401 commit 5e69b3d
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2828b7b013811f1f4b0b0e2ee0f6d9b23c73878b
edac19b0600a1ede64bb0b07e61556013a3e84fe
8 changes: 8 additions & 0 deletions dev/defs/animation/layer_state.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
"string": "y"
},
"runtime": false
},
"flags": {
"type": "uint",
"initialValue": "0",
"key": {
"int": 536,
"string": "flags"
}
}
}
}
9 changes: 9 additions & 0 deletions dev/defs/animation/state_transition.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
"string": "interpolatorid"
},
"description": "The id of the custom interpolator used when interpolation is Cubic."
},
"randomWeight": {
"type": "uint",
"initialValue": "1",
"key": {
"int": 537,
"string": "randomweight"
},
"description": "Weight of the transition in the overall random options"
}
}
}
24 changes: 24 additions & 0 deletions include/rive/animation/layer_state_flags.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _RIVE_LAYER_STATE_FLAGS_HPP_
#define _RIVE_LAYER_STATE_FLAGS_HPP_

#include <type_traits>

namespace rive
{
enum class LayerStateFlags : unsigned char
{
None = 0,

/// Whether the transition is disabled.
Random = 1 << 0,

};

inline constexpr LayerStateFlags operator&(LayerStateFlags lhs, LayerStateFlags rhs)
{
return static_cast<LayerStateFlags>(
static_cast<std::underlying_type<LayerStateFlags>::type>(lhs) &
static_cast<std::underlying_type<LayerStateFlags>::type>(rhs));
}
} // namespace rive
#endif
2 changes: 2 additions & 0 deletions include/rive/animation/state_machine_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class StateMachineInstance : public Scene
friend class SMIInput;
friend class KeyedProperty;
friend class HitComponent;
friend class StateMachineLayerInstance;

private:
/// Provide a hitListener if you want to process a down or an up for the pointer position
Expand All @@ -53,6 +54,7 @@ class StateMachineInstance : public Scene
InstType* getNamedInput(const std::string& name) const;
void notifyEventListeners(const std::vector<EventReport>& events, NestedArtboard* source);
void sortHitComponents();
double randomValue();

public:
StateMachineInstance(const StateMachine* machine, ArtboardInstance* instance);
Expand Down
4 changes: 4 additions & 0 deletions include/rive/animation/state_transition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class StateTransition : public StateTransitionBase
return static_cast<StateTransitionFlags>(flags());
}
LayerState* m_StateTo = nullptr;
uint32_t m_EvaluatedRandomWeight = 1;
CubicInterpolator* m_Interpolator = nullptr;

std::vector<TransitionCondition*> m_Conditions;
Expand All @@ -45,6 +46,9 @@ class StateTransition : public StateTransitionBase
const LayerState* stateTo() const { return m_StateTo; }
inline CubicInterpolator* interpolator() const { return m_Interpolator; }

inline uint32_t evaluatedRandomWeight() const { return m_EvaluatedRandomWeight; }
void evaluatedRandomWeight(uint32_t value) { m_EvaluatedRandomWeight = value; }

StatusCode onAddedDirty(CoreContext* context) override;
StatusCode onAddedClean(CoreContext* context) override;

Expand Down
36 changes: 36 additions & 0 deletions include/rive/generated/animation/layer_state_base.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _RIVE_LAYER_STATE_BASE_HPP_
#define _RIVE_LAYER_STATE_BASE_HPP_
#include "rive/animation/state_machine_layer_component.hpp"
#include "rive/core/field_types/core_uint_type.hpp"
namespace rive
{
class LayerStateBase : public StateMachineLayerComponent
Expand All @@ -27,7 +28,42 @@ class LayerStateBase : public StateMachineLayerComponent

uint16_t coreType() const override { return typeKey; }

static const uint16_t flagsPropertyKey = 535;

private:
uint32_t m_Flags = 0;

public:
inline uint32_t flags() const { return m_Flags; }
void flags(uint32_t value)
{
if (m_Flags == value)
{
return;
}
m_Flags = value;
flagsChanged();
}

void copy(const LayerStateBase& object)
{
m_Flags = object.m_Flags;
StateMachineLayerComponent::copy(object);
}

bool deserialize(uint16_t propertyKey, BinaryReader& reader) override
{
switch (propertyKey)
{
case flagsPropertyKey:
m_Flags = CoreUintType::deserialize(reader);
return true;
}
return StateMachineLayerComponent::deserialize(propertyKey, reader);
}

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

Expand Down
18 changes: 18 additions & 0 deletions include/rive/generated/animation/state_transition_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class StateTransitionBase : public StateMachineLayerComponent
static const uint16_t exitTimePropertyKey = 160;
static const uint16_t interpolationTypePropertyKey = 349;
static const uint16_t interpolatorIdPropertyKey = 350;
static const uint16_t randomWeightPropertyKey = 536;

private:
uint32_t m_StateToId = -1;
Expand All @@ -42,6 +43,7 @@ class StateTransitionBase : public StateMachineLayerComponent
uint32_t m_ExitTime = 0;
uint32_t m_InterpolationType = 1;
uint32_t m_InterpolatorId = -1;
uint32_t m_RandomWeight = 1;

public:
inline uint32_t stateToId() const { return m_StateToId; }
Expand Down Expand Up @@ -110,6 +112,17 @@ class StateTransitionBase : public StateMachineLayerComponent
interpolatorIdChanged();
}

inline uint32_t randomWeight() const { return m_RandomWeight; }
void randomWeight(uint32_t value)
{
if (m_RandomWeight == value)
{
return;
}
m_RandomWeight = value;
randomWeightChanged();
}

Core* clone() const override;
void copy(const StateTransitionBase& object)
{
Expand All @@ -119,6 +132,7 @@ class StateTransitionBase : public StateMachineLayerComponent
m_ExitTime = object.m_ExitTime;
m_InterpolationType = object.m_InterpolationType;
m_InterpolatorId = object.m_InterpolatorId;
m_RandomWeight = object.m_RandomWeight;
StateMachineLayerComponent::copy(object);
}

Expand All @@ -144,6 +158,9 @@ class StateTransitionBase : public StateMachineLayerComponent
case interpolatorIdPropertyKey:
m_InterpolatorId = CoreUintType::deserialize(reader);
return true;
case randomWeightPropertyKey:
m_RandomWeight = CoreUintType::deserialize(reader);
return true;
}
return StateMachineLayerComponent::deserialize(propertyKey, reader);
}
Expand All @@ -155,6 +172,7 @@ class StateTransitionBase : public StateMachineLayerComponent
virtual void exitTimeChanged() {}
virtual void interpolationTypeChanged() {}
virtual void interpolatorIdChanged() {}
virtual void randomWeightChanged() {}
};
} // namespace rive

Expand Down
12 changes: 12 additions & 0 deletions include/rive/generated/core_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,9 @@ class CoreRegistry
case ListenerFireEventBase::eventIdPropertyKey:
object->as<ListenerFireEventBase>()->eventId(value);
break;
case LayerStateBase::flagsPropertyKey:
object->as<LayerStateBase>()->flags(value);
break;
case ListenerInputChangeBase::inputIdPropertyKey:
object->as<ListenerInputChangeBase>()->inputId(value);
break;
Expand Down Expand Up @@ -538,6 +541,9 @@ class CoreRegistry
case StateTransitionBase::interpolatorIdPropertyKey:
object->as<StateTransitionBase>()->interpolatorId(value);
break;
case StateTransitionBase::randomWeightPropertyKey:
object->as<StateTransitionBase>()->randomWeight(value);
break;
case StateMachineFireEventBase::eventIdPropertyKey:
object->as<StateMachineFireEventBase>()->eventId(value);
break;
Expand Down Expand Up @@ -1256,6 +1262,8 @@ class CoreRegistry
return object->as<SoloBase>()->activeComponentId();
case ListenerFireEventBase::eventIdPropertyKey:
return object->as<ListenerFireEventBase>()->eventId();
case LayerStateBase::flagsPropertyKey:
return object->as<LayerStateBase>()->flags();
case ListenerInputChangeBase::inputIdPropertyKey:
return object->as<ListenerInputChangeBase>()->inputId();
case ListenerInputChangeBase::nestedInputIdPropertyKey:
Expand Down Expand Up @@ -1308,6 +1316,8 @@ class CoreRegistry
return object->as<StateTransitionBase>()->interpolationType();
case StateTransitionBase::interpolatorIdPropertyKey:
return object->as<StateTransitionBase>()->interpolatorId();
case StateTransitionBase::randomWeightPropertyKey:
return object->as<StateTransitionBase>()->randomWeight();
case StateMachineFireEventBase::eventIdPropertyKey:
return object->as<StateMachineFireEventBase>()->eventId();
case StateMachineFireEventBase::occursValuePropertyKey:
Expand Down Expand Up @@ -1779,6 +1789,7 @@ class CoreRegistry
case NestedAnimationBase::animationIdPropertyKey:
case SoloBase::activeComponentIdPropertyKey:
case ListenerFireEventBase::eventIdPropertyKey:
case LayerStateBase::flagsPropertyKey:
case ListenerInputChangeBase::inputIdPropertyKey:
case ListenerInputChangeBase::nestedInputIdPropertyKey:
case AnimationStateBase::animationIdPropertyKey:
Expand All @@ -1805,6 +1816,7 @@ class CoreRegistry
case StateTransitionBase::exitTimePropertyKey:
case StateTransitionBase::interpolationTypePropertyKey:
case StateTransitionBase::interpolatorIdPropertyKey:
case StateTransitionBase::randomWeightPropertyKey:
case StateMachineFireEventBase::eventIdPropertyKey:
case StateMachineFireEventBase::occursValuePropertyKey:
case LinearAnimationBase::fpsPropertyKey:
Expand Down
Loading

0 comments on commit 5e69b3d

Please sign in to comment.