-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallbackArray.au3
245 lines (169 loc) · 7.95 KB
/
CallbackArray.au3
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.16.1
Author(s): Zvend
Discord(s): zvend
Script Function:
_CallbackArray_Init($nSize) -> CallbackArray
_CallbackArray_Add(ByRef $aArray, Const $nIndex, Const $sCallback) -> Bool
_CallbackArray_Remove(ByRef $aArray, Const $nIndex, Const $sCallbackToRemove) -> Bool
_CallbackArray_GetSize(Const ByRef $aArray) -> UInt32
_CallbackArray_Get(Const ByRef $aArray, Const $nIndex) -> Array[Count, "Callback1", ..., "CallbackN"]
_CallbackArray_IsCallbackArray(Const ByRef $aArray) -> Bool
_CallbackArray_IsIndexValid(Const ByRef $aArray, Const $nIndex, Const $bSkipCheck = False) -> Bool
Description:
Callback Arrays are 1D arrays storing sub arrays of strings. They are meant to be ID controlled.
Those are meant for a wider callback structure than only one simple array.
#ce ----------------------------------------------------------------------------
#cs - Guide --------------------------------------------------------------------
Example: You make an event system with 3 events.
Global Enum _
$EVENT_MOUSE_BTN_DOWN, _
$EVENT_MOUSE_BTN_UP , _
$EVENT_MOUSE_MOVE , _
$EVENT_COUNT
So you initialize your array like:
Global $g_aMyCallbacks = _CallbackArray_Init($EVENT_COUNT) // Note that resizing is not supported
Now you can add (multiple) function callbacks to each event:
_CallbackArray_Add($g_aMyCallbacks, $EVENT_MOUSE_BTN_DOWN, "OnMouseButtonDown1")
_CallbackArray_Add($g_aMyCallbacks, $EVENT_MOUSE_BTN_DOWN, "OnMouseButtonDown2")
_CallbackArray_Add($g_aMyCallbacks, $EVENT_MOUSE_BTN_DOWN, "OnMouseButtonDown3")
_CallbackArray_Add($g_aMyCallbacks, $EVENT_MOUSE_BTN_UP , "OnMouseButtonUp1")
_CallbackArray_Add($g_aMyCallbacks, $EVENT_MOUSE_BTN_UP , "OnMouseButtonUp2")
_CallbackArray_Add($g_aMyCallbacks, $EVENT_MOUSE_MOVE , "OnMouseMove")
Your CallbackArray would look like this now:
Global $g_aMyCallbacks =
[ ;~ [Count, Name_1, Name_2, ..., Name_N]
"CallbackArray",
[3, "OnMouseButtonDown", "OnMouseButtonDown", "OnMouseButtonDown"],
[2, "OnMouseButtonUp", "OnMouseButtonUp"],
[1, "OnMouseMove"]
]
But this would cause an error in autoit since the sub arrays are not of the same size.
They all would need to be a subarray of size 4.
But I use a workaround for it, so you dont have to worry about the structure of the array.
Just make sure to get the callbacks over the _CallbackArray_Get() function.
Example:
Func Broadcast_MouseButtonDown(Const $nButtonId)
Local $aCallbacks = _CallbackArray_Get($g_aMyCallbacks, $EVENT_MOUSE_BTN_DOWN)
If @error Then Return 0
Local $nCallbackCount = $aCallbacks[0]
If $nCallbackCount = 0 Then Return 0
For $i = 1 To $nCallbackCount
Call($aCallbacks[$i], $nButtonId)
Next
Return 1
EndFunc
And now you can call all callbacks for $EVENT_MOUSE_BTN_DOWN using this broadcast function.
This array supports n-doubles - So you can add the same callback multiple time and it would
be called multiple times. Could not think of a usecase why this is useful but it didnt hurt
to support that feature.
Removing a specific callback can be done like this:
_CallbackArray_Remove($g_aMyCallbacks, $EVENT_MOUSE_BTN_DOWN, "OnMouseButtonDown1")
Note that if you have added the callback more than once -> all of them will be removed!
#ce ----------------------------------------------------------------------------
#include-once
#include ".\Integer.au3"
#include ".\Function.au3"
Global Enum _
$CALLBACKARRAY_ERR_NONE , _
$CALLBACKARRAY_ERR_BAD_SIZE , _
$CALLBACKARRAY_ERR_INVALID , _
$CALLBACKARRAY_ERR_INVALID_INDEX , _
$CALLBACKARRAY_ERR_INVALID_CALLBACK, _
$__CALLBACKARRAY_ERR_COUNT
Global Enum _
$__CALLBACKARRAY_IDENTIFIER, _
$__CALLBACKARRAY_PARAMS
Func _CallbackArray_Init($nSize) ;-> CallbackArray
If Not IsInt($nSize) Or $nSize < 1 Then
Return SetError($CALLBACKARRAY_ERR_BAD_SIZE, 0, Null)
EndIf
;~ using a temp array here cause ReDim only works if the given var is already
;~ declared as an array. Neat trick to get rid of it.
Local $aTemp[$nSize + $__CALLBACKARRAY_PARAMS]
$aTemp[$__CALLBACKARRAY_IDENTIFIER] = "CallbackArray"
Local $aEmptyArray = [0]
For $i = $__CALLBACKARRAY_PARAMS To $nSize + $__CALLBACKARRAY_PARAMS - 1
$aTemp[$i] = $aEmptyArray
Next
Return $aTemp
EndFunc
Func _CallbackArray_Add(ByRef $aArray, Const $nIndex, Const $sCallback) ;-> Bool
Local $sFunction = _Function_Validate($sCallback)
If @error Then
Return SetError($CALLBACKARRAY_ERR_INVALID_CALLBACK, 0, 0)
EndIf
If Not _CallbackArray_IsIndexValid($aArray, $nIndex) Then
Return SetError(@error, 0, 0)
EndIf
Local $aCurrentCallbacks = $aArray[$nIndex + $__CALLBACKARRAY_PARAMS]
$aCurrentCallbacks[0] += 1
ReDim $aCurrentCallbacks[$aCurrentCallbacks[0] + 1]
$aCurrentCallbacks[$aCurrentCallbacks[0]] = $sFunction
$aArray[$nIndex + $__CALLBACKARRAY_PARAMS] = $aCurrentCallbacks
Return 1
EndFunc
Func _CallbackArray_Remove(ByRef $aArray, Const $nIndex, Const $sCallbackToRemove) ;-> Bool
Local $sFunction = _Function_Validate($sCallbackToRemove)
If @error Then
Return SetError($CALLBACKARRAY_ERR_INVALID_CALLBACK, 0, 0)
EndIf
If Not _CallbackArray_IsCallbackArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
Local $nSize = UBound($aArray)
If $nSize <= 0 Then
Return 1
EndIf
If Not _CallbackArray_IsIndexValid($aArray, $nIndex) Then
Return SetError(@error, 0, 0)
EndIf
Local $aCurrentCallbacks = $aArray[$nIndex + $__CALLBACKARRAY_PARAMS]
Local $i = 0
Local $j = 0
Local $aNewCallbacks[UBound($aCurrentCallbacks)]
While $i < $aCurrentCallbacks[0]
$i += 1
If $aCurrentCallbacks[$i] == $sFunction Then
ContinueLoop
EndIf
$j += 1
$aNewCallbacks[$j] = $aCurrentCallbacks[$i]
WEnd
ReDim $aNewCallbacks[$j + 1]
$aNewCallbacks[0] = $j
$aArray[$nIndex + $__CALLBACKARRAY_PARAMS] = $aNewCallbacks
Return 1
EndFunc
Func _CallbackArray_GetSize(Const ByRef $aArray) ;-> UInt32
If Not _CallbackArray_IsCallbackArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
Return UBound($aArray) - $__CALLBACKARRAY_PARAMS
EndFunc
Func _CallbackArray_Get(Const ByRef $aArray, Const $nIndex) ;-> Array[Count, "Callback1", ..., "CallbackN"]
Static Local $aEmptyArray = [0]
If Not _CallbackArray_IsIndexValid($aArray, $nIndex) Then
Return SetError(@error, 0, $aEmptyArray)
EndIf
Return $aArray[$nIndex + $__CALLBACKARRAY_PARAMS]
EndFunc
Func _CallbackArray_IsCallbackArray(Const ByRef $aArray) ;-> Bool
If Not IsArray($aArray) Then
Return SetError($CALLBACKARRAY_ERR_INVALID, 0, 0)
EndIf
If UBound($aArray) < $__CALLBACKARRAY_PARAMS Then
Return SetError($CALLBACKARRAY_ERR_INVALID, 0, 0)
EndIf
Return $aArray[$__CALLBACKARRAY_IDENTIFIER] == "CallbackArray"
EndFunc
Func _CallbackArray_IsIndexValid(Const ByRef $aArray, Const $nIndex, Const $bSkipCheck = False) ;-> Bool
If Not $bSkipCheck And Not _CallbackArray_IsCallbackArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
Local $nSize = UBound($aArray) - $__CALLBACKARRAY_PARAMS
If Not _Integer_IsInRange($nIndex, 0, $nSize - 1) Then
Return SetError($CALLBACKARRAY_ERR_INVALID_INDEX, 0, 0)
EndIf
Return 1
EndFunc