Repository navigation
Expand file tree
/
Copy pathMorphic-AnimatedStar.pck.st
More file actions
196 lines (154 loc) · 5.85 KB
/
Copy pathMorphic-AnimatedStar.pck.st
File metadata and controls
196 lines (154 loc) · 5.85 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
'From Cuis7.5 [latest update: #7683] on 3 November 2025 at 5:20:01 pm'!
'Description Subclass of Sample01Star with color and step-based animation.'!
!provides: 'Morphic-AnimatedStar' 1.0.3!
!requires: 'Cuis-Base' '7.9.8257' '7.*.*'!
SystemOrganization addCategory: #'Morphic-AnimatedStar'!
!classDefinition: #AnimatedStar category: #'Morphic-AnimatedStar'!
Sample01Star subclass: #AnimatedStar
instanceVariableNames: 'color bounceCount isTerminating myDelta'
classVariableNames: ''
poolDictionaries: ''
category: 'Morphic-AnimatedStar'!
!classDefinition: 'AnimatedStar class' category: #'Morphic-AnimatedStar'!
AnimatedStar class
instanceVariableNames: ''!
!AnimatedStar commentStamp: '<historical>' prior: 0!
I make colored 5 point stars which bounce off walls to change color.
After maxCount bounces, my bubble pops (circular morph + self delete),
Usage:
AnimatedStar new openInWorld startStepping.
!
!AnimatedStar methodsFor: 'initialization' stamp: 'KenD 5/31/2025 12:40:41'!
initialize
super initialize.
color := Color random.
myDelta := 20@30. "point for vector"
bounceCount := 0.
isTerminating := false.
! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/31/2025 13:19:29'!
color: aColor
color := aColor! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/31/2025 12:49:26'!
maxBounce
^ 10 "bounce off wall N times"! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/31/2025 13:11:33'!
myDelta: aPoint
myDelta := aPoint ! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/31/2025 13:22:09'!
numLiveInstances
"Answer number of live instances of me.
GC is fast enough for this usage."
Smalltalk garbageCollect. "Remove dead instances"
^ self class allInstances size
! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/29/2025 12:34:56'!
radius
"Since a PlacedMorph has no extent, selecting
am AnimatedStar morph shows my scale.
I mostly fit within a circle of radius 100.
You can change my scale, but then must
adjust my radius value as well."
^100! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/29/2025 11:56:17'!
stepTime
^ 60 "millisecondsToNextStep"! !
!AnimatedStar methodsFor: 'accessing' stamp: 'KenD 5/29/2025 11:50:17'!
strokeWidth
^ 12! !
!AnimatedStar methodsFor: 'drawing' stamp: 'KenD 5/31/2025 12:43:47'!
drawCircle: aCanvas
aCanvas
strokeWidth: self strokeWidth
color: color
fillColor: (color alpha: 0.3) do: [
aCanvas circleCenter: 0@0 radius: self radius
]! !
!AnimatedStar methodsFor: 'drawing' stamp: 'KenD 5/31/2025 12:44:58'!
drawOn: aCanvas
isTerminating
ifTrue: [ self drawCircle: aCanvas ]
ifFalse: [ self drawStarOn: aCanvas ]! !
!AnimatedStar methodsFor: 'drawing' stamp: 'KenD 5/31/2025 12:42:40'!
drawStarOn: aCanvas
"A 5 point star -- with local color"
aCanvas strokeWidth: self strokeWidth color: color do: [
aCanvas
moveTo: `(Point rho: 100 theta: 90 degreesToRadians)`;
lineTo: `(Point rho: 100 theta: (360/5*2+90) degreesToRadians)`;
lineTo: `(Point rho: 100 theta: (360/5*4+90) degreesToRadians)`;
lineTo: `(Point rho: 100 theta: (360/5*6+90) degreesToRadians)`;
lineTo: `(Point rho: 100 theta: (360/5*8+90) degreesToRadians)`;
lineTo: `(Point rho: 100 theta: 90 degreesToRadians)`.
].! !
!AnimatedStar methodsFor: 'updating' stamp: 'KenD 5/31/2025 13:20:29'!
animateAStep
"Continue to move in direction or bounce off edge of containing owner"
| ownerExtent hitWall otherDelta |
hitWall := false.
self morphPosition: self morphPosition + myDelta.
ownerExtent := self owner morphExtent.
(self morphPosition x <= self radius) "Hit left wall"
ifTrue: [
hitWall := true.
self morphPosition: self radius @ (self morphPosition y).
myDelta := myDelta x negated @ myDelta y.
otherDelta := myDelta x @ myDelta y negated.
].
(self morphPosition y <= self radius) "Hit top wall"
ifTrue: [
hitWall := true.
self morphPosition: (self morphPosition x) @ self radius.
myDelta := myDelta x @ myDelta y negated.
otherDelta := myDelta x negated @ myDelta y.
].
((self morphPosition x + self radius) >= ownerExtent x) "Hit right wall"
ifTrue: [
hitWall := true.
self morphPosition: (ownerExtent x - self radius)
@ (self morphPosition y).
myDelta := myDelta x negated @ myDelta y.
otherDelta := myDelta x @ myDelta y negated.
].
((self morphPosition y + self radius) >= ownerExtent y) "Hit bottom wall"
ifTrue: [
hitWall := true.
self morphPosition: (self morphPosition x)
@ (ownerExtent y - self radius).
myDelta := myDelta x @ myDelta y negated.
otherDelta := myDelta x negated @ myDelta y.
].
hitWall ifTrue: [
color := Color random.
bounceCount := 1 + bounceCount . "How many times I hit a wall"
"clone self with different direction"
(self numLiveInstances < 10) "Don't spawn exponentially"
ifTrue: [ | myClone |
myClone := self copy.
myClone myDelta: otherDelta;
color: Color random;
openInWorld;
startStepping.
]
].
! !
!AnimatedStar methodsFor: 'updating' stamp: 'KenD 5/31/2025 12:47:54'!
step
"If lifetime over, stop stepping and delete self, else animate a step"
(bounceCount < self maxBounce)
ifTrue: [ self animateAStep ]
ifFalse: [ self terminate ]! !
!AnimatedStar methodsFor: 'updating' stamp: 'KenD 5/31/2025 12:50:53'!
terminate
isTerminating := true. "Visual 'bang'"
self stopStepping; redrawNeeded.
[(Delay forMilliseconds: 300) wait.
self delete] fork.! !
!AnimatedStar methodsFor: 'menus' stamp: 'KenD 5/31/2025 12:28:59'!
addCustomMenuItems: aCustomMenu hand: aHandMorph
"Add morph-specific items to the given menu which was invoked by the given hand. This method provides is invoked both from the halo-menu and from the control-menu regimes."
super addCustomMenuItems: aCustomMenu hand: aHandMorph.
aCustomMenu
"addLine;"
add: 'start stepping' action: #startStepping;
add: 'stop stepping' action: #stopStepping! !