-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_list_slide.lua
106 lines (83 loc) · 2.78 KB
/
event_list_slide.lua
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
require 'text_util'
local class = require '30log'
local json = require 'json'
local Slide = require 'slide'
-- local white_block = resource.load_image('white.png')
local EventListSlide = Slide:extend("EventListSlide")
local EventListItem = require 'event_list_item'
function EventListSlide:init(x, y, width, height, data_filename)
self.super:init()
self.x, self.y = x, y
self.items_start = 150
self.width, self.height = width, height
self.items = {}
self.pages = {}
self:reset()
util.file_watch(data_filename, function(content)
local event_list = json.decode(content)
self.font = resource.load_font(event_list.font)
self.title = event_list.title
self.events = event_list.events
self.duration = event_list.duration
self.items = {}
self.pages = {}
for i, event in ipairs(self.events) do
self.items[i] = EventListItem(WIDTH, 90,
event.name, event.start, event.location,
self.font)
end
self.pages = self:group_items()
self:reset()
end)
end
function EventListSlide:group_items()
local pages = {}
local current_end = self.items_start
local current_page = {}
for i, item in ipairs(self.items) do
if (current_end + item:get_height() < self.height) then
table.insert(current_page, item)
current_end = current_end + item:get_height()
else
table.insert(pages, current_page)
current_page = {}
table.insert(current_page, item)
current_end = self.items_start + item:get_height()
end
end
table.insert(pages, current_page)
return pages
end
function EventListSlide:draw()
self.super:tick()
write_centered(self.title, 50, self.width / 2, 50, 1, 1, 1, 1)
local page_num = math.floor(self.super.active_time / self.duration) + 1
-- Handle edge case where you can get one frame past the available pages
page_num = math.min(page_num, #self.pages)
local page_time = self.super.active_time - self.duration * (page_num - 1)
local item_fade = 0.15
local page_clear_start_time = self.duration - #(self.pages[page_num]) * item_fade
local clearing_page = (page_time > page_clear_start_time)
local y = self.items_start
for i, item in ipairs(self.pages[page_num]) do
local offset = i - 1
local alpha
if clearing_page then
local clearing_time = page_time - page_clear_start_time
alpha = 1 - (clearing_time / item_fade - offset)
else
alpha = page_time / item_fade - offset
end
alpha = math.min(math.max(alpha, 0), 1)
item:draw(50, y, alpha)
y = y + item:get_height()
end
end
function EventListSlide:reset()
self.super:reset()
self.x = -self.width
end
function EventListSlide:is_done()
return (self.super.active_time > self.duration * #self.pages)
end
return EventListSlide