forked from info-beamer/package-conference-room
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.lua
More file actions
138 lines (109 loc) · 3.84 KB
/
background.lua
File metadata and controls
138 lines (109 loc) · 3.84 KB
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
-------------------------------------------------------------------------------
--- Constants (configuration)
local DEFAULT_BG_COLOR = {rgba = function() return 0.5, 0.5, 0.5, 1 end}
local DEFAULT_FG_COLOR = {rgba = function() return 1, 0, 0, 1 end}
local DEFAULT_FONT = CONFIG.font
local DEFAULT_TEXT = "UNKNOWN BACKGROUND STYLE"
local DEFAULT_TEXTSIZE = 0.1
-------------------------------------------------------------------------------
--- Class
local Background = {}
Background.__index = Background
-------------------------------------------------------------------------------
--- Helper Functions
local defaultBGcolTex = resource.create_colored_texture(DEFAULT_BG_COLOR.rgba())
local defaultTextWidth = tools.textWidth(DEFAULT_FONT, DEFAULT_TEXT, DEFAULT_TEXTSIZE)
local function drawBGDefault()
defaultBGcolTex:draw(0, 0, NATIVE_WIDTH, NATIVE_HEIGHT)
tools.drawText(DEFAULT_FONT, 0.5 - defaultTextWidth/2, 0.5 - DEFAULT_TEXTSIZE/2, DEFAULT_TEXT, DEFAULT_TEXTSIZE, DEFAULT_FG_COLOR)
end
local function setupStaticBackground(self)
local image = CONFIG.background_static
tools.debugPrint(2, "background: loading image " .. image.asset_name)
image.load()
self._draw = function()
-- we assume the image already has the correct aspect ratio
image.draw(0, 0, NATIVE_WIDTH, NATIVE_HEIGHT)
end
self._cleanup = function()
tools.debugPrint(2, "background: unloading image " .. image.asset_name)
image.unload()
end
self._updateNeeded = function()
return image.asset_name ~= CONFIG.background_static.asset_name
end
end
local function setupVideoBackground(self)
local video = CONFIG.background_video
tools.debugPrint(2, "background: loading video " .. video.asset_name)
video.load({ loop=true, raw=true, layer=-1 })
self._draw = function()
-- we assume the video already has the correct aspect ratio
video.draw(0, 0, NATIVE_WIDTH, NATIVE_HEIGHT)
end
self._cleanup = function()
tools.debugPrint(2, "background: unloading video " .. video.asset_name)
video.unload()
end
self._updateNeeded = function()
return video.asset_name ~= CONFIG.background_video.asset_name
end
end
local function setupBackground(self, style)
self._draw = nil
self._cleanup = nil
self._updateNeeded = nil
local fancymode = style:match("^fancy%-(.*)$")
if style == "static" then
setupStaticBackground(self)
elseif style == "video" then
setupVideoBackground(self)
elseif fancymode then
local fancy = require "fancy"
fancy.fixaspect = tools.fixAspect
self._draw = function()
fancy.render(fancymode)
end
else
tools.debugPrint(1, "WARNING: invalid background style: " .. style)
return
end
tools.debugPrint(1, "background style is now: " .. style)
end
-------------------------------------------------------------------------------
--- Constructor
function Background.new(style)
local self = {
style = style,
_draw = nil,
_cleanup = nil,
_check = nil
}
if style then setupBackground(self, style) end
return setmetatable(self, Background)
end
-------------------------------------------------------------------------------
--- Member Functions
function Background:update(style)
if style ~= self.style or (self._updateNeeded and self._updateNeeded()) then
self:cleanup()
setupBackground(self, style)
self.style = style
end
end
function Background:cleanup()
if self._cleanup then
self._cleanup()
end
end
function Background:draw()
gl.ortho()
if self._draw then
self._draw()
else
drawBGDefault()
end
end
-------------------------------------------------------------------------------
print("background.lua loaded completely")
return Background