Skip to content

Commit

Permalink
Xxxx data binding data context
Browse files Browse the repository at this point in the history
some small code improvements
- reorganize code to get things ready for binding text runs
- support to bind color two way
- fix bug: new data binds not updating if they were created in animate mode

Diffs=
9cd8759a0 Xxxx data binding data context (#7454)

Co-authored-by: hernan <hernan@rive.app>
  • Loading branch information
bodymovin and bodymovin committed Jun 21, 2024
1 parent 8068e48 commit 6d00230
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31f5ee5c480ab9b2c1a0d263305f56bfd943d780
9cd8759a02aaa45684e80a8d77671c1536ab387d
4 changes: 1 addition & 3 deletions lib/src/rive_core/artboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,7 @@ class Artboard extends ArtboardBase with ShapePaintContainer {
return;
}
for (final dataBind in dataBinds) {
if (dataBind is DataBindContext) {
dataBind.bindToContext();
}
dataBind.bind();
}
if (isRoot) {
globalDataBinds.clear();
Expand Down
10 changes: 10 additions & 0 deletions lib/src/rive_core/data_bind/context/context_value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

abstract class ContextValue {
ViewModelInstanceValue? source;
ContextValue(this.source);
void apply(Core<CoreContext> core, int propertyKey);
void applyToSource(Core<CoreContext> core, int propertyKey);
void update(Core<CoreContext> core) {}
}
21 changes: 21 additions & 0 deletions lib/src/rive_core/data_bind/context/context_value_color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value.dart';

import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_color.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

class ContextValueColor extends ContextValue {
ContextValueColor(ViewModelInstanceValue? source) : super(source);

@override
void apply(Core<CoreContext> core, int propertyKey) {
if (source?.coreType == ViewModelInstanceColorBase.typeKey) {
final sourceColor = source as ViewModelInstanceColor;

RiveCoreContext.setColor(core, propertyKey, sourceColor.propertyValue);
}
}

@override
void applyToSource(Core<CoreContext> core, int propertyKey) {}
}
17 changes: 17 additions & 0 deletions lib/src/rive_core/data_bind/context/context_value_enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

class ContextValueEnum extends ContextValue {
ContextValueEnum(ViewModelInstanceValue? source) : super(source);

@override
void apply(Core<CoreContext> core, int propertyKey) {
// TODO: @hernan implement
}

@override
void applyToSource(Core<CoreContext> core, int propertyKey) {
// TODO: @hernan implement
}
}
17 changes: 17 additions & 0 deletions lib/src/rive_core/data_bind/context/context_value_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

class ContextValueList extends ContextValue {
ContextValueList(ViewModelInstanceValue? source) : super(source);

@override
void apply(Core<CoreContext> core, int propertyKey) {
// TODO: @hernan implement
}

@override
void applyToSource(Core<CoreContext> core, int propertyKey) {
// TODO: @hernan implement
}
}
25 changes: 25 additions & 0 deletions lib/src/rive_core/data_bind/context/context_value_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value.dart';

import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_number.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

class ContextValueNumber extends ContextValue {
ContextValueNumber(ViewModelInstanceValue? source) : super(source);

@override
void apply(Core<CoreContext> core, int propertyKey) {
if (source?.coreType == ViewModelInstanceNumberBase.typeKey) {
final sourceNumber = source as ViewModelInstanceNumber;

RiveCoreContext.setDouble(core, propertyKey, sourceNumber.propertyValue);
}
}

@override
void applyToSource(Core<CoreContext> core, int propertyKey) {
final value = RiveCoreContext.getDouble(core, propertyKey);
final sourceNumber = source as ViewModelInstanceNumber;
sourceNumber.propertyValue = value;
}
}
25 changes: 25 additions & 0 deletions lib/src/rive_core/data_bind/context/context_value_string.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:rive/src/core/core.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value.dart';

import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_string.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

class ContextValueString extends ContextValue {
ContextValueString(ViewModelInstanceValue? source)
: super(source as ViewModelInstanceString);
@override
void apply(Core<CoreContext> core, int propertyKey) {
if (source?.coreType == ViewModelInstanceStringBase.typeKey) {
final sourceString = source as ViewModelInstanceString;

RiveCoreContext.setString(core, propertyKey, sourceString.propertyValue);
}
}

@override
void applyToSource(Core<CoreContext> core, int propertyKey) {
final value = RiveCoreContext.getString(core, propertyKey);
final sourceString = source as ViewModelInstanceString;
sourceString.propertyValue = value;
}
}
30 changes: 30 additions & 0 deletions lib/src/rive_core/data_bind/data_bind.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import 'package:rive/src/generated/data_bind/data_bind_base.dart';
import 'package:rive/src/rive_core/component.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value_color.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value_number.dart';
import 'package:rive/src/rive_core/data_bind/context/context_value_string.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_color.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_enum.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_list.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_number.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_string.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_value.dart';

export 'package:rive/src/generated/data_bind/data_bind_base.dart';

Expand All @@ -12,6 +22,8 @@ enum BindMode {

class DataBind extends DataBindBase {
Component? target;
ViewModelInstanceValue? source;
ContextValue? contextValue;

@override
void onAddedDirty() {
Expand Down Expand Up @@ -40,4 +52,22 @@ class DataBind extends DataBindBase {
void modeValueChanged(int from, int to) {
// TODO: @hernan implement nameChanged
}

void bind() {
switch (source?.coreType) {
case ViewModelInstanceNumberBase.typeKey:
contextValue = ContextValueNumber(source);
break;
case ViewModelInstanceStringBase.typeKey:
contextValue = ContextValueString(source);
break;
case ViewModelInstanceEnumBase.typeKey:
break;
case ViewModelInstanceListBase.typeKey:
break;
case ViewModelInstanceColorBase.typeKey:
contextValue = ContextValueColor(source);
break;
}
}
}
50 changes: 18 additions & 32 deletions lib/src/rive_core/data_bind/data_bind_context.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:rive/src/generated/data_bind/data_bind_context_base.dart';
import 'package:rive/src/rive_core/component_dirt.dart';
import 'package:rive/src/rive_core/container_component.dart';
import 'package:rive/src/rive_core/data_bind/data_bind.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_color.dart';
import 'package:rive/src/rive_core/viewmodel/viewmodel_instance_number.dart';
Expand Down Expand Up @@ -34,37 +33,32 @@ class DataBindContext extends DataBindContextBase
}
}

void bindToContext() {
@override
void bind() {
final dataContext = artboard?.dataContext;
if (dataContext != null) {
final value = dataContext.getViewModelProperty(_ids);
if (value is ViewModelInstanceNumber) {
value.addDependent(this);
} else if (value is ViewModelInstanceString) {
value.addDependent(this);
} else if (value is ViewModelInstanceColor) {
value.addDependent(this);
if (value != null) {
if (value is ViewModelInstanceNumber) {
value.addDependent(this);
} else if (value is ViewModelInstanceString) {
value.addDependent(this);
} else if (value is ViewModelInstanceColor) {
value.addDependent(this);
}
source = value;
super.bind();
}
}
}

@override
void update(int dirt) {
if (dirt & ComponentDirt.bindings != 0) {
final dataContext = artboard?.dataContext;
if (dataContext != null) {
final value = dataContext.getViewModelProperty(_ids);
if (modeValue == BindMode.oneWay.index ||
modeValue == BindMode.twoWay.index) {
if (value is ViewModelInstanceNumber) {
RiveCoreContext.setDouble(
target!, propertyKey, value.propertyValue);
} else if (value is ViewModelInstanceString) {
RiveCoreContext.setString(
target!, propertyKey, value.propertyValue);
} else if (value is ViewModelInstanceColor) {
RiveCoreContext.setColor(target!, propertyKey, value.propertyValue);
}
if (modeValue == BindMode.oneWay.index ||
modeValue == BindMode.twoWay.index) {
if (contextValue != null) {
contextValue!.apply(target!, propertyKey);
}
}
}
Expand All @@ -75,16 +69,8 @@ class DataBindContext extends DataBindContextBase
void updateSourceBinding() {
if (modeValue == BindMode.oneWayToSource.index ||
modeValue == BindMode.twoWay.index) {
final dataContext = artboard?.dataContext;
if (dataContext != null) {
final property = dataContext.getViewModelProperty(_ids);
if (property is ViewModelInstanceNumber) {
final value = RiveCoreContext.getDouble(target!, propertyKey);
property.propertyValue = value;
} else if (property is ViewModelInstanceString) {
final value = RiveCoreContext.getString(target!, propertyKey);
property.propertyValue = value;
}
if (contextValue != null) {
contextValue!.applyToSource(target!, propertyKey);
}
}
}
Expand Down

0 comments on commit 6d00230

Please sign in to comment.