Skip to content

Commit

Permalink
sort hit shapes when draw order changes and stop propagation on hit s…
Browse files Browse the repository at this point in the history
sort hit shapes when draw order changes and stop propagation on hit success

Diffs=
8bca56dca sort hit shapes when draw order changes and stop propagation on hit s… (#6624)

Co-authored-by: hernan <[email protected]>
  • Loading branch information
bodymovin and bodymovin committed Feb 21, 2024
1 parent 03d7c9c commit 71bf9e3
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9d605a1feb39dcad526ac9dda6b53b547921c58d
8bca56dcaffd0f563a91f628b0ed432eca71acb5
11 changes: 11 additions & 0 deletions dev/defs/artboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@
"description": "List of selected animations",
"runtime": false,
"coop": false
},
"viewModelId": {
"type": "Id",
"typeRuntime": "uint",
"initialValue": "Core.missingId",
"initialValueRuntime": "-1",
"key": {
"int": 434,
"string": "viewmodelid"
},
"description": "The view model attached to this artboard data context."
}
}
}
8 changes: 5 additions & 3 deletions include/rive/animation/nested_state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _RIVE_NESTED_STATE_MACHINE_HPP_
#include "rive/animation/state_machine_instance.hpp"
#include "rive/generated/animation/nested_state_machine_base.hpp"
#include "rive/hit_result.hpp"
#include "rive/math/vec2d.hpp"
#include <memory>

Expand All @@ -23,9 +24,10 @@ class NestedStateMachine : public NestedStateMachineBase
void initializeAnimation(ArtboardInstance*) override;
StateMachineInstance* stateMachineInstance();

void pointerMove(Vec2D position);
void pointerDown(Vec2D position);
void pointerUp(Vec2D position);
HitResult pointerMove(Vec2D position);
HitResult pointerDown(Vec2D position);
HitResult pointerUp(Vec2D position);
HitResult pointerExit(Vec2D position);

void addNestedInput(NestedInput* input);
};
Expand Down
20 changes: 11 additions & 9 deletions include/rive/animation/state_machine_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/core/field_types/core_callback_type.hpp"
#include "rive/hit_result.hpp"
#include "rive/listener_type.hpp"
#include "rive/scene.hpp"

Expand All @@ -20,7 +21,7 @@ class SMINumber;
class SMITrigger;
class Shape;
class StateMachineLayerInstance;
class HitShape;
class HitComponent;
class NestedArtboard;
class Event;
class KeyedProperty;
Expand All @@ -41,23 +42,24 @@ class StateMachineInstance : public Scene
{
friend class SMIInput;
friend class KeyedProperty;
friend class HitComponent;

private:
void markNeedsAdvance();

/// Provide a hitListener if you want to process a down or an up for the pointer position
/// too.
void updateListeners(Vec2D position, ListenerType hitListener);
HitResult updateListeners(Vec2D position, ListenerType hitListener);

template <typename SMType, typename InstType>
InstType* getNamedInput(const std::string& name) const;
void notifyEventListeners(std::vector<EventReport> events, NestedArtboard* source);
void sortHitComponents();

public:
StateMachineInstance(const StateMachine* machine, ArtboardInstance* instance);
StateMachineInstance(StateMachineInstance const&) = delete;
~StateMachineInstance() override;

void markNeedsAdvance();
// Advance the state machine by the specified time. Returns true if the
// state machine will continue to animate after this advance.
bool advance(float seconds);
Expand Down Expand Up @@ -88,9 +90,10 @@ class StateMachineInstance : public Scene

bool advanceAndApply(float secs) override;
std::string name() const override;
void pointerMove(Vec2D position) override;
void pointerDown(Vec2D position) override;
void pointerUp(Vec2D position) override;
HitResult pointerMove(Vec2D position) override;
HitResult pointerDown(Vec2D position) override;
HitResult pointerUp(Vec2D position) override;
HitResult pointerExit(Vec2D position) override;

float durationSeconds() const override { return -1; }
Loop loop() const override { return Loop::oneShot; }
Expand Down Expand Up @@ -125,8 +128,7 @@ class StateMachineInstance : public Scene
std::vector<SMIInput*> m_inputInstances; // we own each pointer
size_t m_layerCount;
StateMachineLayerInstance* m_layers;
std::vector<std::unique_ptr<HitShape>> m_hitShapes;
std::vector<NestedArtboard*> m_hitNestedArtboards;
std::vector<std::unique_ptr<HitComponent>> m_hitComponents;
StateMachineInstance* m_parentStateMachineInstance = nullptr;
NestedArtboard* m_parentNestedArtboard = nullptr;
};
Expand Down
3 changes: 3 additions & 0 deletions include/rive/artboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Artboard : public ArtboardBase, public CoreContext, public ShapePaintConta
std::vector<NestedArtboard*> m_NestedArtboards;
std::vector<Joystick*> m_Joysticks;
bool m_JoysticksApplyBeforeUpdate = true;
bool m_HasChangedDrawOrderInLastUpdate = false;

unsigned int m_DirtDepth = 0;
rcp<RenderPath> m_BackgroundPath;
Expand Down Expand Up @@ -100,6 +101,8 @@ class Artboard : public ArtboardBase, public CoreContext, public ShapePaintConta
void onDirty(ComponentDirt dirt) override;

bool advance(double elapsedSeconds);
bool hasChangedDrawOrderInLastUpdate() { return m_HasChangedDrawOrderInLastUpdate; };
Drawable* firstDrawable() { return m_FirstDrawable; };

enum class DrawOption
{
Expand Down
14 changes: 11 additions & 3 deletions include/rive/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "rive/hit_info.hpp"
#include "rive/renderer.hpp"
#include "rive/clip_result.hpp"
#include "rive/drawable_flag.hpp"
#include <vector>

namespace rive
Expand All @@ -15,6 +16,7 @@ class DrawRules;
class Drawable : public DrawableBase
{
friend class Artboard;
friend class StateMachineInstance;

private:
std::vector<ClippingShape*> m_ClippingShapes;
Expand All @@ -34,9 +36,15 @@ class Drawable : public DrawableBase

inline bool isHidden() const
{
// For now we have a single drawable flag, when we have more we can
// make an actual enum for this.
return (drawableFlags() & 0x1) == 0x1 || hasDirt(ComponentDirt::Collapsed);
return (static_cast<DrawableFlag>(drawableFlags()) & DrawableFlag::Hidden) ==
DrawableFlag::Hidden ||
hasDirt(ComponentDirt::Collapsed);
}

inline bool isTargetOpaque() const
{
return (static_cast<DrawableFlag>(drawableFlags()) & DrawableFlag::Opaque) ==
DrawableFlag::Opaque;
}
};
} // namespace rive
Expand Down
26 changes: 26 additions & 0 deletions include/rive/drawable_flag.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _RIVE_DRAWABLE_FLAGS_HPP_
#define _RIVE_DRAWABLE_FLAGS_HPP_

#include "rive/enum_bitset.hpp"

namespace rive
{
enum class DrawableFlag : unsigned short
{
None = 0,

/// Whether the component should be drawn
Hidden = 1 << 0,

/// Editor only
Locked = 1 << 1,

/// Editor only
Disconnected = 1 << 2,

/// Whether this Component lets hit events pass through to components behind it
Opaque = 1 << 3,
};
RIVE_MAKE_ENUM_BITSET(DrawableFlag)
} // namespace rive
#endif
13 changes: 13 additions & 0 deletions include/rive/hit_result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _RIVE_HIT_RESULT_HPP_
#define _RIVE_HIT_RESULT_HPP_

namespace rive
{
enum class HitResult : uint8_t
{
none,
hit,
hitOpaque,
};
} // namespace rive
#endif
8 changes: 5 additions & 3 deletions include/rive/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "rive/math/vec2d.hpp"
#include "rive/animation/keyed_callback_reporter.hpp"
#include "rive/core/field_types/core_callback_type.hpp"
#include "rive/hit_result.hpp"
#include <string>

namespace rive
Expand Down Expand Up @@ -46,9 +47,10 @@ class Scene : public KeyedCallbackReporter, public CallbackContext

void draw(Renderer*);

virtual void pointerDown(Vec2D);
virtual void pointerMove(Vec2D);
virtual void pointerUp(Vec2D);
virtual HitResult pointerDown(Vec2D);
virtual HitResult pointerMove(Vec2D);
virtual HitResult pointerUp(Vec2D);
virtual HitResult pointerExit(Vec2D);

virtual size_t inputCount() const;
virtual SMIInput* input(size_t index) const;
Expand Down
25 changes: 19 additions & 6 deletions src/animation/nested_state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "rive/animation/nested_number.hpp"
#include "rive/animation/nested_state_machine.hpp"
#include "rive/animation/state_machine_instance.hpp"
#include "rive/hit_result.hpp"

using namespace rive;

Expand Down Expand Up @@ -37,28 +38,40 @@ StateMachineInstance* NestedStateMachine::stateMachineInstance()
return m_StateMachineInstance.get();
}

void NestedStateMachine::pointerMove(Vec2D position)
HitResult NestedStateMachine::pointerMove(Vec2D position)
{
if (m_StateMachineInstance != nullptr)
{
m_StateMachineInstance->pointerMove(position);
return m_StateMachineInstance->pointerMove(position);
}
return HitResult::none;
}

void NestedStateMachine::pointerDown(Vec2D position)
HitResult NestedStateMachine::pointerDown(Vec2D position)
{
if (m_StateMachineInstance != nullptr)
{
m_StateMachineInstance->pointerDown(position);
return m_StateMachineInstance->pointerDown(position);
}
return HitResult::none;
}

void NestedStateMachine::pointerUp(Vec2D position)
HitResult NestedStateMachine::pointerUp(Vec2D position)
{
if (m_StateMachineInstance != nullptr)
{
m_StateMachineInstance->pointerUp(position);
return m_StateMachineInstance->pointerUp(position);
}
return HitResult::none;
}

HitResult NestedStateMachine::pointerExit(Vec2D position)
{
if (m_StateMachineInstance != nullptr)
{
return m_StateMachineInstance->pointerExit(position);
}
return HitResult::none;
}

void NestedStateMachine::addNestedInput(NestedInput* input) { m_nestedInputs.push_back(input); }
Loading

0 comments on commit 71bf9e3

Please sign in to comment.