EriKWDev / nanim

Nanim is an easy-to-use framework to create smooth GPU-accelerated animations that can be previewed live inside a glfw window and, when ready, rendered to videos at an arbitrary resolution and framerate.
MIT License
119 stars 3 forks source link

Templetize Simple Tweens using Nim's Templates/Macros #2

Open EriKWDev opened 3 years ago

EriKWDev commented 3 years ago

I tried doing a simpleSingleValueTween template here, but when I did some testing later it seemed like it really didn't work as I expected. Currently, a lot of tweens follow a very copy-paste-ey pattern which seems ideal for a template, but I currently don't know enough about the system to get it to work.

Anyone with more experience in Nim or time to get it to function properly are welcome to Fix the issue :)

This is what my attempt looked like:

template simpleSingleValueTween*(target: typed, startValue: typed, endValue: typed, valueName: untyped) =
  var interpolators: seq[proc(t: float)]

  let interpolator = proc(t: float) =
    target.valueName = interpolate(startValue, endValue, t)

  interpolators.add(interpolator)

  target.valueName = endValue

  result = newTween(interpolators)

but it seemed like the untyped valueName didn't quite work. It would also be nice to support multiple valueNames in a vararg[untyped] kind-of way, but that would require a macro.

EriKWDev commented 2 years ago

The relevant file in the project is src/nanim/animation/animations.nim (https://github.com/EriKWDev/nanim/blob/fea656276374bbbb673b87245f0503f56fe707ec/src/nanim/animation/animations.nim)

All those animations are very copy-paste-y and would benefit enormously from template or macro.