Project

General

Profile

« Previous | Next » 

Revision 9694d2d5

Added by Leszek Koltunski over 2 years ago

Bandaged: add support for two-fingered rotation in the Creator.

View differences:

src/main/java/org/distorted/bandaged/BandagedCreatorView.java
35 35

  
36 36
public class BandagedCreatorView extends GLSurfaceView
37 37
{
38
    private final static int DIRECTION_SENSITIVITY=  12;
38
    private final static float DIRECTION_SENSITIVITY= 12.0f;
39
    public static final int INVALID_POINTER_ID = -1;
39 40

  
40 41
    private BandagedCreatorRenderer mRenderer;
41 42
    private BandagedCreatorTouchControl mTouchControl;
42 43
    private int mScreenWidth, mScreenHeight, mScreenMin;
43 44
    private int mTouchedIndex1, mTouchedIndex2;
44
    private int mX, mY;
45
    private float mX1, mY1, mX2, mY2, mX, mY;
46
    private int mPointer1, mPointer2;
47
    private float mRotAngle, mInitDistance;
45 48

  
46 49
///////////////////////////////////////////////////////////////////////////////////////////////////
47 50
// PUBLIC API
......
133 136
      mScreenMin   = Math.min(width, height);
134 137
      }
135 138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
    private float getAngle(float x1, float y1, float x2, float y2)
142
      {
143
      return (float) Math.atan2(y1-y2, x1-x2);
144
      }
145

  
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

  
148
    private void actionDown(MotionEvent event)
149
      {
150
      mPointer1 = event.getPointerId(0);
151
      mX1 = event.getX();
152
      mY1 = event.getY();
153
      mPointer2 = INVALID_POINTER_ID;
154

  
155
      float x1 = (mX1 -mScreenWidth*0.5f)/mScreenMin;
156
      float y1 = (mScreenHeight*0.5f-mY1)/mScreenMin;
157

  
158
      int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
159

  
160
      if( index<0 )
161
        {
162
        mX = mX1;
163
        mY = mY1;
164
        }
165
      else
166
        {
167
        mX = -1;
168
        mY = -1;
169

  
170
        if( mTouchedIndex1<0 )
171
          {
172
          mTouchedIndex1 = index;
173
          mRenderer.touchCubit(mTouchedIndex1);
174
          }
175
        else
176
          {
177
          mTouchedIndex2 = index;
178

  
179
          if( mTouchedIndex1 != index )
180
            {
181
            mRenderer.touchCubit(mTouchedIndex2);
182
            }
183
          }
184
        }
185
      }
186

  
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

  
189
    private void actionMove(MotionEvent event)
190
      {
191
      int index1 = event.findPointerIndex(mPointer1);
192

  
193
      if( index1>=0 )
194
        {
195
        mX1 = event.getX(index1);
196
        mY1 = event.getY(index1);
197
        }
198

  
199
      int index2 = event.findPointerIndex(mPointer2);
200

  
201
      if( index2>=0 )
202
        {
203
        mX2 = event.getX(index2);
204
        mY2 = event.getY(index2);
205
        }
206

  
207
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
208
        {
209
        float angleNow = getAngle(mX1,mY1,mX2,mY2);
210
        float angleDiff = angleNow-mRotAngle;
211
        float sinA = (float)Math.sin(angleDiff);
212
        float cosA = (float)Math.cos(angleDiff);
213
        mRenderer.setQuatTemp(0,0,sinA,cosA);
214
        mRenderer.resetQuats();
215
        mRotAngle = angleNow;
216

  
217
/*
218
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
219
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
220
        mInitDistance = distNow;
221
        TwistyObject object = mPreRender.getObject();
222
        if( object!=null ) object.setObjectRatio(distQuot, mObjectNode.getMinSize() );
223
 */
224
        }
225
      else
226
        {
227
        float x = event.getX();
228
        float y = event.getY();
229

  
230
        if( mX>=0 && mY>= 0 )
231
          {
232
          float px = mY-y;
233
          float py = mX-x;
234
          float pz = 0;
235
          float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
236

  
237
          if( plen>0 )
238
            {
239
            px /= plen;
240
            py /= plen;
241
            pz /= plen;
242

  
243
            float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
244
            float sinA = (float)Math.sqrt(1-cosA*cosA);
245

  
246
            mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
247
            }
248
          }
249
        if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
250
          {
251
          mX = x;
252
          mY = y;
253
          mRenderer.resetQuats();
254
          }
255
        }
256
      }
257

  
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

  
260
    private void actionUp()
261
      {
262
      mPointer1 = INVALID_POINTER_ID;
263
      mPointer2 = INVALID_POINTER_ID;
264

  
265
      if( mTouchedIndex2>=0 )
266
        {
267
        mRenderer.untouchCubit(mTouchedIndex1);
268
        mRenderer.untouchCubit(mTouchedIndex2);
269
        mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
270
        mTouchedIndex1 = -1;
271
        mTouchedIndex2 = -1;
272
        }
273

  
274
      mX = -1;
275
      mY = -1;
276

  
277
      mRenderer.resetQuats();
278
      }
279

  
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

  
282
    private void actionPointerDown(MotionEvent event)
283
      {
284
      int index = event.getActionIndex();
285

  
286
      if( mPointer1==INVALID_POINTER_ID )
287
        {
288
        mPointer1 = event.getPointerId(index);
289
        mX1 = event.getX(index);
290
        mY1 = event.getY(index);
291
        }
292
      else if( mPointer2==INVALID_POINTER_ID )
293
        {
294
        mPointer2 = event.getPointerId(index);
295
        mX2 = event.getX(index);
296
        mY2 = event.getY(index);
297
        }
298

  
299
      mRotAngle = getAngle(mX1,mY1,mX2,mY2);
300
      mInitDistance = -1;
301
      mRenderer.resetQuats();
302
      }
303

  
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

  
306
    private void actionPointerUp(MotionEvent event)
307
      {
308
      int index = event.getActionIndex();
309

  
310
      if( index==event.findPointerIndex(mPointer1) )
311
        {
312
        mPointer1 = INVALID_POINTER_ID;
313
        mX = mX2;
314
        mY = mY2;
315
        }
316
      else if( index==event.findPointerIndex(mPointer2) )
317
        {
318
        mPointer2 = INVALID_POINTER_ID;
319
        mX = mX1;
320
        mY = mY1;
321
        }
322

  
323
      mRenderer.resetQuats();
324
      }
325

  
136 326
///////////////////////////////////////////////////////////////////////////////////////////////////
137 327

  
138 328
    @Override
......
140 330
      {
141 331
      if( mRenderer.isBusy() ) return true;
142 332

  
143
      int action = event.getAction();
144
      int x = (int)event.getX();
145
      int y = (int)event.getY();
333
      int action = event.getActionMasked();
146 334

  
147 335
      switch(action)
148 336
         {
149
         case MotionEvent.ACTION_DOWN: float x1 = (x -mScreenWidth*0.5f)/mScreenMin;
150
                                       float y1 = (mScreenHeight*0.5f-y)/mScreenMin;
151

  
152
                                       int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
153

  
154
                                       if( index<0 )
155
                                         {
156
                                         mX = x;
157
                                         mY = y;
158
                                         }
159
                                       else
160
                                         {
161
                                         mX = -1;
162
                                         mY = -1;
163

  
164
                                         if( mTouchedIndex1<0 )
165
                                           {
166
                                           mTouchedIndex1 = index;
167
                                           mRenderer.touchCubit(mTouchedIndex1);
168
                                           }
169
                                         else
170
                                           {
171
                                           mTouchedIndex2 = index;
172

  
173
                                           if( mTouchedIndex1 != index )
174
                                             {
175
                                             mRenderer.touchCubit(mTouchedIndex2);
176
                                             }
177
                                           }
178
                                         }
179

  
180
                                       break;
181

  
182
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
183
                                         {
184
                                         float px = mY-y;
185
                                         float py = mX-x;
186
                                         float pz = 0;
187
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
188

  
189
                                         if( plen>0 )
190
                                           {
191
                                           px /= plen;
192
                                           py /= plen;
193
                                           pz /= plen;
194

  
195
                                           float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
196
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
197

  
198
                                           mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
199
                                           }
200
                                         }
201
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
202
                                         {
203
                                         mX = x;
204
                                         mY = y;
205
                                         mRenderer.resetQuats();
206
                                         }
207
                                       break;
208

  
209
         case MotionEvent.ACTION_UP  : if( mTouchedIndex2>=0 )
210
                                         {
211
                                         mRenderer.untouchCubit(mTouchedIndex1);
212
                                         mRenderer.untouchCubit(mTouchedIndex2);
213
                                         mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
214
                                         mTouchedIndex1 = -1;
215
                                         mTouchedIndex2 = -1;
216
                                         }
217

  
218
                                       mX = -1;
219
                                       mY = -1;
220

  
221
        	                             mRenderer.resetQuats();
222
                                       break;
337
         case MotionEvent.ACTION_DOWN        : actionDown(event)       ; break;
338
         case MotionEvent.ACTION_MOVE        : actionMove(event)       ; break;
339
         case MotionEvent.ACTION_UP          : actionUp()              ; break;
340
         case MotionEvent.ACTION_POINTER_DOWN: actionPointerDown(event); break;
341
         case MotionEvent.ACTION_POINTER_UP  : actionPointerUp  (event); break;
223 342
         }
224 343

  
225 344
      return true;

Also available in: Unified diff