OXT Community Stacks

The scripts of Easing Demo

Version 1.0. These are the scripts exactly as they are in the stack that plays here. Each line has its number, and a link to a line keeps it.

the stack script

Easing Demo.oxtstack, stack "Easing Demo", 298 lines

-- A demo of the Easing library (libraries/easing in the OXT Community
-- Stacks): one dot for each curve, all crossing the window in the same
-- time, so that the eye can tell the curves apart. Click anywhere, or
-- the button, to send them off again.
--
-- This is a script-only stack: it has no controls of its own on disk,
-- and builds them when it opens. The library is carried below, word for
-- word, so that the demo plays on its own; a stack of yours would
-- install the library instead and call its handlers the same way.

constant kRowHeight = 26
constant kTop = 92
constant kLeft = 150
constant kDuration = 1600

local sStarted, sRunning

on preOpenStack
   edBuild
end preOpenStack

on openStack
   edRun
end openStack

on closeStack
   put false into sRunning
end closeStack

on mouseUp
   edRun
end mouseUp

command edBuild
   local tName, tRow, tY, tRight
   lock screen
   lock messages
   set the defaultStack to the short name of me
   set the width of me to 640
   set the height of me to kTop + kRowHeight * the number of items of ezNames() + 24
   set the backgroundColor of me to "24,28,38"
   repeat while the number of controls of card 1 of me > 0
      delete control 1 of card 1 of me
   end repeat
   put the width of me - 40 into tRight

   create field "Title"
   set the rect of field "Title" to 20, 16, 440, 48
   set the text of field "Title" to "Easing: sixteen ways to get there"
   set the textSize of field "Title" to 20
   set the textStyle of field "Title" to "bold"
   edQuiet the long id of field "Title"

   create field "Hint"
   set the rect of field "Hint" to 20, 50, 520, 74
   set the text of field "Hint" to "Every dot takes the same time. Click to run them again."
   set the textSize of field "Hint" to 13
   edQuiet the long id of field "Hint"
   set the foregroundColor of field "Hint" to "160,170,190"

   create button "Again"
   set the rect of button "Again" to tRight - 90, 18, tRight, 46
   set the label of button "Again" to "Run again"

   put 0 into tRow
   repeat for each item tName in ezNames()
      put kTop + tRow * kRowHeight into tY
      create field ("Name" && tName)
      set the rect of it to 20, tY - 10, kLeft - 20, tY + 12
      set the text of it to tName
      set the textSize of it to 13
      edQuiet the long id of it
      create graphic ("Track" && tName)
      set the style of it to "line"
      set the points of it to kLeft & comma & tY & return & tRight & comma & tY
      set the foregroundColor of it to "52,60,78"
      create graphic ("Dot" && tName)
      set the style of it to "oval"
      set the opaque of it to true
      set the lineSize of it to 0
      set the rect of it to kLeft - 8, tY - 8, kLeft + 8, tY + 8
      set the backgroundColor of it to ezBetween("255,170,60", "90,200,255", tRow / 15)
      add 1 to tRow
   end repeat
   unlock messages
   unlock screen
end edBuild

-- A field that only shows words: no border, no typing, nothing behind it
private command edQuiet pField
   set the lockText of pField to true
   set the traversalOn of pField to false
   set the opaque of pField to false
   set the showBorder of pField to false
   set the threeD of pField to false
   set the foregroundColor of pField to "236,240,248"
end edQuiet

command edRun
   put the milliseconds into sStarted
   if sRunning is not true then
      put true into sRunning
      edStep
   end if
end edRun

-- One frame: every dot goes where its curve says it is by now
on edStep
   local tT, tName, tRow, tY, tRight
   if sRunning is not true then exit edStep
   if there is not a stack "Easing Demo" then exit edStep
   put ezProgress(sStarted, kDuration) into tT
   put the width of stack "Easing Demo" - 40 into tRight
   lock screen
   put 0 into tRow
   repeat for each item tName in ezNames()
      put kTop + tRow * kRowHeight into tY
      set the loc of graphic ("Dot" && tName) of card 1 of stack "Easing Demo" to \
            ezBetween(kLeft & comma & tY, tRight & comma & tY, tT, tName)
      add 1 to tRow
   end repeat
   unlock screen
   if tT < 1 then
      send "edStep" to me in 16 milliseconds
   else
      put false into sRunning
   end if
end edStep

-- ====================================================================
-- The Easing library, as it is in libraries/easing/1.0 of the OXT
-- Community Stacks, carried here so that this demo plays on its own.
-- ====================================================================
/**
Title: Easing
Author: OXT-Beyond contributors
Version: 1.0

Easing curves for smooth movement. A curve turns "how far through the
time" (0 at the start, 1 at the end) into "how far through the change":
slow at first and fast at the end, fast at first with a gentle stop, with
a bounce, and so on.

   put ezProgress(sStarted, 1200) into tT        -- 0 to 1 over 1.2 seconds
   set the loc of button "Ball" to ezBetween("40,200", "560,200", tT, "outBounce")

Every handler here starts with "ez", so that it gets in the way of no
other library's.
*/

constant kEzNames = "linear,inQuad,outQuad,inOutQuad,inCubic,outCubic,inOutCubic,inSine,outSine,inOutSine,inExpo,outExpo,inOutExpo,outBack,outElastic,outBounce"

/**
Summary: Lists the names of the curves.

Returns: The names, with commas between them. "linear" is the first.

Description:
Use <ezNames> to offer the curves in a menu, or to try each in turn.
*/
function ezNames
   return kEzNames
end ezNames

/**
Summary: How far through a change a curve is, at a moment of its time.

Parameters:
pName: The name of a curve, one of <ezNames>. Any other name is "linear".
pT: How far through the time: 0 at the start, 1 at the end. Less than 0 is 0 and more than 1 is 1.

Returns: How far through the change: 0 at the start and 1 at the end. "outBack" and "outElastic" go a little past 1 on the way.

Description:
Use <ezCurve> when you do the arithmetic yourself. <ezBetween> does it
for a number, a point, a rectangle or a colour.
*/
function ezCurve pName, pT
   local tU
   if pT is not a number then return 0
   put max(0, min(1, pT)) into pT
   put 1 - pT into tU
   switch pName
      case "inQuad"
         return pT * pT
      case "outQuad"
         return 1 - tU * tU
      case "inOutQuad"
         if pT < 0.5 then return 2 * pT * pT
         return 1 - 2 * tU * tU
      case "inCubic"
         return pT * pT * pT
      case "outCubic"
         return 1 - tU * tU * tU
      case "inOutCubic"
         if pT < 0.5 then return 4 * pT * pT * pT
         return 1 - 4 * tU * tU * tU
      case "inSine"
         return 1 - cos(pT * pi / 2)
      case "outSine"
         return sin(pT * pi / 2)
      case "inOutSine"
         return (1 - cos(pi * pT)) / 2
      case "inExpo"
         if pT = 0 then return 0
         return 2 ^ (10 * pT - 10)
      case "outExpo"
         if pT = 1 then return 1
         return 1 - 2 ^ (-10 * pT)
      case "inOutExpo"
         if pT = 0 or pT = 1 then return pT
         if pT < 0.5 then return 2 ^ (20 * pT - 10) / 2
         return (2 - 2 ^ (-20 * pT + 10)) / 2
      case "outBack"
         return 1 - 2.70158 * tU * tU * tU + 1.70158 * tU * tU
      case "outElastic"
         if pT = 0 or pT = 1 then return pT
         return 2 ^ (-10 * pT) * sin((pT * 10 - 0.75) * (2 * pi / 3)) + 1
      case "outBounce"
         return ezBounce(pT)
   end switch
   return pT
end ezCurve

private function ezBounce pT
   if pT < 1 / 2.75 then
      return 7.5625 * pT * pT
   else if pT < 2 / 2.75 then
      subtract 1.5 / 2.75 from pT
      return 7.5625 * pT * pT + 0.75
   else if pT < 2.5 / 2.75 then
      subtract 2.25 / 2.75 from pT
      return 7.5625 * pT * pT + 0.9375
   end if
   subtract 2.625 / 2.75 from pT
   return 7.5625 * pT * pT + 0.984375
end ezBounce

/**
Summary: A value part of the way from one value to another, along a curve.

Parameters:
pFrom: Where the change starts: a number, or numbers with commas between them (a point, a rectangle, a colour).
pTo: Where it ends: as many numbers as pFrom has.
pT: How far through the time: 0 at the start, 1 at the end.
pName: The name of a curve, one of <ezNames>. Left out, it is "linear".

Returns: The value at that moment, in the shape of pFrom. A number is whole where both of its ends are whole, so that a point, a rectangle or a colour can be set as it comes.

Description:
Use <ezBetween> to move, resize or recolour something a step at a time:

   set the backgroundColor of graphic "Sky" to ezBetween("20,30,80", "250,180,90", tT, "inOutSine")
*/
function ezBetween pFrom, pTo, pT, pName
   local tK, tResult, tIndex, tA, tB, tValue
   put ezCurve(pName, pT) into tK
   put 0 into tIndex
   repeat for each item tA in pFrom
      add 1 to tIndex
      put item tIndex of pTo into tB
      if tA is not a number or tB is not a number then
         put tA into tValue
      else
         put tA + (tB - tA) * tK into tValue
         if tA is an integer and tB is an integer then
            put round(tValue) into tValue
         end if
      end if
      put tValue into item tIndex of tResult
   end repeat
   return tResult
end ezBetween

/**
Summary: How far through its time something is that started a while ago.

Parameters:
pStarted: The milliseconds when it started.
pDuration: How long it takes, in milliseconds.

Returns: 0 at the start, 1 once the time is up, and the part of the time that has passed in between.

Description:
Use <ezProgress> to get the pT that <ezCurve> and <ezBetween> ask for:

   put the milliseconds into sStarted
   ...
   put ezProgress(sStarted, 800) into tT
   if tT < 1 then send "moveOn" to me in 16 milliseconds
*/
function ezProgress pStarted, pDuration
   if pStarted is not a number or pDuration is not a number or pDuration <= 0 then
      return 1
   end if
   return max(0, min(1, (the milliseconds - pStarted) / pDuration))
end ezProgress