diff --git a/lib/src/core/core.dart b/lib/src/core/core.dart index d8a9d08..6d58162 100644 --- a/lib/src/core/core.dart +++ b/lib/src/core/core.dart @@ -10,6 +10,7 @@ export 'package:rive/src/core/importers/artboard_importer.dart'; export 'package:rive/src/core/importers/linear_animation_importer.dart'; export 'package:rive/src/core/importers/keyed_object_importer.dart'; export 'package:rive/src/core/importers/keyed_property_importer.dart'; +export 'package:rive/src/core/importers/state_machine_importer.dart'; typedef PropertyChangeCallback = void Function(dynamic from, dynamic to); typedef BatchAddCallback = void Function(); diff --git a/lib/src/core/importers/artboard_importer.dart b/lib/src/core/importers/artboard_importer.dart index 171960b..105613f 100644 --- a/lib/src/core/importers/artboard_importer.dart +++ b/lib/src/core/importers/artboard_importer.dart @@ -1,5 +1,7 @@ import 'package:rive/rive.dart'; import 'package:rive/src/core/core.dart'; +import 'package:rive/src/rive_core/animation/animation.dart'; +import 'package:rive/src/rive_core/animation/state_machine.dart'; import 'package:rive/src/rive_core/component.dart'; class ArtboardImporter extends ImportStackObject { @@ -8,11 +10,13 @@ class ArtboardImporter extends ImportStackObject { void addComponent(Core object) => artboard.addObject(object); - void addAnimation(LinearAnimation animation) { + void addAnimation(Animation animation) { artboard.addObject(animation); animation.artboard = artboard; } + void addStateMachine(StateMachine animation) => addAnimation(animation); + @override void resolve() { for (final object in artboard.objects.skip(1)) { diff --git a/lib/src/core/importers/state_machine_importer.dart b/lib/src/core/importers/state_machine_importer.dart new file mode 100644 index 0000000..cdeb6a9 --- /dev/null +++ b/lib/src/core/importers/state_machine_importer.dart @@ -0,0 +1,17 @@ +import 'package:rive/src/core/core.dart'; +import 'package:rive/src/rive_core/animation/state_machine.dart'; +import 'package:rive/src/rive_core/animation/state_machine_component.dart'; + +class StateMachineImporter extends ImportStackObject { + final StateMachine machine; + StateMachineImporter(this.machine); + + void addMachineComponent(StateMachineComponent object) { + machine.context.addObject(object); + object.stateMachine = machine; + } + + @override + void resolve() { + } +} diff --git a/lib/src/generated/animation/state_machine_component_base.dart b/lib/src/generated/animation/state_machine_component_base.dart index 6da1182..01e9f84 100644 --- a/lib/src/generated/animation/state_machine_component_base.dart +++ b/lib/src/generated/animation/state_machine_component_base.dart @@ -12,27 +12,6 @@ abstract class StateMachineComponentBase @override Set get coreTypes => {StateMachineComponentBase.typeKey}; - /// -------------------------------------------------------------------------- - /// MachineId field with key 137. - int _machineId; - static const int machineIdPropertyKey = 137; - - /// Id of the state machine this component belongs to. - int get machineId => _machineId; - - /// Change the [_machineId] field value. - /// [machineIdChanged] will be invoked only if the field's value has changed. - set machineId(int value) { - if (_machineId == value) { - return; - } - int from = _machineId; - _machineId = value; - machineIdChanged(from, value); - } - - void machineIdChanged(int from, int to); - /// -------------------------------------------------------------------------- /// Name field with key 138. String _name; diff --git a/lib/src/generated/rive_core_context.dart b/lib/src/generated/rive_core_context.dart index 4927e1a..9c50736 100644 --- a/lib/src/generated/rive_core_context.dart +++ b/lib/src/generated/rive_core_context.dart @@ -287,11 +287,6 @@ class RiveCoreContext { } } break; - case StateMachineComponentBase.machineIdPropertyKey: - if (object is StateMachineComponentBase && value is int) { - object.machineId = value; - } - break; case StateMachineComponentBase.namePropertyKey: if (object is StateMachineComponentBase) { if (value is String) { @@ -855,7 +850,6 @@ class RiveCoreContext { case AnimationStateBase.animationIdPropertyKey: case KeyedObjectBase.objectIdPropertyKey: case TransitionConditionBase.inputIdPropertyKey: - case StateMachineComponentBase.machineIdPropertyKey: case KeyedPropertyBase.propertyKeyPropertyKey: case KeyFrameBase.framePropertyKey: case KeyFrameBase.interpolationTypePropertyKey: @@ -996,8 +990,6 @@ class RiveCoreContext { return (object as KeyedObjectBase).objectId; case TransitionConditionBase.inputIdPropertyKey: return (object as TransitionConditionBase).inputId; - case StateMachineComponentBase.machineIdPropertyKey: - return (object as StateMachineComponentBase).machineId; case KeyedPropertyBase.propertyKeyPropertyKey: return (object as KeyedPropertyBase).propertyKey; case KeyFrameBase.framePropertyKey: @@ -1264,9 +1256,6 @@ class RiveCoreContext { case TransitionConditionBase.inputIdPropertyKey: (object as TransitionConditionBase).inputId = value; break; - case StateMachineComponentBase.machineIdPropertyKey: - (object as StateMachineComponentBase).machineId = value; - break; case KeyedPropertyBase.propertyKeyPropertyKey: (object as KeyedPropertyBase).propertyKey = value; break; diff --git a/lib/src/rive_core/animation/state_machine.dart b/lib/src/rive_core/animation/state_machine.dart index 54e08e7..e4009a9 100644 --- a/lib/src/rive_core/animation/state_machine.dart +++ b/lib/src/rive_core/animation/state_machine.dart @@ -1,6 +1,7 @@ import 'package:rive/src/core/core.dart'; import 'package:rive/src/rive_core/animation/state_machine_input.dart'; import 'package:rive/src/rive_core/animation/state_machine_layer.dart'; +import 'package:rive/src/rive_core/artboard.dart'; import 'package:rive/src/generated/animation/state_machine_base.dart'; export 'package:rive/src/generated/animation/state_machine_base.dart'; @@ -9,4 +10,13 @@ class StateMachine extends StateMachineBase { StateMachineComponents(); final StateMachineComponents layers = StateMachineComponents(); + @override + bool import(ImportStack stack) { + var artboardImporter = stack.latest(ArtboardBase.typeKey); + if (artboardImporter == null) { + return false; + } + artboardImporter.addStateMachine(this); + return super.import(stack); + } } diff --git a/lib/src/rive_core/animation/state_machine_component.dart b/lib/src/rive_core/animation/state_machine_component.dart index b5e1449..c7db867 100644 --- a/lib/src/rive_core/animation/state_machine_component.dart +++ b/lib/src/rive_core/animation/state_machine_component.dart @@ -1,5 +1,4 @@ import 'dart:collection'; -import 'package:flutter/foundation.dart'; import 'package:rive/src/rive_core/animation/state_machine.dart'; import 'package:rive/src/generated/animation/state_machine_component_base.dart'; export 'package:rive/src/generated/animation/state_machine_component_base.dart'; @@ -13,7 +12,6 @@ abstract class StateMachineComponent extends StateMachineComponentBase { } var from = _stateMachine; _stateMachine = value; - machineId = value?.id; machineChanged(from, _stateMachine); } @@ -27,25 +25,12 @@ abstract class StateMachineComponent extends StateMachineComponentBase { } } - @override - @mustCallSuper - void machineIdChanged(int from, int to) { - if (context != null) { - stateMachine = context.resolve(to); - } - } - @override void nameChanged(String from, String to) {} @override void onAdded() {} @override - void onAddedDirty() { - if (machineId != null) { - stateMachine = context?.resolve(machineId); - } - } - + void onAddedDirty() {} @override void onRemoved() { super.onRemoved(); diff --git a/lib/src/rive_file.dart b/lib/src/rive_file.dart index 3946e93..618f1d0 100644 --- a/lib/src/rive_file.dart +++ b/lib/src/rive_file.dart @@ -1,18 +1,17 @@ import 'dart:collection'; import 'dart:typed_data'; -import 'package:meta/meta.dart'; import 'package:rive/src/core/field_types/core_field_type.dart'; import 'package:rive/src/generated/animation/keyed_property_base.dart'; import 'package:rive/src/generated/animation/state_machine_base.dart'; import 'package:rive/src/rive_core/animation/keyed_property.dart'; +import 'package:rive/src/rive_core/animation/state_machine.dart'; import 'package:rive/src/rive_core/component.dart'; import 'package:rive/src/rive_core/runtime/runtime_header.dart'; import 'package:rive/src/rive_core/backboard.dart'; import 'package:rive/src/core/core.dart'; import 'package:rive/src/utilities/binary_buffer/binary_reader.dart'; import 'package:rive/src/rive_core/runtime/exceptions/rive_format_error_exception.dart'; -import 'package:rive/src/rive_core/animation/animation.dart'; import 'package:rive/src/rive_core/animation/keyed_object.dart'; import 'package:rive/src/rive_core/animation/linear_animation.dart'; import 'package:rive/src/rive_core/artboard.dart'; @@ -97,8 +96,9 @@ class RiveFile { break; } case StateMachineBase.typeKey: - // stackObject = _LinearAnimationStackObject(object as LinearAnimation); - // helper = _AnimationImportHelper(); + stackObject = StateMachineImporter(object as StateMachine); + // stackObject = _LinearAnimationStackObject(object as + // LinearAnimation); helper = _AnimationImportHelper(); break; default: if (object is Component) { @@ -123,107 +123,6 @@ class RiveFile { } importStack.resolve(); - // _backboard = _readRuntimeObject(reader, propertyToField); - // if (_backboard == null) { - // throw const RiveFormatErrorException( - // 'expected first object to be a Backboard'); - // } - - // int numArtboards = reader.readVarUint(); - // for (int i = 0; i < numArtboards; i++) { - // var numObjects = reader.readVarUint(); - // if (numObjects == 0) { - // throw const RiveFormatErrorException( - // 'artboards must contain at least one object (themselves)'); - // } - // var artboard = - // _readRuntimeObject(reader, propertyToField, RuntimeArtboard()); - // // Kind of weird, but the artboard is the core context at runtime, so we - // // want other objects to be able to resolve it. It's always at the 0 - // // index. - // artboard?.addObject(artboard); - // _artboards.add(artboard); - // // var objects = List>(numObjects); - // for (int i = 1; i < numObjects; i++) { - // Core object = _readRuntimeObject(reader, propertyToField); - // // N.B. we add objects that don't load (null) too as we need to look - // // them up by index. - // artboard.addObject(object); - // } - - // // Animations also need to reference objects, so make sure they get read - // // in before the hierarchy resolves (batch add completes). - // var numAnimations = reader.readVarUint(); - // for (int i = 0; i < numAnimations; i++) { - // var animation = _readRuntimeObject(reader, propertyToField); - // if (animation == null) { - // continue; - // } - // artboard.addObject(animation); - // animation.artboard = artboard; - // if (animation is LinearAnimation) { - // var numKeyedObjects = reader.readVarUint(); - // var keyedObjects = List.filled(numKeyedObjects, null); - // for (int j = 0; j < numKeyedObjects; j++) { - // var keyedObject = - // _readRuntimeObject(reader, propertyToField); - // if (keyedObject == null) { - // continue; - // } - // keyedObjects[j] = keyedObject; - // artboard.addObject(keyedObject); - - // animation.internalAddKeyedObject(keyedObject); - - // var numKeyedProperties = reader.readVarUint(); - // for (int k = 0; k < numKeyedProperties; k++) { - // var keyedProperty = - // _readRuntimeObject(reader, propertyToField); - // if (keyedProperty == null) { - // continue; - // } - // artboard.addObject(keyedProperty); - // keyedObject.internalAddKeyedProperty(keyedProperty); - - // var numKeyframes = reader.readVarUint(); - // for (int l = 0; l < numKeyframes; l++) { - // var keyframe = - // _readRuntimeObject(reader, propertyToField); - // if (keyframe == null) { - // continue; - // } - // artboard.addObject(keyframe); - // keyedProperty.internalAddKeyFrame(keyframe); - // keyframe.computeSeconds(animation); - // } - // } - // } - - // for (final keyedObject in keyedObjects) { - // keyedObject?.objectId ??= artboard.id; - // } - // } - // } - - // // Any component objects with no id map to the artboard. Skip first item - // // as it's the artboard itself. - // for (final object in artboard.objects.skip(1)) { - // if (object is Component && object.parentId == null) { - // object.parent = artboard; - // } - // object?.onAddedDirty(); - // } - - // assert(!artboard.children.contains(artboard), - // 'artboard should never contain itself as a child'); - // for (final object in artboard.objects.toList(growable: false)) { - // if (object == null) { - // continue; - // } - // object.onAdded(); - // } - // artboard.clean(); - return true; } } @@ -269,92 +168,3 @@ Core _readRuntimeObject( } return object; } - -class _ArtboardImportStackObject extends ImportStackObject { - final RuntimeArtboard artboard; - _ArtboardImportStackObject(this.artboard); - - void addObject(Core object) => artboard.addObject(object); - - @override - void resolve() { - for (final object in artboard.objects.skip(1)) { - if (object is Component && object.parentId == null) { - object.parent = artboard; - } - object?.onAddedDirty(); - } - assert(!artboard.children.contains(artboard), - 'artboard should never contain itself as a child'); - for (final object in artboard.objects.toList(growable: false)) { - if (object == null) { - continue; - } - object.onAdded(); - } - artboard.clean(); - } -} - -class _ArtboardObjectImportHelper> - extends ImportHelper { - @override - bool import(T object, ImportStack stack) { - _ArtboardImportStackObject artboardStackObject = - stack.latest(ArtboardBase.typeKey); - if (artboardStackObject == null) { - return false; - } - artboardStackObject.addObject(object); - withArtboard(object, artboardStackObject); - return true; - } - - @protected - void withArtboard(T object, _ArtboardImportStackObject artboardStackObject) {} -} - -class _AnimationImportHelper extends _ArtboardObjectImportHelper { - @override - void withArtboard( - Animation object, _ArtboardImportStackObject artboardStackObject) { - object.artboard = artboardStackObject.artboard; - } -} - -class _LinearAnimationStackObject extends ImportStackObject { - final LinearAnimation linearAnimation; - final keyedObjects = []; - - _LinearAnimationStackObject(this.linearAnimation); - - void addKeyedObject(KeyedObject object) { - keyedObjects.add(object); - linearAnimation.internalAddKeyedObject(object); - } - - @override - void resolve() { - for (final keyedObject in keyedObjects) { - keyedObject?.objectId ??= linearAnimation.artboard.id; - } - } -} - -class _KeyedObjectImportHelper - extends _ArtboardObjectImportHelper { - @override - bool import(KeyedObject object, ImportStack stack) { - if (!super.import(object, stack)) { - return false; - } - _LinearAnimationStackObject animationStackObject = - stack.latest(LinearAnimationBase.typeKey); - if (animationStackObject == null) { - return false; - } - animationStackObject.addKeyedObject(object); - - return true; - } -}