]> git.sesse.net Git - nageru/blob - theme.lua
Hook up some timers for the Lua theme, and make a more beautiful transition experience.
[nageru] / theme.lua
1 -- The theme is what decides what's actually shown on screen, what kind of
2 -- transitions are available (if any), and what kind of inputs there are,
3 -- if any. In general, it drives the entire display logic by creating Movit
4 -- chains, setting their parameters and then deciding which to show when.
5 --
6 -- Themes are written in Lua, which reflects a simplified form of the Movit API
7 -- where all the low-level details (such as texture formats) are handled by the
8 -- C++ side and you generally just build chains.
9 io.write("hello from lua\n");
10
11 local zoom_start = -2.0;
12 local zoom_end = -1.0;
13 local zoom_src = 0.0;
14 local zoom_dst = 1.0;
15
16 local live_signal_num = 0;
17 local preview_signal_num = 1;
18
19 -- The main live chain.
20 local main_chain = EffectChain.new(16, 9);
21 local input0 = main_chain:add_live_input();
22 input0:connect_signal(0);
23 local input1 = main_chain:add_live_input();
24 input1:connect_signal(1);
25 local resample_effect = main_chain:add_effect(ResampleEffect.new(), input0);
26 local padding_effect = main_chain:add_effect(IntegralPaddingEffect.new());
27 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0);
28
29 local resample2_effect = main_chain:add_effect(ResampleEffect.new(), input1);
30 -- Effect *saturation_effect = main_chain->add_effect(new SaturationEffect());
31 -- CHECK(saturation_effect->set_float("saturation", 0.3f));
32 local wb_effect = main_chain:add_effect(WhiteBalanceEffect.new());
33 wb_effect:set_float("output_color_temperature", 3500.0);
34 local padding2_effect = main_chain:add_effect(IntegralPaddingEffect.new());
35
36 main_chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect);
37 main_chain:finalize(true);
38
39 -- A chain to show a single input on screen.
40 local simple_chain = EffectChain.new(16, 9);
41 local simple_chain_input = simple_chain:add_live_input();
42 simple_chain_input:connect_signal(0);  -- First input card. Can be changed whenever you want.
43 simple_chain:finalize(false);
44
45 -- Returns the number of outputs in addition to the live (0) and preview (1).
46 -- Called only once, at the start of the program.
47 function num_channels()
48         return 2;
49 end
50
51 -- Called every frame.
52 function get_transitions()
53         return {"Cut", "Fade", "Zoom!"};
54 end
55
56 function transition_clicked(num, t)
57         -- local temp = live_signal_num;
58         -- live_signal_num = preview_signal_num;
59         -- preview_signal_num = temp;
60
61         zoom_start = t;
62         zoom_end = t + 1.0;
63
64         local temp = zoom_src;
65         zoom_src = zoom_dst;
66         zoom_dst = temp;
67 end
68
69 function channel_clicked(num, t)
70         -- Presumably change the preview here.
71         io.write("STUB: channel_clicked\n");
72 end
73
74 -- Called every frame. Get the chain for displaying at input <num>,
75 -- where 0 is live, 1 is preview, 2 is the first channel to display
76 -- in the bottom bar, and so on up to num_channels()+1. t is the 
77 -- current time in seconds. width and height are the dimensions of
78 -- the output, although you can ignore them if you don't need them
79 -- (they're useful if you want to e.g. know what to resample by).
80 --
81 -- You should return two objects; the chain itself, and then a
82 -- function (taking no parameters) that is run just before rendering.
83 -- The function needs to call connect_signal on any inputs, so that
84 -- it gets updated video data for the given frame. (You are allowed
85 -- to switch which input your input is getting from between frames,
86 -- but not calling connect_signal results in undefined behavior.)
87 -- If you want to change any parameters in the chain, this is also
88 -- the right place.
89 --
90 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
91 -- if and only if num==0.
92 function get_chain(num, t, width, height)
93         if num == 0 then  -- Live.
94                 prepare = function()
95                         if (t < zoom_start) then
96                                 prepare_sbs_chain(zoom_src, width, height);
97                         elseif (t > zoom_end) then
98                                 prepare_sbs_chain(zoom_dst, width, height);
99                         else
100                                 local tt = (t - zoom_start) / (zoom_end - zoom_start);
101                                 -- Smooth it a bit.
102                                 tt = math.sin(tt * 3.14159265358 * 0.5)
103                                 prepare_sbs_chain(zoom_src + (zoom_dst - zoom_src) * tt, width, height);
104                         end
105                 end
106                 return main_chain, prepare;
107         end
108         if num == 1 then  -- Preview.
109                 prepare = function()
110                         simple_chain_input:connect_signal(preview_signal_num);
111                 end
112                 return simple_chain, prepare;
113         end
114         if num == 2 then
115                 prepare = function()
116                         simple_chain_input:connect_signal(0);
117                 end
118                 return simple_chain, prepare;
119         end
120         if num == 3 then
121                 prepare = function()
122                         simple_chain_input:connect_signal(1);
123                 end
124                 return simple_chain, prepare;
125         end
126 end
127
128 function place_rectangle(resample_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height)
129         local srcx0 = 0.0;
130         local srcx1 = 1.0;
131         local srcy0 = 0.0;
132         local srcy1 = 1.0;
133
134         -- Cull.
135         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
136                 resample_effect:set_int("width", 1);
137                 resample_effect:set_int("height", 1);
138                 resample_effect:set_float("zoom_x", screen_width);
139                 resample_effect:set_float("zoom_y", screen_height);
140                 padding_effect:set_int("left", screen_width + 100);
141                 padding_effect:set_int("top", screen_height + 100);
142                 return;
143         end
144
145         -- Clip. (TODO: Clip on upper/left sides, too.)
146         if x1 > screen_width then
147                 srcx1 = (screen_width - x0) / (x1 - x0);
148                 x1 = screen_width;
149         end
150         if y1 > screen_height then
151                 srcy1 = (screen_height - y0) / (y1 - y0);
152                 y1 = screen_height;
153         end
154
155         local x_subpixel_offset = x0 - math.floor(x0);
156         local y_subpixel_offset = y0 - math.floor(y0);
157
158         -- Resampling must be to an integral number of pixels. Round up,
159         -- and then add an extra pixel so we have some leeway for the border.
160         local width = math.ceil(x1 - x0) + 1;
161         local height = math.ceil(y1 - y0) + 1;
162         resample_effect:set_int("width", width);
163         resample_effect:set_int("height", height);
164
165         -- Correct the discrepancy with zoom. (This will leave a small
166         -- excess edge of pixels and subpixels, which we'll correct for soon.)
167         local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
168         local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
169         resample_effect:set_float("zoom_x", zoom_x);
170         resample_effect:set_float("zoom_y", zoom_y);
171         resample_effect:set_float("zoom_center_x", 0.0);
172         resample_effect:set_float("zoom_center_y", 0.0);
173
174         -- Padding must also be to a whole-pixel offset.
175         padding_effect:set_int("left", math.floor(x0));
176         padding_effect:set_int("top", math.floor(y0));
177
178         -- Correct _that_ discrepancy by subpixel offset in the resampling.
179         resample_effect:set_float("left", -x_subpixel_offset / zoom_x);
180         resample_effect:set_float("top", -y_subpixel_offset / zoom_y);
181
182         -- Finally, adjust the border so it is exactly where we want it.
183         padding_effect:set_float("border_offset_left", x_subpixel_offset);
184         padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width));
185         padding_effect:set_float("border_offset_top", y_subpixel_offset);
186         padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height));
187 end
188
189 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
190 function round(x)
191         return math.floor(x + 0.5)
192 end
193
194 function prepare_sbs_chain(t, screen_width, screen_height)
195         input0:connect_signal(live_signal_num);
196         input1:connect_signal(1);
197
198         -- First input is positioned (16,48) from top-left.
199         local width0 = round(848 * screen_width/1280.0);
200         local height0 = round(width0 * 9.0 / 16.0);
201
202         local top0 = 48 * screen_height/720.0;
203         local left0 = 16 * screen_width/1280.0;
204         local bottom0 = top0 + height0;
205         local right0 = left0 + width0;
206
207         -- Second input is positioned (16,48) from the bottom-right.
208         local width1 = 384 * screen_width/1280.0;
209         local height1 = 216 * screen_height/720.0;
210
211         local bottom1 = screen_height - 48 * screen_height/720.0;
212         local right1 = screen_width - 16 * screen_width/1280.0;
213         local top1 = bottom1 - height1;
214         local left1 = right1 - width1;
215
216         -- Interpolate between the fullscreen and side-by-side views.
217         local scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
218         local tx0 = 0.0 + t * (-left0 * scale0);
219         local ty0 = 0.0 + t * (-top0 * scale0);
220
221         top0 = top0 * scale0 + ty0;
222         bottom0 = bottom0 * scale0 + ty0;
223         left0 = left0 * scale0 + tx0;
224         right0 = right0 * scale0 + tx0;
225
226         top1 = top1 * scale0 + ty0;
227         bottom1 = bottom1 * scale0 + ty0;
228         left1 = left1 * scale0 + tx0;
229         right1 = right1 * scale0 + tx0;
230         place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0, screen_width, screen_height);
231         place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1, screen_width, screen_height);
232 end