feat(editor): Expose ScrollConstraint velocity and scrollActive computed properties (#12671) d797171a0b Customer asked whether its possible to hide scrollbars when a scroll constraint is inactive. Rather than implement that behavior, we expose computed velocity values in both x and y directions which will be non-zero when the scroll constraint is being dragged or swiped and released. In addition, we expose a scrollActive computed property that indicates if the scroll constraint is active in any way (dragging, swiped and released until being settled, being dragged by a scrollbar). Co-authored-by: Philip Chung <philterdesign@gmail.com>
diff --git a/.rive_head b/.rive_head index c7a2a83..5f2e560 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -8957983a44b178d3746bf37ebfd27391bf7a1d3d +d797171a0be43ba59fbd9ed031f1505c253f8154
diff --git a/decoders/src/texture_decoder.cpp b/decoders/src/texture_decoder.cpp index f6dcfe3..8d63b04 100644 --- a/decoders/src/texture_decoder.cpp +++ b/decoders/src/texture_decoder.cpp
@@ -67,8 +67,8 @@ return decode_bc_texture(blocks, byteCount, width, height, format); #else fprintf(stderr, - "BC texture not supported " - "(build with --with_rive_bc_decoder)\n"); + "BC texture not supported " + "(build with --with_rive_bc_decoder)\n"); return nullptr; #endif @@ -77,8 +77,8 @@ return decode_etc_texture(blocks, byteCount, width, height, format); #else fprintf(stderr, - "ETC texture not supported " - "(build with --with_rive_etc_decoder)\n"); + "ETC texture not supported " + "(build with --with_rive_etc_decoder)\n"); return nullptr; #endif
diff --git a/dev/defs/constraints/scrolling/scroll_constraint.json b/dev/defs/constraints/scrolling/scroll_constraint.json index faa5be9..6e789ba 100644 --- a/dev/defs/constraints/scrolling/scroll_constraint.json +++ b/dev/defs/constraints/scrolling/scroll_constraint.json
@@ -131,6 +131,39 @@ }, "description": "Minimum threshold in pixels to trigger the scrolling behavior", "bindable": true + }, + "velocityX": { + "type": "double", + "initialValue": "0", + "key": { + "int": 1023, + "string": "velocityx" + }, + "description": "Velocity in the x direction when the constraint is scrolling", + "bindable": true, + "passthrough": true + }, + "velocityY": { + "type": "double", + "initialValue": "0", + "key": { + "int": 1024, + "string": "velocityy" + }, + "description": "Velocity in the y direction when the constraint is scrolling", + "bindable": true, + "passthrough": true + }, + "scrollActive": { + "type": "bool", + "initialValue": "false", + "key": { + "int": 1025, + "string": "scrollactive" + }, + "description": "Whether the scroll constraint is being dragged, is scrolling, or is being moved by a scrollbar constraint", + "bindable": true, + "passthrough": true } } } \ No newline at end of file
diff --git a/include/rive/constraints/scrolling/scroll_bar_constraint_proxy.hpp b/include/rive/constraints/scrolling/scroll_bar_constraint_proxy.hpp index 68ad709..f954acf 100644 --- a/include/rive/constraints/scrolling/scroll_bar_constraint_proxy.hpp +++ b/include/rive/constraints/scrolling/scroll_bar_constraint_proxy.hpp
@@ -23,10 +23,7 @@ bool isOpaque() override { return true; } bool startDrag(Vec2D mousePosition, float timeStamp = 0) override; bool drag(Vec2D mousePosition, float timeStamp = 0) override; - bool endDrag(Vec2D mousePosition, float timeStamp = 0) override - { - return true; - } + bool endDrag(Vec2D mousePosition, float timeStamp = 0) override; }; class TrackDraggableProxy : public DraggableProxy
diff --git a/include/rive/constraints/scrolling/scroll_constraint.hpp b/include/rive/constraints/scrolling/scroll_constraint.hpp index 967ae02..62dbbbf 100644 --- a/include/rive/constraints/scrolling/scroll_constraint.hpp +++ b/include/rive/constraints/scrolling/scroll_constraint.hpp
@@ -26,6 +26,7 @@ ScrollPhysics* m_physics; Mat2D m_scrollTransform; bool m_isDragging = false; + bool m_isScrollBarDragging = false; ScrollVirtualizer* m_virtualizer = nullptr; std::vector<LayoutNodeProvider*> m_layoutChildren; int m_childConstraintAppliedCount = 0; @@ -100,6 +101,17 @@ void setScrollPercentY(float value) override; void setScrollIndex(float value) override; + float velocityX() override; + float velocityY() override; + void setVelocityX(float value) override {} + void setVelocityY(float value) override {} + + bool scrollActive() override; + void setScrollActive(bool value) override {} + + bool isScrollBarDragging() const { return m_isScrollBarDragging; } + void isScrollBarDragging(bool value) { m_isScrollBarDragging = value; } + size_t scrollItemCount(); std::vector<LayoutNodeProvider*>& scrollChildren() {
diff --git a/include/rive/constraints/scrolling/scroll_physics.hpp b/include/rive/constraints/scrolling/scroll_physics.hpp index ad556db..b3bbfee84 100644 --- a/include/rive/constraints/scrolling/scroll_physics.hpp +++ b/include/rive/constraints/scrolling/scroll_physics.hpp
@@ -61,6 +61,7 @@ } virtual void stop() { m_isRunning = false; } virtual void reset(); + Vec2D speed() const { return m_speed; } StatusCode import(ImportStack& importStack) override; /// Initiate animated scroll to target position.
diff --git a/include/rive/generated/constraints/scrolling/scroll_constraint_base.hpp b/include/rive/generated/constraints/scrolling/scroll_constraint_base.hpp index 14f6f66..bf3bb40 100644 --- a/include/rive/generated/constraints/scrolling/scroll_constraint_base.hpp +++ b/include/rive/generated/constraints/scrolling/scroll_constraint_base.hpp
@@ -44,6 +44,9 @@ static const uint16_t infinitePropertyKey = 851; static const uint16_t interactivePropertyKey = 891; static const uint16_t thresholdPropertyKey = 894; + static const uint16_t velocityXPropertyKey = 1023; + static const uint16_t velocityYPropertyKey = 1024; + static const uint16_t scrollActivePropertyKey = 1025; protected: float m_ScrollOffsetX = 0.0f; @@ -192,6 +195,42 @@ thresholdChanged(); } + virtual void setVelocityX(float value) = 0; + virtual float velocityX() = 0; + void velocityX(float value) + { + if (velocityX() == value) + { + return; + } + setVelocityX(value); + velocityXChanged(); + } + + virtual void setVelocityY(float value) = 0; + virtual float velocityY() = 0; + void velocityY(float value) + { + if (velocityY() == value) + { + return; + } + setVelocityY(value); + velocityYChanged(); + } + + virtual void setScrollActive(bool value) = 0; + virtual bool scrollActive() = 0; + void scrollActive(bool value) + { + if (scrollActive() == value) + { + return; + } + setScrollActive(value); + scrollActiveChanged(); + } + Core* clone() const override; void copy(const ScrollConstraintBase& object) { @@ -255,6 +294,9 @@ virtual void infiniteChanged() {} virtual void interactiveChanged() {} virtual void thresholdChanged() {} + virtual void velocityXChanged() {} + virtual void velocityYChanged() {} + virtual void scrollActiveChanged() {} }; } // namespace rive
diff --git a/include/rive/generated/core_registry.hpp b/include/rive/generated/core_registry.hpp index 920eb85..fc1024f 100644 --- a/include/rive/generated/core_registry.hpp +++ b/include/rive/generated/core_registry.hpp
@@ -387,18 +387,12 @@ return new DataEnumSystem(); case ViewModelPropertyViewModelBase::typeKey: return new ViewModelPropertyViewModel(); - case DataEnumValueBase::typeKey: - return new DataEnumValue(); - case ViewModelPropertyTriggerBase::typeKey: - return new ViewModelPropertyTrigger(); - case ViewModelPropertyStringBase::typeKey: - return new ViewModelPropertyString(); - case ViewModelPropertyColorBase::typeKey: - return new ViewModelPropertyColor(); - case ViewModelPropertyBooleanBase::typeKey: - return new ViewModelPropertyBoolean(); case ViewModelInstanceBase::typeKey: return new ViewModelInstance(); + case ViewModelPropertyBooleanBase::typeKey: + return new ViewModelPropertyBoolean(); + case ViewModelPropertyColorBase::typeKey: + return new ViewModelPropertyColor(); case ViewModelPropertyAssetImageBase::typeKey: return new ViewModelPropertyAssetImage(); case ViewModelInstanceBooleanBase::typeKey: @@ -411,12 +405,18 @@ return new ViewModelInstanceTrigger(); case ViewModelInstanceSymbolListIndexBase::typeKey: return new ViewModelInstanceSymbolListIndex(); + case ViewModelPropertyStringBase::typeKey: + return new ViewModelPropertyString(); case ViewModelInstanceViewModelBase::typeKey: return new ViewModelInstanceViewModel(); + case ViewModelPropertyTriggerBase::typeKey: + return new ViewModelPropertyTrigger(); case ViewModelInstanceAssetBase::typeKey: return new ViewModelInstanceAsset(); case ViewModelInstanceAssetImageBase::typeKey: return new ViewModelInstanceAssetImage(); + case DataEnumValueBase::typeKey: + return new DataEnumValue(); case CustomPropertyTriggerBase::typeKey: return new CustomPropertyTrigger(); case ScriptInputTriggerBase::typeKey: @@ -1861,6 +1861,9 @@ case ScrollConstraintBase::interactivePropertyKey: object->as<ScrollConstraintBase>()->interactive(value); break; + case ScrollConstraintBase::scrollActivePropertyKey: + object->as<ScrollConstraintBase>()->scrollActive(value); + break; case ScrollBarConstraintBase::autoSizePropertyKey: object->as<ScrollBarConstraintBase>()->autoSize(value); break; @@ -2396,6 +2399,12 @@ case ScrollConstraintBase::thresholdPropertyKey: object->as<ScrollConstraintBase>()->threshold(value); break; + case ScrollConstraintBase::velocityXPropertyKey: + object->as<ScrollConstraintBase>()->velocityX(value); + break; + case ScrollConstraintBase::velocityYPropertyKey: + object->as<ScrollConstraintBase>()->velocityY(value); + break; case ElasticScrollPhysicsBase::frictionPropertyKey: object->as<ElasticScrollPhysicsBase>()->friction(value); break; @@ -3718,6 +3727,8 @@ return object->as<ScrollConstraintBase>()->infinite(); case ScrollConstraintBase::interactivePropertyKey: return object->as<ScrollConstraintBase>()->interactive(); + case ScrollConstraintBase::scrollActivePropertyKey: + return object->as<ScrollConstraintBase>()->scrollActive(); case ScrollBarConstraintBase::autoSizePropertyKey: return object->as<ScrollBarConstraintBase>()->autoSize(); case NestedArtboardBase::isPausedPropertyKey: @@ -3848,6 +3859,10 @@ return object->as<ScrollConstraintBase>()->scrollIndex(); case ScrollConstraintBase::thresholdPropertyKey: return object->as<ScrollConstraintBase>()->threshold(); + case ScrollConstraintBase::velocityXPropertyKey: + return object->as<ScrollConstraintBase>()->velocityX(); + case ScrollConstraintBase::velocityYPropertyKey: + return object->as<ScrollConstraintBase>()->velocityY(); case ElasticScrollPhysicsBase::frictionPropertyKey: return object->as<ElasticScrollPhysicsBase>()->friction(); case ElasticScrollPhysicsBase::speedMultiplierPropertyKey: @@ -4563,6 +4578,7 @@ case ScrollConstraintBase::virtualizePropertyKey: case ScrollConstraintBase::infinitePropertyKey: case ScrollConstraintBase::interactivePropertyKey: + case ScrollConstraintBase::scrollActivePropertyKey: case ScrollBarConstraintBase::autoSizePropertyKey: case NestedArtboardBase::isPausedPropertyKey: case NestedArtboardBase::isStatefulPropertyKey: @@ -4619,6 +4635,8 @@ case ScrollConstraintBase::scrollPercentYPropertyKey: case ScrollConstraintBase::scrollIndexPropertyKey: case ScrollConstraintBase::thresholdPropertyKey: + case ScrollConstraintBase::velocityXPropertyKey: + case ScrollConstraintBase::velocityYPropertyKey: case ElasticScrollPhysicsBase::frictionPropertyKey: case ElasticScrollPhysicsBase::speedMultiplierPropertyKey: case ElasticScrollPhysicsBase::elasticFactorPropertyKey: @@ -5441,6 +5459,8 @@ return object->is<ScrollConstraintBase>(); case ScrollConstraintBase::interactivePropertyKey: return object->is<ScrollConstraintBase>(); + case ScrollConstraintBase::scrollActivePropertyKey: + return object->is<ScrollConstraintBase>(); case ScrollBarConstraintBase::autoSizePropertyKey: return object->is<ScrollBarConstraintBase>(); case NestedArtboardBase::isPausedPropertyKey: @@ -5593,6 +5613,10 @@ return object->is<ScrollConstraintBase>(); case ScrollConstraintBase::thresholdPropertyKey: return object->is<ScrollConstraintBase>(); + case ScrollConstraintBase::velocityXPropertyKey: + return object->is<ScrollConstraintBase>(); + case ScrollConstraintBase::velocityYPropertyKey: + return object->is<ScrollConstraintBase>(); case ElasticScrollPhysicsBase::frictionPropertyKey: return object->is<ElasticScrollPhysicsBase>(); case ElasticScrollPhysicsBase::speedMultiplierPropertyKey:
diff --git a/src/constraints/scrolling/scroll_bar_constraint_proxy.cpp b/src/constraints/scrolling/scroll_bar_constraint_proxy.cpp index 3743036..10220f5 100644 --- a/src/constraints/scrolling/scroll_bar_constraint_proxy.cpp +++ b/src/constraints/scrolling/scroll_bar_constraint_proxy.cpp
@@ -14,6 +14,13 @@ bool ThumbDraggableProxy::startDrag(Vec2D mousePosition, float timeStamp) { m_lastPosition = mousePosition; + m_constraint->scrollConstraint()->isScrollBarDragging(true); + return true; +} + +bool ThumbDraggableProxy::endDrag(Vec2D mousePosition, float timeStamp) +{ + m_constraint->scrollConstraint()->isScrollBarDragging(false); return true; }
diff --git a/src/constraints/scrolling/scroll_constraint.cpp b/src/constraints/scrolling/scroll_constraint.cpp index 32c6ed2..4073a89 100644 --- a/src/constraints/scrolling/scroll_constraint.cpp +++ b/src/constraints/scrolling/scroll_constraint.cpp
@@ -417,6 +417,22 @@ return infinite() ? contentHeight() : maxOffsetY(); } +float ScrollConstraint::velocityX() +{ + return m_physics != nullptr ? m_physics->speed().x : 0.0f; +} + +float ScrollConstraint::velocityY() +{ + return m_physics != nullptr ? m_physics->speed().y : 0.0f; +} + +bool ScrollConstraint::scrollActive() +{ + return m_isDragging || m_isScrollBarDragging || + (m_physics != nullptr && m_physics->isRunning()); +} + float ScrollConstraint::scrollPercentX() { return maxOffsetX() != 0 ? scrollOffsetX() / maxOffsetXForPercent() : 0;
diff --git a/src/constraints/scrolling/scroll_physics.cpp b/src/constraints/scrolling/scroll_physics.cpp index fec46c3..12612c0 100644 --- a/src/constraints/scrolling/scroll_physics.cpp +++ b/src/constraints/scrolling/scroll_physics.cpp
@@ -45,6 +45,7 @@ std::chrono::high_resolution_clock::now().time_since_epoch()) .count(); } + m_speed = Vec2D(); m_acceleration = Vec2D(); stop(); }
diff --git a/tests/unit_tests/runtime/scroll_velocity_test.cpp b/tests/unit_tests/runtime/scroll_velocity_test.cpp new file mode 100644 index 0000000..7ada556 --- /dev/null +++ b/tests/unit_tests/runtime/scroll_velocity_test.cpp
@@ -0,0 +1,172 @@ +#include "rive/constraints/scrolling/scroll_constraint.hpp" +#include "rive/layout_component.hpp" +#include "utils/no_op_factory.hpp" +#include "rive_file_reader.hpp" +#include "rive_testing.hpp" +#include <catch.hpp> +#include <cstdio> + +TEST_CASE("ScrollConstraint velocity and scrollActive during drag", + "[layoutscroll]") +{ + auto file = ReadRiveFile("assets/layout/layout_scroll_vertical.riv"); + auto artboard = file->artboard(); + auto artboardInstance = artboard->instance(); + auto stateMachine = artboard->stateMachine("State Machine 1"); + REQUIRE(stateMachine != nullptr); + + rive::StateMachineInstance* smi = + new rive::StateMachineInstance(stateMachine, artboardInstance.get()); + + auto scroll = artboardInstance->find<rive::ScrollConstraint>()[0]; + REQUIRE(scroll != nullptr); + + artboardInstance->advance(0.0f); + + // Initially idle: velocity is 0, scrollActive is false. + REQUIRE(scroll->velocityX() == 0.0f); + REQUIRE(scroll->velocityY() == 0.0f); + REQUIRE(scroll->scrollActive() == false); + + // Hover over the scroll area. + smi->pointerMove(rive::Vec2D(50.0f, 250.0f)); + + // Pointer down: starts drag. + smi->pointerDown(rive::Vec2D(50.0f, 250.0f)); + artboardInstance->advance(0.1f); + smi->advanceAndApply(0.1f); + + // isDragging is now true, so scrollActive should be true even with no + // movement. + REQUIRE(scroll->scrollActive() == true); + REQUIRE(scroll->velocityY() == 0.0f); + + // Drag upward 200px. + smi->pointerMove(rive::Vec2D(50.0f, 50.0f)); + artboardInstance->advance(0.0f); + smi->advanceAndApply(0.0f); + + // Velocity should be non-zero after movement. + REQUIRE(scroll->velocityY() != 0.0f); + REQUIRE(scroll->scrollActive() == true); + + // Pause: hold still (no pointer move). scrollActive remains true because + // we're still dragging. + smi->advanceAndApply(0.1f); + REQUIRE(scroll->scrollActive() == true); + + // Release: triggers physics. + smi->pointerUp(rive::Vec2D(50.0f, 50.0f)); + + REQUIRE(scroll->physics()->isRunning() == true); + REQUIRE(scroll->scrollActive() == true); + + delete smi; +} + +TEST_CASE("ScrollConstraint velocity resets after physics settles", + "[layoutscroll]") +{ + auto file = ReadRiveFile("assets/layout/layout_scroll_vertical.riv"); + auto artboard = file->artboard(); + auto artboardInstance = artboard->instance(); + auto stateMachine = artboard->stateMachine("State Machine 1"); + REQUIRE(stateMachine != nullptr); + + rive::StateMachineInstance* smi = + new rive::StateMachineInstance(stateMachine, artboardInstance.get()); + + auto scroll = artboardInstance->find<rive::ScrollConstraint>()[0]; + REQUIRE(scroll != nullptr); + + artboardInstance->advance(0.0f); + + // Swipe: down → move → up. + smi->pointerMove(rive::Vec2D(50.0f, 250.0f)); + smi->pointerDown(rive::Vec2D(50.0f, 250.0f)); + artboardInstance->advance(0.1f); + smi->advanceAndApply(0.1f); + + smi->pointerMove(rive::Vec2D(50.0f, 50.0f)); + artboardInstance->advance(0.0f); + smi->advanceAndApply(0.0f); + + smi->pointerUp(rive::Vec2D(50.0f, 50.0f)); + + REQUIRE(scroll->physics()->isRunning() == true); + REQUIRE(scroll->scrollActive() == true); + + // Advance until physics settles. + float dt = 0.016f; + for (int i = 0; i < 600; i++) + { + smi->advanceAndApply(dt); + if (!scroll->physics()->isRunning()) + { + break; + } + } + + REQUIRE(scroll->physics()->isRunning() == false); + REQUIRE(scroll->scrollActive() == false); + // After reset, velocity should be zero. + REQUIRE(scroll->velocityX() == 0.0f); + REQUIRE(scroll->velocityY() == 0.0f); + + delete smi; +} + +TEST_CASE("ScrollConstraint horizontal velocity", "[layoutscroll]") +{ + auto file = ReadRiveFile("assets/layout/layout_scroll_horizontal.riv"); + auto artboard = file->artboard(); + auto artboardInstance = artboard->instance(); + auto stateMachine = artboard->stateMachine("State Machine 1"); + REQUIRE(stateMachine != nullptr); + + rive::StateMachineInstance* smi = + new rive::StateMachineInstance(stateMachine, artboardInstance.get()); + + auto scroll = artboardInstance->find<rive::ScrollConstraint>()[0]; + REQUIRE(scroll != nullptr); + + artboardInstance->advance(0.0f); + + smi->pointerMove(rive::Vec2D(250.0f, 50.0f)); + smi->pointerDown(rive::Vec2D(250.0f, 50.0f)); + artboardInstance->advance(0.1f); + smi->advanceAndApply(0.1f); + + // Drag left 200px. + smi->pointerMove(rive::Vec2D(50.0f, 50.0f)); + artboardInstance->advance(0.0f); + smi->advanceAndApply(0.0f); + + // Horizontal velocity should be non-zero, vertical should be ~0. + REQUIRE(scroll->velocityX() != 0.0f); + REQUIRE(scroll->velocityY() == 0.0f); + REQUIRE(scroll->scrollActive() == true); + + smi->pointerUp(rive::Vec2D(50.0f, 50.0f)); + + delete smi; +} + +TEST_CASE("ScrollConstraint scrollActive false when idle", "[layoutscroll]") +{ + auto file = ReadRiveFile("assets/layout/layout_scroll_vertical.riv"); + auto artboard = file->artboard(); + auto artboardInstance = artboard->instance(); + + auto scroll = artboardInstance->find<rive::ScrollConstraint>()[0]; + REQUIRE(scroll != nullptr); + + artboardInstance->advance(0.0f); + + // Programmatic scroll (no drag, no physics). + scroll->setScrollPercentY(0.5f); + + REQUIRE(scroll->scrollActive() == false); + REQUIRE(scroll->velocityX() == 0.0f); + REQUIRE(scroll->velocityY() == 0.0f); +}