-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathAWTFrameProcessor.java
More file actions
688 lines (581 loc) · 18.6 KB
/
AWTFrameProcessor.java
File metadata and controls
688 lines (581 loc) · 18.6 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
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
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.system;
import java.awt.Component;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import com.jme3.app.Application;
import com.jme3.input.AWTKeyInput;
import com.jme3.input.AWTMouseInput;
import com.jme3.post.SceneProcessor;
import com.jme3.profile.AppProfiler;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.texture.FrameBuffer;
/**
* A frame processor that enables to render JMonkey frame buffer onto an AWT component.
* <p>
* This class is based on the
* <a href="http://www.oracle.com/technetwork/java/javase/overview/javafx-overview-2158620.html">JavaFX</a>
* original code provided by Alexander Brui (see <a href="https://github.com/JavaSaBr/JME3-JFX">JME3-FX</a>)
* </p>
*
* @author Julien Seinturier - COMEX SA - <a href="http://www.seinturier.fr">http://www.seinturier.fr</a>
* @author Alexander Brui (JavaSaBr)
*
*/
public class AWTFrameProcessor implements SceneProcessor, PropertyChangeListener {
public enum TransferMode {
ALWAYS, ON_CHANGES
}
private Application application = null;
/**
* The width listener.
*/
protected PropertyChangeListener widthListener;
/**
* The height listener.
*/
protected PropertyChangeListener heightListener;
/**
* The ration listener.
*/
protected PropertyChangeListener rationListener;
/**
* The flag to decide when we should resize.
*/
private final AtomicInteger reshapeNeeded;
/**
* The render manager.
*/
private RenderManager renderManager;
/**
* The source view port.
*/
private ViewPort viewPort;
/**
* The frame transfer.
*/
private AWTComponentRenderer frameTransfer;
/**
* The transfer mode.
*/
private TransferMode transferMode;
/**
* The destination of jMe frames.
*/
protected volatile Component destination;
/**
* The flag is true if this processor is main.
*/
private volatile boolean main;
private int askWidth;
private int askHeight;
private boolean askFixAspect;
private boolean enabled;
@Override
public void initialize(RenderManager rm, ViewPort vp) {
this.renderManager = rm;
}
@Override
public void reshape(ViewPort vp, int w, int h) {
// TODO Auto-generated method stub
}
@Override
public boolean isInitialized() {
return frameTransfer != null;
}
@Override
public void preFrame(float tpf) {
// TODO Auto-generated method stub
}
@Override
public void postQueue(RenderQueue rq) {
// TODO Auto-generated method stub
}
@Override
public void postFrame(FrameBuffer out) {
if (!isEnabled()) {
return;
}
final AWTComponentRenderer frameTransfer = getFrameTransfer();
if (frameTransfer != null) {
frameTransfer.copyFrameBufferToImage(getRenderManager());
}
// for the next frame
if (hasDestination() && reshapeNeeded.get() > 0 && reshapeNeeded.decrementAndGet() >= 0) {
if (frameTransfer != null) {
frameTransfer.dispose();
}
setFrameTransfer(reshapeInThread(askWidth, askHeight, askFixAspect));
}
}
@Override
public void cleanup() {
final AWTComponentRenderer frameTransfer = getFrameTransfer();
if (frameTransfer != null) {
frameTransfer.dispose();
setFrameTransfer(null);
}
}
@Override
public void setProfiler(AppProfiler profiler) {
// not implemented
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Property changed: " + evt.getPropertyName() + " " + evt.getOldValue() + " -> "
+ evt.getNewValue());
}
public AWTFrameProcessor() {
transferMode = TransferMode.ALWAYS;
askWidth = 1;
askHeight = 1;
main = true;
reshapeNeeded = new AtomicInteger(2);
}
/**
* Notify about that the ratio was changed.
*
* @param newValue
* the new value of the ratio.
*/
protected void notifyChangedRatio(Boolean newValue) {
notifyComponentResized(destination.getWidth(), destination.getHeight(), newValue);
}
/**
* Notify about that the height was changed.
*
* @param newValue
* the new value of the height.
*/
protected void notifyChangedHeight(Number newValue) {
notifyComponentResized(destination.getWidth(), newValue.intValue(), isPreserveRatio());
}
/**
* Notify about that the width was changed.
*
* @param newValue
* the new value of the width.
*/
protected void notifyChangedWidth(Number newValue) {
notifyComponentResized(newValue.intValue(), destination.getHeight(), isPreserveRatio());
}
/**
* Gets the application.
*
* @return the application.
*/
protected Application getApplication() {
return application;
}
/**
* Sets the application.
*
* @param application
* the application.
*/
protected void setApplication(Application application) {
this.application = application;
}
/**
* Gets the current destination.
*
* @return the current destination.
*/
protected Component getDestination() {
return destination;
}
/**
* Sets the destination.
*
* @param destination
* the destination.
*/
protected void setDestination(Component destination) {
this.destination = destination;
}
/**
* Checks of existing destination.
*
* @return true if destination is exists.
*/
protected boolean hasDestination() {
return destination != null;
}
/**
* Checks of existing application.
*
* @return true if destination is exists.
*/
protected boolean hasApplication() {
return application != null;
}
/**
* Gets the frame transfer.
*
* @return the file transfer.
*/
protected AWTComponentRenderer getFrameTransfer() {
return frameTransfer;
}
/**
* Sets the frame transfer.
*
* @param frameTransfer
* the file transfer.
*/
protected void setFrameTransfer(AWTComponentRenderer frameTransfer) {
this.frameTransfer = frameTransfer;
}
/**
* Gets the view port.
*
* @return the view port.
*/
protected ViewPort getViewPort() {
return viewPort;
}
/**
* Gets the render manager.
*
* @return the render manager.
*/
protected RenderManager getRenderManager() {
return renderManager;
}
public boolean isMain() {
return main;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Handle resizing.
*
* @param newWidth
* the new width.
* @param newHeight
* the new height.
* @param fixAspect
* true to fix the aspect ratio.
*/
protected void notifyComponentResized(int newWidth, int newHeight, boolean fixAspect) {
newWidth = Math.max(newWidth, 1);
newHeight = Math.max(newHeight, 1);
if (askWidth == newWidth && askWidth == newHeight && askFixAspect == fixAspect) {
return;
}
askWidth = newWidth;
askHeight = newHeight;
askFixAspect = fixAspect;
reshapeNeeded.set(2);
}
public void reshape() {
reshapeNeeded.set(2);
}
/**
* Is preserve ratio.
*
* @return is preserve ratio.
*/
protected boolean isPreserveRatio() {
return false;
}
/**
* Gets destination width.
*
* @return the destination width.
*/
protected int getDestinationWidth() {
return getDestination().getWidth();
}
/**
* Gets destination height.
*
* @return the destination height.
*/
protected int getDestinationHeight() {
return getDestination().getHeight();
}
/**
* Bind this processor.
*
* @param destination
* the destination.
* @param application
* the application.
*/
public void bind(Component destination, Application application) {
final RenderManager renderManager = application.getRenderManager();
if (renderManager == null) {
throw new RuntimeException("No render manager available from the application.");
}
List<ViewPort> postViews = renderManager.getPostViews();
if (postViews.isEmpty()) {
throw new RuntimeException("the list of a post view is empty.");
}
bind(destination, application, postViews.get(postViews.size() - 1), true);
}
/**
* Bind this processor.
*
* @param destination
* the destination.
* @param application
* the application.
* @param viewPort
* the view port.
*/
public void bind(Component destination, Application application, ViewPort viewPort) {
bind(destination, application, viewPort, true);
}
/**
* Bind this processor.
*
* @param destination
* the destination.
* @param application
* the application.
* @param viewPort
* the view port.
* @param main
* true if this processor is main.
*/
public void bind(Component destination, Application application, ViewPort viewPort, boolean main) {
if (hasApplication()) {
throw new RuntimeException("This process is already bonded.");
}
setApplication(application);
setEnabled(true);
this.main = main;
this.viewPort = viewPort;
this.viewPort.addProcessor(this);
if (EventQueue.isDispatchThread()) {
bindDestination(application, destination);
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
bindDestination(application, destination);
}
});
}
}
/**
* Unbind this processor from its current destination.
*/
public void unbind() {
if (viewPort != null) {
viewPort.removeProcessor(this);
viewPort = null;
}
if (EventQueue.isDispatchThread()) {
unbindDestination();
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
unbindDestination();
}
});
}
}
/**
* Bind this processor.
*
* @param application
* the application.
* @param destination
* the destination.
*/
protected void bindDestination(Application application, Component destination) {
if (!EventQueue.isDispatchThread()) {
throw new RuntimeException("bind has to be done from the Event Dispatching thread.");
}
if (isMain()) {
if (application.getContext() != null) {
if (application.getContext() instanceof AWTContext) {
AWTContext context = (AWTContext) application.getContext();
AWTMouseInput mouseInput = context.getMouseInput();
mouseInput.bind(destination);
AWTKeyInput keyInput = context.getKeyInput();
keyInput.bind(destination);
setDestination(destination);
bindListeners();
notifyComponentResized(getDestinationWidth(), getDestinationHeight(), isPreserveRatio());
} else {
throw new IllegalArgumentException(
"Underlying application has to use AWTContext (actually using "
+ application.getContext().getClass().getSimpleName() + ")");
}
} else {
throw new IllegalArgumentException(
"Underlying application has to use a valid AWTContext (context is null)");
}
}
}
/**
* Unbind this processor from destination.
*/
protected void unbindDestination() {
if (!EventQueue.isDispatchThread()) {
throw new RuntimeException("unbind has to be done from the Event Dispatching thread.");
}
if (hasApplication() && isMain()) {
final AWTContext context = (AWTContext) getApplication().getContext();
final AWTMouseInput mouseInput = context.getMouseInput();
mouseInput.unbind();
final AWTKeyInput keyInput = context.getKeyInput();
keyInput.unbind();
}
setApplication(null);
if (hasDestination()) {
unbindListeners();
setDestination(null);
}
}
protected void bindListeners() {
Component destination = getDestination();
destination.addPropertyChangeListener(this);
destination.addPropertyChangeListener(this);
}
protected void unbindListeners() {
Component destination = getDestination();
destination.removePropertyChangeListener(this);
destination.removePropertyChangeListener(this);
}
/**
* Reshape the current frame transfer for the new size.
*
* @param width
* the width.
* @param height
* the height.
* @param fixAspect
* true to fix the aspect ratio.
* @return the new frame transfer.
*/
protected AWTComponentRenderer reshapeInThread(int width, int height, boolean fixAspect) {
reshapeCurrentViewPort(width, height);
ViewPort viewPort = getViewPort();
RenderManager renderManager = getRenderManager();
FrameBuffer frameBuffer = viewPort.getOutputFrameBuffer();
AWTComponentRenderer frameTransfer = createFrameTransfer(frameBuffer, width, height);
frameTransfer.init(renderManager.getRenderer(), isMain());
if (isMain()) {
AWTContext context = (AWTContext) getApplication().getContext();
context.setHeight(height);
context.setWidth(width);
}
return frameTransfer;
}
/**
* Create a new frame transfer.
*
* @param frameBuffer
* the frame buffer.
* @param width
* the width.
* @param height
* the height.
* @return the new frame transfer.
*/
protected AWTComponentRenderer createFrameTransfer(FrameBuffer frameBuffer, int width, int height) {
return new AWTComponentRenderer(getDestination(), getTransferMode(), isMain() ? null : frameBuffer,
width, height);
}
/**
* Reshape the current view port.
*
* @param width
* the width.
* @param height
* the height.
*/
protected void reshapeCurrentViewPort(int width, int height) {
ViewPort viewPort = getViewPort();
Camera camera = viewPort.getCamera();
int cameraAngle = getCameraAngle();
float aspect = (float) camera.getWidth() / camera.getHeight();
if (isMain()) {
getRenderManager().notifyReshape(width, height);
camera.setFrustumPerspective(cameraAngle, aspect, 1f, 10000);
return;
}
camera.resize(width, height, true);
camera.setFrustumPerspective(cameraAngle, aspect, 1f, 10000);
final List<SceneProcessor> processors = viewPort.getProcessors();
boolean found = false;
Iterator<SceneProcessor> iter = processors.iterator();
while (!found && iter.hasNext()) {
if (!(iter.next() instanceof AWTFrameProcessor)) {
found = true;
}
}
if (found) {
FrameBuffer frameBuffer = AWTUtils.getFrameBuffer(width, height, 1);
viewPort.setOutputFrameBuffer(frameBuffer);
}
for (SceneProcessor sceneProcessor : processors) {
if (!sceneProcessor.isInitialized()) {
sceneProcessor.initialize(renderManager, viewPort);
} else {
sceneProcessor.reshape(viewPort, width, height);
}
}
}
/**
* Gets camera angle.
*
* @return the camera angle.
*/
protected int getCameraAngle() {
final String angle = System.getProperty("awt.frame.transfer.camera.angle", "45");
return Integer.parseInt(angle);
}
public TransferMode getTransferMode() {
return transferMode;
}
public void setTransferMode(TransferMode transferMode) {
this.transferMode = transferMode;
}
}