Skip to content

Commit

Permalink
refactor conditions
Browse files Browse the repository at this point in the history
refactor code:
- adds template for comparing view models
- unifies evaluate method for view model and input conditions

Diffs=
1131f30e6 refactor conditions (#7747)

Co-authored-by: hernan <[email protected]>
  • Loading branch information
bodymovin and bodymovin committed Aug 6, 2024
1 parent 076f1a2 commit eb1e576
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
35a52873cf0adabbda2670673762a36c91822e96
1131f30e6a45bee5c0561de3a35b19985bf46820
4 changes: 2 additions & 2 deletions include/rive/animation/state_machine_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class StateMachineInstance : public Scene, public NestedEventNotifier, public Ne

/// Allow anything referencing a concrete StateMachineInstace access to
/// the backing artboard (explicitly not allowed on Scenes).
Artboard* artboard() { return m_artboardInstance; }
Artboard* artboard() const { return m_artboardInstance; }

void setParentStateMachineInstance(StateMachineInstance* instance)
{
Expand All @@ -136,7 +136,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);
BindableProperty* bindablePropertyInstance(BindableProperty* bindableProperty) const;
DataBind* bindableDataBind(BindableProperty* bindableProperty);
#ifdef TESTING
size_t hitComponentsCount() { return m_hitComponents.size(); };
Expand Down
2 changes: 1 addition & 1 deletion include/rive/animation/transition_bool_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace rive
class TransitionBoolCondition : public TransitionBoolConditionBase
{
public:
bool evaluate(const SMIInput* inputInstance) const override;
bool evaluate(const StateMachineInstance* stateMachineInstance) const override;

protected:
bool validateInputType(const StateMachineInput* input) const override;
Expand Down
2 changes: 1 addition & 1 deletion include/rive/animation/transition_comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TransitionComparator : public TransitionComparatorBase
StatusCode import(ImportStack& importStack) override;
virtual bool compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance);
const StateMachineInstance* stateMachineInstance);

protected:
bool compareNumbers(float left, float right, TransitionConditionOp op);
Expand Down
3 changes: 3 additions & 0 deletions include/rive/animation/transition_condition.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _RIVE_TRANSITION_CONDITION_HPP_
#define _RIVE_TRANSITION_CONDITION_HPP_
#include "rive/generated/animation/transition_condition_base.hpp"
#include "rive/animation/state_machine_instance.hpp"

namespace rive
{
Expand All @@ -15,6 +16,8 @@ class TransitionCondition : public TransitionConditionBase

StatusCode import(ImportStack& importStack) override;

virtual bool evaluate(const StateMachineInstance* stateMachineInstance) const { return true; }

protected:
virtual bool validateInputType(const StateMachineInput* input) const { return true; }
};
Expand Down
2 changes: 0 additions & 2 deletions include/rive/animation/transition_input_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace rive
class TransitionInputCondition : public TransitionInputConditionBase
{
public:
virtual bool evaluate(const SMIInput* inputInstance) const { return true; }

StatusCode import(ImportStack& importStack) override;
};
} // namespace rive
Expand Down
2 changes: 1 addition & 1 deletion include/rive/animation/transition_number_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TransitionNumberCondition : public TransitionNumberConditionBase
bool validateInputType(const StateMachineInput* input) const override;

public:
bool evaluate(const SMIInput* inputInstance) const override;
bool evaluate(const StateMachineInstance* stateMachineInstance) const override;
};
} // namespace rive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _RIVE_TRANSITION_PROPERTY_VIEW_MODEL_COMPARATOR_HPP_
#include "rive/generated/animation/transition_property_viewmodel_comparator_base.hpp"
#include "rive/data_bind/bindable_property.hpp"
#include "rive/animation/state_machine_instance.hpp"
#include <stdio.h>
namespace rive
{
Expand All @@ -11,12 +12,18 @@ class TransitionPropertyViewModelComparator : public TransitionPropertyViewModel
StatusCode import(ImportStack& importStack) override;
bool compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance) override;
float propertyValueNumber(StateMachineInstance* stateMachineInstance);
std::string propertyValueString(StateMachineInstance* stateMachineInstance);
int propertyValueColor(StateMachineInstance* stateMachineInstance);
bool propertyValueBoolean(StateMachineInstance* stateMachineInstance);
uint16_t propertyValueEnum(StateMachineInstance* stateMachineInstance);
const StateMachineInstance* stateMachineInstance) override;
template <typename T = BindableProperty, typename U>
U value(const StateMachineInstance* stateMachineInstance)
{
if (m_bindableProperty->is<T>())
{
auto bindableInstance =
stateMachineInstance->bindablePropertyInstance(m_bindableProperty);
return bindableInstance->as<T>()->propertyValue();
}
return (new T())->propertyValue();
};

protected:
BindableProperty* m_bindableProperty;
Expand Down
2 changes: 1 addition & 1 deletion include/rive/animation/transition_trigger_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace rive
class TransitionTriggerCondition : public TransitionTriggerConditionBase
{
public:
bool evaluate(const SMIInput* inputInstance) const override;
bool evaluate(const StateMachineInstance* stateMachineInstance) const override;

protected:
bool validateInputType(const StateMachineInput* input) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TransitionValueBooleanComparator : public TransitionValueBooleanComparator
public:
bool compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance) override;
const StateMachineInstance* stateMachineInstance) override;
};
} // namespace rive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TransitionValueColorComparator : public TransitionValueColorComparatorBase
public:
bool compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance) override;
const StateMachineInstance* stateMachineInstance) override;
};
} // namespace rive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TransitionValueNumberComparator : public TransitionValueNumberComparatorBa
public:
bool compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance) override;
const StateMachineInstance* stateMachineInstance) override;
};
} // namespace rive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TransitionValueStringComparator : public TransitionValueStringComparatorBa
public:
bool compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance) override;
const StateMachineInstance* stateMachineInstance) override;
};
} // namespace rive

Expand Down
6 changes: 3 additions & 3 deletions include/rive/animation/transition_viewmodel_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class TransitionViewModelCondition : public TransitionViewModelConditionBase
TransitionComparator* m_rightComparator;

public:
bool evaluateCondition(StateMachineInstance* stateMachineInstance);
TransitionComparator* leftComparator() { return m_leftComparator; };
TransitionComparator* rightComparator() { return m_rightComparator; };
bool evaluate(const StateMachineInstance* stateMachineInstance) const override;
TransitionComparator* leftComparator() const { return m_leftComparator; };
TransitionComparator* rightComparator() const { return m_rightComparator; };
void comparator(TransitionComparator* value)
{
if (m_leftComparator == nullptr)
Expand Down
3 changes: 2 additions & 1 deletion src/animation/state_machine_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,8 @@ void StateMachineInstance::notifyEventListeners(const std::vector<EventReport>&
}
}

BindableProperty* StateMachineInstance::bindablePropertyInstance(BindableProperty* bindableProperty)
BindableProperty* StateMachineInstance::bindablePropertyInstance(
BindableProperty* bindableProperty) const
{
auto bindablePropertyInstance = m_bindablePropertyInstances.find(bindableProperty);
if (bindablePropertyInstance == m_bindablePropertyInstances.end())
Expand Down
22 changes: 3 additions & 19 deletions src/animation/state_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,10 @@ AllowTransition StateTransition::allowed(StateInstance* stateFrom,

for (auto condition : m_Conditions)
{
if (condition->is<TransitionInputCondition>())
if ((ignoreTriggers && condition->is<TransitionTriggerCondition>()) ||
!condition->evaluate(stateMachineInstance))
{
auto inputCondition = condition->as<TransitionInputCondition>();
// N.B. state machine instance sanitizes these for us...
auto input = stateMachineInstance->input(inputCondition->inputId());

if ((ignoreTriggers && inputCondition->is<TransitionTriggerCondition>()) ||
!inputCondition->evaluate(input))
{
return AllowTransition::no;
}
}
else if (condition->is<TransitionViewModelCondition>())
{
auto transitionViewModelCondition = condition->as<TransitionViewModelCondition>();

if (!transitionViewModelCondition->evaluateCondition(stateMachineInstance))
{
return AllowTransition::no;
}
return AllowTransition::no;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/animation/transition_bool_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ bool TransitionBoolCondition::validateInputType(const StateMachineInput* input)
return input == nullptr || input->is<StateMachineBool>();
}

bool TransitionBoolCondition::evaluate(const SMIInput* inputInstance) const
bool TransitionBoolCondition::evaluate(const StateMachineInstance* stateMachineInstance) const
{
auto inputInstance = stateMachineInstance->input(inputId());
if (inputInstance == nullptr)
{
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/animation/transition_comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool TransitionComparator::compareColors(int left, int right, TransitionConditio

bool TransitionComparator::compare(TransitionComparator* comparand,
TransitionConditionOp operation,
StateMachineInstance* stateMachineInstance)
const StateMachineInstance* stateMachineInstance)
{
return false;
}
3 changes: 1 addition & 2 deletions src/animation/transition_condition.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "rive/animation/transition_bool_condition.hpp"
#include "rive/animation/transition_condition.hpp"
#include "rive/animation/state_transition.hpp"
#include "rive/importers/state_transition_importer.hpp"

Expand All @@ -10,7 +10,6 @@ StatusCode TransitionCondition::onAddedClean(CoreContext* context) { return Stat

StatusCode TransitionCondition::import(ImportStack& importStack)
{

auto transitionImporter = importStack.latest<StateTransitionImporter>(StateTransition::typeKey);
if (transitionImporter == nullptr)
{
Expand Down
3 changes: 2 additions & 1 deletion src/animation/transition_number_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ bool TransitionNumberCondition::validateInputType(const StateMachineInput* input
return input == nullptr || input->is<StateMachineNumber>();
}

bool TransitionNumberCondition::evaluate(const SMIInput* inputInstance) const
bool TransitionNumberCondition::evaluate(const StateMachineInstance* stateMachineInstance) const
{
auto inputInstance = stateMachineInstance->input(inputId());
if (inputInstance == nullptr)
{
return true;
Expand Down
Loading

0 comments on commit eb1e576

Please sign in to comment.