-
Notifications
You must be signed in to change notification settings - Fork 9
/
hotkey_counter.py
223 lines (166 loc) · 6.57 KB
/
hotkey_counter.py
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
import obspython as S
__version__ = "1.1.0"
class TextContent:
def __init__(self, source_name=None, text_string="This is default text"):
self.source_name = source_name
self.text_string = text_string
self.counter = 0
def update_text(self, counter_text, counter_value=0):
source = S.obs_get_source_by_name(self.source_name)
settings = S.obs_data_create()
if counter_value == 1:
self.counter += 1
if counter_value == -1:
self.counter -= 1
if counter_value == 0:
self.counter = 0
if isinstance(counter_value, str):
self.counter = int(counter_value)
self.text_string = f"{counter_text}{self.counter}"
S.obs_data_set_string(settings, "text", self.text_string)
S.obs_source_update(source, settings)
S.obs_data_release(settings)
S.obs_source_release(source)
class Driver(TextContent):
def increment(self):
self.update_text(self.counter_text, 1)
def decrement(self):
self.update_text(self.counter_text, -1)
def reset(self):
self.update_text(self.counter_text, 0)
def do_custom(self, val):
self.update_text(self.counter_text, str(val))
class Hotkey:
def __init__(self, callback, obs_settings, _id):
self.obs_data = obs_settings
self.hotkey_id = S.OBS_INVALID_HOTKEY_ID
self.hotkey_saved_key = None
self.callback = callback
self._id = _id
self.load_hotkey()
self.register_hotkey()
self.save_hotkey()
def register_hotkey(self):
description = "Htk " + str(self._id)
self.hotkey_id = S.obs_hotkey_register_frontend(
"htk_id" + str(self._id), description, self.callback
)
S.obs_hotkey_load(self.hotkey_id, self.hotkey_saved_key)
def load_hotkey(self):
self.hotkey_saved_key = S.obs_data_get_array(
self.obs_data, "htk_id" + str(self._id)
)
S.obs_data_array_release(self.hotkey_saved_key)
def save_hotkey(self):
self.hotkey_saved_key = S.obs_hotkey_save(self.hotkey_id)
S.obs_data_set_array(
self.obs_data, "htk_id" + str(self._id), self.hotkey_saved_key
)
S.obs_data_array_release(self.hotkey_saved_key)
class HotkeyDataHolder:
htk_copy = None # this attribute will hold instance of Hotkey
hotkeys_counter_1 = Driver()
hotkeys_counter_2 = Driver()
h01 = HotkeyDataHolder()
h02 = HotkeyDataHolder()
h03 = HotkeyDataHolder()
h11 = HotkeyDataHolder()
h12 = HotkeyDataHolder()
h13 = HotkeyDataHolder()
# ------------------------------------------------------------------------------
def callback_up1(pressed):
if pressed:
return hotkeys_counter_1.increment()
def callback_down1(pressed):
if pressed:
return hotkeys_counter_1.decrement()
def callback_custom1(*args):
hotkeys_counter_1.do_custom(S.obs_data_get_int(args[2], "counter_1"))
return True
def callback_reset1(pressed):
if pressed:
return hotkeys_counter_1.reset()
def callback_up2(pressed):
if pressed:
return hotkeys_counter_2.increment()
def callback_down2(pressed):
if pressed:
return hotkeys_counter_2.decrement()
def callback_reset2(pressed):
if pressed:
return hotkeys_counter_2.reset()
def callback_custom2(*args):
hotkeys_counter_2.do_custom(S.obs_data_get_int(args[2], "counter_2"))
return True
def script_description():
return "COUNTER 2"
def script_update(settings):
hotkeys_counter_1.source_name = S.obs_data_get_string(settings, "source1")
hotkeys_counter_1.counter_text = S.obs_data_get_string(settings, "counter_text1")
hotkeys_counter_2.source_name = S.obs_data_get_string(settings, "source2")
hotkeys_counter_2.counter_text = S.obs_data_get_string(settings, "counter_text2")
def script_properties():
props = S.obs_properties_create()
S.obs_properties_add_text(
props, "counter_text1", "[1]Set counter text", S.OBS_TEXT_DEFAULT
)
p = S.obs_properties_add_int(
props, "counter_1", "Set custom value", -999999, 999999, 1
)
S.obs_property_set_modified_callback(p, callback_custom1)
p1 = S.obs_properties_add_list(
props,
"source1",
"[1]Text Source",
S.OBS_COMBO_TYPE_EDITABLE,
S.OBS_COMBO_FORMAT_STRING,
)
S.obs_properties_add_text(
props, "counter_text2", "[2]Set counter text", S.OBS_TEXT_DEFAULT
)
p = S.obs_properties_add_int(
props, "counter_2", "Set custom value", -999999, 999999, 1
)
S.obs_property_set_modified_callback(p, callback_custom2)
p2 = S.obs_properties_add_list(
props,
"source2",
"[2]Text Source",
S.OBS_COMBO_TYPE_EDITABLE,
S.OBS_COMBO_FORMAT_STRING,
)
sources = S.obs_enum_sources()
if sources is not None:
for source in sources:
source_id = S.obs_source_get_unversioned_id(source)
if source_id == "text_gdiplus" or source_id == "text_ft2_source":
name = S.obs_source_get_name(source)
S.obs_property_list_add_string(p1, name, name)
S.obs_property_list_add_string(p2, name, name)
S.source_list_release(sources)
return props
def script_load(settings):
hotkeys_counter_1.counter = S.obs_data_get_int(settings, "counter1")
hotkeys_counter_2.counter = S.obs_data_get_int(settings, "counter2")
h01.htk_copy = Hotkey(callback_up1, settings, "count_up1")
h02.htk_copy = Hotkey(callback_down1, settings, "count_down1")
h03.htk_copy = Hotkey(callback_reset1, settings, "reset1")
h11.htk_copy = Hotkey(callback_up2, settings, "count_up2")
h12.htk_copy = Hotkey(callback_down2, settings, "count_down2")
h13.htk_copy = Hotkey(callback_reset2, settings, "reset2")
def script_save(settings):
S.obs_data_set_int(settings, "counter1", hotkeys_counter_1.counter)
S.obs_data_set_int(settings, "counter2", hotkeys_counter_2.counter)
for h in [h01, h02, h03, h11, h12, h13]:
h.htk_copy.save_hotkey()
description = """
<h2>Version : {__version__}</h2>
<a href="https://github.com/upgradeQ/Obscounter"> Webpage </a>
<h3 style="color:orange">Authors</h3>
<a href="https://github.com/upgradeQ"> upgradeQ </a> <br>
""".format(
**locals()
)
def script_description():
print(description, "Released under MIT license")
return description