CameraControlLayer.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.view.visualization.layer;
00018 
00019 import android.content.Context;
00020 import android.os.Handler;
00021 import android.view.GestureDetector;
00022 import android.view.MotionEvent;
00023 import android.view.ScaleGestureDetector;
00024 import org.ros.android.view.visualization.Camera;
00025 import org.ros.android.view.visualization.RotateGestureDetector;
00026 import org.ros.android.view.visualization.VisualizationView;
00027 import org.ros.concurrent.ListenerGroup;
00028 import org.ros.concurrent.SignalRunnable;
00029 import org.ros.node.ConnectedNode;
00030 import org.ros.rosjava_geometry.FrameTransformTree;
00031 
00032 import java.util.concurrent.ExecutorService;
00033 
00040 public class CameraControlLayer extends DefaultLayer {
00041 
00042   private final Context context;
00043   private final ListenerGroup<CameraControlListener> listeners;
00044 
00045   private GestureDetector translateGestureDetector;
00046   private RotateGestureDetector rotateGestureDetector;
00047   private ScaleGestureDetector zoomGestureDetector;
00048 
00059   public CameraControlLayer(Context context, ExecutorService executorService) {
00060     this.context = context;
00061     listeners = new ListenerGroup<CameraControlListener>(executorService);
00062   }
00063 
00064   public void addListener(CameraControlListener listener) {
00065     listeners.add(listener);
00066   }
00067 
00068   @Override
00069   public boolean onTouchEvent(VisualizationView view, MotionEvent event) {
00070     if (translateGestureDetector == null || rotateGestureDetector == null
00071         || zoomGestureDetector == null) {
00072       return false;
00073     }
00074     return translateGestureDetector.onTouchEvent(event)
00075         || rotateGestureDetector.onTouchEvent(event) || zoomGestureDetector.onTouchEvent(event);
00076   }
00077 
00078   @Override
00079   public void onStart(ConnectedNode connectedNode, Handler handler,
00080       FrameTransformTree frameTransformTree, final Camera camera) {
00081     handler.post(new Runnable() {
00082       @Override
00083       public void run() {
00084         translateGestureDetector =
00085             new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
00086               @Override
00087               public boolean onScroll(MotionEvent event1, MotionEvent event2,
00088                   final float distanceX, final float distanceY) {
00089                 camera.translate(-distanceX, distanceY);
00090                 listeners.signal(new SignalRunnable<CameraControlListener>() {
00091                   @Override
00092                   public void run(CameraControlListener listener) {
00093                     listener.onTranslate(-distanceX, distanceY);
00094                   }
00095                 });
00096                 return true;
00097               }
00098             });
00099         rotateGestureDetector =
00100             new RotateGestureDetector(new RotateGestureDetector.OnRotateGestureListener() {
00101               @Override
00102               public boolean onRotate(MotionEvent event1, MotionEvent event2,
00103                   final double deltaAngle) {
00104                 final double focusX = (event1.getX(0) + event1.getX(1)) / 2;
00105                 final double focusY = (event1.getY(0) + event1.getY(1)) / 2;
00106                 camera.rotate(focusX, focusY, deltaAngle);
00107                 listeners.signal(new SignalRunnable<CameraControlListener>() {
00108                   @Override
00109                   public void run(CameraControlListener listener) {
00110                     listener.onRotate(focusX, focusY, deltaAngle);
00111                   }
00112                 });
00113                 // Don't consume this event in order to allow the zoom gesture
00114                 // to also be detected.
00115                 return false;
00116               }
00117             });
00118         zoomGestureDetector =
00119             new ScaleGestureDetector(context,
00120                 new ScaleGestureDetector.SimpleOnScaleGestureListener() {
00121                   @Override
00122                   public boolean onScale(ScaleGestureDetector detector) {
00123                     if (!detector.isInProgress()) {
00124                       return false;
00125                     }
00126                     final float focusX = detector.getFocusX();
00127                     final float focusY = detector.getFocusY();
00128                     final float factor = detector.getScaleFactor();
00129                     camera.zoom(focusX, focusY, factor);
00130                     listeners.signal(new SignalRunnable<CameraControlListener>() {
00131                       @Override
00132                       public void run(CameraControlListener listener) {
00133                         listener.onZoom(focusX, focusY, factor);
00134                       }
00135                     });
00136                     return true;
00137                   }
00138                 });
00139       }
00140     });
00141   }
00142 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33