# Animations

There are two types of animations in the presentation library: slide intro animations, and element animations. The included ones are presented here below.

## Slide Intro Animations

Slide intro animations need to have names that end with `Intro` to be usable from the presentation language (the presentation language automatically adds the suffix `Intro` before resolving the name). The following ones are included in the library, but custom ones can be created as well:

- `FadeIn`

  Fades in using transparency. The duration can optionally be specified as a parameter.

- `SlideDown <fade>`

  Slide down from the top. If `<fade>` is true, then the slide is faded in as well. A custom duration can be specified as the first parameter.

- `Enlarge`

  Enlarge the old slide and fade it out. A custom duration can optionally be specified as a parameter.

### Custom Intro Animations

To create custom animations, simply create a new subclass to `presentation.SlideIntro`, and override the `draw` member.

## Element Animations

Element animations need to have names that end in `Animation` to be usable from the presentation language (the presentation language automatically adds the suffix `Animation` before resolving the name). The following animations are provided by the library:

- `Show`

  Display the element at the specified step. No animation.

- `Hide`

  Stop showing the element at the specified stop. No animation.

- `FadeIn`

  Fades the element in from transparent.

- `FadeOut`

  Fades the element out to transparent.

- `Raise`

  Animation that fades the element in from transparent, but also raises the object while doing so. The height to raise the element can optionally be specified as a parameter.

- `Grow`

  Grow the element vertically.

- `Shrink`

  Shrink the element vertically.

- `ZoomIn`

  Zoom the element in. Like `Grow` but affects both dimensions.

- `ZoomOut`

  Zoom the element out. Like `Shrink` but affects both dimensions.

### Custom Element Animations

To create custom element animations, simply create a subclass to `presentation.Animation`. The subclass must have a constructor that accepts at least one `Nat`. The presentation language passes the step to which the animation applies as the first parameter. This is expected to be forwarded to the superclass' constructor.

The class then needs to override the following two members:

- `core.Bool before(presentation.Element element, ui.Graphics g, core.Nat cStep, core.Duration time)`

  Called before drawing the element. May return `false` to prevent painting the element.

- `void after(presentation.Element element, ui.Graphics g, core.Nat cStep, core.Duration time)`

  Called after drawing the element to restore any graphics state set up by `before`.

The following convenience functions are also available to simplify implementation of animations:

- `core.Float val(core.Duration d)`

  Convert a time into a float in the range 0..1.

- `core.Float smoothVal(core.Duration d)`

  Convert to a smooth value in the range 0..1.

- `core.Float smooth(core.Float x)`

  Convert a value in the range 0..1 to a "smooth" value in the same range.

Finally, there are a few convenience classes that perform some common tasks:

- `presentation.Trigger`

  An animation that acts like a trigger, i.e. it triggers once at the desired time and does something at that moment only. This class is intended to provide functionality to other embedded things, and has no real use in isolation as-is.

  - `void trigger(presentation.Element element)`

    Called when the event is supposed to trigger.

- `presentation.AnimationIntro`

  Interface to simplify animations that animate the entrance of an object.

  Adapts the interface of Animation into an interface that is simpler to use, but less flexible.

  - `void before(presentation.Element element, ui.Graphics g, core.Float pos)`

    Override to update the graphics context. Expected to call "push" in some form. "pos" is a float in range 0..1 indicating the current position of the animation.

  - `void after(presentation.Element element, ui.Graphics g)`

    Override to restore the graphics context. Default implementation simply pops it once.

- `presentation.AnimationOutro`

  Interface to simplify animations that animate the disappearence of an object.

  Adapts the interface of Animation into an interface that is simpler to use, but less flexible.

  - `void before(presentation.Element element, ui.Graphics g, core.Float pos)`

    Override to update the graphics context. Expected to call "push" in some form. "pos" is a float in range 0..1 indicating the current position of the animation.

  - `void after(presentation.Element element, ui.Graphics g)`

    Override to restore the graphics context. Default implementation simply pops it once.