-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrackerTask.xml
More file actions
2839 lines (2358 loc) · 92 KB
/
TrackerTask.xml
File metadata and controls
2839 lines (2358 loc) · 92 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?rokuml version="1.0" encoding="utf-8" ?>
<!--********** Copyright 2022 Roku Corp. All Rights Reserved. **********-->
<component name="TrackerTask" extends="Task">
<interface>
<field id="event" type="assocarray"/>
<field id="isExternallyExposed" type="boolean" value="true"/>
<function name="UIThread_init"/>
<function name="UIThread_showSelectorView"/>
<function name="UIThread_hideSelectorView"/>
<function name="UIThread_selectNode"/>
<function name="UIThread_updateNode"/>
<function name="UIThread_getNodeData"/>
<function name="UIThread_getItemList"/>
<function name="UIThread_getNodeTree"/>
<function name="UIThread_getNodeById"/>
<function name="UIThread_getNodeByName"/>
<function name="UIThread_addChild"/>
<function name="UIThread_removeChild"/>
<function name="UIThread_moveChild"/>
<function name="UIThread_setField"/>
<function name="UIThread_removeField"/>
<function name="UIThread_setFocus"/>
<function name="UIThread_selectFocusedNode"/>
<function name="UIThread_setBoundingRect"/>
<function name="UIThread_getRegistrySections"/>
<function name="UIThread_clearRegistry"/>
<function name="UIThread_addRegistrySection"/>
<function name="UIThread_removeRegistrySection"/>
<function name="UIThread_addRegistryField"/>
<function name="UIThread_removeRegistryField"/>
<function name="UIThread_editRegistryField"/>
<function name="UIThread_log"/>
<function name="UIThread_setLogVerbosity"/>
<function name="UIThread_setLogFormat"/>
<function name="UIThread_setRulerLine"/>
<function name="UIThread_setRulerLines"/>
<function name="UIThread_removeRulerLine"/>
<function name="UIThread_removeAllRulerLines"/>
</interface>
<script type = "text/brightscript" >
<![CDATA[
' RALE_Config - RALE config singleton class
' @return {object} - RALE config properties
Function RALE_Config() as Object
if m.appConfig = Invalid then m.appConfig = RALE_InitConfig()
return m.appConfig
End Function
' RALE_InitConfig - RALE config init function. Defines all config properties
' @return {object} - RALE config properties
Function RALE_InitConfig() as Object
this = {
bufferSize: 1024,
minSocketPort: 49152,
maxSocketPort: 65535,
screensaverResetTimeout: 30000,
commandTimeout: 10000,
defaultLogLevel: 3,
logFormat: "[RALE][%level%] - %message%"
}
return this
End Function
' CreateGuides - Selector (live) view class constructor
' @return {object} - selector (live) view
Function CreateGuides() as Object
this = {}
node = CreateObject("roSGNode", "Group")
node.addFields({"isExternallyExposed": true})
this.node = node
this.childMap = {}
this.setLine = Guides_SetLine
this.setRulerLines = Guides_SetRulerLines
this.removeLine = Guides_RemoveLine
this.removeAllLines = Guides_RemoveAllLines
this.resolution = m.top.getScene().getField("currentDesignResolution")
return this
End Function
Sub Guides_SetLine(id, position, coords)
if (m.childMap[id] = invalid) then
Guides_AddLine(id, position, coords, m.node, m.childMap)
else
Guides_UpdatePosition(id, coords, m.childMap)
end if
end Sub
Sub Guides_SetRulerLines(rulerLines)
For Each line In rulerLines.Items()
Guides_AddLine(line.key, line.value.position, line.value.coords, m.node, m.childMap)
End For
end Sub
Sub Guides_AddLine(id, position, coords, node, childMap) as Object
line = CreateObject("roSGNode", "Rectangle")
line.setField("id", id)
line.setField("color", "#00F8FF")
line.addFields({"position": position})
Guides_SetLineCoords(line, coords)
node.appendChild(line)
childMap[id] = line
end Sub
Sub Guides_UpdatePosition(id, coords, childMap) as Object
line = childMap[id]
Guides_SetLineCoords(line, coords)
end Sub
Sub Guides_SetLineCoords(node, coords) as Integer
if node.position = "horizontal" then
node.setField("translation", [coords.x,0])
node.setField("width", 1)
node.setField("height", m.resolution.height)
end if
if node.position = "vertical" then
node.setField("translation", [0,coords.y])
node.setField("width", m.resolution.width)
node.setField("height", 1)
end if
end Sub
Sub Guides_RemoveLine(id)
line = m.childMap[id]
m.node.removeChild(line)
m.childMap[id] = invalid
end Sub
Sub Guides_RemoveAllLines()
childrenList = m.node.getChildren(-1,0)
m.node.removeChildren(childrenList)
m.childMap = {}
end Sub
' RALE_Logger - Constructor of Logger class
' @param {integer} level - log verbosity level (0-4)
' @param {string} format - log message format ("[MY_CUSTOM_PREFIX]%message%")
' @return {object} - logger instance
function RALE_Logger(level = -1 as Integer, format = "%message%" as String) as Object
this = {}
this.verbosityList = ["OFF", "ERROR", "WARN", "INFO", "DEBUG"]
this.verbosityMap = {
off : 0
error : 1
warning : 2
info : 3
debug : 4
}
this.formatMethods = {
date: "getDate",
time: "getTime",
level: "getLevel",
message: "getMessage"
}
this.dt = createObject("roDateTime")
this.setVerbosity = RALE_Logger__SetVerbosityLevel
this.setFormat = RALE_Logger__SetFormat
this.getDate = RALE_Logger__GetDate
this.getTime = RALE_Logger__GetTime
this.getLevel = RALE_Logger__GetLevel
this.getMessage = RALE_Logger__GetMessage
this.debug = RALE_Logger__DebugLog
this.info = RALE_Logger__InfoLog
this.warning = RALE_Logger__WarningLog
this.error = RALE_Logger__ErrorLog
this.logMessage = RALE_Logger__LogMessage
this.setVerbosity(level)
this.setFormat(format)
return this
end function
' RALE_Logger__SetVerbosityLevel - change logger verbosity level
' @param {integer} level - log verbosity level (0-4)
sub RALE_Logger__SetVerbosityLevel(level = 4 as Integer)
if level > m.verbosityMap.debug then
m.verbosityLevel = m.verbosityMap.debug
else if level < m.verbosityMap.off then
m.verbosityLevel = m.verbosityMap.off
else
m.verbosityLevel = level
end if
end sub
' RALE_Logger__SetFormat - Change logger message format
' @param {string} format - log message format ("[MY_CUSTOM_PREFIX]%message%")
' Available format variables:
' %message% - message with which the log function is called (at the end by default)
' %date% - current date in format "[year]-[month]-[day]"
' %time% - current time in format "[hours]:[minutes]:[seconds].[miliseconds]"
' %level% - verbosity level ("ERROR", "WARN", "INFO" or "DEBUG")
sub RALE_Logger__SetFormat(format = "%message%" as String)
if Instr(1, format, "%message%") = 0 then
format = format + "%message%"
end if
m.formatList = format.split("%")
end sub
'==========================================
' Wrappers over various levels of logging
'==========================================
' RALE_Logger__DebugLog - Log message into console with DEBUG verbosity level
' @param {string} message - log message
sub RALE_Logger__DebugLog(message = "")
m.logMessage(message, m.verbosityMap.debug)
end sub
' RALE_Logger__InfoLog - Log message into console with INFO verbosity level
' @param {string} message - log message
sub RALE_Logger__InfoLog(message = "")
m.logMessage(message, m.verbosityMap.info)
end sub
' RALE_Logger__WarningLog - Log message into console with WARNING verbosity level
' @param {string} message - log message
sub RALE_Logger__WarningLog(message = "")
m.logMessage(message, m.verbosityMap.warning)
end sub
' RALE_Logger__ErrorLog - Log message into console with ERROR verbosity level
' @param {string} message - log message
sub RALE_Logger__ErrorLog(message)
m.logMessage(message, m.verbosityMap.error)
end sub
'==========================================
' Main log function
'==========================================
' RALE_Logger__LogMessage - Log message into console by message format if logLevel is more than current verbosityLevel
' @param {string} message - log message
' @param {integer} logLevel - log verbosity level (0-4)
sub RALE_Logger__LogMessage(message as String, logLevel as Integer)
if logLevel > 0 and m.verbosityLevel >= logLevel then
logMessage = ""
for each item in m.formatList
formatMethod = m.formatMethods[item]
if formatMethod = Invalid then
logMessage = logMessage + item
else
logMessage = logMessage + m[formatMethod](message, logLevel)
end If
end for
print logMessage
end if
end sub
'==========================================
' Utitlity functions
'==========================================
' RALE_Logger__GetTime - Returns current time
' @param {string} message - log message
' @return {string} - current time in format: "[hours]:[minutes]:[seconds].[miliseconds]"
function RALE_Logger__GetTime(message as String, level as Integer)
m.dt.Mark()
h = RALE_Logger__getZeroNumber(m.dt.getHours())
n = RALE_Logger__getZeroNumber(m.dt.getMinutes())
s = RALE_Logger__getZeroNumber(m.dt.getSeconds())
ms = str(m.dt.getMilliseconds()).trim()
if len(ms) = 1
ms = ms + "0"
else
ms = left(ms, 2)
end if
return h + ":" + n + ":" + s + "." + ms
end function
' RALE_Logger__GetDate - Returns current date
' @param {string} message - log message
' @return {string} - current date in format: "[year]-[month]-[day]"
function RALE_Logger__GetDate(message as String, level as Integer)
m.dt.Mark()
y = str(m.dt.getYear()).trim()
mm = RALE_Logger__getZeroNumber(m.dt.getMonth())
d = RALE_Logger__getZeroNumber(m.dt.getDayOfMonth())
return y + "-" + mm + "-" + d
end function
' RALE_Logger__GetLevel - Returns current verbosity level
' @param {string} message - log message
' @return {integer} - verbosity level
function RALE_Logger__GetLevel(message as String, level as Integer)
return m.verbosityList[level]
end function
' RALE_Logger__GetMessage - Returns log message (needed for formating log message)
' @param {string} message - log message
' @return {string} - log message
function RALE_Logger__GetMessage(message as String, level as Integer)
return message
end function
' RALE_Logger__getZeroNumber - Convert number to format "0[number]" if it has less then 2 digits
' @param {number} n - number for converting
' @param {number} to_length - the necessary count of digits
' @return {string} - converted number format
function RALE_Logger__getZeroNumber(n, to_length=2)
n = str(n).trim()
if len(n) < to_length
n = String(to_length - 1, "0") + n
end if
return n
end function
' CreateSelectorView - Selector (live) view class constructor
' @return {object} - selector (live) view
Function CreateSelectorView() as Object
view = CreateObject("roSGNode", "Group")
m.resolution = m.top.getScene().getField("currentDesignResolution")
m.resWidth = m.resolution.width
m.resHeight = m.resolution.height
children = [
{subtype : "Rectangle", fields : {id : "l"}}
{subtype : "Rectangle", fields : {id : "r"}}
{subtype : "Rectangle", fields : {id : "b"}}
{subtype : "Rectangle", fields : {id : "t"}}
{subtype : "Rectangle", fields : {id : "horiz"}}
{subtype : "Rectangle", fields : {id : "vert"}}
{subtype : "Rectangle", fields : {id : "leftLine"}}
{subtype : "Rectangle", fields : {id : "topLine"}}
{subtype : "Rectangle", fields : {id : "rightLine"}}
{subtype : "Rectangle", fields : {id : "bottomLine"}}
{subtype : "Label", fields : {id : "leftCords"}}
{subtype : "Label", fields : {id : "topCords"}}
{subtype : "Label", fields : {id : "rightCords"}}
{subtype : "Label", fields : {id : "bottomCords"}}
{subtype : "Rectangle", fields : {id : "fill", color : "#ffffff44"}}
{subtype : "Timer", fields : {id : "timer", duration : 1 / 60, repeat : true}}
]
viewObject = NodeUtils_AddChildrenToNode(view, children)
childrenMap = {}
for each child in viewObject.children
node = child.node
childrenMap[node.id] = node
end for
interface = [
{id:"width", type:"float", value : "1280", onChange : "SelectorView_UpdateView"}
{id:"height", type:"float", value : "40", onChange : "SelectorView_UpdateView"}
{id:"border", type:"float", value : "2", onChange : "SelectorView_UpdateView"}
{id:"oldBoundingRect", type:"assocarray" }
{id:"boundingRect", type:"assocarray", onChange : "SelectorView_UpdateViewFromBRect"}
{id:"color", type:"color", value : "#FFFFFF", onChange : "SelectorView_UpdateColors"}
{id:"attachedView", type:"node", onChange : "SelectorView_AttachToView"}
{id:"isExternallyExposed", type:"boolean", value : "true"}
{id:"childrenMap", type:"assocarray", value : childrenMap}
]
NodeUtils_AddInterfaceToNode(view, interface)
timer = view.childrenMap.timer
timer.observeField("fire","SelectorView_UpdateViewPosition")
timer.control = "start"
return view
End Function
' NodeUtils_AddInterfaceToNode - Adds interface to node
' @param {object} node - node
' @param {object} interface - interface
' @return {boolean} - is interface was added
Function NodeUtils_AddInterfaceToNode(node, interface as Object) as Boolean
if node = invalid OR interface = invalid then return false
for each field in interface
NodeUtils_AddNodeInterfaceField(node, field)
end for
return true
End Function
' NodeUtils_AddNodeInterfaceField - Adds interface field to node
' @param {object} node - node
' @param {object} fieldConfig - field config
' @return {boolean} - is interface was added
Function NodeUtils_AddNodeInterfaceField(node, fieldConfig as Object) as Boolean
if node = invalid OR fieldConfig = invalid then return false
if fieldConfig.id = invalid then return false
if fieldConfig.type = invalid then return false
if fieldConfig.alwaysNotify = invalid then fieldConfig.alwaysNotify = true
if node.hasField(fieldConfig.id) then return false
node.addField(fieldConfig.id, fieldConfig.type, fieldConfig.alwaysNotify)
if not node.hasField(fieldConfig.id) then return false
if fieldConfig.value <> invalid then
node[fieldConfig.id] = fieldConfig.value
end if
if fieldConfig.onChange <> invalid then
node.observeField(fieldConfig.id, fieldConfig.onChange)
end if
return true
End Function
' NodeUtils_AddChildrenToNode - Adds children to node
' @param {object} node - node
' @param {object} childrenConfig - children config
' @return {object} - view object
Function NodeUtils_AddChildrenToNode(node, childrenConfig as Object) as Object
if node = invalid then return invalid
viewObject = {node : node, children : []}
if childrenConfig <> invalid then
for each childConfig in childrenConfig
childObject = NodeUtils_AddChildToNode(node, childConfig)
viewObject.children.push(childObject)
end for
end if
return viewObject
End Function
' NodeUtils_AddChildToNode - Adds child to node
' @param {object} node - node
' @param {object} childConfig - child config
' @return {object} - view object
Function NodeUtils_AddChildToNode(node, childConfig) as Object
if node = invalid or childConfig = invalid then return invalid
if childConfig.subtype = invalid then return invalid
view = node.CreateChild(childConfig.subtype)
if view = invalid then return invalid
if childConfig.fields <> invalid then view.setFields(childConfig.fields)
return NodeUtils_AddChildrenToNode(view, childConfig.children)
End Function
' NumberToFixed - formats a number using fixed-point notation
' @param {float, double} number - formated number
' @param {integer} digits - the number of digits to appear after the decimal point
' @return {string} - a string representing the given number using fixed-point notation
Function NumberToFixed(number, digits) as String
str = number.toStr()
arr = str.split(".")
if arr.count() > 1 and digits > 0 then
return arr[0] + "." + Left(arr[1], digits)
end if
return arr[0]
End Function
' SelectorView_UpdateView - width, height and border change event handler. Updates Selector (live) view
' @param {object} event - event data
Sub SelectorView_UpdateView(event)
node = event.getRoSGNode()
border = node.border
width = node.width
height = node.height
x = node.boundingRect.x
y = node.boundingRect.y
rightPos = x + width + border*2
bottomPos = y + height + border*2
l = node.childrenMap.l
l.width = border
l.height = height + border*2
l.translation = [-border, -border]
r = node.childrenMap.r
r.width = border
r.height = height + border*2
r.translation = [width, -border]
t = node.childrenMap.t
t.width = width + border*2
t.height = border
t.translation = [-border, -border]
b = node.childrenMap.b
b.width = width + border*2
b.height = border
b.translation = [-border, height]
horiz = node.childrenMap.horiz
horiz.width = width
horiz.height = border
horiz.translation = [0, (height - border) / 2]
vert = node.childrenMap.vert
vert.width = border
vert.height = height
vert.translation = [(width - border) / 2, 0]
leftLine = node.childrenMap.leftLine
leftLine.width = x
leftLine.height = border
leftLine.translation = [-x, (height - border) / 2]
topLine = node.childrenMap.topLine
topLine.width = border
topLine.height = y
topLine.translation = [(width - border) / 2, -y]
rightLine = node.childrenMap.rightLine
rightLine.width = m.resWidth - x + 10
rightLine.height = border
rightLine.translation = [width + border, (height - border) / 2]
bottomLine = node.childrenMap.bottomLine
bottomLine.width = border
bottomLine.height = m.resHeight - y + 10
bottomLine.translation = [(width - border) / 2, height + border]
leftCords = node.childrenMap.leftCords
leftCords.font.size = 20
leftCords.color = "#FF0000"
leftCords.text = NumberToFixed(x, 0)
leftCords.translation = [-x/2 - 10, (height - border) / 2 - 20]
topCords = node.childrenMap.topCords
topCords.font.size = 20
topCords.color = "#FF0000"
topCords.text = NumberToFixed(y, 0)
topCords.translation = [(width - border) / 2 + 10, -y/2 - 10]
rightCords = node.childrenMap.rightCords
rightCords.font.size = 20
rightCords.color = "#FF0000"
rightCords.text = NumberToFixed(m.resWidth - x - width, 0)
rightCords.translation = [width + (m.resWidth - rightPos)/2 - 5, (height - border) / 2 - 20]
bottomCords = node.childrenMap.bottomCords
bottomCords.font.size = 20
bottomCords.color = "#FF0000"
bottomCords.text = NumberToFixed(m.resHeight - y - height, 0)
bottomCords.translation = [(width - border) / 2 + 10, height + (m.resHeight - bottomPos)/2 - 5]
fill = node.childrenMap.fill
fill.width = width
fill.height = height
End Sub
' SelectorView_UpdateColors - color change event handler. Updates Selector (live) view color
' @param {object} event - event data
Sub SelectorView_UpdateColors(event)
node = event.getRoSGNode()
for each view in node.getchildren(-1,0)
if view.isSubtype("Rectangle") and view.id <> "fill" then view.color = node.color
end for
End Sub
' SelectorView_UpdateViewFromBRect - boundingRect change event handler.
' Updates Selector (live) view translation, width and height
' @param {object} event - event data
Sub SelectorView_UpdateViewFromBRect(event)
node = event.getRoSGNode()
boundingRect = node.boundingRect
if boundingRect <> Invalid then
node.setFields({
translation : [boundingRect.x, boundingRect.y]
width : boundingRect.width
height : boundingRect.height
})
end if
End Sub
' isBoundingRectChanged - checks is bounding rect was changed
' @param {object} br - node bounding rect
' @param {object} node - node
' @return {boolean} - true if node bounding rect was changed
Sub isBoundingRectChanged(br, node) as Boolean
o = node.oldBoundingRect
if o = Invalid then
node.oldBoundingRect = br
return true
end if
if o.x = br.x and o.y = br.y and o.width = br.width and o.height = br.height then
return false
else
node.oldBoundingRect = br
return true
end if
End Sub
' SelectorView_UpdateViewPosition - interval event handler. Updates selector (live) view position
' @param {object} event - node bounding rect
Sub SelectorView_UpdateViewPosition(event)
timer = event.getRoSGNode()
node = timer.getparent()
if node.attachedView <> Invalid then
boundingRect = node.attachedView.sceneboundingRect()
else
boundingRect = {x: -100, y: -100, width: 0, height: 0}
end if
if isBoundingRectChanged(boundingRect, node) then
node.boundingRect = boundingRect
end if
End Sub
' RALE_SocketConnection - RALE Socket connection class constructor
Function RALE_SocketConnection() as Object
this = {}
this.startConnection = SocketConnection_StartConnection
return this
End Function
' socketConnection - Starts socket connection. Keep alive while connection won't be closed
' @param {object} addr - roSocketAddress
' @param {object} messagePort - roMessagePort
' @param {number} port - socket connection port
' @param {array} buffer - buffer for socket packets
' @param {object} handlersMap - map that contains all RALE commands handlers
' @param {object} config - configuration constants
' @return {string} - connection status
Sub SocketConnection_StartConnection(addr, messagePort, port, buffer, handlersMap, config) as String
connections = {}
appManager = createObject("roAppManager")
screensaverTimer = CreateObject("roTimespan")
screensaverTimer.Mark()
addr.setPort(port)
tcpListen = CreateObject("roStreamSocket")
tcpListen.setMessagePort(messagePort)
tcpListen.setAddress(addr)
tcpListen.notifyReadable(true)
tcpListen.listen(4)
wasConnected = false
RALE_InfoLog("Start socket server on port " + FW_AsString(port))
while True
if not wasConnected then
event = wait(3000, messagePort)
if event = Invalid then
tcpListen.close()
RALE_WarningLog("No 'init' command received. Socket connection closed")
return "connection timeout"
end if
else
event = wait(config.commandTimeout, messagePort)
end if
' Resets the screensaver timer
if screensaverTimer.TotalMilliseconds() > config.screensaverResetTimeout then
screensaverTimer.Mark()
appManager.UpdateLastKeyPressTime()
end if
' ? type(event)
if type(event) = "roSocketEvent"
wasConnected = true
changedID = event.getSocketID()
if changedID = tcpListen.getID() and tcpListen.isReadable()
' New
newConnection = tcpListen.accept()
if newConnection = Invalid
RALE_WarningLog("Connection accept failed")
else
RALE_InfoLog("Accepted new connection " + FW_AsString(newConnection.getID()))
newConnection.notifyReadable(true)
newConnection.setMessagePort(messagePort)
connections[Stri(newConnection.getID())] = newConnection
end if
else
' Activity on an open connection
connection = connections[Stri(changedID)]
closed = False
if connection.isReadable()
str = RALE_ReceiveLargeString(connection)
if str.len() > 0
timer = CreateObject("roTimespan")
timer.Mark()
commandList = SocketConnection_ParseData(str)
for i = 0 to commandList.Count() - 1
SocketConnection_HandleCommand(commandList[i], handlersMap, connection, timer)
end for
end if
end if
if closed or not connection.eOK()
connection.close()
connections.delete(Stri(changedID))
exit while
end if
end if
end if
end while
tcpListen.close()
for each id in connections
connections[id].close()
end for
RALE_InfoLog("Socket connection closed")
return "connection closed"
End Sub
' SocketConnection_HandleCommand - Handle socket connection command
' @param {string} command - one socket command
' @param {object} handlersMap - map that contains all RALE commands handlers
' @param {object} connection - socket connection
' @param {object} timer - socket connection timer
Sub SocketConnection_HandleCommand(command, handlersMap, connection, timer) as Object
request = ParseJson(command)
buffer = CreateObject("roByteArray")
buffer[1024] = 0
if request <> Invalid then
response = {}
if request["command"] <> Invalid then
handler = handlersMap[request["command"]]
if handler <> Invalid then
RALE_DebugLog("Handling command: " + request["command"])
RALE_DebugLog("Received arguments: " + FW_AsString(request["args"]))
response = callCommand(handler, request["args"])
else
RALE_ErrorLog("No such command: " + request["command"])
response = getError("No such command")
end if
end if
id = request.uuid
idLen = FW_AsString(Len(id))
json = FormatJson(response)
res = "[start][uuid:" + idLen + "]" + id + json + "[end]"
RALE_SendLargeString(res, connection)
RALE_DebugLog("Command handled in: " + FW_AsString(timer.TotalMilliseconds()) + "ms")
end if
End Sub
' SocketConnection_ParseData - parse from string to array of commands
' @param {string} request - request from js. Includes [start] and [end]
' @return {array} - array of commands
Sub SocketConnection_ParseData(request) as Object
commandList = SocketConnection_SplitOnStart(request)
if commandList[0].len() = 0 then
commandList.Delete(0)
end if
for i = 0 to commandList.Count() - 1
commandList[i] = SocketConnection_RemoveEnd(commandList[i])
end for
return commandList
end Sub
' SocketConnection_SplitOnStart - splits the request using the separator "[start]"
' @param {string} request - request from js. Includes [start] and [end]
' @return {array} - an array of the split token strings (not including the "[start]").
Sub SocketConnection_SplitOnStart(request) as Object
return request.Split("[start]")
end Sub
' SocketConnection_RemoveEnd - remove "[end]" from the string
' @param {string} command - socket command
' @return {string} - command without [start] and [end]
Sub SocketConnection_RemoveEnd(command) as Object
return command.Replace("[end]", "")
end Sub
' getCommandMap - Defines map that contains all RALE commands handlers
' @return {object} - handlers map
Sub getCommandMap() as Object
return {
"init" : "UIThread_init",
' Config commands
"showSelectorView" : "UIThread_showSelectorView",
"hideSelectorView" : "UIThread_hideSelectorView",
' Node commands
"selectNode" : "UIThread_selectNode",
"updateNode" : "UIThread_updateNode",
"getNodeData" : "UIThread_getNodeData",
' Tree View commands
"getItemList" : "UIThread_getItemList",
"getNodeTree" : "UIThread_getNodeTree",
"getNodeById" : "UIThread_getNodeById",
"getNodeByName" : "UIThread_getNodeByName",
' Children commands
"addChild" : "UIThread_addChild",
"removeChild" : "UIThread_removeChild",
"moveChild" : "UIThread_moveChild",
' Field commands
"setField" : "UIThread_setField",
"removeField" : "UIThread_removeField",
' Focus commands
"setFocus" : "UIThread_setFocus",
"selectFocusedNode" : "UIThread_selectFocusedNode",
' Bounding Rect commands
"setBoundingRect" : "UIThread_setBoundingRect",
' Registry commands
"getRegistrySections" : "UIThread_getRegistrySections",
"clearRegistry" : "UIThread_clearRegistry",
"addRegistrySection" : "UIThread_addRegistrySection",
"removeRegistrySection" : "UIThread_removeRegistrySection",
"addRegistryField" : "UIThread_addRegistryField",
"removeRegistryField" : "UIThread_removeRegistryField",
"editRegistryField" : "UIThread_editRegistryField",
' Logger commands
"setLogVerbosity" : "UIThread_setLogVerbosity",
"setLogFormat" : "UIThread_setLogFormat",
"log" : "UIThread_log"
' Ruler lines commands
"setRulerLine" : "UIThread_setRulerLine",
"setRulerLines" : "UIThread_setRulerLines",
"removeRulerLine" : "UIThread_removeRulerLine",
"removeAllRulerLines" : "UIThread_removeAllRulerLines",
}
End Sub
' callCommand - Call RALE command functuin from UI thread
' @param {string} command - command name
' @param {object} args - command arguments
' @return {object} - response of RALE command
Sub callCommand(command, args) as Object
return m.top.callFunc(command, args)
End Sub
' UIThread_init - RALE "init" command
' @param {object} args - command arguments
' @return {object} - response of command. Contains RALE version and session id
Sub UIThread_init(args) as Object
root = m.top.GetScene()
m.currentNode = root
m.currentPath = []
if m.selectorView = Invalid then
m.selectorView = CreateSelectorView()
m.selectorView.color = "#ff0000"
m.selectorView.opacity = 0.6
m.showSelectorView = true
root.appendChild(m.selectorView)
end if
if m.guides = Invalid then
m.guides = CreateGuides()
root.appendChild(m.guides.node)
else
UIThread_removeAllRulerLines({})
end if
if args.logVerbosity >= 0 then
UIThread_setLogVerbosity({ level: args.logVerbosity })
end if
if FW_IsString(args.logFormat) then
UIThread_setLogFormat({ format: args.logFormat })
end if
m.logger.info("RALE Initialized")
return { raleVersion: m.raleVersion, sessionid: m.sessionId }
End Sub
'=======================
' Config commands
'=======================
' UIThread_showSelectorView - RALE "showSelectorView" command handler. Shows Selector (Live) View
' @param {object} args - command arguments
Sub UIThread_showSelectorView(args) as Object
m.showSelectorView = true
setSelectorView(m.currentNode, type(m.currentNode), m.currentPath)
m.logger.debug("Selector View was disabled")
End Sub
' UIThread_hideSelectorView - RALE "hideSelectorView" command handler. Hides Selector (Live) View
' @param {object} args - command arguments
Sub UIThread_hideSelectorView(args) as Object
m.showSelectorView = false
setSelectorView(m.currentNode, type(m.currentNode), m.currentPath)
m.logger.debug("Selector View was enabled")
End Sub
'=======================
' Node commands
'=======================
' UIThread_selectNode - RALE "selectNode" command handler. Selects node by path
' @param {object} args - command arguments
' @return {object} - response of command. Contains node path and node data (id, type, fields, layout etc.)
Sub UIThread_selectNode(args) as Object
path = args["path"]
root = m.top.getScene()
node = RALE_getNodeByPath(root, path)
if node = Invalid then return getError("Invalid Path")
nodeType = type(node)
if nodeType <> "roSGNode" and nodeType = "roArray" and nodeType = "roAssociativeArray" then
return getError("Invalid Node Type")
end if
if nodeType = "roSGNode" and node.isExternallyExposed <> Invalid then
return getError("Node Is Externally Exposed")
end if
if nodeType <> "roSGNode" or not node.IsSameNode(m.currentNode) then
m.currentPath = path
m.currentNode = node
end if
setSelectorView(node, nodeType, path)
m.logger.debug("Node selected: " + FW_AsString(m.currentNode))
return {
path: m.currentPath,
node: getNodeData(m.currentNode, m.currentPath, root)
}
End Sub
' UIThread_updateNode - RALE "updateNode" command handler. Updates selected node
' @param {object} args - command arguments
' @return {object} - response of command. Contains node path and data (id, type, fields, layout etc.)
Sub UIThread_updateNode(args) as Object
return {
path: m.currentPath,
node: getNodeData(m.currentNode, m.currentPath, Invalid)
}
End Sub
' UIThread_getNodeData - RALE "getNodeData" command handler. Returns selected node data
' @param {object} args - command arguments
' @return {object} - response of command. Node data (id, type, fields, layout etc.)
Sub UIThread_getNodeData(args) as Object
path = args["path"]
if path = Invalid then
path = m.currentPath
node = m.currentNode
else
root = m.top.getScene()
node = RALE_getNodeByPath(root, path)
if node = Invalid then return getError("Invalid Path")
end if