mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-27 18:28:18 +08:00
Adding machine importer.
This commit is contained in:
@ -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();
|
||||
|
@ -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<CoreContext> 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)) {
|
||||
|
17
lib/src/core/importers/state_machine_importer.dart
Normal file
17
lib/src/core/importers/state_machine_importer.dart
Normal file
@ -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() {
|
||||
}
|
||||
}
|
@ -12,27 +12,6 @@ abstract class StateMachineComponentBase<T extends CoreContext>
|
||||
@override
|
||||
Set<int> 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;
|
||||
|
@ -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;
|
||||
|
@ -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<StateMachineInput>();
|
||||
final StateMachineComponents<StateMachineLayer> layers =
|
||||
StateMachineComponents<StateMachineLayer>();
|
||||
@override
|
||||
bool import(ImportStack stack) {
|
||||
var artboardImporter = stack.latest<ArtboardImporter>(ArtboardBase.typeKey);
|
||||
if (artboardImporter == null) {
|
||||
return false;
|
||||
}
|
||||
artboardImporter.addStateMachine(this);
|
||||
return super.import(stack);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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<Backboard>(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<Core<RiveCoreContext>>(numObjects);
|
||||
// for (int i = 1; i < numObjects; i++) {
|
||||
// Core<CoreContext> 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<Animation>(reader, propertyToField);
|
||||
// if (animation == null) {
|
||||
// continue;
|
||||
// }
|
||||
// artboard.addObject(animation);
|
||||
// animation.artboard = artboard;
|
||||
// if (animation is LinearAnimation) {
|
||||
// var numKeyedObjects = reader.readVarUint();
|
||||
// var keyedObjects = List<KeyedObject>.filled(numKeyedObjects, null);
|
||||
// for (int j = 0; j < numKeyedObjects; j++) {
|
||||
// var keyedObject =
|
||||
// _readRuntimeObject<KeyedObject>(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<KeyedProperty>(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<KeyFrame>(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<CoreContext> _readRuntimeObject(
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
class _ArtboardImportStackObject extends ImportStackObject {
|
||||
final RuntimeArtboard artboard;
|
||||
_ArtboardImportStackObject(this.artboard);
|
||||
|
||||
void addObject(Core<CoreContext> 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<T extends Core<CoreContext>>
|
||||
extends ImportHelper<T> {
|
||||
@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<Animation> {
|
||||
@override
|
||||
void withArtboard(
|
||||
Animation object, _ArtboardImportStackObject artboardStackObject) {
|
||||
object.artboard = artboardStackObject.artboard;
|
||||
}
|
||||
}
|
||||
|
||||
class _LinearAnimationStackObject extends ImportStackObject {
|
||||
final LinearAnimation linearAnimation;
|
||||
final keyedObjects = <KeyedObject>[];
|
||||
|
||||
_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<KeyedObject> {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user