diff options
Diffstat (limited to 'lib/util.moon')
| -rw-r--r-- | lib/util.moon | 62 |
1 files changed, 58 insertions, 4 deletions
diff --git a/lib/util.moon b/lib/util.moon index ed643aa..0f36e02 100644 --- a/lib/util.moon +++ b/lib/util.moon @@ -1,17 +1,71 @@ import Op from require 'core' -class pick extends Op +class switch_ extends Op + @doc: "(switch i v0 [v1 v2...]) - switch between multiple inputs + +when i is true, the first value is reproduced. +when i is false, the second value is reproduced. +when i is a num, it is (floor)ed and the matching argument (starting from 0) is reproduced." + setup: (@i, ...) => @choices = { ... } update: (dt) => @i\update dt + i = @i\get! + for choice in *@choices choice\update dt - i = 1 + (math.floor @i\get!) % #@choices - @value = @choices[i]\get! + active = switch @i\get! + when true + @choices[1] + when false + @choices[2] + else + i = 1 + (math.floor i) % #@choices + @choices[i] + @value = active and active\get! + +class switch_pause extends Op + @doc: "(switch- i v0 [v1 v2...]) - switch and pause multiple inputs + +like (switch ...) except that the unused inputs are paused." + + setup: (@i, ...) => + @choices = { ... } + + update: (dt) => + @i\update dt + i = @i\get! + active = switch @i\get! + when true + @choices[1] + when false + @choices[2] + else + i = 1 + (math.floor i) % #@choices + @choices[i] + + @value = if active + active\update dt + active\get! + +class keep extends Op + @doc: "(keep value) - keep the last non-nil value + +always reproduces the last non-nil value the input produced" + + setup: (@i) => + + update: (dt) => + @i\update dt + + next = @i\get! + @value = next or @value { - :pick + 'switch': switch_ + 'switch-', switch_pause + :keep } |
