countdown.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. obs = obslua
  2. source_name = ""
  3. total_seconds = 0
  4. cur_seconds = 0
  5. last_text = ""
  6. stop_text = ""
  7. activated = false
  8. hotkey_id = obs.OBS_INVALID_HOTKEY_ID
  9. -- Function to set the time text
  10. function set_time_text()
  11. local seconds = math.floor(cur_seconds % 60)
  12. local total_minutes = math.floor(cur_seconds / 60)
  13. local minutes = math.floor(total_minutes % 60)
  14. local hours = math.floor(total_minutes / 60)
  15. local text = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  16. if cur_seconds < 1 then
  17. text = stop_text
  18. end
  19. if text ~= last_text then
  20. local source = obs.obs_get_source_by_name(source_name)
  21. if source ~= nil then
  22. local settings = obs.obs_data_create()
  23. obs.obs_data_set_string(settings, "text", text)
  24. obs.obs_source_update(source, settings)
  25. obs.obs_data_release(settings)
  26. obs.obs_source_release(source)
  27. end
  28. end
  29. last_text = text
  30. end
  31. function timer_callback()
  32. cur_seconds = cur_seconds - 1
  33. if cur_seconds < 0 then
  34. obs.remove_current_callback()
  35. cur_seconds = 0
  36. end
  37. set_time_text()
  38. end
  39. function activate(activating)
  40. if activated == activating then
  41. return
  42. end
  43. activated = activating
  44. if activating then
  45. cur_seconds = total_seconds
  46. set_time_text()
  47. obs.timer_add(timer_callback, 1000)
  48. else
  49. obs.timer_remove(timer_callback)
  50. end
  51. end
  52. -- Called when a source is activated/deactivated
  53. function activate_signal(cd, activating)
  54. local source = obs.calldata_source(cd, "source")
  55. if source ~= nil then
  56. local name = obs.obs_source_get_name(source)
  57. if (name == source_name) then
  58. activate(activating)
  59. end
  60. end
  61. end
  62. function source_activated(cd)
  63. activate_signal(cd, true)
  64. end
  65. function source_deactivated(cd)
  66. activate_signal(cd, false)
  67. end
  68. function reset(pressed)
  69. if not pressed then
  70. return
  71. end
  72. activate(false)
  73. local source = obs.obs_get_source_by_name(source_name)
  74. if source ~= nil then
  75. local active = obs.obs_source_active(source)
  76. obs.obs_source_release(source)
  77. activate(active)
  78. end
  79. end
  80. function reset_button_clicked(props, p)
  81. reset(true)
  82. return false
  83. end
  84. ----------------------------------------------------------
  85. -- A function named script_properties defines the properties that the user
  86. -- can change for the entire script module itself
  87. function script_properties()
  88. local props = obs.obs_properties_create()
  89. obs.obs_properties_add_int(props, "duration", "Duration (minutes)", 1, 100000, 1)
  90. local p = obs.obs_properties_add_list(props, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
  91. local sources = obs.obs_enum_sources()
  92. if sources ~= nil then
  93. for _, source in ipairs(sources) do
  94. source_id = obs.obs_source_get_unversioned_id(source)
  95. if source_id == "text_gdiplus" or source_id == "text_ft2_source" then
  96. local name = obs.obs_source_get_name(source)
  97. obs.obs_property_list_add_string(p, name, name)
  98. end
  99. end
  100. end
  101. obs.source_list_release(sources)
  102. obs.obs_properties_add_text(props, "stop_text", "Final Text", obs.OBS_TEXT_DEFAULT)
  103. obs.obs_properties_add_button(props, "reset_button", "Reset Timer", reset_button_clicked)
  104. return props
  105. end
  106. -- A function named script_description returns the description shown to
  107. -- the user
  108. function script_description()
  109. return "Sets a text source to act as a countdown timer when the source is active.\n\nMade by Lain"
  110. end
  111. -- A function named script_update will be called when settings are changed
  112. function script_update(settings)
  113. activate(false)
  114. total_seconds = obs.obs_data_get_int(settings, "duration") * 60
  115. source_name = obs.obs_data_get_string(settings, "source")
  116. stop_text = obs.obs_data_get_string(settings, "stop_text")
  117. reset(true)
  118. end
  119. -- A function named script_defaults will be called to set the default settings
  120. function script_defaults(settings)
  121. obs.obs_data_set_default_int(settings, "duration", 5)
  122. obs.obs_data_set_default_string(settings, "stop_text", "Starting soon (tm)")
  123. end
  124. -- A function named script_save will be called when the script is saved
  125. --
  126. -- NOTE: This function is usually used for saving extra data (such as in this
  127. -- case, a hotkey's save data). Settings set via the properties are saved
  128. -- automatically.
  129. function script_save(settings)
  130. local hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
  131. obs.obs_data_set_array(settings, "reset_hotkey", hotkey_save_array)
  132. obs.obs_data_array_release(hotkey_save_array)
  133. end
  134. -- a function named script_load will be called on startup
  135. function script_load(settings)
  136. -- Connect hotkey and activation/deactivation signal callbacks
  137. --
  138. -- NOTE: These particular script callbacks do not necessarily have to
  139. -- be disconnected, as callbacks will automatically destroy themselves
  140. -- if the script is unloaded. So there's no real need to manually
  141. -- disconnect callbacks that are intended to last until the script is
  142. -- unloaded.
  143. local sh = obs.obs_get_signal_handler()
  144. obs.signal_handler_connect(sh, "source_activate", source_activated)
  145. obs.signal_handler_connect(sh, "source_deactivate", source_deactivated)
  146. hotkey_id = obs.obs_hotkey_register_frontend("reset_timer_thingy", "Reset Timer", reset)
  147. local hotkey_save_array = obs.obs_data_get_array(settings, "reset_hotkey")
  148. obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
  149. obs.obs_data_array_release(hotkey_save_array)
  150. end