Created
March 24, 2022 04:52
-
-
Save MrFlick/cb84410d6beab4cdd0e1b6d8e2c0f102 to your computer and use it in GitHub Desktop.
OBS On Screen Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obs = obslua | |
source_name = "" | |
total_seconds = 0 | |
timer_state = 0 | |
time_delta = 30 | |
cur_seconds = 0 | |
last_text = "" | |
stop_text = "" | |
activated = false | |
reset_hotkey_id = obs.OBS_INVALID_HOTKEY_ID | |
time_add_hotkey_id = obs.OBS_INVALID_HOTKEY_ID | |
time_sub_hotkey_id = obs.OBS_INVALID_HOTKEY_ID | |
time_toggle_hotkey_id = obs.OBS_INVALID_HOTKEY_ID | |
-- Function to set the time text | |
function set_time_text() | |
if cur_seconds < 0 then | |
cur_seconds = 0 | |
end | |
local seconds = math.floor(cur_seconds % 60) | |
local total_minutes = math.floor(cur_seconds / 60) | |
local minutes = math.floor(total_minutes % 60) | |
local text = string.format("%02d:%02d", minutes, seconds) | |
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 | |
set_is_activated(false) | |
cur_seconds = 0 | |
end | |
set_time_text() | |
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 time_add(pressed) | |
if not pressed then | |
return | |
end | |
print("add") | |
adjust_time(0+time_delta) | |
end | |
function time_sub(pressed) | |
if not pressed then | |
return | |
end | |
print("sub") | |
adjust_time(0-time_delta) | |
end | |
function adjust_time(diff) | |
print("adjust") | |
print(tostring(diff)) | |
total_seconds = total_seconds + diff | |
if (total_seconds < 0) then | |
total_seconds = 0 | |
end | |
cur_seconds = cur_seconds + diff | |
if (cur_seconds < 0 ) then | |
cur_seconds = 0 | |
end | |
if (cur_seconds > total_seconds) then | |
cur_seconds = total_seconds | |
end | |
set_time_text() | |
end | |
function time_toggle(pressed) | |
if not pressed then | |
return | |
end | |
print("Toggle") | |
if activated then | |
set_is_activated(false) | |
else | |
if (cur_seconds <= 0) then | |
cur_seconds = total_seconds | |
set_time_text() | |
else | |
set_is_activated(true) | |
end | |
end | |
end | |
function set_is_activated(activating) | |
if activated == activating then | |
return | |
end | |
if activating then | |
activated = true | |
obs.timer_add(timer_callback, 1000) | |
else | |
activated = false | |
obs.timer_remove(timer_callback) | |
end | |
print(tostring(activated)) | |
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 | |
cur_seconds = total_seconds | |
set_time_text() | |
set_is_activated(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 reset_button_clicked(props, p) | |
reset(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) | |
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. | |
Modified version of default OBS script originally made by Jim]] | |
end | |
-- A function named script_update will be called when settings are changed | |
function script_update(settings) | |
set_is_activated(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(reset_hotkey_id) | |
obs.obs_data_set_array(settings, "reset_hotkey", hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
local hotkey_save_array = obs.obs_hotkey_save(time_add_hotkey_id) | |
obs.obs_data_set_array(settings, "time_add_hotkey", hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
local hotkey_save_array = obs.obs_hotkey_save(time_sub_hotkey_id) | |
obs.obs_data_set_array(settings, "time_sub_hotkey", hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
local hotkey_save_array = obs.obs_hotkey_save(time_toggle_hotkey_id) | |
obs.obs_data_set_array(settings, "time_tog_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) | |
reset_hotkey_id = obs.obs_hotkey_register_frontend("reset_trail_timer", "Reset Trial Timer", reset) | |
local hotkey_save_array = obs.obs_data_get_array(settings, "reset_hotkey") | |
obs.obs_hotkey_load(reset_hotkey_id, hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
time_add_hotkey_id = obs.obs_hotkey_register_frontend("trial_add_time", "Add Trial Time", time_add) | |
local hotkey_save_array = obs.obs_data_get_array(settings, "time_add_hotkey") | |
obs.obs_hotkey_load(time_add_hotkey_id, hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
time_sub_hotkey_id = obs.obs_hotkey_register_frontend("trial_sub_time", "Subtract Trial Time", time_sub) | |
local hotkey_save_array = obs.obs_data_get_array(settings, "time_sub_hotkey") | |
obs.obs_hotkey_load(time_sub_hotkey_id, hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
time_toggle_hotkey_id = obs.obs_hotkey_register_frontend("trial_tog_time", "Toggle Trial Timer", time_toggle) | |
local hotkey_save_array = obs.obs_data_get_array(settings, "time_tog_hotkey") | |
obs.obs_hotkey_load(time_toggle_hotkey_id, hotkey_save_array) | |
obs.obs_data_array_release(hotkey_save_array) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment