Skip to content

Commit a0eae20

Browse files
committed
dispatch snippet chapter contents into different existing chapters where it make more sense, and remove it.
1 parent bb5bf07 commit a0eae20

File tree

10 files changed

+686
-665
lines changed

10 files changed

+686
-665
lines changed

Chapters/Attic/graphicsfoundation/bitmapFormDisplay.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ pict colors: map.
6262
pict initFromArray: bitmap.
6363
```
6464

65+
### transform a Form into ByteString and vice versa
66+
67+
Starting from a ByteString (self), we can have a Form using this code:
68+
69+
```st
70+
Form fromBinaryStream:
71+
self base64Decoded asByteArray readStream
72+
```
73+
74+
and starting from a Form, we can translate it into a ByteString using this code:
75+
76+
```st
77+
byteString := (ByteArray streamContents: [ :out |
78+
PNGReadWriter putForm: form onStream: out ]) base64Encoded.
79+
```
80+
81+
It seems that doing the translation ByteString -> Form -> ByteString doesn't give the same ByteString between input/output but Form -> ByteString -> Form seems to work correctly (except maybe small changes invisible to the human eye).
82+
83+
Also here we use `PNGReadWriter` but other subclasses of `ImageReadWriter` can be used depending on the format you want to save
84+
85+
try this:
86+
87+
```st
88+
String streamContents: [:s | self storeOn: s]
89+
```
90+
91+
```st
92+
OpalCompiler new
93+
source: self;
94+
evaluate
95+
```
96+
```st
97+
Object readFrom: byteString
98+
```
99+
65100
### Fun script
66101

67102
We should revise it.

Chapters/bloc/animation.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,27 @@ BlTransformAnimation new
247247
easing: BlEasing bounceOut.
248248
```
249249

250+
#### To go further with animations
251+
252+
The most common animations are translations and rotations but, whenever we translate or rotate an object, its position doesn't change even if it does visually.
253+
254+
But this doesn't mean that nothing changes, in fact the origin of the "transform axis" moves with the transformation, meaning that if we apply a horizontal translation to an element that was previously rotated for example 90 degrees, then the element will visually translate vertically like in this example.
255+
256+
```st
257+
elt := BlElement new background: Color purple.
258+
259+
t1 := (BlTransformAnimation translate: 150@0) duration: 2 seconds.
260+
t2 := (BlTransformAnimation translate: 150@0) duration: 2 seconds.
261+
rotateAnimation := (BlTransformAnimation rotate: 90) duration: 2 seconds.
262+
263+
elt addAnimation: (t1 onFinishedDo: [ elt addAnimation: (rotateAnimation onFinishedDo: [ elt addAnimation: (t2 onFinishedDo: [ elt position inspect ]) ]) ]).
264+
265+
elt openInSpace.
266+
```
267+
268+
And you will see that the position of the element hasn't changed and still is 0@0, but you can ask for `BlElement>>transformedBounds` to have the bounds of your element after its transformations if you need to exploit this visual position.
269+
270+
250271
#### Transform animation rotation pitfall
251272

252273
A transform rotation has some peculiarities its worth highlighting.
@@ -354,4 +375,71 @@ BlNumberTransition new
354375
ifFalse: [ anElement background: Color blue ] ].
355376
```
356377

378+
### Example - Explosion animation
379+
380+
We create an explosion animation by creating some BlElements and making them translate along a rotating line starting from a center point
381+
382+
Here we also add a rotation on the BlElements themselves with random color
383+
384+
```smalltalk
385+
space := BlSpace new.
386+
explosionCenter := 400 @ 300.
387+
388+
0 to: 359 by: 12 do: [ :angle |
389+
| anElement relativeFinalPosition |
390+
relativeFinalPosition := 0@400 rotateBy: angle degreesToRadians about: 0@0.
391+
392+
anElement := BlElement new
393+
background: Color random;
394+
position: explosionCenter;
395+
addAnimation: (BlTransformAnimation new
396+
duration: 1200 milliSeconds;
397+
transformDo: [ :t |
398+
t translateBy: relativeFinalPosition.
399+
t rotateBy: 180 ];
400+
onFinishedDo: [ anElement removeFromParent ];
401+
yourself);
402+
yourself.
403+
404+
space root addChild: anElement ].
405+
space show.
406+
407+
```
408+
409+
### Enqueue tasks
410+
411+
This snippet is an example of how to enqueue tasks and delay the time of the next instruction. This is also an example of a circular animation using an offset on a shadow effect
412+
413+
```smalltalk
414+
a := BlElement new
415+
position: 50 @ 100;
416+
size: 100 asPoint;
417+
background: Color green;
418+
yourself.
419+
420+
a addAnimation:
421+
(BlNumberTransition new
422+
from: 0;
423+
to: Float twoPi;
424+
duration: 5 seconds;
425+
onStepDo: [ :t |
426+
| offset |
427+
offset := (t cos * 50) @ (t sin * 50).
428+
a effect:
429+
(BlSimpleShadowEffect
430+
color: Color blue
431+
offset: offset) ];
432+
onFinishedDo: [
433+
| removeAction |
434+
removeAction :=
435+
BlDelayedTaskAction new
436+
delay: 2 seconds;
437+
action: [ a removeFromParent ];
438+
yourself.
439+
a enqueueTask: removeAction ];
440+
yourself).
441+
442+
a openInSpace
443+
```
444+
357445
### Conclusion (missing)

Chapters/bloc/blocArchitecture.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,126 @@ The phases are:
1919

2020
### Space explanation
2121

22+
BlUniverse contain multiple BlSpace.
23+
We call `BlElement openInNewSpace`
24+
This will open a new OS Windows, with our scene
25+
26+
BlSpace is your main entry point to your application. It'll connect you to
27+
OS window, manage which element needs drawing and the rendering of your scene.
28+
Rendering is done by a dedicated render class, BARenderer by default.
29+
30+
Host has their own pulse loop which are then transmitted to their BlSpace
31+
My loop follows the next rules:
32+
33+
• The #pulsePeriod duration is the minimum amount of time between two subsequent sends of #pulse.
34+
• If a pulse took more time than #pulsePeriod, then the next pulse may either send the next #pulse immediately, or do a small wait before if another process with lower priority is suspended (and may be starving).
35+
36+
The opened spaces listen the pulse to be synchronized and to update their state when it is needed.
37+
38+
39+
40+
Each BlSpace is hosted in a specific BlHost environment.
41+
All these BlHost are then accessible through BlUniverse.
42+
43+
As of this writing, it can be Morphic or an OS Window,
44+
using OSWindow and SDL2 backend.
45+
46+
47+
48+
We begin with `BlElement >> openInNewSpace`
49+
or `BlElement >> openInSpace` which are equivalent.
50+
51+
```smalltalk
52+
BlElement >> openInNewSpace
53+
"Add self to a new BlSpace and show it. Answer such space."
54+
55+
| aSpace |
56+
aSpace := BlSpace new.
57+
aSpace root addChild: self.
58+
aSpace show.
59+
^ aSpace
60+
```
61+
62+
### Space extent following its child extent
63+
64+
```smalltalk
65+
space := BlSpace new.
66+
space show.
67+
68+
rootUniqueChild := BlElement new
69+
geometry: BlEllipseGeometry new;
70+
background: Color blue;
71+
"Transfer the new extent to the space"
72+
addEventHandlerOn: BlElementExtentChangedEvent
73+
do: [ :evt | space extent: evt target extent ];
74+
yourself.
75+
space addChild: rootUniqueChild.
76+
77+
"Gradually expand element in a background process"
78+
[ 50 to: 400 by: 50 do: [ :sideLength |
79+
rootUniqueChild size: (sideLength*2) @ sideLength.
80+
500 milliSeconds wait ].
81+
space close ] fork
82+
```
83+
84+
Here, space extent is redefined whenever the extent of its child changes using a basic eventHandler
85+
86+
### Center a space
87+
88+
```smalltalk
89+
space := BlSpace new.
90+
space root background: Color lightGreen.
91+
92+
space enqueueTask:
93+
(BlTaskAction new
94+
action: [ space center ];
95+
yourself).
96+
97+
space show
98+
```
99+
100+
Spaces are by default opened with their origin on the top left or the screen, but we can change this by sending the message `BlSpace>>center`.
101+
However you might wonder why in this example we send it as a task that we enqueue, the reason behind it is that (apparently) the space needs to be rendered to be centered and executing something like the following snippet doesn't work
102+
103+
```st
104+
space show; center
105+
```
106+
107+
### Root Element is special
108+
109+
```smalltalk
110+
a := BlElement new.
111+
aSpace := BlSpace new.
112+
aSpace root addChild: a.
113+
aSpace root background: Color transparent.
114+
aSpace extent: 200 asPoint; show.
115+
116+
wheel := Color wheel: 20.
117+
[ wheel do: [ :eachColor |
118+
0.25 seconds wait.
119+
a position: (a position + 5 asPoint).
120+
a background: eachColor ] ] fork
121+
```
122+
123+
When you set Color transparent as root's background, nothing clears the space
124+
125+
```smalltalk
126+
a := BlElement new.
127+
aSpace := BlSpace new.
128+
aSpace root addChild: a.
129+
aSpace root background: Color blue.
130+
aSpace root size: 100 asPoint.
131+
aSpace extent: 150 asPoint; show.
132+
133+
wheel := Color wheel: 20.
134+
[ wheel do: [ :eachColor |
135+
0.25 seconds wait.
136+
a position: (a position + 5 asPoint).
137+
a background: eachColor ] ] fork.
138+
```
139+
140+
"When you set an exact root size, only a portion of the space might be cleared between frames"
141+
142+
[Enzo] I keep this note here but I don't see any changes personally
143+
22144
###

0 commit comments

Comments
 (0)