-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support property anim, based on timeline #368
Open
qlli
wants to merge
1
commit into
goplus:main
Choose a base branch
from
qlli:propAnim
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package timeline | ||
|
||
import "fmt" | ||
|
||
type Interval struct { | ||
Offset float64 | ||
Duration float64 | ||
} | ||
|
||
func (i Interval) End() float64 { | ||
return i.Offset + i.Duration | ||
} | ||
|
||
func (i *Interval) Step(time float64) { | ||
i.Offset -= time | ||
} | ||
|
||
func (i Interval) Scale(scale float32) Interval { | ||
return Interval{ | ||
Offset: float64(float64(i.Offset) * float64(scale)), | ||
Duration: float64(float64(i.Duration) * float64(scale)), | ||
} | ||
} | ||
|
||
func (i Interval) Contains(time float64) bool { | ||
return i.Offset <= time && time <= i.End() | ||
} | ||
|
||
func (i Interval) String() string { | ||
return fmt.Sprintf("Interval{offset:%.3f, duration:%.3f}", i.Offset, i.Duration) | ||
} |
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,192 @@ | ||
package timeline | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sync" | ||
) | ||
|
||
const EPSILON float64 = 0.0001 | ||
const DEFAULT_TRANSITION float64 = 0.5 | ||
|
||
var debugTimeline bool = false | ||
var idSeed int64 | ||
var idSeedM sync.Mutex | ||
|
||
type ITimeline interface { | ||
GetTimeline() *Timeline | ||
Step(time *float64) ITimeline | ||
SetActive(bool) | ||
} | ||
|
||
type Timeline struct { | ||
ID int64 | ||
active bool | ||
offset float64 | ||
speed float64 | ||
fadeIn *Interval | ||
fadeOut *Interval | ||
freezingTime float64 | ||
next ITimeline | ||
group *TimelineGroup | ||
onStep func(*float64) ITimeline | ||
onActive func(bool) | ||
} | ||
|
||
func (t *Timeline) Init() *Timeline { | ||
idSeedM.Lock() | ||
defer idSeedM.Unlock() | ||
t.ID = idSeed | ||
idSeed++ | ||
if debugTimeline { | ||
log.Printf("Timeline %X", t.ID) | ||
} | ||
t.speed = 1.0 | ||
return t | ||
} | ||
|
||
func (t *Timeline) GetTimeline() *Timeline { | ||
return t | ||
} | ||
|
||
func (t *Timeline) SetActive(on bool) { | ||
if t.active == on { | ||
return | ||
} | ||
if debugTimeline { | ||
log.Println("SetActive", on, t.ID) | ||
} | ||
t.active = on | ||
if t.onActive != nil { | ||
t.onActive(on) | ||
} | ||
} | ||
|
||
func (t *Timeline) Step(time *float64) ITimeline { | ||
if *time < 0.0 { | ||
*time = 0.0 | ||
} | ||
|
||
var running ITimeline = t | ||
var loopLimit int = 10000 | ||
for running != nil && *time > EPSILON && loopLimit > 0 { | ||
loopLimit-- | ||
var realStep, scaledTime, oldScaledTime float64 | ||
var oldRunning ITimeline | ||
r := running.GetTimeline() | ||
|
||
// step until time <= 0 || offset <= 0 | ||
var min float64 = *time // minimum step in consideration of time, offset, fadeout.end, freezingTime | ||
if r.offset > EPSILON { | ||
if min > r.offset { | ||
min = r.offset | ||
} | ||
*time -= min | ||
r.offset -= min | ||
continue | ||
} | ||
|
||
r.SetActive(true) | ||
|
||
if r.fadeOut != nil { | ||
end := r.fadeOut.End() | ||
if end < 0.0 { | ||
end = 0.0 | ||
} | ||
if min > end { | ||
min = end | ||
} | ||
} | ||
|
||
// step until time <= 0 || fadeOut.end <= 0 || freezingTime <= 0 | ||
if r.freezingTime > EPSILON { | ||
if min > r.freezingTime { | ||
min = r.freezingTime | ||
} | ||
*time -= min | ||
r.freezingTime -= min | ||
if r.fadeIn != nil { | ||
r.fadeIn.Step(min) | ||
} | ||
if r.fadeOut != nil { | ||
r.fadeOut.Step(min) | ||
} | ||
|
||
goto CHECK_FADE_OUT | ||
} | ||
|
||
// step until time <= 0 || fadeOut.end <= 0 || runOut.end <= 0 | ||
scaledTime = min * r.speed | ||
oldScaledTime = scaledTime | ||
oldRunning = running | ||
running = r.onStep(&scaledTime) | ||
if r.speed > EPSILON { | ||
realStep = (oldScaledTime - scaledTime) / r.speed | ||
} else { | ||
realStep = min | ||
} | ||
*time -= realStep | ||
if r.fadeIn != nil { | ||
r.fadeIn.Step(realStep) | ||
} | ||
if r.fadeOut != nil { | ||
r.fadeOut.Step(realStep) | ||
} | ||
|
||
if running != oldRunning { | ||
if running != nil { | ||
continue | ||
} | ||
|
||
// running == null, means the timeline has run out, should check whether fade out | ||
if r.fadeOut == nil || r.fadeOut.End() <= EPSILON { | ||
oldRunning.SetActive(false) | ||
running = r.next | ||
continue | ||
} | ||
|
||
// step until time <= 0 || fadeOut.end <= 0 | ||
end2 := r.fadeOut.End() | ||
if end2 < 0.0 { | ||
end2 = 0.0 | ||
} | ||
min2 := *time | ||
if min2 > end2 { | ||
min2 = end2 | ||
} | ||
*time -= min2 | ||
if r.fadeIn != nil { | ||
r.fadeIn.Step(min2) | ||
} | ||
if r.fadeOut != nil { | ||
r.fadeOut.Step(min2) | ||
} | ||
running = oldRunning | ||
} | ||
|
||
CHECK_FADE_OUT: | ||
t2 := running.GetTimeline() | ||
if t2.fadeOut != nil && t2.fadeOut.End() <= EPSILON { | ||
t2.SetActive(false) | ||
running = t2.next | ||
} | ||
} | ||
|
||
if loopLimit <= 0 { | ||
panic("LOOP LIMIT") | ||
} | ||
return running | ||
} | ||
|
||
func (t *Timeline) String() string { | ||
var nID int64 = 0 | ||
if t.next != nil { | ||
nID = t.next.GetTimeline().ID | ||
} | ||
var gID int64 = 0 | ||
if t.group != nil { | ||
gID = t.group.ID | ||
} | ||
return fmt.Sprintf("{id:%x,active:%t,offset:%.3f,speed:%.3f,in:%s,out:%s,frz:%.3f,next:%x,group:%x}", | ||
t.ID, t.active, t.offset, t.speed, t.fadeIn, t.fadeOut, t.freezingTime, nID, gID) | ||
} |
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,16 @@ | ||
package timeline | ||
|
||
type TimelineGroup struct { | ||
Timeline | ||
Tracks []ITimeline | ||
} | ||
|
||
func (tg *TimelineGroup) Step(time *float64) ITimeline { | ||
tg.Timeline.Step(time) | ||
return nil | ||
} | ||
|
||
// AddTrack 方法 | ||
func (tg *TimelineGroup) AddTrack(track ITimeline) { | ||
tg.Tracks = append(tg.Tracks, track) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use goto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In most cases, the use of goto should be avoided. The exception is when it is the only clear and concise way to handle control flow. By ensuring that all cases ultimately check for a fade-out condition, the control flow remains clear and understandable.