1 /**
2  * Copyright 2010 JogAmp Community. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice, this list of
8  *       conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *       of conditions and the following disclaimer in the documentation and/or other materials
12  *       provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  * or implied, of JogAmp Community.
27  */
28 package com.jogamp.opengl.test.junit.graph.demos.ui;
29 
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.io.StringWriter;
34 
35 import com.jogamp.opengl.FPSCounter;
36 import com.jogamp.opengl.GL;
37 import com.jogamp.opengl.GL2ES2;
38 import com.jogamp.opengl.GLAnimatorControl;
39 import com.jogamp.opengl.GLAutoDrawable;
40 import com.jogamp.opengl.GLEventListener;
41 import com.jogamp.opengl.GLException;
42 import com.jogamp.opengl.GLPipelineFactory;
43 import com.jogamp.opengl.GLRunnable;
44 
45 import com.jogamp.graph.curve.Region;
46 import com.jogamp.graph.curve.opengl.RegionRenderer;
47 import com.jogamp.newt.event.KeyEvent;
48 import com.jogamp.newt.event.KeyListener;
49 import com.jogamp.newt.event.MouseEvent;
50 import com.jogamp.newt.event.MouseListener;
51 import com.jogamp.newt.opengl.GLWindow;
52 import com.jogamp.opengl.util.GLReadBufferUtil;
53 
54 /**
55  *
56  * Action Keys:
57  * - 1/2: zoom in/out
58  * - 4/5: increase/decrease shape/text spacing
59  * - 6/7: increase/decrease corner size
60  * - 0/9: rotate
61  * - v: toggle v-sync
62  * - s: screenshot
63  */
64 public abstract class UIListenerBase01 implements GLEventListener {
65     private final GLReadBufferUtil screenshot;
66     private final int renderModes;
67     private final RegionRenderer rRenderer;
68     private final boolean debug;
69     private final boolean trace;
70 
71     protected LabelButton button;
72 
73     private KeyAction keyAction;
74     private MouseAction mouseAction;
75 
76     private volatile GLAutoDrawable autoDrawable = null;
77 
78     private final float[] position = new float[] {0,0,0};
79 
80     private float xTran = -10;
81     private float yTran =  10;
82     private float ang = 0f;
83     private float zoom = -70f;
84 
85     boolean ignoreInput = false;
86 
UIListenerBase01(final int renderModes, final RegionRenderer rRenderer, final boolean debug, final boolean trace)87     public UIListenerBase01(final int renderModes, final RegionRenderer rRenderer, final boolean debug, final boolean trace) {
88         this.renderModes = renderModes;
89         this.rRenderer = rRenderer;
90         this.debug = debug;
91         this.trace = trace;
92         this.screenshot = new GLReadBufferUtil(false, false);
93     }
94 
getRegionRenderer()95     public final RegionRenderer getRegionRenderer() { return rRenderer; }
getZoom()96     public final float getZoom() { return zoom; }
getXTran()97     public final float getXTran() { return xTran; }
getYTran()98     public final float getYTran() { return yTran; }
getAngle()99     public final float getAngle() { return ang; }
getPosition()100     public final float[] getPosition() { return position; }
101 
setMatrix(final float xtrans, final float ytrans, final float angle, final int zoom)102     public void setMatrix(final float xtrans, final float ytrans, final float angle, final int zoom) {
103         this.xTran = xtrans;
104         this.yTran = ytrans;
105         this.ang = angle;
106         this.zoom = zoom;
107     }
108 
init(final GLAutoDrawable drawable)109     public void init(final GLAutoDrawable drawable) {
110         autoDrawable = drawable;
111         GL2ES2 gl = drawable.getGL().getGL2ES2();
112         if(debug) {
113             gl = gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", null, gl, null) ).getGL2ES2();
114         }
115         if(trace) {
116             gl = gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", null, gl, new Object[] { System.err } ) ).getGL2ES2();
117         }
118         gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
119         getRegionRenderer().init(gl, renderModes);
120     }
121 
reshape(final GLAutoDrawable drawable, final int xstart, final int ystart, final int width, final int height)122     public void reshape(final GLAutoDrawable drawable, final int xstart, final int ystart, final int width, final int height) {
123         final GL2ES2 gl = drawable.getGL().getGL2ES2();
124 
125         gl.glViewport(xstart, ystart, width, height);
126         rRenderer.reshapePerspective(45.0f, width, height, 0.1f, 7000.0f);
127         dumpMatrix();
128     }
129 
dispose(final GLAutoDrawable drawable)130     public void dispose(final GLAutoDrawable drawable) {
131         autoDrawable = null;
132         final GL2ES2 gl = drawable.getGL().getGL2ES2();
133         screenshot.dispose(gl);
134         rRenderer.destroy(gl);
135     }
136 
zoom(final int v)137     public void zoom(final int v){
138         zoom += v;
139         dumpMatrix();
140     }
141 
move(final float x, final float y)142     public void move(final float x, final float y){
143         xTran += x;
144         yTran += y;
145         dumpMatrix();
146     }
rotate(final float delta)147     public void rotate(final float delta){
148         ang += delta;
149         ang %= 360.0f;
150         dumpMatrix();
151     }
152 
dumpMatrix()153     void dumpMatrix() {
154         System.err.println("Matrix: " + xTran + "/" + yTran + " x"+zoom + " @"+ang);
155     }
156 
157     /** Attach the input listener to the window */
attachInputListenerTo(final GLWindow window)158     public void attachInputListenerTo(final GLWindow window) {
159         if ( null == keyAction ) {
160             keyAction = new KeyAction();
161             window.addKeyListener(keyAction);
162         }
163         if ( null == mouseAction ) {
164             mouseAction = new MouseAction();
165             window.addMouseListener(mouseAction);
166         }
167     }
168 
detachFrom(final GLWindow window)169     public void detachFrom(final GLWindow window) {
170         if ( null == keyAction ) {
171             return;
172         }
173         if ( null == mouseAction ) {
174             return;
175         }
176         window.removeGLEventListener(this);
177         window.removeKeyListener(keyAction);
178         window.removeMouseListener(mouseAction);
179     }
180 
printScreen(final GLAutoDrawable drawable, final String dir, final String tech, final String objName, final boolean exportAlpha)181     public void printScreen(final GLAutoDrawable drawable, final String dir, final String tech, final String objName, final boolean exportAlpha) throws GLException, IOException {
182         final StringWriter sw = new StringWriter();
183         final PrintWriter pw = new PrintWriter(sw);
184         pw.printf("-%03dx%03d-Z%04d-T%04d-%s", drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), (int)Math.abs(zoom), 0, objName);
185 
186         final String filename = dir + tech + sw +".png";
187         if(screenshot.readPixels(drawable.getGL(), false)) {
188             screenshot.write(new File(filename));
189         }
190     }
191 
192     int screenshot_num = 0;
193 
setIgnoreInput(final boolean v)194     public void setIgnoreInput(final boolean v) {
195         ignoreInput = v;
196     }
getIgnoreInput()197     public boolean getIgnoreInput() {
198         return ignoreInput;
199     }
200 
201     public class MouseAction implements MouseListener{
202 
mouseClicked(final MouseEvent e)203         public void mouseClicked(final MouseEvent e) {
204 
205         }
206 
mouseEntered(final MouseEvent e)207         public void mouseEntered(final MouseEvent e) {
208         }
209 
mouseExited(final MouseEvent e)210         public void mouseExited(final MouseEvent e) {
211         }
212 
mousePressed(final MouseEvent e)213         public void mousePressed(final MouseEvent e) {
214             button.setLabelColor(0.8f,0.8f,0.8f);
215             button.setColor(0.1f, 0.1f, 0.1f, 1.0f);
216         }
217 
mouseReleased(final MouseEvent e)218         public void mouseReleased(final MouseEvent e) {
219             button.setLabelColor(1.0f,1.0f,1.0f);
220             button.setColor(0.6f,0.6f,0.6f, 1.0f);
221         }
222 
mouseMoved(final MouseEvent e)223         public void mouseMoved(final MouseEvent e) {
224             // TODO Auto-generated method stub
225 
226         }
227 
mouseDragged(final MouseEvent e)228         public void mouseDragged(final MouseEvent e) {
229             // TODO Auto-generated method stub
230 
231         }
232 
mouseWheelMoved(final MouseEvent e)233         public void mouseWheelMoved(final MouseEvent e) {
234             // TODO Auto-generated method stub
235 
236         }
237 
238     }
239 
240     public class KeyAction implements KeyListener {
keyPressed(final KeyEvent arg0)241         public void keyPressed(final KeyEvent arg0) {
242             if(ignoreInput) {
243                 return;
244             }
245 
246             if(arg0.getKeyCode() == KeyEvent.VK_1){
247                 zoom(10);
248             }
249             else if(arg0.getKeyCode() == KeyEvent.VK_2){
250                 zoom(-10);
251             }
252             else if(arg0.getKeyCode() == KeyEvent.VK_UP){
253                 move(0, -1);
254             }
255             else if(arg0.getKeyCode() == KeyEvent.VK_DOWN){
256                 move(0, 1);
257             }
258             else if(arg0.getKeyCode() == KeyEvent.VK_LEFT){
259                 move(1, 0);
260             }
261             else if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){
262                 move(-1, 0);
263             }
264             else if(arg0.getKeyCode() == KeyEvent.VK_4){
265                 button.setSpacing(button.getSpacingX()-0.01f, button.getSpacingY()-0.005f);
266                 System.err.println("Button Spacing: " + button.getSpacingX());
267             }
268             else if(arg0.getKeyCode() == KeyEvent.VK_5){
269                 button.setSpacing(button.getSpacingX()+0.01f, button.getSpacingY()+0.005f);
270                 System.err.println("Button Spacing: " + button.getSpacingX());
271             }
272             else if(arg0.getKeyCode() == KeyEvent.VK_6){
273                 button.setCorner(button.getCorner()-0.01f);
274                 System.err.println("Button Corner: " + button.getCorner());
275             }
276             else if(arg0.getKeyCode() == KeyEvent.VK_7){
277                 button.setCorner(button.getCorner()+0.01f);
278                 System.err.println("Button Corner: " + button.getCorner());
279             }
280             else if(arg0.getKeyCode() == KeyEvent.VK_0){
281                 rotate(1);
282             }
283             else if(arg0.getKeyCode() == KeyEvent.VK_9){
284                 rotate(-1);
285             }
286             else if(arg0.getKeyCode() == KeyEvent.VK_V) {
287                 if(null != autoDrawable) {
288                     autoDrawable.invoke(false, new GLRunnable() {
289                         @Override
290                         public boolean run(final GLAutoDrawable drawable) {
291                             final GL gl = drawable.getGL();
292                             final int _i = gl.getSwapInterval();
293                             final int i;
294                             switch(_i) {
295                                 case  0: i =  1; break;
296                                 case  1: i = -1; break;
297                                 case -1: i =  0; break;
298                                 default: i =  1; break;
299                             }
300                             gl.setSwapInterval(i);
301 
302                             final GLAnimatorControl a = drawable.getAnimator();
303                             if( null != a ) {
304                                 a.resetFPSCounter();
305                             }
306                             if(drawable instanceof FPSCounter) {
307                                 ((FPSCounter)drawable).resetFPSCounter();
308                             }
309                             System.err.println("Swap Interval: "+_i+" -> "+i+" -> "+gl.getSwapInterval());
310                             return true;
311                         }
312                     });
313                 }
314             }
315             else if(arg0.getKeyCode() == KeyEvent.VK_S){
316                 rotate(-1);
317                     if(null != autoDrawable) {
318                         autoDrawable.invoke(false, new GLRunnable() {
319                             public boolean run(final GLAutoDrawable drawable) {
320                                 try {
321                                     final String type = Region.getRenderModeString(renderModes);
322                                     printScreen(drawable, "./", "demo-"+type, "snap"+screenshot_num, false);
323                                     screenshot_num++;
324                                 } catch (final GLException e) {
325                                     e.printStackTrace();
326                                 } catch (final IOException e) {
327                                     e.printStackTrace();
328                                 }
329                                 return true;
330                             }
331                         });
332                     }
333             }
334         }
keyReleased(final KeyEvent arg0)335         public void keyReleased(final KeyEvent arg0) {}
336     }
337 }
338