-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
player options now go through a broker
- split up the different actions on a model into their own files to make it easier to understand - introduced a broker for dispatching actions. this will ultimately be how the extension talks to timelines - fixed some cyclical conditions that caused update to run two or three times in one frame in certain situations
- Loading branch information
1 parent
c9e738a
commit 4065e0b
Showing
44 changed files
with
379 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { loopOff } from '../core/timeloop' | ||
import { publish } from '../core/events' | ||
import { CANCEL, S_INACTIVE, _ } from '../utils/constants' | ||
import { all } from '../utils/lists' | ||
|
||
export const cancel = (model: ITimelineModel) => { | ||
all(model.players, effect => effect.cancel()) | ||
|
||
model.state = S_INACTIVE | ||
model.time = _ | ||
model.round = _ | ||
model.players = _ | ||
|
||
loopOff(model.id) | ||
publish(model.id, CANCEL, _) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { unsubscribeAll } from '../core/events' | ||
import { destroyModel } from '../model/store' | ||
import { cancel } from './cancel' | ||
|
||
export const destroy = (model: ITimelineModel) => { | ||
cancel(model) | ||
unsubscribeAll(model.id) | ||
destroyModel(model.id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { loopOff } from '../core/timeloop' | ||
import { publish } from '../core/events' | ||
import { ITimelineModel } from '../core/types' | ||
import { UPDATE, FINISH, S_FINISHED } from '../utils/constants' | ||
import { destroy } from './destroy' | ||
import { update } from './update' | ||
|
||
export const finish = (model: ITimelineModel) => { | ||
model.round = 0 | ||
model.state = S_FINISHED | ||
|
||
if (!model.yoyo) { | ||
model.time = model.rate < 0 ? 0 : model.duration | ||
} | ||
|
||
loopOff(model.id) | ||
update(model) | ||
publish(model.id, UPDATE, model.time) | ||
publish(model.id, FINISH, model.time) | ||
|
||
if (model.destroy) { | ||
destroy(model) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { publish } from '../core/events' | ||
import { loopOff } from '../core/timeloop' | ||
import { S_PAUSED, PAUSE } from '../utils/constants' | ||
import { update } from './update' | ||
|
||
export const pause = (model: ITimelineModel) => { | ||
model.state = S_PAUSED | ||
|
||
loopOff(model.id) | ||
update(model) | ||
publish(model.id, PAUSE, model.time) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { PlayOptions, ITimelineModel } from '../core/types' | ||
import { loopOn } from '../core/timeloop' | ||
import { publish } from '../core/events' | ||
import { S_PLAYING, PLAY } from '../utils/constants' | ||
import { update } from './update' | ||
|
||
export const play = (model: ITimelineModel, options: PlayOptions) => { | ||
if (options) { | ||
model.repeat = options.repeat | ||
model.yoyo = options.alternate === true | ||
model.destroy = !!options.destroy | ||
} | ||
|
||
model.repeat = model.repeat || 1 | ||
model.yoyo = model.yoyo || false | ||
model.state = S_PLAYING | ||
|
||
// set current time (this will automatically start playing when the state is running) | ||
const isForwards = model.rate >= 0 | ||
if (isForwards && model.time === model.duration) { | ||
model.time = 0 | ||
} else if (!isForwards && model.time === 0) { | ||
model.time = model.duration | ||
} | ||
|
||
loopOn(model.id) | ||
update(model) | ||
publish(model.id, PLAY, model.time) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { publish } from '../core/events' | ||
import { REVERSE } from '../utils/constants' | ||
import { update } from './update' | ||
|
||
export const reverse = (model: ITimelineModel) => { | ||
model.rate *= -1 | ||
update(model) | ||
publish(model.id, REVERSE, model.time) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ITimelineModel, AnimationPlayer } from '../core/types' | ||
import { plugins } from '../core/plugins' | ||
import { getEffects } from '../model/effects' | ||
import { all, push } from '../utils/lists' | ||
import { max } from '../utils/math' | ||
import { _ } from '../utils/constants' | ||
|
||
export const setup = (model: ITimelineModel) => { | ||
const animations: AnimationPlayer[] = [] | ||
|
||
all(getEffects(model), effect => { | ||
const controller = plugins[effect.plugin].animate(effect) as AnimationPlayer | ||
if (controller) { | ||
// controller.config = effect.config | ||
controller.from = effect.from | ||
controller.to = effect.to | ||
push(animations, controller) | ||
} | ||
}) | ||
|
||
// change duration to max to | ||
model.duration = max.apply(_, animations.filter(a => isFinite(a.to)).map(a => a.to)) | ||
model.time = isFinite(model.time) ? model.time : model.rate < 0 ? model.duration : 0 | ||
model.players = animations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { S_STARTING, S_PLAYING, _ } from '../utils/constants' | ||
import { inRange } from '../utils/math' | ||
import { finish } from './finish' | ||
import { update } from './update' | ||
|
||
export const tick = (model: ITimelineModel, delta: number) => { | ||
// calculate running range | ||
const duration = model.duration | ||
const repeat = model.repeat | ||
const rate = model.rate | ||
const isReversed = rate < 0 | ||
|
||
// set time use existing | ||
let time = model.time === _ ? (rate < 0 ? duration : 0) : model.time | ||
|
||
let iteration = model.round || 0 | ||
|
||
if (model.state === S_STARTING) { | ||
model.state = S_PLAYING | ||
|
||
// reset position properties if necessary | ||
if (time === _ || (isReversed && time > duration) || (!isReversed && time < 0)) { | ||
// if at finish, reset to start time | ||
time = isReversed ? duration : 0 | ||
} | ||
if (iteration === repeat) { | ||
// if at finish reset iterations to 0 | ||
iteration = 0 | ||
} | ||
} | ||
|
||
time += delta * rate | ||
|
||
// check if timeline has finished | ||
let iterationEnded = false | ||
if (!inRange(time, 0, duration)) { | ||
model.round = ++iteration | ||
time = isReversed ? 0 : duration | ||
iterationEnded = true | ||
|
||
// reverse direction on alternate | ||
if (model.yoyo) { | ||
model.rate = (model.rate || 0) * -1 | ||
} | ||
|
||
// reset the clock | ||
time = model.rate < 0 ? duration : 0 | ||
} | ||
|
||
// call update | ||
model.time = time | ||
model.round = iteration | ||
|
||
if (iterationEnded && repeat === iteration) { | ||
// end the cycle | ||
finish(model) | ||
return | ||
} | ||
|
||
// if not the last iteration reprocess this tick from the new starting point/direction | ||
update(model) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { publish } from '../core/events' | ||
import { loopOff } from '../core/timeloop' | ||
import { S_PLAYING, UPDATE, _ } from '../utils/constants' | ||
import { inRange, minMax, flr } from '../utils/math' | ||
import { all } from '../utils/lists' | ||
import { setup } from './setup' | ||
|
||
export const update = (model: ITimelineModel) => { | ||
// setup effects if required | ||
if (model.players === _) { | ||
setup(model) | ||
} | ||
|
||
// check current state | ||
const isActive = model.state === S_PLAYING | ||
|
||
// remove tick from loop if no timelines are active | ||
if (!isActive) { | ||
loopOff(model.id) | ||
} | ||
|
||
// update effects` | ||
all(model.players, effect => { | ||
const { from, to } = effect | ||
const isAnimationActive = isActive && inRange(flr(model.time), from, to) | ||
const offset = minMax((model.time - from) / (to - from), 0, 1) | ||
|
||
effect.update(offset, model.rate, isAnimationActive) | ||
}) | ||
|
||
publish(model.id, UPDATE, model.time) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { update } from './update' | ||
|
||
export const updateRate = (model: ITimelineModel, rate: number) => { | ||
model.rate = rate || 1 | ||
update(model) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ITimelineModel } from '../core/types' | ||
import { update } from './update' | ||
|
||
export const updateTime = (model: ITimelineModel, time: number) => { | ||
const currentTime = +time | ||
model.time = isFinite(currentTime) ? currentTime : model.rate < 0 ? model.duration : 0 | ||
update(model) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ITimelineModel } from './types' | ||
import { getModel } from '../model/store' | ||
|
||
export interface ChangeHandler { | ||
(model: ITimelineModel, data?: any): void | ||
} | ||
|
||
const handlers: Record<string, ChangeHandler> = {} | ||
|
||
export function register(action: string, handler: ChangeHandler) { | ||
handlers[action] = handler | ||
} | ||
|
||
export function dispatch(action: string, id: string, data?: any) { | ||
const fn = handlers[action] | ||
const model = getModel(id) | ||
|
||
if (fn && model) { | ||
fn(model, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.