-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
OSWorldRenderer.class.st
442 lines (336 loc) · 10.1 KB
/
OSWorldRenderer.class.st
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
"
I know how to render the world in an OSWindow
"
Class {
#name : 'OSWorldRenderer',
#superclass : 'AbstractWorldRenderer',
#instVars : [
'driver',
'osWindow',
'display',
'windowCloseAction',
'previousFrameRenderingTime'
],
#classInstVars : [
'canvasScaleFactor',
'autoSetCanvasScaleFactor'
],
#category : 'OSWindow-Core-Morphic',
#package : 'OSWindow-Core',
#tag : 'Morphic'
}
{ #category : 'accessing' }
OSWorldRenderer class >> autoSetCanvasScaleFactor [
^ autoSetCanvasScaleFactor ifNil: [ autoSetCanvasScaleFactor := true ]
]
{ #category : 'accessing' }
OSWorldRenderer class >> autoSetCanvasScaleFactor: boolean [
autoSetCanvasScaleFactor := boolean
]
{ #category : 'accessing' }
OSWorldRenderer class >> canvasScaleFactor [
^ canvasScaleFactor ifNil: [ canvasScaleFactor := 1 ]
]
{ #category : 'accessing' }
OSWorldRenderer class >> canvasScaleFactor: newScale [
canvasScaleFactor := 1 max: newScale asInteger
]
{ #category : 'accessing' }
OSWorldRenderer class >> defaultExtent [
^ 976@665
]
{ #category : 'accessing' }
OSWorldRenderer class >> isApplicableFor: aWorld [
^ Smalltalk isHeadless and: [ CommandLineArguments new hasOption: 'interactive' ]
]
{ #category : 'accessing' }
OSWorldRenderer class >> priority [
^ 2
]
{ #category : 'settings' }
OSWorldRenderer class >> settingsOn: aBuilder [
<systemsettings>
(aBuilder group: #canvasScaleFactorGroup)
parent: #appearance;
label: 'World renderer canvas scaling';
with: [
(aBuilder pickOne: #canvasScaleFactor)
label: 'Canvas scale factor';
target: self;
domainValues: (1 to: 5);
default: 1.
(aBuilder setting: #autoSetCanvasScaleFactor)
label: 'Set canvas scale factor automatically';
target: self;
default: true ]
]
{ #category : 'accessing' }
OSWorldRenderer >> activateCursor: aCursor withMask: maskForm [
osWindow ifNotNil: [
osWindow
setMouseCursor: aCursor
mask: maskForm
andScale: world worldState worldRenderer screenScaleFactor ]
]
{ #category : 'accessing' }
OSWorldRenderer >> actualDisplaySize [
^ self actualScreenSize * self canvasScaleFactor
]
{ #category : 'accessing' }
OSWorldRenderer >> actualScreenSize [
^ (self windowExtent / self screenScaleFactor) ceiling max: 1@1
]
{ #category : 'private' }
OSWorldRenderer >> canvasScaleFactor [
^ self class canvasScaleFactor
]
{ #category : 'events' }
OSWorldRenderer >> checkForNewScreenSize [
| windowRenderer |
"Under certain circunstances, it may happen that renderer is still being
created when the UI process call this method. In that case, we need to
skip the check. "
windowRenderer := self osWindowRenderer.
windowRenderer ifNil: [ ^ self ].
(world == World and: [ self class autoSetCanvasScaleFactor ]) ifTrue: [
self class canvasScaleFactor: (windowRenderer outputExtent / self windowExtent) min * self screenScaleFactor ].
(display isNil or: [ display extent = self actualDisplaySize and: [ world worldState realWindowExtent = self actualScreenSize ] ])
ifTrue: [ ^ self ].
display := Form extent: self actualDisplaySize depth: 32.
windowRenderer form: display.
world worldState realWindowExtent: self actualScreenSize.
world restoreMorphicDisplay
]
{ #category : 'accessing' }
OSWorldRenderer >> clipboardText [
^ osWindow clipboardText
]
{ #category : 'accessing' }
OSWorldRenderer >> clipboardText: aString [
osWindow clipboardText: aString asString
]
{ #category : 'utilities' }
OSWorldRenderer >> convertWindowMouseEventPosition: aPosition [
^ (aPosition / self screenScaleFactor) rounded
]
{ #category : 'screenshot' }
OSWorldRenderer >> copyRectangle: aRectangle into: aForm [
| scale copiedForm |
scale := self canvasScaleFactor.
copiedForm := scale = 1
ifTrue: [ aForm ]
ifFalse: [ Form extent: aForm extent * scale depth: aForm depth ].
copiedForm
copyBits: (aRectangle origin floor * scale extent: copiedForm extent)
from: display
at: 0 @ 0
clippingBox: copiedForm boundingBox
rule: Form over
fillColor: nil.
scale = 1 ifFalse: [
(copiedForm scaledToSize: aForm extent)
contentsOfArea: aForm boundingBox into: aForm ].
^ aForm
]
{ #category : 'private' }
OSWorldRenderer >> deactivate [
osWindow ifNotNil: [ osWindow destroy. osWindow := nil ].
display := nil
]
{ #category : 'accessing' }
OSWorldRenderer >> defaultWindowIcon [
^ self iconNamed: #pharoBig
]
{ #category : 'accessing' }
OSWorldRenderer >> defaultWindowTitle [
^ Smalltalk image imageFile fullName
]
{ #category : 'operations' }
OSWorldRenderer >> deferUpdatesDuring: aBlock [
self osWindowRenderer ifNotNil: [:renderer | renderer deferUpdatesWhile: aBlock ]
]
{ #category : 'screenshot' }
OSWorldRenderer >> depth [
^ display depth
]
{ #category : 'accessing' }
OSWorldRenderer >> display [
^ display
]
{ #category : 'private' }
OSWorldRenderer >> doActivate [
| attributes initialExtent |
initialExtent := world worldState realWindowExtent ifNil: [
self class defaultExtent ].
attributes := OSWindowAttributes new.
attributes
extent: initialExtent;
title: self defaultWindowTitle;
windowCentered: true;
icon: self defaultWindowIcon.
display := Form extent: initialExtent * self canvasScaleFactor depth: 32.
world extent: initialExtent.
driver := self pickMostSuitableWindowDriver.
attributes preferableDriver: driver.
osWindow := OSWindow
createWithAttributes: attributes
eventHandler: (OSWindowMorphicEventHandler for: world).
driver afterMainPharoWindowCreated: osWindow.
driver
afterSetWindowTitle: self defaultWindowTitle
onWindow: osWindow.
driver startUp: true.
world worldState doFullRepaint.
world displayWorld.
OSWindowClipboard new beDefault.
"SDL2 on MacOS presented a bug if this message send is done earlier on this method.
See: https://github.com/pharo-project/pharo/issues/10981"
osWindow focus
]
{ #category : 'operations' }
OSWorldRenderer >> drawDuring: aBlock [
osWindow ifNil: [ ^ self ].
self isProfilingRenderingTime ifTrue: [ ^ self drawWhileProfilingRenderingTimeDuring: aBlock ].
self osWindowRenderer deferUpdatesWhile: [
self osWindowRenderer drawDuring: aBlock
]
]
{ #category : 'profiling' }
OSWorldRenderer >> drawWhileProfilingRenderingTimeDuring: aBlock [
| measurements |
previousFrameRenderingTime := [
self osWindowRenderer deferUpdatesWhile: [
self osWindowRenderer drawDuring: [ :canvas |
| frameCanvasRenderingTime displayRectangle |
frameCanvasRenderingTime := [ aBlock value: canvas ] timeToRunWithoutGC.
measurements := {
'Content' -> frameCanvasRenderingTime
}.
previousFrameRenderingTime ifNotNil: [
measurements := measurements , {
'Previous frame' -> previousFrameRenderingTime.
}
].
displayRectangle := (0@0 extent: 250@40).
canvas
fillRectangle: displayRectangle color: Color white.
measurements doWithIndex: [ :each :index |
| line |
line := self formatRenderingTimeMeasurement: each.
canvas drawString: line at: 10@(10*index) font: nil color: Color black.
].
self osWindowRenderer updateRectangle: (displayRectangle scaleBy: self canvasScaleFactor).
]
]
] timeToRunWithoutGC.
self logRenderingTimeMeasurements: measurements
]
{ #category : 'accessing' }
OSWorldRenderer >> driver [
^ driver
]
{ #category : 'operations' }
OSWorldRenderer >> fullscreenMode: aValue [
osWindow fullscreen: aValue.
self checkForNewScreenSize
]
{ #category : 'events' }
OSWorldRenderer >> handleOSWindowCloseEvent: event [
windowCloseAction value
]
{ #category : 'accessing' }
OSWorldRenderer >> icon: aForm [
osWindow icon: aForm
]
{ #category : 'initialization' }
OSWorldRenderer >> initialize [
super initialize.
windowCloseAction := [ self currentWorld defer: [ WorldState quitSession ] ]
]
{ #category : 'accessing' }
OSWorldRenderer >> osWindow [
^ osWindow
]
{ #category : 'accessing' }
OSWorldRenderer >> osWindowRenderer [
osWindow ifNil: [ ^ nil ].
^ osWindow renderer ifNil: [
osWindow newFormRenderer: display.
osWindow renderer ]
]
{ #category : 'private' }
OSWorldRenderer >> pickMostSuitableWindowDriver [
driver := OSWindowDriver current.
^ driver
]
{ #category : 'events' }
OSWorldRenderer >> requestStopTextEditing [
osWindow stopTextInput
]
{ #category : 'events' }
OSWorldRenderer >> requestTextEditingAt: aRectangle [
osWindow startTextInputAtRectangle: aRectangle
]
{ #category : 'accessing' }
OSWorldRenderer >> screenScaleFactor [
^ world scaleFactor * self windowScaleFactor
]
{ #category : 'system startup' }
OSWorldRenderer >> shutDown: quitting [
super shutDown: quitting.
driver ifNotNil: [
driver shutDown: quitting ]
]
{ #category : 'system startup' }
OSWorldRenderer >> startUp: resuming [
super startUp: resuming.
driver ifNotNil: [
driver startUp: resuming ]
]
{ #category : 'operations' }
OSWorldRenderer >> updateDamage: allDamage [
| scale scaledDamage |
scale := self canvasScaleFactor.
scaledDamage := scale = 1 ifTrue: [ allDamage ] ifFalse: [ allDamage collect: [ :rectangle | rectangle scaleBy: scale ] ].
"quickly copy altered rects of canvas to Display:"
self osWindowRenderer updateAreas: scaledDamage immediate: false
]
{ #category : 'events' }
OSWorldRenderer >> updateToNewResolution [
self window updateToNewResolution
]
{ #category : 'activation' }
OSWorldRenderer >> updateWindowTitle [
osWindow title: Smalltalk imageFile fullName.
driver
afterSetWindowTitle: Smalltalk imageFile fullName
onWindow: osWindow
]
{ #category : 'accessing' }
OSWorldRenderer >> window [
^ osWindow
]
{ #category : 'accessing' }
OSWorldRenderer >> windowCloseAction [
^ windowCloseAction
]
{ #category : 'accessing' }
OSWorldRenderer >> windowCloseAction: anObject [
windowCloseAction := anObject
]
{ #category : 'accessing' }
OSWorldRenderer >> windowExtent [
^ osWindow ifNil: [ 240@120 ] ifNotNil: [ osWindow extent ]
]
{ #category : 'scale factor' }
OSWorldRenderer >> windowScaleFactor [
^ osWindow ifNil: [ 1 ] ifNotNil: [ osWindow screenScaleFactor max: 1 ]
]
{ #category : 'accessing' }
OSWorldRenderer >> windowTitle: aString [
osWindow title: aString
]
{ #category : 'accessing' }
OSWorldRenderer >> world: aWorld [
world := aWorld
]