-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdownMarcos.lua
225 lines (186 loc) · 6.27 KB
/
countdownMarcos.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
obs = obslua
source_name = ""
total_seconds = 0
cur_seconds = 0
last_text = ""
stop_text = ""
activated = false
addtime = 0
hotkey_id = obs.OBS_INVALID_HOTKEY_ID
-- Function to set the time text
function set_time_text()
local seconds = math.floor(cur_seconds % 60)
local total_minutes = math.floor(cur_seconds / 60)
local minutes = math.floor(total_minutes % 60)
local hours = math.floor(total_minutes / 60)
local text = string.format("%02d:%02d:%02d", hours, minutes, seconds)
if cur_seconds < 1 then
text = stop_text
end
if text ~= last_text then
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
local settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
end
end
last_text = text
end
function timer_callback()
cur_seconds = cur_seconds - 1
if cur_seconds < 0 then
obs.remove_current_callback()
cur_seconds = 0
end
set_time_text()
end
function activate(activating)
if activated == activating then
return
end
activated = activating
if activating then
cur_seconds = total_seconds
set_time_text()
obs.timer_add(timer_callback, 1000)
else
obs.timer_remove(timer_callback)
end
end
function add_10minutes(activating)
if activated == activating then
return
end
activated = activating
if activating then
cur_seconds = cur_seconds + 600
set_time_text()
obs.timer_add(timer_callback,1000)
else
obs.timer_remove(timer_callback)
end
end
-- Called when a source is activated/deactivated
function activate_signal(cd, activating)
local source = obs.calldata_source(cd, "source")
if source ~= nil then
local name = obs.obs_source_get_name(source)
if (name == source_name) then
activate(activating)
end
end
end
function source_activated(cd)
activate_signal(cd, true)
end
function source_deactivated(cd)
activate_signal(cd, false)
end
function reset(pressed)
if not pressed then
return
end
activate(false)
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
local active = obs.obs_source_active(source)
obs.obs_source_release(source)
activate(active)
end
end
function add_timeM(pressed)
if not pressed then
return
end
add_10minutes(false)
local source = obs.obs_get_source_by_name(source_name)
if source ~= nil then
local active = obs.obs_source_active(source)
obs.obs_source_release(source)
add_10minutes(active)
end
end
function reset_button_clicked(props, p)
reset(true)
return false
end
function add_time_button_clicked(props, p)
add_timeM(true)
return false
end
----------------------------------------------------------
-- A function named script_properties defines the properties that the user
-- can change for the entire script module itself
function script_properties()
local props = obs.obs_properties_create()
obs.obs_properties_add_int(props, "duration", "Duration (minutes)", 1, 100000, 1)
local p = obs.obs_properties_add_list(props, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
local sources = obs.obs_enum_sources()
if sources ~= nil then
for _, source in ipairs(sources) do
source_id = obs.obs_source_get_unversioned_id(source)
if source_id == "text_gdiplus" or source_id == "text_ft2_source" then
local name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)
end
end
end
obs.source_list_release(sources)
obs.obs_properties_add_text(props, "stop_text", "Final Text", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_button(props, "reset_button", "Reset Timer", reset_button_clicked)
obs.obs_properties_add_button(props, "add_10minutes", "Add time", add_time_button_clicked)
return props
end
-- A function named script_description returns the description shown to
-- the user
function script_description()
return "Sets a text source to act as a countdown timer when the source is active.\n\nMade by Jim"
end
-- A function named script_update will be called when settings are changed
function script_update(settings)
activate(false)
total_seconds = obs.obs_data_get_int(settings, "duration") * 60
source_name = obs.obs_data_get_string(settings, "source")
stop_text = obs.obs_data_get_string(settings, "stop_text")
reset(true)
end
-- A function named script_defaults will be called to set the default settings
function script_defaults(settings)
obs.obs_data_set_default_int(settings, "duration", 5)
obs.obs_data_set_default_string(settings, "stop_text", "Starting soon (tm)")
end
-- A function named script_save will be called when the script is saved
--
-- NOTE: This function is usually used for saving extra data (such as in this
-- case, a hotkey's save data). Settings set via the properties are saved
-- automatically.
function script_save(settings)
local hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
obs.obs_data_set_array(settings, "reset_hotkey", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
end
-- a function named script_load will be called on startup
function script_load(settings)
-- Connect hotkey and activation/deactivation signal callbacks
--
-- NOTE: These particular script callbacks do not necessarily have to
-- be disconnected, as callbacks will automatically destroy themselves
-- if the script is unloaded. So there's no real need to manually
-- disconnect callbacks that are intended to last until the script is
-- unloaded.
local sh = obs.obs_get_signal_handler()
obs.signal_handler_connect(sh, "source_activate", source_activated)
obs.signal_handler_connect(sh, "source_deactivate", source_deactivated)
hotkey_id = obs.obs_hotkey_register_frontend("reset_timer_thingy", "Reset Timer", reset)
local hotkey_save_array = obs.obs_data_get_array(settings, "reset_hotkey")
obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
--Noob try of copying functionality to add button 10 minutes add to the countdown timer
id_add_time = obs.obs_hotkey_register_frontend("id_add_timer_marcos", "Add time", add_10minutes)
local obs_hotkey_addtime = obs.obs_data_get_array (settings, "addtimeMarcos_hotkey")
obs.obs_hotkey_load(id_add_time, hotkey_addtimemarcos)
obs.obs_data_array_release(hotkey_addtimemarcos)
end