• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1function scale_text_delta(template, delta)
2    template = template.slide
3    for i = 1, #template do
4        local paint = template[i].paint
5        paint:setTextSize(paint:getTextSize() + delta)
6    end
7end
8
9function slide_transition(prev, next, is_forward)
10    local rec = {
11        proc = function(self, canvas, drawSlideProc)
12            if self:isDone() then
13                drawSlideProc(canvas)
14                return nil
15            end
16            self.prevDrawable:draw(canvas, self.curr_x, 0)
17            self.nextDrawable:draw(canvas, self.curr_x + 640, 0)
18            self.curr_x = self.curr_x + self.step_x
19            return self
20        end
21    }
22    if is_forward then
23        rec.prevDrawable = prev
24        rec.nextDrawable = next
25        rec.curr_x = 0
26        rec.step_x = -15
27        rec.isDone = function (self) return self.curr_x <= -640 end
28    else
29        rec.prevDrawable = next
30        rec.nextDrawable = prev
31        rec.curr_x = -640
32        rec.step_x = 15
33        rec.isDone = function (self) return self.curr_x >= 0 end
34    end
35    return rec
36end
37
38function sqr(value) return value * value end
39
40function set_blur(paint, alpha)
41    local sigma = sqr(1 - alpha) * 20
42    if gUseBlurInTransitions then
43        paint:setImageFilter(Sk.newBlurImageFilter(sigma, sigma))
44    end
45    paint:setAlpha(alpha)
46end
47
48function fade_slide_transition(prev, next, is_forward)
49    local rec = {
50        paint = Sk.newPaint(),
51        prevDrawable = prev,
52        nextDrawable = next,
53        proc = function(self, canvas, drawSlideProc)
54            if self:isDone() then
55                drawSlideProc(canvas)
56                return nil
57            end
58
59            set_blur(self.paint, self.prev_a)
60            self.prevDrawable:draw(canvas, self.prev_x, 0, self.paint)
61
62            set_blur(self.paint, self.next_a)
63            self.nextDrawable:draw(canvas, self.next_x, 0, self.paint)
64            self:step()
65            return self
66        end
67    }
68    if is_forward then
69        rec.prev_x = 0
70        rec.prev_a = 1
71        rec.next_x = 640
72        rec.next_a = 0
73        rec.isDone = function (self) return self.next_x <= 0 end
74        rec.step = function (self)
75            self.next_x = self.next_x - 20
76            self.next_a = (640 - self.next_x) / 640
77            self.prev_a = 1 - self.next_a
78        end
79    else
80        rec.prev_x = 0
81        rec.prev_a = 1
82        rec.next_x = 0
83        rec.next_a = 0
84        rec.isDone = function (self) return self.prev_x >= 640 end
85        rec.step = function (self)
86            self.prev_x = self.prev_x + 20
87            self.prev_a = (640 - self.prev_x) / 640
88            self.next_a = 1 - self.prev_a
89        end
90    end
91    return rec
92end
93
94function fade_transition(prev, next, is_forward)
95    local rec = {
96        paint = Sk.newPaint(),
97        prevDrawable = prev,
98        nextDrawable = next,
99        proc = function(self, canvas, drawSlideProc)
100            if self:isDone() then
101                drawSlideProc(canvas)
102                return nil
103            end
104
105            set_blur(self.paint, self.prev_a)
106            self.prevDrawable:draw(canvas, 0, 0, self.paint)
107
108            set_blur(self.paint, self.next_a)
109            self.nextDrawable:draw(canvas, 0, 0, self.paint)
110            self:step()
111            return self
112        end
113    }
114    rec.prev_a = 1
115    rec.next_a = 0
116    rec.isDone = function (self) return self.next_a >= 1 end
117    rec.step = function (self)
118        self.prev_a = math.max(self.prev_a - 0.025, 0)
119        self.next_a = 1 - self.prev_a
120    end
121
122    return rec
123end
124
125function rotate_transition(prev, next, is_forward)
126    local rec = {
127        angle = 0,
128        prevDrawable = prev,
129        nextDrawable = next,
130        activeDrawable = prev,
131        proc = function(self, canvas, drawSlideProc)
132            if self:isDone() then
133                drawSlideProc(canvas)
134                return nil
135            end
136
137            canvas:save()
138            canvas:translate(320, 240)
139            canvas:rotate(self.angle)
140            canvas:translate(-320, -240)
141            self.activeDrawable:draw(canvas, 0, 0)
142            self:step()
143            return self
144        end,
145        isDone = function (self) return self.angle >= 360 or self.angle <= -360 end
146    }
147    if is_forward then
148        rec.step = function (self)
149            self.angle = self.angle + 10
150            if self.angle >= 180 then
151                self.activeDrawable = self.nextDrawable
152            end
153        end
154    else
155        rec.step = function (self)
156            self.angle = self.angle - 10
157            if self.angle <= -180 then
158                self.activeDrawable = self.nextDrawable
159            end
160        end
161    end
162    return rec
163end
164
165function zoom_transition(prev, next, is_forward)
166    local rec = {
167        scale = 1,
168        scale_delta = .95,
169        scale_limit = 0.2,
170        pivot_x = 320,
171        pivot_y = 240,
172        prevDrawable = prev,
173        nextDrawable = next,
174        activeDrawable = prev,
175        proc = function(self, canvas, drawSlideProc)
176            if self:isDone() then
177                drawSlideProc(canvas)
178                return nil
179            end
180
181            canvas:translate(self.pivot_x, self.pivot_y)
182            canvas:scale(self.scale, self.scale)
183            canvas:translate(-self.pivot_x, -self.pivot_y)
184            self.activeDrawable:draw(canvas, 0, 0)
185            self:step()
186            return self
187        end,
188        isDone = function (self) return self.scale > 1 end,
189        step = function (self)
190            if self.scale < self.scale_limit then
191                self.scale = self.scale_limit
192                self.scale_delta = 1 / self.scale_delta
193                self.activeDrawable = self.nextDrawable
194            end
195            self.scale = self.scale * self.scale_delta
196        end
197    }
198    return rec
199end
200
201gTransitionTable = {
202    fade = fade_transition,
203    slide = slide_transition,
204    fade_slide = fade_slide_transition,
205    rotate = rotate_transition,
206    zoom = zoom_transition,
207}
208
209