summaryrefslogtreecommitdiffstats
path: root/src/demos/nurbs/surfaceapp/SurfaceMouseListener.java
blob: 5e8da06800ae216fb939c5628862bbf1af970f4f (plain)
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
package demos.nurbs.surfaceapp;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

import com.jogamp.opengl.GL;
import com.jogamp.opengl.glu.GLU;

/**
 * Třída zpracovávající události myši (implementuje rozhraní zpracovávající stisk tlačítek i pohyb a tažení myší)
 * @author Tomáš Hráský
 *
 */
public class SurfaceMouseListener implements MouseListener, MouseMotionListener {
  /**
   * Index aktuálně vybraného řídícího bodu
   */
  private int bodIndex;

  /**
   * Okno k nemuž liustener patří
   */
  private SurfaceApp appWindow;

  /**
   * Typ prováděné činnosti
   */
  private String actionType;

  /**
   * Tolerance pro indikaci kliku na řídící bod
   */
  private static final int TOLERANCE=10;

  /**
   * Vytvoří listener s odkazem na zadané okno
   * @param app rodičovské okno
   */
  public SurfaceMouseListener(SurfaceApp app) {
    this.appWindow=app;
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
   */
  public void mouseClicked(MouseEvent e) {
    if(actionType==SurfaceApp.PRIDAT_AC){
      //			Surface.getInstance().setIsSurfaceFinished(false);
      //			float x=e.getX();
      //			float y=e.getY();
      //			float z=0;
      //			float w=1;
      //			int size;
      //			float[] newCtrls;
      //			try{
      //				size=Surface.getInstance().getCtrlPoints().length;
      //			}catch (Exception ex) {
      //				size=0;
      //			}
      //			newCtrls=new float[size+4];
      //			System.arraycopy(Surface.getInstance().getCtrlPoints(),0,newCtrls,0,size);
      //			
      //			newCtrls[size]=x;
      //			newCtrls[size+1]=y;
      //			newCtrls[size+2]=z;
      //			newCtrls[size+3]=w;
      //			Surface.getInstance().setCtrlPoints(newCtrls);
    }else if(actionType==SurfaceApp.SMAZAT_AC&&bodIndex>=0){
      //			Surface.getInstance().setIsSurfaceFinished(false);
      //			int size=Surface.getInstance().getCtrlPoints().length;
      //			float[] newCtrls=new float[size-4];
      //			
      //			int firstPartSize=(bodIndex)*4;
      //			int secondPartSize=newCtrls.length-firstPartSize;
      //			System.arraycopy(Surface.getInstance().getCtrlPoints(),0,newCtrls,0,firstPartSize);
      //			System.arraycopy(Surface.getInstance().getCtrlPoints(),firstPartSize+4,newCtrls,firstPartSize,secondPartSize);
      //			bodIndex=-1;
      //			Surface.getInstance().setBodIndex(bodIndex);
      //			Surface.getInstance().setCtrlPoints(newCtrls);
    }else if(actionType==SurfaceApp.SMAZAT_AC_RADEK&&bodIndex>=0){
			
      Vector<float[]> oldPoints=new Vector<float[]>();
      Surface srf=Surface.getInstance();
      for(int i=0;i<srf.getCtrlPoints().length;i+=4){
        float[] pole={srf.getCtrlPoints()[i],srf.getCtrlPoints()[i+1],srf.getCtrlPoints()[i+2],srf.getCtrlPoints()[i+3]};
        oldPoints.add(pole);
      }
			
      int index=bodIndex+1;
      while(!(index%srf.getPointsInV()==0))
        index--;
      //			index--;
      Vector<Integer> indexes=new Vector<Integer>();
      for(int i=index;i<index+srf.getPointsInV();i++)
        indexes.add(i);
      Vector<float[]> newOldPoints=new Vector<float[]>();
      for(int i=0;i<oldPoints.size();i++){
        if(!indexes.contains(Integer.valueOf(i)))
          newOldPoints.add(oldPoints.get(i));
      }
      //				oldPoints.remove(i);
      float[] newPoints=new float[newOldPoints.size()*4];
      int i=0;
      for(float[] f:newOldPoints){
        newPoints[i++]=f[0];
        newPoints[i++]=f[1];
        newPoints[i++]=f[2];
        newPoints[i++]=f[3];
      }
      srf.setIsSurfaceFinished(false);
      srf.setPointsInU(srf.getPointsInU()-1);
      bodIndex=-1;
      srf.setBodIndex(-1);
      srf.setCtrlPoints(newPoints);
			
    }else if(actionType==SurfaceApp.SMAZAT_AC_SLOUPEC&&bodIndex>=0){
      Vector<float[]> oldPoints=new Vector<float[]>();
      Surface srf=Surface.getInstance();
      for(int i=0;i<srf.getCtrlPoints().length;i+=4){
        float[] pole={srf.getCtrlPoints()[i],srf.getCtrlPoints()[i+1],srf.getCtrlPoints()[i+2],srf.getCtrlPoints()[i+3]};
        oldPoints.add(pole);
      }
			
      int index=bodIndex+1;
			
      Vector<Integer> indexes=new Vector<Integer>();
			
      for(int i=index;i>=0;i-=srf.getPointsInV()){
        indexes.add(i-1);
      }
      for(int i=index;i<srf.getCtrlPoints().length;i+=srf.getPointsInV()){
        indexes.add(i-1);
      }
			
      //			index--;
      //			for(int i=index;i<index+srf.getPointsInV();i++)
      //				
			
      Vector<float[]> newOldPoints=new Vector<float[]>();
      for(int i=0;i<oldPoints.size();i++){
        if(!indexes.contains(Integer.valueOf(i)))
          newOldPoints.add(oldPoints.get(i));
      }
      //				oldPoints.remove(i);
      float[] newPoints=new float[newOldPoints.size()*4];
      int i=0;
      for(float[] f:newOldPoints){
        newPoints[i++]=f[0];
        newPoints[i++]=f[1];
        newPoints[i++]=f[2];
        newPoints[i++]=f[3];
      }
      srf.setIsSurfaceFinished(false);
      srf.setPointsInV(srf.getPointsInV()-1);
      bodIndex=-1;
      srf.setBodIndex(-1);
      srf.setCtrlPoints(newPoints);

    }
		
    appWindow.updateGLCanvas();
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
   */
  public void mousePressed(MouseEvent e) {
    float[] ctrlpoints=Surface.getInstance().getCtrlPoints();
    int xE=e.getX();
    int yE=e.getY();
    //		System.out.println(xE+" "+yE);
    double x,y,z=0;
		
    this.bodIndex=-1;
		
    GL gl=appWindow.getGlCanvas().getGL();
    GLU glu=appWindow.getGlListener().getGlu();
		
    int[] viewport;
    double[] mvmatrix;
    double[] projmatrix;
    int realy;
    double[] wcoord=new double[4];

    viewport=appWindow.getGlListener().getViewport();
    mvmatrix=appWindow.getGlListener().getMvmatrix();
    projmatrix=appWindow.getGlListener().getProjmatrix();

		
    for(int i=0;i<ctrlpoints.length/4;i++){
      x = ctrlpoints[i*4]/ctrlpoints[i*4+3];
      y = ctrlpoints[i*4+1]/ctrlpoints[i*4+3];
      z=ctrlpoints[i*4+2]/ctrlpoints[i*4+3];			
      //projekce souřadnic do okna
      glu.gluProject(x,y,z,mvmatrix,0,projmatrix,0,viewport,0,wcoord,0);
			
      x=wcoord[0];
      y=(viewport[3]-wcoord[1]-1);
			
      if(xE>=x-TOLERANCE&&xE<=x+TOLERANCE&&yE>=y-TOLERANCE&&yE<=y+TOLERANCE){
        this.bodIndex=i;
      }                    
    }
		
    Surface.getInstance().setBodIndex(bodIndex);
    appWindow.updateGLCanvas();
		
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
   */
  public void mouseReleased(MouseEvent e) {
    //		this.bodIndex=-1;
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
   */
  public void mouseEntered(MouseEvent e) {

  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
   */
  public void mouseExited(MouseEvent e) {

  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
   */
  public void mouseDragged(MouseEvent e) {
    //		if(this.bodIndex>=0){
    //			int x=e.getX();
    //			int y=e.getY();
    //			
    //			Surface.getInstance().setActiveX(x);
    //			Surface.getInstance().setActiveY(y);
    //		}
    //		appWindow.updateGLCanvas();
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
   */
  public void mouseMoved(MouseEvent e) {
		
  }

  /**
   * Nastaví typ prováděné činnosti
   * @param action typ prováděné činnosti
   */
  public void setActionType(String action) {
    this.actionType=action;
  }

  /**
   * Vrací index aktuálně vybraného řídícího bodu
   * @return index aktuálně vybraného řídícího bodu
   */
  public int getBodIndex() {
    return bodIndex;
  }

  /**
   * Vrací index aktuálně vybraného řídícího bodu
   * @param bodIndex aktuálně vybraného řídícího bodu
   */
  public void setBodIndex(int bodIndex) {
    this.bodIndex = bodIndex;
  }
}