1
1
import obspython as S # studio
2
2
from contextlib import contextmanager , ExitStack
3
- from types import SimpleNamespace as dot
4
- from pynput .mouse import Controller # python -m pip install pynput
3
+ from itertools import cycle
4
+ from pynput .mouse import (
5
+ Controller ,
6
+ ) # There mighbt be an import error try run this command: python -m pip install pynput
5
7
6
- __version__ = "2.1.1 "
8
+ __version__ = "2.2.0 "
7
9
c = Controller ()
8
10
get_position = lambda : c .position
9
11
@@ -88,49 +90,52 @@ def send_mouse_move_to_browser(
88
90
S .obs_source_send_mouse_move (source , event , False ) # do not leave
89
91
90
92
91
- G = dot ()
93
+ def apply_scale (x , y , width , height ):
94
+ width = round (width * x )
95
+ height = round (height * y )
96
+ return width , height
92
97
93
- G .LMB = G .RMB = G .MOUSE_HOOKED = False
94
- # Not yet implemented functionality for mouse up/down events
95
98
99
+ def lerp (minVal , maxVal , k ):
100
+ val = minVal + ((maxVal - minVal ) * k )
101
+ return val
96
102
97
- def HTK_1_CB (pressed ):
98
- G .LMB = pressed
99
103
104
+ class Hotkey :
105
+ def __init__ (self , callback , obs_settings , _id ):
106
+ self .obs_data = obs_settings
107
+ self .hotkey_id = S .OBS_INVALID_HOTKEY_ID
108
+ self .hotkey_saved_key = None
109
+ self .callback = callback
110
+ self ._id = _id
100
111
101
- def HTK_2_CB (pressed ):
102
- G .RMB = pressed
112
+ self .load_hotkey ()
113
+ self .register_hotkey ()
114
+ self .save_hotkey ()
103
115
116
+ def register_hotkey (self ):
117
+ description = "Htk " + str (self ._id )
118
+ self .hotkey_id = S .obs_hotkey_register_frontend (
119
+ "htk_id" + str (self ._id ), description , self .callback
120
+ )
121
+ S .obs_hotkey_load (self .hotkey_id , self .hotkey_saved_key )
104
122
105
- def hook_mouse_buttons ():
106
- if G .MOUSE_HOOKED :
107
- raise RuntimeError ("already hooked mouse" )
108
- key_1 = '{"htk_1_mouse": [ { "key": "OBS_KEY_MOUSE1" } ], '
109
- key_2 = '"htk_2_mouse": [ { "key": "OBS_KEY_MOUSE2" } ]}'
110
- json_s = key_1 + key_2
111
- default_hotkeys = [
112
- dot (id = "htk_1_mouse" , des = "LMB state" , callback = HTK_1_CB ),
113
- dot (id = "htk_2_mouse" , des = "RMB state" , callback = HTK_2_CB ),
114
- ]
115
- settings = S .obs_data_create_from_json (json_s )
116
- for k in default_hotkeys :
117
- a = S .obs_data_get_array (settings , k .id )
118
- h = S .obs_hotkey_register_frontend (k .id , k .des , k .callback )
119
- S .obs_hotkey_load (h , a )
120
- S .obs_data_array_release (a )
121
- S .obs_data_release (settings )
122
- G .MOUSE_HOOKED = True
123
+ def load_hotkey (self ):
124
+ self .hotkey_saved_key = S .obs_data_get_array (
125
+ self .obs_data , "htk_id" + str (self ._id )
126
+ )
127
+ S .obs_data_array_release (self .hotkey_saved_key )
123
128
129
+ def save_hotkey (self ):
130
+ self .hotkey_saved_key = S .obs_hotkey_save (self .hotkey_id )
131
+ S .obs_data_set_array (
132
+ self .obs_data , "htk_id" + str (self ._id ), self .hotkey_saved_key
133
+ )
134
+ S .obs_data_array_release (self .hotkey_saved_key )
124
135
125
- def apply_scale (x , y , width , height ):
126
- width = round (width * x )
127
- height = round (height * y )
128
- return width , height
129
136
130
-
131
- def lerp (minVal , maxVal , k ):
132
- val = minVal + ((maxVal - minVal ) * k )
133
- return val
137
+ class HotkeyDataHolder :
138
+ htk_copy = None # this attribute will hold instance of Hotkey
134
139
135
140
136
141
class CursorAsSource :
@@ -228,8 +233,9 @@ def ticker(self): # it is not a thread because obs might not close properly
228
233
S .remove_current_callback ()
229
234
230
235
236
+ HOTKEY_DATA_HOLDER = HotkeyDataHolder ()
231
237
PY_CURSOR = CursorAsSource ()
232
- hook_mouse_buttons ( )
238
+ HOTKEY_TOGGLE_ITERATOR = cycle ([ True , False ] )
233
239
############### ############### ###############
234
240
235
241
@@ -245,6 +251,25 @@ def start_pressed(props, prop):
245
251
PY_CURSOR .flag = False # to keep only one timer callback
246
252
247
253
254
+ def callback_on_off (pressed ):
255
+ if pressed :
256
+ a = b = "undefined"
257
+ if next (HOTKEY_TOGGLE_ITERATOR ):
258
+ start_pressed (a , b )
259
+ else :
260
+ stop_pressed (a , b )
261
+
262
+
263
+ def script_load (settings ):
264
+ HOTKEY_DATA_HOLDER .htk_copy = Hotkey (
265
+ callback_on_off , settings , "On/Off cursor skin"
266
+ )
267
+
268
+
269
+ def script_save (settings ):
270
+ HOTKEY_DATA_HOLDER .htk_copy .save_hotkey ()
271
+
272
+
248
273
def react_property (props , prop , settings ):
249
274
p = S .obs_properties_get (props , "target" )
250
275
p2 = S .obs_properties_get (props , "browser" )
@@ -347,9 +372,11 @@ def script_properties():
347
372
<h3 style="color:orange">Authors</h3>
348
373
<a href="https://github.com/upgradeQ"> upgradeQ </a> <br>
349
374
<a href="https://github.com/3_4_700"> 34700 </a>
350
- """ .format (** locals ())
375
+ """ .format (
376
+ ** locals ()
377
+ )
351
378
352
379
353
380
def script_description ():
354
- print (description ,"Released under MIT license" )
381
+ print (description , "Released under MIT license" )
355
382
return description
0 commit comments