7058
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
6696
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libview' }"
|
|
15 |
|
7058
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
6696
|
18 |
DisplayTransform variableFloatSubclass:#TranslationTransform
|
|
19 |
instanceVariableNames:'translation'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Graphics-Transformations'
|
|
23 |
!
|
|
24 |
|
|
25 |
!TranslationTransform class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
instances of WindowingTransformation can be used to scale, translate or
|
|
44 |
generally transform other objects in 2D space.
|
|
45 |
They can also be set as the translation in a graphic context,
|
|
46 |
which will then apply this to all of its drawing operations
|
|
47 |
(see GraphicContext>>transformation:).
|
|
48 |
|
|
49 |
All 2-D objects are supposed to be able to be transformed using
|
|
50 |
instances of me. Multiple instances of me can also be combined to form a
|
|
51 |
single composite transformation.
|
|
52 |
|
|
53 |
[Instance variables:]
|
|
54 |
scale <Number> or <Point> representing a linear scaling factor.
|
|
55 |
nil is interpreted as 1@1
|
|
56 |
|
|
57 |
translation <Number> or <Point> representing a translation in 2-D.
|
|
58 |
nil is interpreted as 0@0
|
|
59 |
|
|
60 |
|
|
61 |
[author:]
|
|
62 |
Claus Gittinger
|
|
63 |
"
|
|
64 |
|
|
65 |
"Modified: 25.4.1996 / 16:53:07 / cg"
|
|
66 |
!
|
|
67 |
|
|
68 |
examples
|
|
69 |
"
|
|
70 |
example (drawing in inches):
|
|
71 |
[exBegin]
|
|
72 |
|v|
|
|
73 |
|
|
74 |
v := View new realize.
|
|
75 |
(Delay forSeconds:3) wait.
|
|
76 |
v transformation:(WindowingTransformation unit:#inch on:Display).
|
|
77 |
'now, we can think of drawing in inches ...'.
|
|
78 |
v displayLineFrom:0.5@0.5 to:1@1
|
|
79 |
[exEnd]
|
|
80 |
|
|
81 |
|
|
82 |
example (drawing in millimeters):
|
|
83 |
[exBegin]
|
|
84 |
|v|
|
|
85 |
|
|
86 |
v := View new realize.
|
|
87 |
(Delay forSeconds:3) wait.
|
|
88 |
v transformation:(WindowingTransformation unit:#mm on:Display).
|
|
89 |
'now, we can think of drawing in millimeters ...'.
|
|
90 |
v displayLineFrom:5@5 to:20@5
|
|
91 |
[exEnd]
|
|
92 |
|
|
93 |
|
|
94 |
example (drawing magnified):
|
|
95 |
[exBegin]
|
|
96 |
|v|
|
|
97 |
|
|
98 |
v := View new realize.
|
|
99 |
(Delay forSeconds:3) wait.
|
|
100 |
v transformation:(WindowingTransformation scale:2 translation:0).
|
|
101 |
'now, everything is magnfied by 2'.
|
|
102 |
v displayLineFrom:10@10 to:30@30
|
|
103 |
[exEnd]
|
|
104 |
|
|
105 |
example (drawing shrunk):
|
|
106 |
[exBegin]
|
|
107 |
|v|
|
|
108 |
|
|
109 |
v := View new realize.
|
|
110 |
(Delay forSeconds:3) wait.
|
|
111 |
v transformation:(WindowingTransformation scale:0.5 translation:0).
|
|
112 |
'now, everything is shrunk by 2'.
|
|
113 |
v displayLineFrom:10@10 to:30@30
|
|
114 |
[exEnd]
|
|
115 |
"
|
|
116 |
|
|
117 |
"Modified: 27.4.1996 / 19:45:43 / cg"
|
|
118 |
! !
|
|
119 |
|
|
120 |
!TranslationTransform class methodsFor:'instance creation'!
|
|
121 |
|
|
122 |
identity
|
|
123 |
"returns a windowing transformation with no scaling (1@1)
|
|
124 |
and no translation (0@0)."
|
|
125 |
|
|
126 |
^ self basicNew "/ scale:nil translation:nil
|
|
127 |
|
|
128 |
"
|
|
129 |
WindowingTransformation identity
|
|
130 |
"
|
|
131 |
|
|
132 |
"Modified: 30.12.1996 / 16:59:27 / cg"
|
|
133 |
!
|
|
134 |
|
|
135 |
translation:aTranslation
|
|
136 |
"returns a windowing transformation with no scaling and a translation offset of aTranslation."
|
|
137 |
|
|
138 |
^ self basicNew translation:aTranslation
|
|
139 |
|
|
140 |
"
|
|
141 |
|v|
|
|
142 |
|
|
143 |
v := View new openAndWait.
|
|
144 |
v displayLineFrom:10@10 to:30@30.
|
|
145 |
v transformation:(WindowingTransformation scale:2 translation:0).
|
|
146 |
'now, everything is magnfied by 2'.
|
|
147 |
v displayLineFrom:10@10 to:30@30.
|
|
148 |
"
|
|
149 |
"
|
|
150 |
|v|
|
|
151 |
|
|
152 |
v := View new openAndWait.
|
|
153 |
v displayLineFrom:10@10 to:30@30.
|
|
154 |
v transformation:(WindowingTransformation translation:50).
|
|
155 |
'now, everything is offset by 50 pixels'.
|
|
156 |
v displayLineFrom:10@10 to:30@30.
|
|
157 |
"
|
|
158 |
! !
|
|
159 |
|
|
160 |
!TranslationTransform methodsFor:'accessing'!
|
|
161 |
|
|
162 |
translation
|
|
163 |
"return a copy of the receiver's translation."
|
|
164 |
|
|
165 |
translation isNil ifTrue:[^ Point x:0 y:0 ].
|
|
166 |
^ translation copy
|
|
167 |
!
|
|
168 |
|
|
169 |
translation:aTranslation
|
|
170 |
"Set the receiver's translation to aTranslation, a Point or Number."
|
|
171 |
|
|
172 |
aTranslation isNil ifTrue:[
|
|
173 |
translation := aTranslation
|
|
174 |
] ifFalse:[
|
|
175 |
aTranslation = 0 ifTrue:[
|
|
176 |
translation := nil
|
|
177 |
] ifFalse:[
|
|
178 |
translation := aTranslation asPoint
|
|
179 |
]
|
|
180 |
]
|
|
181 |
|
|
182 |
"Modified: / 13.6.1998 / 14:04:15 / cg"
|
|
183 |
!
|
|
184 |
|
|
185 |
translationX
|
|
186 |
"return the receiver's x-translation."
|
|
187 |
|
|
188 |
translation isNil ifTrue:[^ 0].
|
|
189 |
^ translation x
|
|
190 |
|
|
191 |
"Created: 21.5.1996 / 21:13:10 / cg"
|
|
192 |
!
|
|
193 |
|
|
194 |
translationY
|
|
195 |
"return the receiver's x-translation."
|
|
196 |
|
|
197 |
translation isNil ifTrue:[^ 0].
|
|
198 |
^ translation y
|
|
199 |
|
|
200 |
"Created: 21.5.1996 / 21:13:21 / cg"
|
|
201 |
! !
|
|
202 |
|
|
203 |
!TranslationTransform methodsFor:'applying transform'!
|
|
204 |
|
|
205 |
applyInverseTo:anObject
|
|
206 |
"Apply the inverse of the receiver to anObject
|
|
207 |
and return the result. This can be used to map back from logical
|
|
208 |
to physical coordinates, for example."
|
|
209 |
|
|
210 |
translation isNil ifTrue:[
|
|
211 |
^ anObject
|
|
212 |
].
|
|
213 |
^ anObject translatedBy:(self inverseTranslation).
|
|
214 |
!
|
|
215 |
|
|
216 |
applyInverseToX:aNumber
|
|
217 |
"Apply the receiver to a number representing an x-coordinate
|
|
218 |
and return the result."
|
|
219 |
|
|
220 |
|t|
|
|
221 |
|
|
222 |
translation isNil ifTrue:[t := 0] ifFalse:[t := translation x].
|
|
223 |
^ (aNumber - t)
|
|
224 |
!
|
|
225 |
|
|
226 |
applyInverseToY:aNumber
|
|
227 |
"Apply the receiver to a number representing an y-coordinate
|
|
228 |
and return the result."
|
|
229 |
|
|
230 |
|t|
|
|
231 |
|
|
232 |
translation isNil ifTrue:[t := 0] ifFalse:[t := translation y].
|
|
233 |
^ (aNumber - t)
|
|
234 |
!
|
|
235 |
|
|
236 |
applyTo:anObject
|
|
237 |
"Apply the receiver to anObject and return the result."
|
|
238 |
|
|
239 |
translation isNil ifTrue:[
|
|
240 |
^ anObject
|
|
241 |
].
|
|
242 |
^ anObject translatedBy:translation
|
|
243 |
!
|
|
244 |
|
|
245 |
applyToX:aNumber
|
|
246 |
"Apply the receiver to a number representing an x-coordinate
|
|
247 |
and return the result."
|
|
248 |
|
|
249 |
|t|
|
|
250 |
|
|
251 |
translation isNil ifTrue:[t := 0] ifFalse:[t := translation x].
|
|
252 |
^ aNumber + t
|
|
253 |
!
|
|
254 |
|
|
255 |
applyToY:aNumber
|
|
256 |
"Apply the receiver to a number representing an y-coordinate
|
|
257 |
and return the result."
|
|
258 |
|
|
259 |
|t|
|
|
260 |
|
|
261 |
translation isNil ifTrue:[t := 0] ifFalse:[t := translation y].
|
|
262 |
^ aNumber + t
|
|
263 |
!
|
|
264 |
|
|
265 |
compose:aTransformation
|
|
266 |
"return a new WindowingTransformation that is the
|
|
267 |
composition of the receiver and aTransformation.
|
|
268 |
The effect of applying the resulting WindowingTransformation
|
|
269 |
to an object is the same as that of first applying
|
|
270 |
aTransformation to the object and then applying the
|
|
271 |
receiver to its result."
|
|
272 |
|
|
273 |
|aTransformationScale newScale newTranslation|
|
|
274 |
|
|
275 |
aTransformationScale := aTransformation scale.
|
|
276 |
aTransformation isNoScale ifTrue:[
|
|
277 |
newScale := nil
|
|
278 |
] ifFalse:[
|
|
279 |
newScale := aTransformationScale
|
|
280 |
].
|
|
281 |
newTranslation := (translation ? 0) + aTransformation translation.
|
|
282 |
|
|
283 |
^ (self class)
|
|
284 |
scale:newScale
|
|
285 |
translation:newTranslation
|
|
286 |
!
|
|
287 |
|
|
288 |
composedWithLocal: aTransformation
|
|
289 |
^ self compose:aTransformation
|
|
290 |
!
|
|
291 |
|
|
292 |
transformPoint:p
|
|
293 |
"Apply the receiver to a point, returning a new point."
|
|
294 |
|
|
295 |
translation isNil ifTrue:[
|
|
296 |
^ p
|
|
297 |
].
|
|
298 |
^ p + translation
|
7058
|
299 |
!
|
|
300 |
|
|
301 |
transformRectangle:aRectangle
|
|
302 |
"Apply the receiver to a rectangle, returning a new rectangle."
|
|
303 |
|
|
304 |
translation isNil ifTrue:[
|
|
305 |
^ aRectangle.
|
|
306 |
].
|
|
307 |
^ aRectangle translatedBy:translation.
|
6696
|
308 |
! !
|
|
309 |
|
|
310 |
!TranslationTransform methodsFor:'printing & storing'!
|
|
311 |
|
|
312 |
printOn:aStream
|
|
313 |
"append a user printed representation of the receiver to aStream.
|
|
314 |
The format is suitable for a human - not meant to be read back."
|
|
315 |
|
8714
|
316 |
aStream nextPutAll:self className.
|
6696
|
317 |
aStream nextPutAll:' translation: '.
|
|
318 |
translation printOn:aStream
|
8714
|
319 |
|
|
320 |
"Modified: / 28-06-2019 / 09:22:17 / Claus Gittinger"
|
6696
|
321 |
! !
|
|
322 |
|
|
323 |
!TranslationTransform methodsFor:'private'!
|
|
324 |
|
|
325 |
inverseTranslation
|
7058
|
326 |
"return with a Point or Number representing the inverse of my translation."
|
6696
|
327 |
|
7058
|
328 |
translation isNil ifTrue:[
|
|
329 |
^ nil.
|
|
330 |
].
|
|
331 |
^ translation negated.
|
6696
|
332 |
! !
|
|
333 |
|
|
334 |
!TranslationTransform methodsFor:'testing'!
|
|
335 |
|
|
336 |
isIdentityTransformation
|
|
337 |
"return true if this is an identity transformation;
|
|
338 |
return false, otherwise."
|
|
339 |
|
|
340 |
^ translation isNil or:[translation x = 0 and:[translation y = 0]]
|
|
341 |
!
|
|
342 |
|
|
343 |
isNoScale
|
|
344 |
"return true if the identity scale is in effect (i.e. saleFactor is 1);
|
|
345 |
return false, otherwise."
|
|
346 |
|
|
347 |
^ true
|
|
348 |
! !
|
|
349 |
|
|
350 |
!TranslationTransform methodsFor:'transformations'!
|
|
351 |
|
|
352 |
scaleBy:aScale
|
|
353 |
"scale the receiver.
|
|
354 |
This is a destructive operation, modifying the transformation
|
|
355 |
represented by the receiver"
|
|
356 |
|
|
357 |
self error:'not allowed'
|
|
358 |
!
|
|
359 |
|
|
360 |
scaledBy:aScale
|
|
361 |
"return a new WindowingTransformation with the scale and translation of
|
|
362 |
the receiver both scaled by aScale."
|
|
363 |
|
|
364 |
^ WindowingTransformation
|
|
365 |
scale:aScale
|
|
366 |
translation:translation
|
|
367 |
!
|
|
368 |
|
|
369 |
translateBy:aTranslation
|
|
370 |
"translate the receiver.
|
|
371 |
This is a destructive operation, modifying the transformation
|
|
372 |
represented by the receiver"
|
|
373 |
|
|
374 |
aTranslation isNil ifTrue:[^ self].
|
|
375 |
|
|
376 |
translation isNil ifTrue:[
|
|
377 |
translation := 0@0
|
|
378 |
].
|
|
379 |
translation := translation + aTranslation asPoint
|
|
380 |
!
|
|
381 |
|
|
382 |
translatedBy:aPoint
|
|
383 |
"return a new WindowingTransformation with the same scale and
|
|
384 |
rotations as the receiver and with a translation of the current
|
|
385 |
translation plus aPoint."
|
|
386 |
|
|
387 |
^ (self class)
|
|
388 |
translation:(translation + aPoint)
|
|
389 |
! !
|
|
390 |
|
|
391 |
!TranslationTransform class methodsFor:'documentation'!
|
|
392 |
|
|
393 |
version
|
7058
|
394 |
^ '$Header$'
|
6696
|
395 |
!
|
|
396 |
|
|
397 |
version_CVS
|
7058
|
398 |
^ '$Header$'
|
6696
|
399 |
! !
|
|
400 |
|