Debian Codegraph @

godot/addons/gut/signal_watcher.gd

raw
1 # ##############################################################################
2 # The MIT License (MIT)
3 # =====================
4 #
5 # Copyright (c) 2020 Tom "Butch" Wesley
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 # ##############################################################################
26
27 # Some arbitrary string that should never show up by accident. If it does, then
28 # shame on you.
29 const ARG_NOT_SET = '_*_argument_*_is_*_not_set_*_'
30
31 # This hash holds the objects that are being watched, the signals that are being
32 # watched, and an array of arrays that contains arguments that were passed
33 # each time the signal was emitted.
34 #
35 # For example:
36 # _watched_signals => {
37 # ref1 => {
38 # 'signal1' => [[], [], []],
39 # 'signal2' => [[p1, p2]],
40 # 'signal3' => [[p1]]
41 # },
42 # ref2 => {
43 # 'some_signal' => [],
44 # 'other_signal' => [[p1, p2, p3], [p1, p2, p3], [p1, p2, p3]]
45 # }
46 # }
47 #
48 # In this sample:
49 # - signal1 on the ref1 object was emitted 3 times and each time, zero
50 # parameters were passed.
51 # - signal3 on ref1 was emitted once and passed a single parameter
52 # - some_signal on ref2 was never emitted.
53 # - other_signal on ref2 was emitted 3 times, each time with 3 parameters.
54 var _watched_signals = {}
55 var _utils = load('res://addons/gut/utils.gd').get_instance()
56
57 func _add_watched_signal(obj, name):
58 # SHORTCIRCUIT - ignore dupes
59 if(_watched_signals.has(obj) and _watched_signals[obj].has(name)):
60 return
61
62 if(!_watched_signals.has(obj)):
63 _watched_signals[obj] = {name:[]}
64 else:
65 _watched_signals[obj][name] = []
66 obj.connect(name, self, '_on_watched_signal', [obj, name])
67
68 # This handles all the signals that are watched. It supports up to 9 parameters
69 # which could be emitted by the signal and the two parameters used when it is
70 # connected via watch_signal. I chose 9 since you can only specify up to 9
71 # parameters when dynamically calling a method via call (per the Godot
72 # documentation, i.e. some_object.call('some_method', 1, 2, 3...)).
73 #
74 # Based on the documentation of emit_signal, it appears you can only pass up
75 # to 4 parameters when firing a signal. I haven't verified this, but this should
76 # future proof this some if the value ever grows.
77 func _on_watched_signal(arg1=ARG_NOT_SET, arg2=ARG_NOT_SET, arg3=ARG_NOT_SET, \
78 arg4=ARG_NOT_SET, arg5=ARG_NOT_SET, arg6=ARG_NOT_SET, \
79 arg7=ARG_NOT_SET, arg8=ARG_NOT_SET, arg9=ARG_NOT_SET, \
80 arg10=ARG_NOT_SET, arg11=ARG_NOT_SET):
81 var args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]
82
83 # strip off any unused vars.
84 var idx = args.size() -1
85 while(str(args[idx]) == ARG_NOT_SET):
86 args.remove(idx)
87 idx -= 1
88
89 # retrieve object and signal name from the array and remove them. These
90 # will always be at the end since they are added when the connect happens.
91 var signal_name = args[args.size() -1]
92 args.pop_back()
93 var object = args[args.size() -1]
94 args.pop_back()
95
96 _watched_signals[object][signal_name].append(args)
97
98 func does_object_have_signal(object, signal_name):
99 var signals = object.get_signal_list()
100 for i in range(signals.size()):
101 if(signals[i]['name'] == signal_name):
102 return true
103 return false
104
105 func watch_signals(object):
106 var signals = object.get_signal_list()
107 for i in range(signals.size()):
108 _add_watched_signal(object, signals[i]['name'])
109
110 func watch_signal(object, signal_name):
111 var did = false
112 if(does_object_have_signal(object, signal_name)):
113 _add_watched_signal(object, signal_name)
114 did = true
115 return did
116
117 func get_emit_count(object, signal_name):
118 var to_return = -1
119 if(is_watching(object, signal_name)):
120 to_return = _watched_signals[object][signal_name].size()
121 return to_return
122
123 func did_emit(object, signal_name):
124 var did = false
125 if(is_watching(object, signal_name)):
126 did = get_emit_count(object, signal_name) != 0
127 return did
128
129 func print_object_signals(object):
130 var list = object.get_signal_list()
131 for i in range(list.size()):
132 print(list[i].name, "\n ", list[i])
133
134 func get_signal_parameters(object, signal_name, index=-1):
135 var params = null
136 if(is_watching(object, signal_name)):
137 var all_params = _watched_signals[object][signal_name]
138 if(all_params.size() > 0):
139 if(index == -1):
140 index = all_params.size() -1
141 params = all_params[index]
142 return params
143
144 func is_watching_object(object):
145 return _watched_signals.has(object)
146
147 func is_watching(object, signal_name):
148 return _watched_signals.has(object) and _watched_signals[object].has(signal_name)
149
150 func clear():
151 for obj in _watched_signals:
152 if(_utils.is_not_freed(obj)):
153 for signal_name in _watched_signals[obj]:
154 obj.disconnect(signal_name, self, '_on_watched_signal')
155 _watched_signals.clear()
156
157 # Returns a list of all the signal names that were emitted by the object.
158 # If the object is not being watched then an empty list is returned.
159 func get_signals_emitted(obj):
160 var emitted = []
161 if(is_watching_object(obj)):
162 for signal_name in _watched_signals[obj]:
163 if(_watched_signals[obj][signal_name].size() > 0):
164 emitted.append(signal_name)
165
166 return emitted