summaryrefslogtreecommitdiffstats
path: root/src/demos/nurbs/curveapp/CurveMouseListener.java
blob: b74e1cfe3dc8e6ce1b158cba67f1053c68b5a5f6 (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
package demos.nurbs.curveapp;

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

/**
 * Class reacting to mouse events (implements interface for button press, drag and movement)
 * 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 CurveMouseListener implements MouseListener, MouseMotionListener {
  /**
   * Actually selected control point index
   * Index aktuálně vybraného řídícího bodu
   */
  private int bodIndex;

  /**
   * Window listener is connected to
   * Okno k nemuž listener patří
   */
  private CurveApp appWindow;

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

  /**
   * Pixel tolerance when selecting control point by clicking
   * Tolerance pro indikaci kliku na řídící bod
   */
  private static final int TOLERANCE=10;

  /**
   * Creates new listener with connection to given app window
   * Vytvoří listener s odkazem na zadané okno
   * @param app rodičovské okno
   */
  public CurveMouseListener(CurveApp app) {
    this.appWindow=app;
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
   */
  public void mouseClicked(MouseEvent e) {
    if(actionType==CurveApp.PRIDAT_AC){
      Curve.getInstance().setIsCurveFinished(false);
      float x=e.getX();
      float y=e.getY();
      float z=0;
      float w=1;
      int size;
      float[] newCtrls;
      try{
        size=Curve.getInstance().getCtrlPoints().length;
      }catch (Exception ex) {
        size=0;
      }
      newCtrls=new float[size+4];
      System.arraycopy(Curve.getInstance().getCtrlPoints(),0,newCtrls,0,size);
			
      newCtrls[size]=x;
      newCtrls[size+1]=y;
      newCtrls[size+2]=z;
      newCtrls[size+3]=w;
      Curve.getInstance().setCtrlPoints(newCtrls);
    }else if(actionType==CurveApp.SMAZAT_AC&&bodIndex>=0){
      Curve.getInstance().setIsCurveFinished(false);
      int size=Curve.getInstance().getCtrlPoints().length;
      float[] newCtrls=new float[size-4];
			
      int firstPartSize=(bodIndex)*4;
      int secondPartSize=newCtrls.length-firstPartSize;
      System.arraycopy(Curve.getInstance().getCtrlPoints(),0,newCtrls,0,firstPartSize);
      System.arraycopy(Curve.getInstance().getCtrlPoints(),firstPartSize+4,newCtrls,firstPartSize,secondPartSize);
      bodIndex=-1;
      Curve.getInstance().setBodIndex(bodIndex);
      Curve.getInstance().setCtrlPoints(newCtrls);
    }
    appWindow.updateGLCanvas();
  }

  /* (non-Javadoc)
   * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
   */
  public void mousePressed(MouseEvent e) {
    //		if(actionType==MOVE_AC){
    float[] ctrlpoints=Curve.getInstance().getCtrlPoints();
    int x=e.getX();
    int y=e.getY();
    this.bodIndex=-1;
    //			System.out.println(ctrlpoints.length);
    for(int i=0;i<ctrlpoints.length/4;i++){
      float xS = ctrlpoints[i*4]/ctrlpoints[i*4+3];
      float yS = ctrlpoints[i*4+1]/ctrlpoints[i*4+3];
      if(x>=xS-TOLERANCE&&x<=xS+TOLERANCE&&y>=yS-TOLERANCE&&y<=yS+TOLERANCE){
        this.bodIndex=i;
      }                    
    }
			
    Curve.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();
			
      Curve.getInstance().setActiveX(x);
      Curve.getInstance().setActiveY(y);
    }
    appWindow.updateGLCanvas();
  }

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

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

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

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