From 82268c1bf2036cf80884ef71a05a24c32d9ecedb Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 31 Oct 2023 19:09:40 +0100 Subject: [PATCH] Animation: add possibility to manually specify where the trigger effect should be checked This change changes the Public API so in the name of Semantic Versioning convention it should be included in new MAJOR release --- animation.go | 4 +++- animator.go | 11 +++++++++-- color_flow.go | 2 +- move.go | 2 +- resize_animation.go | 11 ++++++----- transition.go | 2 +- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/animation.go b/animation.go index 5524450..2d3f191 100644 --- a/animation.go +++ b/animation.go @@ -15,7 +15,9 @@ type Animation interface { // BuildNormal is called every frame when animation is not running // starter is animation link to Animator.Start - BuildNormal(currentKeyFrame KeyFrame, starterFunc StarterFunc) + // triggerCheck should be placed where the animator should check for trigger. If not called - Animator will do that + // after rendering the widget (after calling BuildNormal). + BuildNormal(currentKeyFrame KeyFrame, starterFunc StarterFunc, triggerCheck func()) // BuildAnimation is called when running an animation. // It receives several important arguments: // - animationPercentage after applying specified by Animator diff --git a/animator.go b/animator.go index ed271b7..a6e9b9d 100644 --- a/animator.go +++ b/animator.go @@ -274,10 +274,17 @@ func (a *AnimatorWidget) Build() { return } - a.animation.BuildNormal(cf, a) + wasCalled := false + triggerValue := false + a.animation.BuildNormal(cf, a, func() { + wasCalled = true + triggerValue = triggerValue || a.triggerFunc() + }) if a.triggerFunc != nil { - triggerValue := a.triggerFunc() + if !wasCalled { + triggerValue = a.triggerFunc() + } switch a.triggerType { case TriggerNever: diff --git a/color_flow.go b/color_flow.go index ff22e3e..471f29b 100644 --- a/color_flow.go +++ b/color_flow.go @@ -91,7 +91,7 @@ func (c *ColorFlowAnimation) KeyFramesCount() int { } // BuildNormal builds animation in normal, not-triggered state. -func (c *ColorFlowAnimation) BuildNormal(currentKeyFrame KeyFrame, _ StarterFunc) { +func (c *ColorFlowAnimation) BuildNormal(currentKeyFrame KeyFrame, _ StarterFunc, _ func()) { normalColor := c.color[currentKeyFrame]() c.build(normalColor) diff --git a/move.go b/move.go index 6ee3cfb..295b851 100644 --- a/move.go +++ b/move.go @@ -66,7 +66,7 @@ func (m *MoveAnimation) KeyFramesCount() int { } // BuildNormal implements Animation. -func (m *MoveAnimation) BuildNormal(currentKF KeyFrame, starter StarterFunc) { +func (m *MoveAnimation) BuildNormal(currentKF KeyFrame, starter StarterFunc, _ func()) { imgui.SetCursorPos(m.getPosition(currentKF)) m.widget(starter).Build() diff --git a/resize_animation.go b/resize_animation.go index b0d09f7..2b81c6d 100644 --- a/resize_animation.go +++ b/resize_animation.go @@ -83,7 +83,7 @@ func (r *ResizeAnimation[T]) KeyFramesCount() int { } // BuildNormal implements Animation. -func (r *ResizeAnimation[T]) BuildNormal(currentKeyFrame KeyFrame, _ StarterFunc) { +func (r *ResizeAnimation[T]) BuildNormal(currentKeyFrame KeyFrame, _ StarterFunc, triggerCheck func()) { // This may happen if user forgot to pass size vectors. In this case just allow to build unchanged widget. if int(currentKeyFrame) > len(r.sizes)-1 { r.widget.Build() @@ -94,6 +94,7 @@ func (r *ResizeAnimation[T]) BuildNormal(currentKeyFrame KeyFrame, _ StarterFunc r.trickCursorBefore(r.sizes[currentKeyFrame], imgui.Vec2{}) r.widget.Size(r.sizes[currentKeyFrame].X, r.sizes[currentKeyFrame].Y).Build() + triggerCheck() r.trickCursorAfter(r.sizes[currentKeyFrame]) } @@ -122,7 +123,7 @@ func (r *ResizeAnimation[T]) BuildAnimation( } func (r *ResizeAnimation[T]) trickCursorBefore(current, delta imgui.Vec2) { - move := imgui.CursorPos() + move := imgui.Vec2{} if r.trickCursor&TrickCursorBeforeX != 0 { move.X -= (current.X + delta.X - r.sizes[0].X) / 2 @@ -132,11 +133,11 @@ func (r *ResizeAnimation[T]) trickCursorBefore(current, delta imgui.Vec2) { move.Y -= (current.Y + delta.Y - r.sizes[0].Y) / 2 } - imgui.SetCursorPos(move) + imgui.Dummy(move) } func (r *ResizeAnimation[T]) trickCursorAfter(currentSize imgui.Vec2) { - move := imgui.CursorPos() + move := imgui.Vec2{} if r.trickCursor&TrickCursorAfterX != 0 { move.X -= (currentSize.X - r.sizes[0].X) / 2 @@ -146,7 +147,7 @@ func (r *ResizeAnimation[T]) trickCursorAfter(currentSize imgui.Vec2) { move.Y -= (currentSize.Y - r.sizes[0].Y) / 2 } - imgui.SetCursorPos(move) + imgui.Dummy(move) } type resizable2D[T giu.Widget] interface { diff --git a/transition.go b/transition.go index 5d74bf0..c2c2939 100644 --- a/transition.go +++ b/transition.go @@ -35,7 +35,7 @@ func (t *TransitionAnimation) Init() { } // BuildNormal implements Animation interface. -func (t *TransitionAnimation) BuildNormal(f KeyFrame, starter StarterFunc) { +func (t *TransitionAnimation) BuildNormal(f KeyFrame, starter StarterFunc, _ func()) { t.renderers[f](starter) }