]> git.sesse.net Git - nageru/blob - nageru/simple.lua
Rework the chain concept.
[nageru] / nageru / simple.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 (called “scenes”), setting their parameters and then deciding which
5 -- to show when.
6 --
7 -- Themes are written in Lua, which reflects a simplified form of the Movit API
8 -- where all the low-level details (such as texture formats) and alternatives
9 -- (e.g. turning scaling on or off) are handled by the C++ side and you
10 -- generally just build scenes.
11 --
12 -- This is a much simpler theme than the default theme; it only allows you to
13 -- switch between inputs and set white balance, no transitions or the likes.
14 -- Thus, it should be simpler to understand.
15
16 local input_neutral_color = {{0.5, 0.5, 0.5}, {0.5, 0.5, 0.5}}
17
18 local live_signal_num = 0
19 local preview_signal_num = 1
20
21 local img = ImageInput.new("bg.jpeg")
22
23 local scene = Scene.new(16, 9)
24 local input = scene:add_input()
25 local wb_effect = scene:add_effect(WhiteBalanceEffect.new())
26 scene:finalize()
27
28 -- API ENTRY POINT
29 -- Returns the number of outputs in addition to the live (0) and preview (1).
30 -- Called only once, at the start of the program.
31 function num_channels()
32         return 2
33 end
34
35 -- API ENTRY POINT
36 -- Returns the name for each additional channel (starting from 2).
37 -- Called at the start of the program, and then each frame for live
38 -- channels in case they change resolution.
39 function channel_name(channel)
40         if channel == 2 then
41                 return "First input"
42         elseif channel == 3 then
43                 return "Second input"
44         end
45 end
46
47 -- API ENTRY POINT
48 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
49 -- Should return -1 if the channel does not correspond to a simple signal.
50 -- (The information is used for whether right-click on the channel should bring up
51 -- an input selector or not.)
52 -- Called once for each channel, at the start of the program.
53 -- Will never be called for live (0) or preview (1).
54 function channel_signal(channel)
55         if channel == 2 then
56                 return 0
57         elseif channel == 3 then
58                 return 1
59         else
60                 return -1
61         end
62 end
63
64 -- API ENTRY POINT
65 -- Called every frame. Returns the color (if any) to paint around the given
66 -- channel. Returns a CSS color (typically to mark live and preview signals);
67 -- "transparent" is allowed.
68 -- Will never be called for live (0) or preview (1).
69 function channel_color(channel)
70         return "transparent"
71 end
72
73 -- API ENTRY POINT
74 -- Returns if a given channel supports setting white balance (starting from 2).
75 -- Called only once for each channel, at the start of the program.
76 function supports_set_wb(channel)
77         return channel == 2 or channel == 3
78 end
79
80 -- API ENTRY POINT
81 -- Gets called with a new gray point when the white balance is changing.
82 -- The color is in linear light (not sRGB gamma).
83 function set_wb(channel, red, green, blue)
84         if channel == 2 then
85                 input_neutral_color[1] = { red, green, blue }
86         elseif channel == 3 then
87                 input_neutral_color[2] = { red, green, blue }
88         end
89 end
90
91 -- API ENTRY POINT
92 -- Called every frame.
93 function get_transitions(t)
94         if live_signal_num == preview_signal_num then
95                 -- No transitions possible.
96                 return {}
97         else
98                 return {"Cut"}
99         end
100 end
101
102 -- API ENTRY POINT
103 -- Called when the user clicks a transition button. For our case,
104 -- we only do cuts, so we ignore the parameters; just switch live and preview.
105 function transition_clicked(num, t)
106         local temp = live_signal_num
107         live_signal_num = preview_signal_num
108         preview_signal_num = temp
109 end
110
111 -- API ENTRY POINT
112 function channel_clicked(num)
113         preview_signal_num = num
114 end
115
116 -- API ENTRY POINT
117 -- Called every frame. Get the scene for displaying at input <num>,
118 -- where 0 is live, 1 is preview, 2 is the first channel to display
119 -- in the bottom bar, and so on up to num_channels()+1. t is the
120 -- current time in seconds. width and height are the dimensions of
121 -- the output, although you can ignore them if you don't need them
122 -- (they're useful if you want to e.g. know what to resample by).
123 --
124 -- <signals> is basically an exposed InputState, which you can use to
125 -- query for information about the signals at the point of the current
126 -- frame. In particular, you can call get_width() and get_height()
127 -- for any signal number, and use that to e.g. assist in scene selection.
128 --
129 -- You should return the scene to use, after having set any parameters you
130 -- want to set (through set_int() etc.). The parameters will be snapshot
131 -- at return time and used during rendering.
132 function get_scene(num, t, width, height, signals)
133         local signal_num
134         if num == 0 then  -- Live (right pane).
135                 signal_num = live_signal_num
136         elseif num == 1 then  -- Preview (left pane).
137                 signal_num = preview_signal_num
138         else  -- One of the two previews (bottom panes).
139                 signal_num = num - 2
140         end
141
142         if num == 3 then
143                 input:display(img)
144         else
145                 input:display(signal_num)
146         end
147
148         local color = input_neutral_color[signal_num + 1]
149         wb_effect:set_vec3("neutral_color", color[1], color[2], color[3])
150
151         return scene
152 end