]> git.sesse.net Git - nageru/blob - nageru/simple.lua
Make it possible to set number of channels imperatively instead of using a callback.
[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 -- Set some global state.
29 Nageru.set_num_channels(2)  -- Can only be called at the start of the program.
30 Nageru.set_channel_name(2, "First input")
31 Nageru.set_channel_name(3, "Second input")
32
33 -- API ENTRY POINT
34 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
35 -- Should return -1 if the channel does not correspond to a simple signal.
36 -- (The information is used for whether right-click on the channel should bring up
37 -- an input selector or not.)
38 -- Called once for each channel, at the start of the program.
39 -- Will never be called for live (0) or preview (1).
40 function channel_signal(channel)
41         if channel == 2 then
42                 return 0
43         elseif channel == 3 then
44                 return 1
45         else
46                 return -1
47         end
48 end
49
50 -- API ENTRY POINT
51 -- Called every frame. Returns the color (if any) to paint around the given
52 -- channel. Returns a CSS color (typically to mark live and preview signals);
53 -- "transparent" is allowed.
54 -- Will never be called for live (0) or preview (1).
55 function channel_color(channel)
56         return "transparent"
57 end
58
59 -- API ENTRY POINT
60 -- Returns if a given channel supports setting white balance (starting from 2).
61 -- Called only once for each channel, at the start of the program.
62 function supports_set_wb(channel)
63         return channel == 2 or channel == 3
64 end
65
66 -- API ENTRY POINT
67 -- Gets called with a new gray point when the white balance is changing.
68 -- The color is in linear light (not sRGB gamma).
69 function set_wb(channel, red, green, blue)
70         if channel == 2 then
71                 input_neutral_color[1] = { red, green, blue }
72         elseif channel == 3 then
73                 input_neutral_color[2] = { red, green, blue }
74         end
75 end
76
77 -- API ENTRY POINT
78 -- Called every frame.
79 function get_transitions(t)
80         if live_signal_num == preview_signal_num then
81                 -- No transitions possible.
82                 return {}
83         else
84                 return {"Cut"}
85         end
86 end
87
88 -- API ENTRY POINT
89 -- Called when the user clicks a transition button. For our case,
90 -- we only do cuts, so we ignore the parameters; just switch live and preview.
91 function transition_clicked(num, t)
92         local temp = live_signal_num
93         live_signal_num = preview_signal_num
94         preview_signal_num = temp
95 end
96
97 -- API ENTRY POINT
98 function channel_clicked(num)
99         preview_signal_num = num
100 end
101
102 -- API ENTRY POINT
103 -- Called every frame. Get the scene for displaying at input <num>,
104 -- where 0 is live, 1 is preview, 2 is the first channel to display
105 -- in the bottom bar, and so on up to num_channels()+1. t is the
106 -- current time in seconds. width and height are the dimensions of
107 -- the output, although you can ignore them if you don't need them
108 -- (they're useful if you want to e.g. know what to resample by).
109 --
110 -- <signals> is basically an exposed InputState, which you can use to
111 -- query for information about the signals at the point of the current
112 -- frame. In particular, you can call get_width() and get_height()
113 -- for any signal number, and use that to e.g. assist in scene selection.
114 --
115 -- You should return the scene to use, after having set any parameters you
116 -- want to set (through set_int() etc.). The parameters will be snapshot
117 -- at return time and used during rendering.
118 function get_scene(num, t, width, height, signals)
119         local signal_num
120         if num == 0 then  -- Live (right pane).
121                 signal_num = live_signal_num
122         elseif num == 1 then  -- Preview (left pane).
123                 signal_num = preview_signal_num
124         else  -- One of the two previews (bottom panes).
125                 signal_num = num - 2
126         end
127
128         if num == 3 then
129                 input:display(img)
130         else
131                 input:display(signal_num)
132         end
133
134         local color = input_neutral_color[signal_num + 1]
135         wb_effect:set_vec3("neutral_color", color[1], color[2], color[3])
136
137         return scene
138 end