-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.go
208 lines (180 loc) · 5.76 KB
/
animation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"errors"
// "image"
// "image/color"
// "image/gif"
"log"
"math"
"strings"
"time"
"unicode"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var glyphSize fyne.Size = fyne.NewSize(20, 20)
var glyphSpacing float32 = 1.0
type RuneGlyph struct {
Letter rune
StartPos, EndPos fyne.Position
}
type Animation struct {
Glyphs []RuneGlyph
Rows, Cols int
}
func NthRuneIndex(layout []RuneLayoutElement, r rune, n int) int {
index := 0
foundCount := 0
for index < len(layout) {
if layout[index].Rune == r {
foundCount += 1
if foundCount == n {
return index
}
}
index += 1
}
return -1
}
func NewAnimation(input, anagram string, maxRows, maxCols int) (*Animation, error) {
inputRC := NewRuneCluster(input)
anagramRC := NewRuneCluster(anagram)
if !inputRC.Equals(anagramRC) {
return nil, errors.New("input doesn't match anagram")
}
inputLC := strings.ToLower(input)
anagramLC := strings.ToLower(anagram)
inputLayout, inputRows := MakeRuneLayout(inputLC, maxCols)
anagramLayout, anagramRows := MakeRuneLayout(anagramLC, maxCols)
numGlyphs := max(len(inputLayout), len(anagramLayout))
glyphs := make([]RuneGlyph, 0, numGlyphs)
runeCounts := make(map[rune]int)
glyphsUsed := make([]bool, len(anagramLayout))
for _, element := range inputLayout {
startPos := fyne.NewPos(float32(element.Col)*(glyphSize.Width+glyphSpacing), float32(element.Row)*(glyphSize.Height+glyphSpacing))
runeCounts[element.Rune] += 1
n := runeCounts[element.Rune]
endPos := fyne.NewPos(-2*glyphSize.Width, -2*glyphSize.Height)
endIndex := NthRuneIndex(anagramLayout, element.Rune, n)
if endIndex >= 0 {
glyphsUsed[endIndex] = true
endPos.X = float32(anagramLayout[endIndex].Col) * (glyphSize.Width + glyphSpacing)
endPos.Y = float32(anagramLayout[endIndex].Row) * (glyphSize.Height + glyphSpacing)
}
glyphs = append(glyphs, RuneGlyph{element.Rune, startPos, endPos})
}
for i, used := range glyphsUsed {
if !used {
endElement := anagramLayout[i]
startPos := fyne.NewPos(-2*glyphSize.Width, -2*glyphSize.Height)
endPos := fyne.NewPos(float32(endElement.Col)*(glyphSize.Width+glyphSpacing), float32(endElement.Row)*(glyphSize.Height+glyphSpacing))
glyphs = append(glyphs, RuneGlyph{endElement.Rune, startPos, endPos})
}
}
animation := Animation{glyphs, max(inputRows, anagramRows), maxCols}
return &animation, nil
}
type AnimationDisplay struct {
widget.BaseWidget
surface *fyne.Container
scroll *container.Scroll
animations []*fyne.Animation
Duration time.Duration
Icon fyne.Resource
Badge string
running bool
}
func NewAnimationDisplay(icon fyne.Resource) *AnimationDisplay {
surface := container.NewWithoutLayout()
scroll := container.NewScroll(surface)
scroll.Direction = container.ScrollNone
ad := &AnimationDisplay{surface: surface, scroll: scroll, Duration: 6 * time.Second, Icon: icon, Badge: "made with KarmaManager"}
ad.ExtendBaseWidget(ad)
return ad
}
func (ad *AnimationDisplay) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(ad.scroll)
}
func (ad *AnimationDisplay) AnimateAnagram(input, anagram string) {
dispSize := ad.surface.Size()
maxCols := int(math.Floor(float64(dispSize.Width / (glyphSize.Width + glyphSpacing))))
maxRows := int(math.Floor(float64(dispSize.Height / (glyphSize.Height + glyphSpacing))))
icon := canvas.NewImageFromResource(ad.Icon)
icon.SetMinSize(fyne.NewSize(64, 64))
icon.FillMode = canvas.ImageFillContain
badge := widget.NewLabel(ad.Badge)
animation, err := NewAnimation(input, anagram, maxRows, maxCols)
if err != nil {
log.Println(err)
return
}
numGlyphs := len(animation.Glyphs)
ad.animations = make([]*fyne.Animation, numGlyphs+1)
ad.surface.RemoveAll()
style := fyne.TextStyle{Monospace: true}
for index, glyph := range animation.Glyphs {
text := canvas.NewText(string(unicode.ToUpper(glyph.Letter)), theme.TextColor())
text.TextStyle = style
text.TextSize = 20.0
ad.surface.Add(text)
anim := canvas.NewPositionAnimation(glyph.StartPos, glyph.EndPos, ad.Duration, text.Move)
anim.AutoReverse = true
anim.RepeatCount = fyne.AnimationRepeatForever
anim.Start()
ad.animations[index] = anim
}
ad.surface.Add(icon)
ad.surface.Add(badge)
// iconStartPos := fyne.NewPos(dispSize.Width, dispSize.Height)
iconEndPos := fyne.NewPos(10, dispSize.Height-icon.MinSize().Height-10)
// iconAnim := canvas.NewPositionAnimation(iconStartPos, iconEndPos, ad.Duration, func(p fyne.Position) {
// icon.Move(p)
// ad.surface.Refresh()
// })
// iconAnim.AutoReverse = true
// iconAnim.RepeatCount = fyne.AnimationRepeatForever
// icon.Move(iconStartPos)
icon.Move(iconEndPos)
icon.Resize(icon.MinSize())
badgeStartPos := fyne.NewPos(0-badge.MinSize().Width-10, dispSize.Height)
badgeEndPos := fyne.NewPos(20+icon.MinSize().Width, dispSize.Height-badge.MinSize().Height-10)
// badge.Move(badgeEndPos)
// badge.Resize(badge.MinSize())
badgeAnim := canvas.NewPositionAnimation(badgeStartPos, badgeEndPos, ad.Duration, func(p fyne.Position) {
badge.Move(p)
})
badgeAnim.AutoReverse = true
badgeAnim.RepeatCount = fyne.AnimationRepeatForever
// iconAnim.Start()
badgeAnim.Start()
// ad.animations[numGlyphs+1] = iconAnim
ad.animations[numGlyphs] = badgeAnim
ad.running = true
}
func (ad *AnimationDisplay) Start() {
for _, anim := range ad.animations {
anim.Start()
}
ad.running = true
}
func (ad *AnimationDisplay) Stop() {
for _, anim := range ad.animations {
anim.Stop()
}
ad.running = false
}
func (ad *AnimationDisplay) Tapped(pe *fyne.PointEvent) {
if ad.running {
ad.Stop()
} else {
ad.Start()
}
}
func (ad *AnimationDisplay) Clear() {
ad.animations = make([]*fyne.Animation, 0)
ad.surface.RemoveAll()
ad.surface.Refresh()
}