-
Notifications
You must be signed in to change notification settings - Fork 1
/
glob.r
1773 lines (1526 loc) · 40.8 KB
/
glob.r
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
REBOL [
; -- Core Header attributes --
title: "Glob | Graphic layered objects"
file: %glob.r
version: 1.0.1
date: 2013-9-10
author: "Maxim Olivier-Adlhoch"
purpose: {Core Canvas library to build interactive graphics, structured visual documents and more. Has layers, uses lazy dataflow, allows arbitrary shaped input regions.}
web: http://www.revault.org/modules/glob.rmrk
source-encoding: "Windows-1252"
note: {slim Library Manager is Required to use this module.}
; -- slim - Library Manager --
slim-name: 'glob
slim-version: 1.2.1
slim-prefix: none
slim-update: http://www.revault.org/downloads/modules/glob.r
; -- Licensing details --
copyright: "Copyright © 2013 Maxim Olivier-Adlhoch"
license-type: "Apache License v2.0"
license: {Copyright © 2013 Maxim Olivier-Adlhoch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.}
;- / history
history: {
v0.0.1 - 27-Dec-2006/19:24:59 (MOA)
-after a lot of prototyping with combining AGG, view and liquid...
-create core !glob
v0.0.2 - 16-Feb-2007/20:44:02 (MOA)
v0.0.2 - 15-Apr-2007/21:36:50 (Unknown)
-add layers to glob engine
v0.0.3 - 15-Apr-2007/23:47:41 (Unknown)
v0.0.4 - 29-Apr-2007/7:55:51 (Unknown)
v0.0.5 - 30-Apr-2007/16:56:30 (Unknown)
v0.0.6 - 30-Apr-2007/22:07:39 (Unknown)
-fixed glob/unlink
-fully implemented all destruction of all glob nodes.
v0.0.7 - 11-May-2007/0:46:55 (Unknown)
v0.0.9 - 11-May-2007/2:30:21 (Unknown)
v0.0.11 - 15-Jul-2007/0:58:44 (Unknown)
v0.1.0 - 23/04/2009/1:31AM (MOA)
-added reflection system.
v0.2.0 - 23/04/2009/1:31AM (MOA)
-refurbishing feel system.
-building generic glob event handler
-integrated ON-OVER()
v0.2.1 - 30/04/2009/10:02PM (MOA)
-several on-event are integrated within handler
-added !word input type.
v0.2.2 - 05/05/2009/1:30PM (MOA)
-added /reset to !glob's LINK() method, solves a discrepancy with newer liquid versions, which added /reset.
v1.0.0 - 02/02/2010 (MOA)
-just a milestone release since this is now a publicly distributed file, I'm signing it off as a v1 reference release.
v1.0.1 - 2013-9-10 (MOA)
-license change to Apache v2}
;- \ history
;- / documentation
documentation: {
globs are a very low-level layer of the GLASS framework.
They have been used in many applications and are very robust so far.
The nice thing about globs is that you can link them up directly so that you could create your own
custom globs and display them in a GLASS display without requiring any special code.
The only complication will be the mapping of the bitmask plane to Glass marbles within
the stream engine. Some analysis and development will be required in order to use
globs directly within a GLASS display and not create errors when handling the input
related those globs.
we might simply add handler support to globs via an intermediate layer. or make a glob
marble which is only usable within a special canvas frame or viewport.
globs as such are independent from GLASS but they where developped with GLASS in mind.
you may use globs directly for your own graphic needs.
}
;- \ documentation
]
;--------------------------------------
; unit testing setup
;--------------------------------------
;
; test-enter-slim 'glob
;
;--------------------------------------
slim/register [
; these are declared, in order for slim to set them within the glob lib.
; otherwise they pollute the global context.
liquify: none
content: none
!plug: none
fill: none
link: none
retrieve-plug: none
;core: slim/open 'core none
;- LIBS
;gl: slim/open 'glayout 0.4.16
lqd: slim/open/expose 'liquid none [ !plug liquify fill content link retrieve-plug ]
;glue: slim/open 'glue none
;- FUNCTIONS
; ;--------------------
; ;- --init--()
; ;--------------------
; --init--: func [
; ""
; ][
; vin/tags ["Glob/--init--()"]
; vout/tags [tt]
; true
; ]
;--------------------
;- to-color()
;--------------------
to-color: func [
""
value [integer!]
][
; the 'REVERSE is for litle endian system...
0.0.0.0 + to-tuple next reverse third make struct! [int [int]] reduce [value ]
;<FIXME>
; return big endian value on big-endian system
]
;--------------------
;- to-sid()
;--------------------
to-sid: func [
""
value [tuple! none!]
][
if tuple? value [
if 4 <= length? value [value: make tuple! reduce [value/1 value/2 value/3]]
to-integer to-binary value
]
]
;--------------------
;- vlength()
;--------------------
vlength: func [v] [v: v * v square-root v/x + v/y]
;--------------------
;- point()
;--------------------
point: func [
{compute the specified point on the line}
start [pair!]
end [pair!]
at [decimal! integer!]
/bounded {truncates at if it ends up being larger than vector, note does not support negative at}
/local vector points
] [
; solve "degenerate case" first
if equal? start end [return start]
vector: end - start
if integer? at [
; convert AT to decimal
at: at / (to-integer vlength vector)
]
if all[
bounded
at > 1.0
][
return end
]
; compute from end (instead of start)
if negative? at [
at: 1 + at
]
start + to-pair reduce [to-integer vector/x * at to-integer vector/y * at]
]
;--------------------
;- segment()
;--------------------
segment: func [
{compute the specified segment from the line}
start [pair!]
end [pair!]
from [decimal! integer! none!]
to [decimal! integer! none!]
/local vector points length
][
; solve "degenerate case" first
if equal? start end [return reduce [start start]]
vector: end - start
length: vlength vector
;----------
; FROM
if integer? from [
; convert from to decimal
from: from / length
]
if none? from [
from: 0
]
if negative? from [
; this case lets us define a segment using the end as the reference
from: 1 + from ; from is negative, so this actually substracts from length
]
;----------
; TO
if integer? to [
; convert to, to decimal
to: to / length
]
if none? to [
to: 1
]
if negative? to [
; this case lets us define a segment using the end as the reference
to: 1 + to ; to is negative, so this actually substracts from length
]
reduce [start + to-pair reduce [to-integer vector/x * from to-integer vector/y * from] start + to-pair reduce [to-integer vector/x * to to-integer vector/y * to] ]
]
sizer-face: make face [ size: 1000x200 edge: none para: none font: none]
;?? sizer-face
;ask "..."
;--------------------
;- sizetext()
;--------------------
sizetext: func [
""
text
font
][
sizer-face/font: font
sizer-face/text: text
;?? sizer-face
size-text sizer-face
]
;-
;------------------------
;- intypes
;------------------------
intypes: context [
;linked-container?: true
;- !any
!any: make lqd/!plug [
valve: make valve [
type: 'glob-intype-any
linked-container?: false
datatype: none
;--------------------
;- purify()
;--------------------
purify: func [
""
plug [object!]
][
vin/tags ["glob/intypes["plug/sid"]/purify()"] [purify]
if datatype [
unless (type? plug/liquid) = datatype [
;print "GLOB TYPE MISMATCH"
;probe plug/liquid
;probe datatype
plug/liquid: switch/default to-word plug/valve/datatype [
string! [
;print "STRING!"
either none? plug/liquid [
""
][
mold plug/liquid
]
]
][
;probe plug/valve/datatype
any [
attempt [make datatype plug/liquid]
attempt [make datatype none]
]
]
]
]
vout/tags [purify]
false
]
;--------------------
;- cleanse()
;--------------------
cleanse: func [
""
plug
][
vin/tags ["!intypes/[" plug/sid "]cleanse()"] [cleanse]
;new-pipe plug
if datatype [
fill plug switch to-word datatype [
pair! [
0x0
]
tuple! [
random 255.255.255
]
integer! [
1
]
block! [
copy []
]
string! [
copy ""
]
]
]
vout/tags [cleanse]
]
]
]
;- !pair
!pair: make !any [
valve: make valve [
type: 'glob-intype-pair
datatype: pair!
purify: func [
plug
][
;print ["pair: " plug/liquid]
false
]
]
]
;- !color
!color: make !any [
valve: make valve [
type: 'glob-intype-color
datatype: tuple!
]
]
;- !integer
!integer: make !any [
valve: make valve [
type: 'glob-intype-integer
datatype: integer!
]
]
;- !decimal
!decimal: make !any [
valve: make valve [
type: 'glob-intype-decimal
datatype: decimal!
]
]
;- !bool
!bool: make !any [
valve: make valve [
type: 'glob-intype-bool
datatype: logic!
]
]
;- !block
!block: make !any [
valve: make valve [
type: 'glob-intype-block
datatype: block!
]
]
;- !state
!state: make !any [
valve: make valve [
type: 'glob-intype-state
datatype: logic!
]
]
;- !time
!time: make !any [
valve: make valve [
type: 'glob-intype-time
datatype: time!
]
]
;- !date
!date: make !any [
valve: make valve [
type: 'glob-intype-date
datatype: date!
]
]
;- !string
!string: make !any [
valve: make valve [
type: 'glob-intype-string
datatype: string!
]
]
;- !word
!word: make !any [
valve: make valve [
type: 'glob-intype-word
datatype: word!
]
]
]
;-
;------------------------
;- !GEL
;------------------------
!gel: make lqd/!plug [
draw-spec: none
plug-sid: none
; points to the glob this gel is included in.
glob: none
valve: make valve [
type: '!gel
category: '!gel
;--------------------
;- cleanse()
;--------------------
cleanse: func [
""
gel [object!]
][
vin/tags ["!gel[" gel/sid "]/cleanse()"] [cleanse]
gel/liquid: copy []
;plug/input/offset/valve/fill plug/input/offset 0x0
vout/tags [cleanse]
]
;--------------------
;- destroy()
;--------------------
destroy: func [
""
plug
][
vin/tags ["!gel/destroy()"] [destroy]
plug/draw-spec: none ; this is a shared value with the glob. do not clear it.
plug/glob: none
!plug/valve/destroy plug
vout/tags [destroy]
]
;--------------------
;- process()
;--------------------
process: func [
""
gel [object!]
data [block!]
/local plug tmp value blk offset pos size clr
][
vin/tags ["!gel[" gel/sid "]/process()"] [process]
clear gel/liquid
append gel/liquid compose/deep bind/copy gel/draw-spec 'data
vout/tags [process]
]
; ;--------------------
; ;- blocking-state()
; ;--------------------
; blocking-state: func [
; "This allows you to define states in which the node will not propagate dirtyness. This can be usefull to prevent useless computing when some input is not actually contributing to output in some specific states. A possible future api extension being researched "
; plug [object!]
; ][
; vin/tags ["blocking-state()"] [ !glob blocking-state]
;
;
; vout/tags [!glob blocking-state]
;
; ;return true if we are in a blocking state. This might even be a manually locked eventually.
; ; defaults to false, unless we really have a reason to block.
; false
; ]
;blocking-state: none ; just a faster alternative nop... but keep the spec above.
; ;--------------------
; ;- instigate()
; ;--------------------
; instigate: func [
; ""
; plug
; ][
; print "INSTIGATING!"
; probe !plug/valve/instigate plug
; ]
; ;---------------------
; ;- propagate
; ;---------------------
; ; cause observers to become dirty
; ;---------------------
; propagate: func [
; plug [object!]
; ][
; vin/tags ["glob/" type "[" plug/sid "]/propagate" ] [ !glob propagate]
; either blocking-state plug [
; print "BLOCKING!"
; ][
; !plug/valve/propagate plug
; ]
; vout/tags [!glob propagate]
; ]
]
]
;-
;------------------------
;;- !STACK
;------------------------
!stack: make lqd/!plug [
linked-container?: true ; the container data is the !stack parameters if they are not linked
; store compiled layers
layers: none
valve: make valve [
type: '!stack
category: '!stack
;--------------------
;- cleanse()
;--------------------
cleanse: func [
""
plug [object!]
][
vin/tags ["!glob[" plug/sid "]/cleanse()"] [cleanse]
plug/liquid: copy []
plug/layers: copy []
;plug/input/offset/valve/fill plug/input/offset 0x0
vout/tags [cleanse]
]
;--------------------
;- destroy()
;--------------------
destroy: func [
""
plug
][
vin/tags ["!stack/destroy()"] [destroy]
clear head plug/layers
plug/layers: none
!plug/valve/destroy plug
vout/tags [destroy]
]
;--------------------
;- get-items()
;--------------------
get-items: func [
""
blk
lbl
/local rblk
][
if rblk: find blk lbl [
rblk: copy/part next rblk any [
find next rblk word!
tail rblk
]
]
rblk
]
;--------------------
;- process()
;--------------------
process: func [
""
plug [object!]
data
/local blk layer
][
vin/tags ["!stack[" plug/sid "]/process()"] [process]
clear head plug/liquid
foreach layer data [
append plug/liquid layer
]
vout/tags [process]
]
]
]
;-
;------------------------
;- !GLOB
;------------------------
!glob: make lqd/!plug [
;----
;- input:
input: none
;----
;- linked-container?:
; <TO DO> set this to false by default... should drastically improve load performance for large networks...
linked-container?: true ; this contains a single graphic definition which is then output in two blocks
;----
;- clr-bak:
clr-bak: red
;----
;- drag-origin:
drag-origin: 0x0
;----
;- layers:
; this is a list of !gel nodes, one for each layer
; stack will link their own layer nodes to these nodes.
; the glob will no long connect itself to the inputs, but rather will connect its layers to them.
layers: none
;----
;- draw-spec:
; use the reflect() in the valve to set layers to use as outputs.
draw-spec: none
;----
;- reflection:
; intermediate node which pipes dirty messages and compiles draw blocks from internal layers.
reflection: none
;----
;- VALVE:
valve: make valve [
type: '!glob
category: '!glob
;
;- input-spec:
input-spec: none
;- gel-spec:
gel-spec: none
;-----------------
;- reflect()
;-----------------
; setup the glob so that its content is set to reflect the contents of
; selected layers. this is sort of a hack, since an internal plug is created
; which will propagate dirty messages to the glob.
;
; the glob, when asked for its content, will then actually refer to the reflection plug
; and set itself to that data.
;
; this setup allows a glob to autonomously output its content, without the need for a complex
; external viewport-like node.
;-----------------
reflect: func [
glob [object!] "plug to reflect"
layers [block! integer! none!] "a list or single layer to reflect, none removes the reflection"
/local glob* layer
][
vin ["!glob[" glob/sid "]/reflect()"]
either layers: all [layers compose [(layers)]] [
vprint "Setting up reflection"
unless object? glob/reflection [
vprint "Creating new reflection"
glob*: glob
glob/reflection: liquify/with !plug [
;stainless?: true
glob: glob*
valve: make valve [
type: '!reflection
;-----------------
;- propagate?()
;-----------------
; we are using this function outside of its intended use, as a callback.
; since we return true, its still valid.
;
; this function will propagate the dirtyness of glob's reflected gels or stacks
; to the glob itself!
;-----------------
propagate?: func [
plug [object!]
][
vin ["!glob/reflection/[" plug/sid "]/propagate?()"]
vprobe plug/valve/type
plug/glob/valve/dirty plug/glob
vout
true
]
;-----------------
;- process()
;-----------------
process: func [
plug
data
][
plug/liquid: copy []
vin ["!glob/reflection/[" plug/sid "]/process()"]
forall data [
append plug/liquid first data
]
vout
]
]
]
]
glob/reflection/valve/unlink glob/reflection
foreach layer layers [
vprint ["linking to layer: " layer]
either layer: pick glob/layers layer [
glob/reflection/valve/link glob/reflection layer
][
vprint "This layer isn't set in glob"
]
]
][
; layers set to none, deconstruct reflection if one is currently set.
; <TO DO>
]
vout
]
;--------------------
;- process()
;--------------------
process: func [
""
plug [object!]
data [block!]
][
vin/tags ["!glob[" plug/sid "]/process()"] [process]
; clear first plug/liquid
; clear second plug/liquid
;
; append first plug/liquid compose/deep bind/copy specs/1 'data
; append second plug/liquid compose/deep bind/copy specs/2 'data
;probe first plug/liquid
;probe second plug/liquid
;append/only plug/liquid head insert (copy/deep first plug/liquid) compose [pen (to-color plug/sid)]
;plug/liquid: data
if plug/reflection [
plug/liquid: plug/reflection/valve/content plug/reflection
]
vout/tags [process]
]
;--------------------
;- pre-allocate-layers()
; this creates a number of stack nodes ready for use. so observers can be
; connected before the glob connects to other globs.
;--------------------
pre-allocate-layers: func [
glob [object!]
count [integer!]
;/local stack
][
if none? glob/layers [
glob/layers: copy []
]
loop count [
append glob/layers liquify !stack
]
]
;--------------------
;- add-layers()
;--------------------
add-layers: func [
""
glob [object!]
/local words spec paren ext val
][
vin/tags ["!glob[" glob/sid "]/add-layers()"] [add-layers]
if gel-spec [
parse gel-spec [
some [
;(words: copy [])
copy val some word! (words: val if words/1 = 'none [words: none])
|
copy val paren! (ext: bind/copy to-block val/1 'process )
|
copy val block! (spec: val add-layer/with glob words spec/1 ext ext: none)
]
]
]
vout/tags [add-layers]
]
;--------------------
;- add-layer()
;--------------------
add-layer: func [
""
glob [object!]
words [block! none!]
spec [block!]
/with extension
/local gel word input
][
vin/tags ["!glob[" glob/sid "]/add-layer()"] [add-layer]
vprint ["adding layer: " words]
;probe extension
;probe words
extension: any [extension []]
gel: liquify/with !gel extension
gel/glob: glob
;probe get in gel 'blocking-state
append glob/layers gel
;lqd/von
; link appropriate layers to it
if words [
foreach word words [
unless input: select glob/input word [
to-error rejoin ["GLOB/add-layer(): glob has no input named '" word]
]
gel/valve/link/label gel input word
]
]
;link our plug sid, this never changes, so don't make a plug for this
gel/plug-sid: glob/sid
;lqd/voff
; share appropriate effects blk
gel/draw-spec: spec
vout/tags [add-layer]
]
;--------------------
;- add-inputs()
;--------------------
add-inputs: func [
""
glob
/local item type spec input default
][
vin/tags ["!glob[" glob/sid "]/add-inputs()"] [add-inputs]
;probe glob/valve/input-spec
if glob/valve/input-spec [
; new flexible dialect
until [
;probe ">"
; input spec is a value of the valve, so its bound to it
item: pick glob/valve/input-spec 1
switch type?/word item [
word! [
either #"!" = first to-string item [
type: item
; reset default, to prevent data type errors
; note: if you don't change types between inputs, all will
; use the same default (this is a feature)
default: none
][
; add input if we have everything we need
if all [input type][
add-input/preset glob input type default
]
; name of following node
input: item ; name of the input
;---
; note: even if the last thing of a input block is a word (and
; not allocated here, the end of the loop will create it)
]
]
; block! [
; probe item
; ]
paren! [
; parens allows the user to supply programmable default values
;probe item
default: do item
]
]
glob/valve/input-spec: skip glob/valve/input-spec 1
tail? glob/valve/input-spec
]
glob/valve/input-spec: head glob/valve/input-spec
; at the end of the loop, we generally have one input left to allocate... (unless missing data)
if all [input type][
add-input/preset glob input type default
]
]
vout/tags [add-inputs]
]
;--------------------
;- add-input()
;--------------------