Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parallel animation added #25

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/components/animation/ParallelAnimationFactory.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
' @import /components/animator/AnimatorFactory.brs
' @import /components/getProperty.brs
' @import /components/rokuComponents/ParallelAnimation.brs

' @class
function ParallelAnimationFactory() as Object
prototype = {}

' Creates ParallelAnimation component with one or more Animations.
' @param {String} name
' @param {Object} [options={}]
' @param {Float} options.delay
' @param {Boolean} options.repeat
' @param {AnimatorFactory~Options} options.animations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AA of {AnimatorFactory~Options}?

According to this, it could be something like:

@param {Object.<string, AnimatorFactory~Options>}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnimatorFactory~Options is already defined in AnimatoFactory.brs so it's ok, isn't it?
Anyway, in my opinion, instead of unclear AA (as Radek mentioned, docs are incorrect now, because AnimatorFactory~Options is a type of value of AA) I'd switch to an array of objects like { elementId, animatorOptions }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnimatorFactory~Options is already defined in AnimatoFactory.brs so it's ok, isn't it?

true, missed that when updating comment 😅 will update my previous comment then

' @param {Node} element
' @returns {Object} - ParallelAnimation component
prototype.createAnimation = function(name as String, options = {} as Object, element = invalid as Object) as Object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

element is unused

parallelAnimationNode = ParallelAnimation()
parallelAnimationNode.setFields({
id: name,
delay: getProperty(options, "delay", 0.001),
repeat: getProperty(options, "repeat", false),
})

factory = AnimatorFactory()
for each animationOption in options.animations.items()
parallelAnimationNode.appendChild(factory.createAnimation(animationOption.key, animationOption.value))
end for
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnimatorFactory.createAnimation has such an interface: function (name as String, options = {} as Object, element = Invalid as Object)
So if you implement my suggestion (https://github.com/getndazn/kopytko-utils/pull/25/files#r1378563587) you could iterate through options.animations objects and code would look clearer:

Suggested change
for each animationOption in options.animations.items()
parallelAnimationNode.appendChild(factory.createAnimation(animationOption.key, animationOption.value))
end for
for each animation in options.animations
parallelAnimationNode.appendChild(factory.createAnimation(animation.elementId, animation.animatorOptions))
end for

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the definition point of view clearer is:

{
  someAnimationConfig: {
     animations: {
       gradient: {
          duration:0.2
          easeFunction: "linear"
          fields: [...]
       }
     }
  }
}

vs

{
   someAnimationConfig: {
     animations: [
      {
        name: "gradient",
        options: {
          duration: 0.2,
          easeFunction: "linear",
          fields: [],
        }
      }
    ]
  }
}

Less code and more compact imho.


return parallelAnimationNode
end function

return prototype
end function
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
' @import /components/KopytkoTestSuite.brs from @dazn/kopytko-unit-testing-framework
' @import /components/rokuComponents/Animation.brs
' @mock /components/animator/AnimatorFactory.brs
function TestSuite__ParallelAnimationFactory() as Object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A unit test testing multiple elements is missing

ts = KopytkoTestSuite()
ts.name = "ParallelAnimatorFactory"

ts.setBeforeEach(sub (ts as Object)
m.__parallelAnimationFactory = ParallelAnimationFactory()

m.__mocks.animatorFactory = {
returnValue: Animation(),
}
end sub)

ts.addTest("it creates animation object with given config", function (ts as Object) as String
' Given
options = {
delay: Csng(0.2),
repeat: true,
animations: {
elementOne: {
delay: Csng(0.2),
duration: Csng(20),
easeFunction: "linear",
easeInPercent: 0.1,
easeOutPercent: 0.2,
optional: false,
repeat: false,
Comment on lines +19 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, aren't fields necessary? Otherwise, it makes no sense to me

},
},
}

' When
_animation = m.__parallelAnimationFactory.createAnimation("testElement", options)

' Then
expectedConfig = {
id: "testElement",
delay: options.delay,
repeat: options.repeat,
type: "ParallelAnimation",
}
constructedConfig = {
id: _animation.id,
delay: Csng(_animation.delay),
repeat: _animation.repeat,
type: _animation.subType(),
}

return ts.assertEqual(expectedConfig, constructedConfig, "Animation config is wrong")
end function)

ts.addTest("it creates child animation object", function (ts as Object) as String
' Given
options = {
delay: Csng(0.2),
repeat: true,
animations: {
elementOne: {
delay: Csng(0.2),
duration: Csng(20),
easeFunction: "linear",
easeInPercent: 0.1,
easeOutPercent: 0.2,
optional: false,
repeat: false,
},
},
}

' When
_animation = m.__parallelAnimationFactory.createAnimation("testElement", options)

' Then
expectedConfig = {
element: invalid,
name: "elementone",
options: {
delay: options.animations.elementOne.delay,
duration: options.animations.elementOne.duration,
easeFunction: options.animations.elementOne.easeFunction,
easeInPercent: options.animations.elementOne.easeInPercent,
easeOutPercent: options.animations.elementOne.easeOutPercent,
optional: options.animations.elementOne.optional,
repeat: options.animations.elementOne.repeat,
},
}

return ts.assertMethodWasCalled("AnimatorFactory.createAnimation", expectedConfig)
end function)

return ts
end function
23 changes: 14 additions & 9 deletions src/components/animator/AnimatorFactory.brs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
' @import /components/rokuComponents/FloatFieldInterpolator.brs
' @import /components/rokuComponents/Vector2DFieldInterpolator.brs

' @typedef {Object} AnimatorFactory~Options
' @property {Float} delay
' @property {Float} duration
' @property {String} easeFunction
' @property {Float} easeInPercent
' @property {Float} easeOutPercent
' @property {Boolean} repeat
' @property {Object[]} fields
' @property {String} fields.field
' @property {Integer[]} fields.key
' @property {Dynamic[]} fields.keyValue
' @property {Boolean} fields.reverse

' @class
function AnimatorFactory() as Object
prototype = {}

' Creates Animation component with one or more interpolators.
' @param {String} name
' @param {Object} [options={}]
' @param {Float} options.delay
' @param {Float} options.duration
' @param {String} options.easeFunction
' @param {Object[]} options.fields
' @param {String} options.fields.field
' @param {Integer[]} options.fields.key
' @param {Dynamic[]} options.fields.keyValue
' @param {Boolean} options.fields.reverse
' @param {AnimatorFactory~Options} [options={}]
' @param {Node} element
' @returns {Object} - Animation component
prototype.createAnimation = function (name as String, options = {} as Object, element = Invalid as Object) as Object
Expand Down
5 changes: 5 additions & 0 deletions src/components/rokuComponents/ParallelAnimation.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
' Wrapper function for creating native ParallelAnimation component.
' @class
function ParallelAnimation() as Object
return CreateObject("roSGNode", "ParallelAnimation")
end function