-
Notifications
You must be signed in to change notification settings - Fork 5
/
SlideshowSettings.lua
297 lines (244 loc) · 11.1 KB
/
SlideshowSettings.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
--[=====[
https://github.com/dustymethod/BrowserImageSlideshow
https://obsproject.com/forum/resources/browser-image-slideshow.852/
--]=====]
obs = obslua
mode = 0
slideDuration = 3800
defaultSlideDuration = 4000
minSlideDuration = 1000
maxSlideDuration = 100000
loopSlideshow = true
startWithAutoplay = true
browserSourceName = "Browser"
local mode_options = {"Random Order", "Alphabetical Order", "Alphabetical Order, start on random", "Alphanumeric"}
captionEnabled = false
HK_PAUSE = obs.OBS_INVALID_HOTKEY_ID
HK_RESUME = obs.OBS_INVALID_HOTKEY_ID
HK_NEXT = obs.OBS_INVALID_HOTKEY_ID
HK_PREV = obs.OBS_INVALID_HOTKEY_ID
HK_TOGGLE_VISIBLE = obs.OBS_INVALID_HOTKEY_ID
HK_TOGGLE_PAUSE = obs.OBS_INVALID_HOTKEY_ID
HK_RESTART = obs.OBS_INVALID_HOTKEY_ID
HK_REFRESH = obs.OBS_INVALID_HOTKEY_ID
-- keycodes (for use with browser)
local KC_PAUSE = 49 --1
local KC_RESUME = 50 --2
local KC_NEXT = 51 --3
local KC_PREV = 52 --4
local KC_TOGGLE_VISIBLE = 53 --5
local KC_TOGGLE_PAUSE = 54 --6
local KC_RESTART = 55 --7
local KC_REFRESH = 56 --8
function script_description()
return [[
<center><h3>Browser Image Slideshow</h3></center>
<br><a href="https://github.com/dustymethod/BrowserImageSlideshow">Github</a>
<br><a href="https://obsproject.com/forum/resources/browser-image-slideshow.852/">OBS Forum</a>
<br>
<br>Press the <b>Refresh</b> button below after changing any settings or adding images. This will update the slideshow and refresh the browser source.
<br>(Or use the Refresh Source [Slideshow] hotkey)
]]
end
function script_properties()
props = obs.obs_properties_create()
local list = obs.obs_properties_add_list(props, "mode", "Mode:", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT)
for i,v in ipairs(mode_options) do
obs.obs_property_list_add_int(list, v, i-1)
end
obs.obs_properties_add_int(props, "slideDuration", "Slide duration (ms):", minSlideDuration, maxSlideDuration, 500)
obs.obs_properties_add_bool(props, "startWithAutoplay", "Autoplay")
obs.obs_properties_add_bool(props, "loopSlideshow", "Loop slideshow")
obs.obs_properties_add_bool(props, "captionEnabled", "Show filename caption")
obs.obs_properties_add_text(props, "browserSourceName", "Browser source name:\n(for use with hotkeys)", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_button(props, "refreshButton", "Refresh", refresh_source)
return props
end
function pause_slideshow(key_pressed)
send_hotkey_to_browser(KC_PAUSE, key_pressed)
end
function resume_slideshow(key_pressed)
send_hotkey_to_browser(KC_RESUME, key_pressed)
end
function next_slide(key_pressed)
send_hotkey_to_browser(KC_NEXT, key_pressed)
end
function previous_slide(key_pressed)
send_hotkey_to_browser(KC_PREV, key_pressed)
end
function toggle_visibility(key_pressed)
send_hotkey_to_browser(KC_TOGGLE_VISIBLE, key_pressed)
end
function toggle_pause(key_pressed)
send_hotkey_to_browser(KC_TOGGLE_PAUSE, key_pressed)
end
function restart_slideshow(key_pressed)
send_hotkey_to_browser(KC_RESTART, key_pressed)
end
function refresh_slideshow(key_pressed)
if key_pressed then
return
end
refresh_source()
end
function refresh_source()
-- update images
update_image_list()
local source = obs.obs_get_source_by_name(browserSourceName)
if source == nil then
print("Browser source \"" .. browserSourceName .. "\" not found. Be sure to enter the correct source's name in the lua script.")
return
end
local settings = obs.obs_source_get_settings(source)
-- refresh browser source. uses exeldro's method - https://github.com/exeldro/obs-lua/blob/master/refresh-browsers.lua
local fps = obs.obs_data_get_int(settings, "fps")
if fps % 2 == 0 then
obs.obs_data_set_int(settings, "fps", fps + 1)
else
obs.obs_data_set_int(settings, "fps", fps - 1)
end
obs.obs_source_update(source, settings)
obs.obs_source_release(source)
obs.obs_data_release(settings)
end
function getUniqueName(name)
return script_path() .. name
end
-- called when script is loaded
function script_load(settings)
obs.obs_data_set_default_int(settings, "slideDuration", defaultSlideDuration)
obs.obs_data_set_default_bool(settings, "startWithAutoplay", true)
obs.obs_data_set_default_bool(settings, "loopSlideshow", true)
obs.obs_data_set_default_bool(settings, "captionEnabled", false)
obs.obs_data_set_default_string(settings, "browserSourceName", "Browser")
update_image_list()
local hotkey_tag = " [Slideshow]"
local hotkey_save_array = obs.obs_data_get_array(settings, "HK_PAUSE")
HK_PAUSE = obs.obs_hotkey_register_frontend(getUniqueName("HK_PAUSE"), "Pause" .. hotkey_tag, pause_slideshow)
obs.obs_hotkey_load(HK_PAUSE, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_RESUME = obs.obs_hotkey_register_frontend(getUniqueName("HK_RESUME"), "Resume" .. hotkey_tag, resume_slideshow)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_RESUME")
obs.obs_hotkey_load(HK_RESUME, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_NEXT = obs.obs_hotkey_register_frontend(getUniqueName("HK_NEXT"), "Next slide" .. hotkey_tag, next_slide)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_NEXT")
obs.obs_hotkey_load(HK_NEXT, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_PREV = obs.obs_hotkey_register_frontend(getUniqueName("HK_PREV"), "Previous slide" .. hotkey_tag, previous_slide)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_PREV")
obs.obs_hotkey_load(HK_PREV, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_TOGGLE_VISIBLE = obs.obs_hotkey_register_frontend(getUniqueName("HK_TOGGLE_VISIBLE"), "Toggle Visibility" .. hotkey_tag, toggle_visibility)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_TOGGLE_VISIBLE")
obs.obs_hotkey_load(HK_TOGGLE_VISIBLE, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_TOGGLE_PAUSE = obs.obs_hotkey_register_frontend(getUniqueName("HK_TOGGLE_PAUSE"), "Toggle Pause/Resume" .. hotkey_tag, toggle_pause)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_TOGGLE_PAUSE")
obs.obs_hotkey_load(HK_TOGGLE_PAUSE, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_RESTART = obs.obs_hotkey_register_frontend(getUniqueName("HK_RESTART"), "Restart" .. hotkey_tag, restart_slideshow)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_RESTART")
obs.obs_hotkey_load(HK_RESTART, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
HK_REFRESH = obs.obs_hotkey_register_frontend(getUniqueName("HK_REFRESH"), "Refresh source" .. hotkey_tag, refresh_slideshow)
hotkey_save_array = obs.obs_data_get_array(settings, "HK_REFRESH")
obs.obs_hotkey_load(HK_REFRESH, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
end
-- called when script settings change. writes changes to settings.js
function script_update(settings)
mode = obs.obs_data_get_int(settings, "mode")
slideDuration = obs.obs_data_get_int(settings, "slideDuration")
loopSlideshow = obs.obs_data_get_bool(settings, "loopSlideshow")
captionEnabled = obs.obs_data_get_bool(settings, "captionEnabled")
startWithAutoplay = obs.obs_data_get_bool(settings, "startWithAutoplay")
browserSourceName = obs.obs_data_get_string(settings, "browserSourceName")
local output = assert(io.open(script_path() .. 'settings.js', "w"))
output:write('let mode = '.. mode .. ';\n')
output:write('let slideDuration = '.. slideDuration .. ';\n')
if loopSlideshow == true then
output:write('let loopSlideshow = true;\n')
else
output:write('let loopSlideshow = false;\n')
end
if startWithAutoplay == true then
output:write('let startWithAutoplay = true;\n')
else
output:write('let startWithAutoplay = false;\n')
end
if captionEnabled == true then
output:write('let captionEnabled = true;\n')
else
output:write('let captionEnabled = false;\n')
end
output:close()
-- log_slideshow_info()
end
function script_save(settings)
local hotkey_save_array = obs.obs_hotkey_save(HK_PAUSE)
obs.obs_data_set_array(settings, "HK_PAUSE", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_RESUME)
obs.obs_data_set_array(settings, "HK_RESUME", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_NEXT)
obs.obs_data_set_array(settings, "HK_NEXT", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_PREV)
obs.obs_data_set_array(settings, "HK_PREV", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_TOGGLE_VISIBLE)
obs.obs_data_set_array(settings, "HK_TOGGLE_VISIBLE", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_TOGGLE_PAUSE)
obs.obs_data_set_array(settings, "HK_TOGGLE_PAUSE", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_RESTART)
obs.obs_data_set_array(settings, "HK_RESTART", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
hotkey_save_array = obs.obs_hotkey_save(HK_REFRESH)
obs.obs_data_set_array(settings, "HK_REFRESH", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
end
function send_hotkey_to_browser(keycode, key_pressed)
if key_pressed then -- only send on keyup
return
end
local source = obs.obs_get_source_by_name(browserSourceName)
if source == nil then
print("Browser source \"" .. browserSourceName .. "\" not found. Be sure to enter the correct source's name in the lua script.")
return
end
--local vk = obs.obs_key_to_virtual_key(keycode)
local vk = keycode
local event = obs.obs_key_event()
event.native_vkey = vk
--event.native_scancode = vk
event.text = ""
obs.obs_source_send_key_click(source, event, key_pressed)
obs.obs_source_release(source)
end
-- write list of images to images.js
function update_image_list()
local output = assert(io.open(script_path() .. 'images/images.js', "w"))
local files = assert(io.popen('dir "'.. script_path() .. 'images/' ..'" /b'))
output:write('let imageNamesStr=`\n')
local imageCount = 0
for line in files:lines() do
output:write(line .. "\n")
imageCount = imageCount + 1
end
files:close()
output:write('`')
output:close()
end
function log_slideshow_info()
print("Slideshow loaded. \n" ..
"source name: \"" .. browserSourceName ..
"\" | mode: ".. mode_options[mode+1] ..
" | slide duration: ".. slideDuration ..
"ms | loop: " .. tostring(loopSlideshow) ..
" | autoplay: " .. tostring(startWithAutoplay) ..
" | captionEnabled: " .. tostring(captionEnabled))
end