-
Notifications
You must be signed in to change notification settings - Fork 4
/
SWTOpenGLDisplaySurface.java
839 lines (738 loc) · 23.4 KB
/
SWTOpenGLDisplaySurface.java
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*******************************************************************************************************
*
* SWTOpenGLDisplaySurface.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation
* platform (v.1.8.2).
*
* (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.opengl.view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.locationtech.jts.geom.Envelope;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAnimatorControl;
import com.jogamp.opengl.GLAutoDrawable;
import msi.gama.common.geometry.Envelope3D;
import msi.gama.common.interfaces.IDisplaySurface;
import msi.gama.common.interfaces.IDisplaySynchronizer;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.common.interfaces.ILayer;
import msi.gama.common.interfaces.ILayerManager;
import msi.gama.common.preferences.GamaPreferences;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.metamodel.shape.IShape;
import msi.gama.metamodel.topology.filter.Different;
import msi.gama.outputs.LayeredDisplayData;
import msi.gama.outputs.LayeredDisplayData.Changes;
import msi.gama.outputs.LayeredDisplayOutput;
import msi.gama.outputs.display.LayerManager;
import msi.gama.outputs.layers.IEventLayerListener;
import msi.gama.outputs.layers.OverlayLayer;
import msi.gama.precompiler.GamlAnnotations.display;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.IScope.IGraphicsScope;
import msi.gaml.expressions.IExpression;
import msi.gaml.operators.Cast;
import msi.gaml.statements.draw.DrawingAttributes;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.opengl.renderer.IOpenGLRenderer;
import ummisco.gama.opengl.renderer.JOGLRenderer;
import ummisco.gama.ui.menus.AgentsMenu;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaIcons;
import ummisco.gama.ui.utils.DPIHelper;
import ummisco.gama.ui.utils.WorkbenchHelper;
import ummisco.gama.ui.views.displays.DisplaySurfaceMenu;
/**
* Class OpenGLSWTDisplaySurface.
*
* @author drogoul
* @since 25 mars 2015
*
*/
@display ("opengl")
@doc ("Displays that uses the OpenGL technology to display their layers in 3D")
public class SWTOpenGLDisplaySurface implements IDisplaySurface.OpenGL {
static {
DEBUG.OFF();
}
/** The animator. */
GLAnimatorControl animator;
/** The renderer. */
IOpenGLRenderer renderer;
/** The zoom fit. */
protected boolean zoomFit = true;
/** The listeners. */
Set<IEventLayerListener> listeners = new HashSet<>();
/** The output. */
final LayeredDisplayOutput output;
/** The layer manager. */
final LayerManager layerManager;
/** The menu manager. */
protected DisplaySurfaceMenu menuManager;
/** The temp focus. */
protected IExpression temp_focus;
/** The scope. */
IGraphicsScope scope;
/** The synchronizer. */
public IDisplaySynchronizer synchronizer;
/** The parent. */
final Composite parent;
/** The disposed. */
volatile boolean disposed;
/** The already updating. */
private volatile boolean alreadyUpdating;
/**
* Instantiates a new SWT open GL display surface.
*
* @param objects
* the objects
*/
public SWTOpenGLDisplaySurface(final Object... objects) {
output = (LayeredDisplayOutput) objects[0];
parent = (Composite) objects[1];
output.getData().addListener(this);
output.setSurface(this);
setDisplayScope(output.getScope().copyForGraphics("in opengl display"));
renderer = createRenderer();
renderer.setDisplaySurface(this);
animator = new GamaGLCanvas(parent, renderer).getAnimator();
layerManager = new LayerManager(this, output);
temp_focus = output.getFacet(IKeyword.FOCUS);
animator.start();
}
/**
* Creates the renderer.
*
* @return the i open GL renderer
*/
protected IOpenGLRenderer createRenderer() {
return new JOGLRenderer();
}
@Override
public void setMenuManager(final Object menuManager) { this.menuManager = (DisplaySurfaceMenu) menuManager; }
/**
* Method getImage()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getImage()
*/
@Override
public BufferedImage getImage(final int w, final int h) {
while (!isRendered()) {
try {
Thread.sleep(1);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
final GLAutoDrawable glad = renderer.getCanvas();
if (glad == null || glad.getGL() == null || glad.getGL().getContext() == null) return null;
final boolean current = glad.getGL().getContext().isCurrent();
if (!current) { glad.getGL().getContext().makeCurrent(); }
final BufferedImage image = getImage(glad.getGL().getGL2(), w, h);
// final AWTGLReadBufferUtil glReadBufferUtil = new AWTGLReadBufferUtil(glad.getGLProfile(), false);
// final BufferedImage image = glReadBufferUtil.readPixelsToBufferedImage(glad.getGL(), true);
if (!current) { glad.getGL().getContext().release(); }
return image;
// return ImageUtils.resize(image, w, h);
}
/** The buffer. */
ByteBuffer buffer;
/**
* Gets the buffer.
*
* @param w
* the w
* @param h
* the h
* @return the buffer
*/
protected ByteBuffer getBuffer(final int w, final int h) {
if (buffer == null || buffer.capacity() != w * h * 4) {
buffer = Buffers.newDirectByteBuffer(w * h * 4);
} else {
buffer.rewind();
}
return buffer;
}
/**
* Gets the image.
*
* @param gl3
* the gl 3
* @param ww
* the ww
* @param hh
* the hh
* @return the image
*/
protected BufferedImage getImage(final GL2 gl3, final int ww, final int hh) {
// See #2628 and https://github.com/sgothel/jogl/commit/ca7f0fb61b0a608b6e684a5bbde71f6ecb6e3fe0
final int width = DPIHelper.autoScaleDown(ww);
final int height = DPIHelper.autoScaleDown(hh);
final BufferedImage screenshot = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics graphics = screenshot.getGraphics();
final ByteBuffer buffer = getBuffer(width, height);
// be sure you are reading from the right fbo (here is supposed to be the default one)
// bind the right buffer to read from
gl3.glReadBuffer(GL.GL_BACK); // or GL.GL_FRONT ?
// if the width is not multiple of 4, set unpackPixel = 1
gl3.glReadPixels(0, 0, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, buffer);
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
// The color are the three consecutive bytes, it's like referencing
// to the next consecutive array elements, so we got red, green, blue..
// red, green, blue, and so on..+ ", "
graphics.setColor(new Color(buffer.get() & 0xff, buffer.get() & 0xff, buffer.get() & 0xff));
buffer.get(); // consume alpha
graphics.drawRect(w, height - h - 1, 1, 1); // height - h is for flipping the image
}
}
return screenshot;
}
/**
* Method updateDisplay()
*
* @see msi.gama.common.interfaces.IDisplaySurface#updateDisplay(boolean)
*/
@Override
public void updateDisplay(final boolean force) {
if (alreadyUpdating) return;
try {
alreadyUpdating = true;
final boolean oldState = animator.isPaused();
if (force) { animator.resume(); }
layerManager.drawLayersOn(renderer);
// EXPERIMENTAL
if (temp_focus != null) {
final IShape geometry = Cast.asGeometry(getScope(), temp_focus.value(getScope()));
if (geometry != null) {
temp_focus = null;
focusOn(geometry);
}
}
if (force && oldState) { animator.pause(); }
} finally {
alreadyUpdating = false;
}
}
@Override
public double getDisplayWidth() { return renderer.getWidth(); }
@Override
public double getDisplayHeight() { return renderer.getHeight(); }
/**
* Method zoomIn()
*
* @see msi.gama.common.interfaces.IDisplaySurface#zoomIn()
*/
@Override
public void zoomIn() {
// if (renderer.getData().cameraInteractionDisabled()) return;
// renderer.getCameraHelper().zoom(true);
}
/**
* Method zoomOut()
*
* @see msi.gama.common.interfaces.IDisplaySurface#zoomOut()
*/
@Override
public void zoomOut() {
// if (renderer.getData().cameraInteractionDisabled()) return;
// renderer.getCameraHelper().zoom(false);
}
/**
* Method zoomFit()
*
* @see msi.gama.common.interfaces.IDisplaySurface#zoomFit()
*/
@Override
public void zoomFit() {
// if (renderer.getData().cameraInteractionDisabled())
// return;
renderer.getCameraHelper().initialize();
output.getData().resetZRotation();
output.getData().setZoomLevel(LayeredDisplayData.INITIAL_ZOOM, true, true);
zoomFit = true;
}
/**
* Method getManager()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getManager()
*/
@Override
public ILayerManager getManager() { return layerManager; }
/**
* Method focusOn()
*
* @see msi.gama.common.interfaces.IDisplaySurface#focusOn(msi.gama.metamodel.shape.IShape)
*/
@Override
public void focusOn(final IShape geometry) {
// FIXME: Need to compute the depth of the shape to adjust ZPos value.
// FIXME: Problem when the geometry is a point how to determine the
// maxExtent of the shape?
// FIXME: Problem when an agent is placed on a layer with a z_value how
// to get this z_layer value to offset it?
renderer.getCameraHelper().zoomFocus(geometry.getEnvelope().yNegated());
}
/**
* Method waitForUpdateAndRun()
*
* @see msi.gama.common.interfaces.IDisplaySurface#waitForUpdateAndRun(java.lang.Runnable)
*/
@Override
public void runAndUpdate(final Runnable r) {
r.run();
if (getScope().isPaused()) { updateDisplay(true); }
if (animator.isPaused()) {
animator.resume();
animator.pause();
}
}
/**
* Method getWidth()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getWidth()
*/
@Override
public int getWidth() {
return renderer.getCanvas().getSurfaceWidth();
// return size.x;
}
/**
* Method getHeight()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getHeight()
*/
@Override
public int getHeight() {
return renderer.getCanvas().getSurfaceHeight();
// return size.y;
}
/**
* Method outputReloaded()
*
* @see msi.gama.common.interfaces.IDisplaySurface#outputReloaded()
*/
@Override
public void outputReloaded() {
setDisplayScope(output.getScope().copyForGraphics("in opengl display"));
if (!GamaPreferences.Runtime.ERRORS_IN_DISPLAYS.getValue()) { getScope().disableErrorReporting(); }
renderer.initScene();
layerManager.outputChanged();
// resizeImage(getWidth(), getHeight(), true);
if (zoomFit) { zoomFit(); }
}
/**
* Method addMouseListener()
*
* @see msi.gama.common.interfaces.IDisplaySurface#addMouseListener(java.awt.event.MouseListener)
*/
@Override
public void addListener(final IEventLayerListener listener) {
listeners.add(listener);
}
/**
* Method removeMouseListener()
*
* @see msi.gama.common.interfaces.IDisplaySurface#removeMouseListener(java.awt.event.MouseListener)
*/
@Override
public void removeListener(final IEventLayerListener listener) {
listeners.remove(listener);
}
@Override
public Collection<IEventLayerListener> getLayerListeners() { return listeners; }
/**
* Method getEnvWidth()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getEnvWidth()
*/
@Override
public double getEnvWidth() { return output.getData().getEnvWidth(); }
/**
* Method getEnvHeight()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getEnvHeight()
*/
@Override
public double getEnvHeight() { return output.getData().getEnvHeight(); }
/**
* Method getModelCoordinates()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getModelCoordinates()
*/
@Override
public GamaPoint getModelCoordinates() {
final GamaPoint mp = renderer.getCameraHelper().getMousePosition();
if (mp == null) return null;
final GamaPoint p = renderer.getRealWorldPointFromWindowPoint(mp);
if (p == null) return null;
return new GamaPoint(p.x, -p.y);
}
@Override
public void getModelCoordinatesInfo(final StringBuilder sb) {
boolean canObtainInfo = getManager().isProvidingCoordinates();
if (!canObtainInfo) {
sb.append("No world coordinates");
return;
}
canObtainInfo = getManager().isProvidingWorldCoordinates();
if (!canObtainInfo) {
sb.append("No world coordinates");
return;
}
// By default, returns the coordinates in the world.
final GamaPoint point = getModelCoordinates();
final String x = point == null ? "N/A" : String.format("%8.6f", point.getX());
final String y = point == null ? "N/A" : String.format("%8.6f", point.getY());
sb.append(String.format("X%15s | Y%15s", x, y));
}
@Override
public Envelope getVisibleRegionForLayer(final ILayer currentLayer) {
if (currentLayer instanceof OverlayLayer) return getScope().getSimulation().getEnvelope();
Envelope e = currentLayer.getData().getVisibleRegion();
if (e == null) {
e = new Envelope();
final Point origin = new Point(0, 0);
int xc = -origin.x;
int yc = -origin.y;
e.expandToInclude(currentLayer.getModelCoordinatesFrom(xc, yc, this));
xc = xc + renderer.getCanvas().getSurfaceWidth();
yc = yc + renderer.getCanvas().getSurfaceHeight();
e.expandToInclude(currentLayer.getModelCoordinatesFrom(xc, yc, this));
currentLayer.getData().setVisibleRegion(e);
}
return e;
}
/**
* Method getModelCoordinatesFrom()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getModelCoordinatesFrom(int, int, java.awt.Point, java.awt.Point)
*/
@Override
public GamaPoint getModelCoordinatesFrom(final int xOnScreen, final int yOnScreen, final Point sizeInPixels,
final Point positionInPixels) {
final GamaPoint mp = new GamaPoint(xOnScreen, yOnScreen);
final GamaPoint p = renderer.getRealWorldPointFromWindowPoint(mp);
return new GamaPoint(p.x, -p.y);
}
/**
* Method selectAgent()
*
* @see msi.gama.common.interfaces.IDisplaySurface#selectAgent(int, int)
*/
@Override
public Collection<IAgent> selectAgent(final int x, final int y) {
final GamaPoint pp = getModelCoordinatesFrom(x, y, null, null);
return scope.getRoot().getTopology().getNeighborsOf(scope, new GamaPoint(pp.getX(), pp.getY()),
renderer.getMaxEnvDim() / 100, Different.with());
}
/**
* Method followAgent()
*
* @see msi.gama.common.interfaces.IDisplaySurface#followAgent(msi.gama.metamodel.agent.IAgent)
*/
@Override
public void followAgent(final IAgent a) {
new Thread(
() -> WorkbenchHelper.asyncRun(() -> renderer.getCameraHelper().zoomFocus(a.getEnvelope().yNegated())))
.start();
}
/**
* Method getZoomLevel()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getZoomLevel()
*/
@Override
public double getZoomLevel() {
if (output.getData().getZoomLevel() == null) {
output.getData().setZoomLevel(computeInitialZoomLevel(), true, false);
}
return output.getData().getZoomLevel();
}
/**
* Compute initial zoom level.
*
* @return the double
*/
protected Double computeInitialZoomLevel() {
return renderer.getCameraHelper().zoomLevel();
}
/**
* Method getDisplayScope()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getDisplayScope()
*/
@Override
public IGraphicsScope getScope() { return scope; }
/**
* Method getOutput()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getOutput()
*/
@Override
public LayeredDisplayOutput getOutput() { return output; }
/**
* Method setPaused()
*
* @see msi.gama.common.interfaces.IDisplaySurface.OpenGL#setPaused(boolean)
*/
@Override
public void setPaused(final boolean paused) {
if (paused) {
animator.pause();
} else {
animator.resume();
}
}
/**
* Method selectAgents()
*
* @see msi.gama.common.interfaces.IDisplaySurface.OpenGL#selectAgents(msi.gama.metamodel.agent.IAgent)
*/
@Override
public void selectAgent(final DrawingAttributes attributes) {
IAgent ag = null;
boolean withHighlight = true;
if (attributes != null) {
if (attributes.getSpeciesName() != null) {
// The picked image is a grid or an image of a grid
withHighlight = false;
final GamaPoint pickedPoint = renderer
.getRealWorldPointFromWindowPoint(renderer.getCameraHelper().getLastMousePressedPosition());
ag = scope.getRoot().getPopulationFor(attributes.getSpeciesName()).getAgent(scope,
new GamaPoint(pickedPoint.x, -pickedPoint.y));
} else {
ag = attributes.getAgentIdentifier();
}
}
/** The cleanup. */
Runnable cleanup = ag != null ? () -> { renderer.getPickingHelper().setPicking(false); } : () -> {
renderer.getPickingHelper().setPicking(false);
// Necessary to avoir situations like issue #3232. The result is however a bit of flickering
getManager().forceRedrawingLayers();
updateDisplay(true);
};
if (withHighlight) {
menuManager.buildMenu((int) renderer.getCameraHelper().getMousePosition().x,
(int) renderer.getCameraHelper().getMousePosition().y, ag, cleanup,
AgentsMenu.getHighlightActionFor(ag));
} else {
menuManager.buildMenu((int) renderer.getCameraHelper().getMousePosition().x,
(int) renderer.getCameraHelper().getMousePosition().y, ag, cleanup);
}
}
/**
* Method selectSeveralAgents()
*
* @see msi.gama.common.interfaces.IDisplaySurface.OpenGL#selectSeveralAgents(java.util.Collection, int)
*/
@Override
public void selectionIn(final Envelope3D env) {
final Envelope3D envInWorld = Envelope3D.withYNegated(env);
final Collection<IAgent> agents = scope.getTopology().getSpatialIndex().allInEnvelope(scope,
envInWorld.centre(), envInWorld, new Different(), false);
final Map<String, Runnable> actions = new LinkedHashMap<>();
final Map<String, Image> images = new HashMap<>();
images.put(renderer.getOpenGLHelper().isStickyROI() ? "Hide region" : "Keep region visible",
GamaIcons.create(IGamaIcons.MENU_FOLLOW).image());
images.put("Focus on region", GamaIcons.create(IGamaIcons.DISPLAY_TOOLBAR_ZOOMFIT).image());
actions.put(renderer.getOpenGLHelper().isStickyROI() ? "Hide region" : "Keep region visible",
() -> renderer.getOpenGLHelper().toogleROI());
actions.put("Focus on region", () -> renderer.getCameraHelper().zoomFocus(env));
WorkbenchHelper.run(() -> {
final Menu menu = menuManager.buildROIMenu((int) renderer.getCameraHelper().getMousePosition().x,
(int) renderer.getCameraHelper().getMousePosition().y, agents, actions, images);
menu.addMenuListener(new MenuListener() {
@Override
public void menuHidden(final MenuEvent e) {
animator.resume();
// Will be run after the selection
WorkbenchHelper.asyncRun(() -> renderer.getOpenGLHelper().cancelROI());
}
@Override
public void menuShown(final MenuEvent e) {
animator.pause();
}
});
menu.setVisible(true);
});
}
/**
* Sets the display scope.
*
* @param scope
* the new display scope
*/
protected void setDisplayScope(final IGraphicsScope scope) {
if (this.scope != null) { GAMA.releaseScope(this.scope); }
this.scope = scope;
}
@Override
public void dispose() {
if (disposed) return;
disposed = true;
if (layerManager != null) { layerManager.dispose(); }
if (animator != null && animator.isStarted()) { animator.stop(); }
this.menuManager = null;
this.listeners.clear();
this.renderer = null;
GAMA.releaseScope(getScope());
setDisplayScope(null);
if (synchronizer != null) { synchronizer.signalRenderingIsFinished(); }
}
@Override
public LayeredDisplayData getData() { return output.getData(); }
/**
* Method changed()
*
* @see msi.gama.outputs.LayeredDisplayData.DisplayDataListener#changed(int, boolean)
*/
@Override
public void changed(final Changes property, final Object value) {
if (renderer == null) return;
switch (property) {
case CHANGE_CAMERA:
renderer.getCameraHelper().setupCamera();
break;
case SPLIT_LAYER:
final double gap = (Double) value;
// if (DEBUG.IS_ON()) {
// DEBUG.OUT("Value received by SWTOpenGLDisplaySurface= " + value);
// }
double currentElevation = 0;
for (final ILayer layer : this.getManager().getItems()) {
layer.getData().addElevation(currentElevation);
currentElevation += gap;
}
renderer.getSceneHelper().layerOffsetChanged();
break;
case CAMERA_POS:
renderer.getCameraHelper().updatePosition();
break;
case CAMERA_ORIENTATION:
renderer.getCameraHelper().updateOrientation();
break;
case CAMERA_TARGET:
renderer.getCameraHelper().updateTarget();
break;
case CAMERA_PRESET:
renderer.getCameraHelper().applyPreset((String) value);
break;
case ZOOM:
renderer.getCameraHelper().zoom((Double) value);
break;
default:
break;
}
}
/**
* Method setSize()
*
* @see msi.gama.common.interfaces.IDisplaySurface#setSize(int, int)
*/
@Override
public void setSize(final int x, final int y) {}
@Override
public void layersChanged() {
renderer.getSceneHelper().layersChanged();
}
/**
* Invalidate visible regions.
*/
public void invalidateVisibleRegions() {
for (final ILayer layer : layerManager.getItems()) { layer.getData().setVisibleRegion(null); }
}
/**
* Method getFPS()
*
* @see msi.gama.common.interfaces.IDisplaySurface#getFPS()
*/
@Override
public int getFPS() { return Math.round(this.animator.getLastFPS()); }
// @Override
// public boolean isRealized() {
// if (renderer == null) return false;
// final GLAutoDrawable d = renderer.getCanvas();
// if (d == null) return false;
// return d.isRealized();
// }
@Override
public boolean isRendered() {
if (renderer == null || renderer.getSceneHelper().getSceneToRender() == null) return false;
return renderer.getSceneHelper().getSceneToRender().rendered();
}
@Override
public boolean isDisposed() { return disposed; }
@Override
public Envelope3D getROIDimensions() { return renderer.getOpenGLHelper().getROIEnvelope(); }
@Override
public void dispatchKeyEvent(final char e) {
for (final IEventLayerListener gl : listeners) { gl.keyPressed(String.valueOf(e)); }
}
@Override
public void dispatchMouseEvent(final int swtMouseEvent, final int x, final int y) {
for (final IEventLayerListener gl : listeners) {
switch (swtMouseEvent) {
case SWT.MouseDown:
gl.mouseDown(x, y, 1);
break;
case SWT.MouseUp:
gl.mouseUp(x, y, 1);
break;
case SWT.MouseMove:
gl.mouseMove(x, y);
break;
case SWT.MouseEnter:
gl.mouseEnter(x, y);
break;
case SWT.MouseExit:
gl.mouseExit(x, y);
break;
case SWT.MenuDetect:
gl.mouseMenu(x, y);
break;
}
}
}
@Override
public void setMousePosition(final int x, final int y) {
// Nothing to do (taken in charge by the camera)
}
@Override
public void selectAgentsAroundMouse() {
// Nothing to do (taken in charge by the camera)
}
@Override
public void draggedTo(final int x, final int y) {
// Nothing to do (taken in charge by the camera
}
@Override
public void setDisplaySynchronizer(final IDisplaySynchronizer s) {
synchronizer = s;
synchronizer.signalSurfaceIsRealized();
}
}