Skip to content

Commit

Permalink
add util for utility functions
Browse files Browse the repository at this point in the history
Right now this just consists of a clamp function, which is more
semantically intuitive than the old min(max()) dance.
  • Loading branch information
torque committed Nov 5, 2022
1 parent 9325845 commit fc714af
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# The order of the sources does matter.
SOURCES := src/log.moon
SOURCES += src/util.moon
SOURCES += src/requires.moon
SOURCES += src/settings.moon
SOURCES += src/Stack.moon
Expand Down
5 changes: 3 additions & 2 deletions src/Animation.moon
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class Animation

update: ( now ) =>
if @isReversed
@linearProgress = math.max 0, math.min 1, @linearProgress + (@lastUpdate - now)*@durationR
@linearProgress = clamp @linearProgress + (@lastUpdate - now) * @durationR, 0, 1
if @linearProgress == 0
@isFinished = true
else
@linearProgress = math.max 0, math.min 1, @linearProgress + (now - @lastUpdate)*@durationR
@linearProgress = clamp @linearProgress + (now - @lastUpdate) * @durationR, 0, 1
if @linearProgress == 1
@isFinished = true

@lastUpdate = now

progress = math.pow @linearProgress, @accel
Expand Down
10 changes: 5 additions & 5 deletions src/HoverTime.moon
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ class HoverTime extends BarAccent
leftMargin = settings['hover-time-left-margin']
bottomMargin = settings['hover-time-bottom-margin']
offScreenPos = settings['hover-time-offscreen-pos']
@line[2] = ('%g,%g')\format math.min( Window.w - rightMargin, math.max( leftMargin, Mouse.x ) ), @position
@line[2] = ('%g,%g')\format clamp( Mouse.x, leftMargin, Window.w - rightMargin ), @position
@line[1] = ([[{%s%s\pos(]])\format settings['default-style'], settings['hover-time-style']
@animation = Animation offScreenPos, bottomMargin, @animationDuration, @\animate, nil, 0.5

resize: =>
super!
@line[2] = ("%g,%g")\format math.min( Window.w - rightMargin, math.max( leftMargin, Mouse.x ) ), @yPos - @animation.value
@line[2] = ("%g,%g")\format clamp( Mouse.x, leftMargin, Window.w - rightMargin ), @yPos - @animation.value

animate: ( value ) =>
@position = @yPos - value
@line[2] = ("%g,%g")\format math.min( Window.w - rightMargin, math.max( leftMargin, Mouse.x ) ), @position
@line[2] = ("%g,%g")\format clamp( Mouse.x, leftMargin, Window.w - rightMargin ), @position
@needsUpdate = true

redraw: =>
if @active
super!
if Mouse.x != @lastX
@line[2] = ("%g,%g")\format math.min( Window.w - rightMargin, math.max( leftMargin, Mouse.x ) ), @position
@line[2] = ("%g,%g")\format clamp( Mouse.x, leftMargin, Window.w - rightMargin ), @position
@lastX = Mouse.x

hoverTime = mp.get_property_number( 'duration', 0 )*Mouse.x/Window.w
hoverTime = mp.get_property_number( 'duration', 0 ) * Mouse.x / Window.w
if hoverTime != @lastTime
@line[4] = ([[%d:%02d:%02d]])\format math.floor( hoverTime/3600 ), math.floor( (hoverTime/60)%60 ), math.floor( hoverTime%60 )
@lastTime = hoverTime
Expand Down
5 changes: 5 additions & 0 deletions src/util.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_mathmin = math.min
_mathmax = math.max

clamp = ( value, min, max ) ->
return _mathmin( max, _mathmax( value, min ) )

0 comments on commit fc714af

Please sign in to comment.