animations

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2023 License: MIT Imports: 7 Imported by: 1

README

GIU ANIMATIONS

This is a module for giu providing an animation system.

giu-animations demo

Documentation

How to use?

For complete code, please check out _examples

important note

Please make shure that you're using the same version of giu as this project (technically, you need to use giu version that uses the same imgui-go version as yours)

Defining an animation

At the moment, there are two implementations of animations:

  • Transition - a smooth transition between two windows/sets of windows e.t.c. NOTE applying this animation to single widgets is not implemented yet and may not work as expected.
  • on-hover color change - you can apply this animation to any single widget that imgui.IsItemHovered applies for. You can specify any style color you want.
  • Movement - moves DrawCursor emulating moving an object (giu.Widget).

Lets shortly discuss particular types of animations:

transition

Lets look at the API:

func Transition(renderer1, renderer2 func(starter func()) *TransitionAnimation {...}

renderer1 and renderer2 are just two stages of trasition. In stage 1, renderer1 is called, and in stage 2 - renderer2. When animation is being plaid, both renderers are called and suitable alpha value is pushed to imgui style for both of them.

The argument to the poth renderers is a pointer to Animator.Start (see later) so that you can call it to play the animation.

Hover color
func HoverColor(
        widget giu.Widget,
        hoverColor, normalColor func() color.RGBA,
        hoverID, normalID giu.StyleColorID,
) *HoverColorAnimation {...}
  • The first argument is a widget that should apply to
  • hoverColor and normalColor are functions that returns hover and standard (non-hover) colors
  • hoverID, normalID - style IDs that hovering applies to.

There is also a variant of the above method called HoverColorStyle, which does not need hoverColor and normalColor arguments. These colors are obtained by function like this:

func() color.RGBA {
    return imgui.CurrentStyle().GetStyleColor(styleID)
}
Move
Move(
    w giu.Widget,
    delta imgui.Vec2,
) *MoveAnimation {...}

This will mmove w from the position, it was at the moment of calling Move(...) (called start) to start + delta.

Easing

There are some extra ways of playing animation flow:

const (
        EasingAlgNone EasingAlgorithmType = iota
        EasingAlgInSine
        EasingAlgOutSine
        EasingAlgInOutSine
        EasingAlgInBack
        EasingAlgOutBack
        EasingAlgInOutBack
        EasingAlgInElastic
        EasingAlgOutElastic
        EasingAlgInOutElastic
        EasingAlgInBounce
        EasingAlgOutBounce
        EasingAlgInOutBounce
)

for further reference, see https://easings.net

Bezier curve

Move animation supports Bezier Curve. It meas that move animation could be very smooth if you find it necessary. You have two ways to calculate these points:

  • go through google and use tone of paper to understand this strange math or
  • just type random values and check what happens 😄

The api looks as follows:

func (m *MoveAnimation) Bezier(controlPoints ...imgui.Vec2) *MoveAnimation {...}

you can add as many control points as you which to. Each point will make the curve stranger. The only thing you need to remember is, that these points are relative to startPos. They will automatically become startPos + controlPoint.

Using animator

After constructing an animation, you need to create a special type of giu widget called AnimatorWidget.

You may want to store it in a temporary variable, but, as you'll see later, animator's api is designed so that you don't need to do so every time.

As an argument to Animator(...) constuctor, you pass perviously created animation.

Animator has some useful methods:

  • Duration allows you to specify animation's duration (default is 0.25 s)
  • FPS sets Frames per second value for animation playback (default is 60) NOTE it is not real application's FPS! It just describes how often animation's status is updated.
  • Start - this method you can use to invoke animation play.
  • IsRunning returns true, if animation is being plaid right now.

Creating your own animation

You can use this API to create your own animation. To do soo, lets take a look on Animation interface.

type Animation interface {
        Init()
        Reset()

        BuildNormal(starter func())
        BuildAnimation(animationPercentage float32, starter func())
}

This is a copy from animation.go, but I've removed comments for clearity

Init

init is called once, during first call of Animator.Build you can put some initialization here.

Reset

Reset is called along with (*Animator).Start

BuildNormal

is called when !(*Animator).IsRunning() It takes a pointer to (*Animator).Start as an argument so you can easily start animation from there.

BuildAnimation

is called instead of BuildNormal when playing an animation. Along with pointer to (*Animator).Start, it also receives current animation progress in percents (0 >= currentPercentage <= 1) You can do some calculations there.

Contribution

If you implement something interessting, find any bugs, or improvements and would be so kind to open a PR, your contribution is welcome!

Motivation

For now, this system is used in one of The Greater Heptavirate's projects. But (as I'm an author of that system) I've decided to share it for public - feel free to use if you can find any use case.

License

This project is shared under (attached) MIT License.

Documentation

Overview

Package animations contains my attempt to create a kind of "animations" in imgui.

Index

Constants

View Source
const (
	DefaultFPS      = 60
	DefaultDuration = time.Second / 4
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Animation

type Animation interface {
	// Init is called once, immediately on start.
	Init()
	// Reset is called whenever needs to restart animation.
	Reset()

	// BuildNormal is called every frame when animation is not running
	// starter is a link to Animator.Start
	BuildNormal(starter func())
	// BuildAnimation is called when running an animation.
	// It receives the current animation progress as a float, where
	// 0 >= animationPercentage <= 1
	// starter is a link to Animator.Start
	BuildAnimation(animationPercentage float32, starter func())
}

Animation is an interface implemented by each animation.

type AnimatorWidget

type AnimatorWidget struct {
	// contains filtered or unexported fields
}

AnimatorWidget is a manager for animation.

func Animator

func Animator(a Animation) *AnimatorWidget

Animator creates a new AnimatorWidget.

func (*AnimatorWidget) Build

func (a *AnimatorWidget) Build()

Build implements giu.Widget

func (*AnimatorWidget) CurrentPercentageProgress

func (a *AnimatorWidget) CurrentPercentageProgress() float32

func (*AnimatorWidget) Duration

func (a *AnimatorWidget) Duration(duration time.Duration) *AnimatorWidget

Duration allows to specify duration value. CAUTION: it will take effect after next call to Start - not applied to currently plaid animation.

func (*AnimatorWidget) FPS

func (a *AnimatorWidget) FPS(fps int) *AnimatorWidget

FPS allows to specify FPS value. CAUTION: it will take effect after next call to Start - not applied to currently plaid animation.

func (*AnimatorWidget) IsRunning

func (a *AnimatorWidget) IsRunning() bool

IsRunning returns true if the animation is already running.

func (*AnimatorWidget) Start

func (a *AnimatorWidget) Start()

Start starts the animation.

type EasingAlgorithm added in v1.2.0

type EasingAlgorithm func(plainPercentage float32) (percentage float32)

type EasingAlgorithmType added in v1.2.0

type EasingAlgorithmType byte

EasingAlgorithmType represents a type of easing algorithm used for animation. Refer https://easings.net/

const (
	EasingAlgNone EasingAlgorithmType = iota
	EasingAlgInSine
	EasingAlgOutSine
	EasingAlgInOutSine
	EasingAlgInBack
	EasingAlgOutBack
	EasingAlgInOutBack
	EasingAlgInElastic
	EasingAlgOutElastic
	EasingAlgInOutElastic
	EasingAlgInBounce
	EasingAlgOutBounce
	EasingAlgInOutBounce
	EasingAlgMax
)

type HoverColorAnimation

type HoverColorAnimation struct {
	giu.Widget
	// contains filtered or unexported fields
}

func HoverColor

func HoverColor(
	widget giu.Widget,
	hoverColor, normalColor func() color.RGBA,
	hoverID, normalID giu.StyleColorID,
) *HoverColorAnimation

func HoverColorStyle

func HoverColorStyle(
	widget giu.Widget,
	hover, normal giu.StyleColorID,
) *HoverColorAnimation

HoverColorStyle wraps HoverColor so that it automatically obtains the color for specified style values.

func (*HoverColorAnimation) BuildAnimation

func (h *HoverColorAnimation) BuildAnimation(percentage float32, _ func())

func (*HoverColorAnimation) BuildNormal

func (h *HoverColorAnimation) BuildNormal(starter func())

func (*HoverColorAnimation) Init

func (h *HoverColorAnimation) Init()

func (*HoverColorAnimation) Reset

func (h *HoverColorAnimation) Reset()

type MoveAnimation added in v1.2.0

type MoveAnimation struct {
	// contains filtered or unexported fields
}

func Move added in v1.2.0

func Move(w func(starter func()) giu.Widget, posDelta imgui.Vec2) *MoveAnimation

func (*MoveAnimation) Algorithm added in v1.2.0

func (m *MoveAnimation) Algorithm(algorithm EasingAlgorithmType) *MoveAnimation

func (*MoveAnimation) Bezier added in v1.2.0

func (m *MoveAnimation) Bezier(points ...imgui.Vec2) *MoveAnimation

func (*MoveAnimation) BuildAnimation added in v1.2.0

func (m *MoveAnimation) BuildAnimation(animationPercentage float32, starter func())

func (*MoveAnimation) BuildNormal added in v1.2.0

func (m *MoveAnimation) BuildNormal(starter func())

func (*MoveAnimation) Init added in v1.2.0

func (m *MoveAnimation) Init()

func (*MoveAnimation) Reset added in v1.2.0

func (m *MoveAnimation) Reset()

func (*MoveAnimation) StartPos added in v1.2.0

func (m *MoveAnimation) StartPos(startPos imgui.Vec2) *MoveAnimation

type TransitionAnimation

type TransitionAnimation struct {
	// contains filtered or unexported fields
}

func Transition

func Transition(renderer1, renderer2 func(starter func())) *TransitionAnimation

func (*TransitionAnimation) BuildAnimation

func (t *TransitionAnimation) BuildAnimation(percentage float32, starter func())

func (*TransitionAnimation) BuildNormal

func (t *TransitionAnimation) BuildNormal(starter func())

func (*TransitionAnimation) Init

func (t *TransitionAnimation) Init()

func (*TransitionAnimation) Reset

func (t *TransitionAnimation) Reset()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL