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 com.google.common.base.Preconditions;
00020 
00021 import android.support.v4.view.GestureDetectorCompat;
00022 import android.view.GestureDetector;
00023 import android.view.MotionEvent;
00024 import android.view.ScaleGestureDetector;
00025 
00026 import org.ros.android.view.visualization.RotateGestureDetector;
00027 import org.ros.android.view.visualization.VisualizationView;
00028 import org.ros.concurrent.ListenerGroup;
00029 import org.ros.concurrent.SignalRunnable;
00030 import org.ros.node.ConnectedNode;
00031 import org.ros.node.NodeMainExecutor;
00032 
00039 public class CameraControlLayer extends DefaultLayer {
00040 
00041   private ListenerGroup<CameraControlListener> listeners;
00042   private GestureDetectorCompat translateGestureDetector;
00043   private RotateGestureDetector rotateGestureDetector;
00044   private ScaleGestureDetector zoomGestureDetector;
00045 
00046   @Override
00047   public void init(NodeMainExecutor nodeMainExecutor) {
00048     listeners =
00049         new ListenerGroup<CameraControlListener>(nodeMainExecutor.getScheduledExecutorService());
00050   }
00051 
00052   public void addListener(CameraControlListener listener) {
00053     Preconditions.checkNotNull(listeners);
00054     listeners.add(listener);
00055   }
00056 
00057   @Override
00058   public boolean onTouchEvent(VisualizationView view, MotionEvent event) {
00059     if (translateGestureDetector == null || rotateGestureDetector == null
00060         || zoomGestureDetector == null) {
00061       return false;
00062     }
00063     final boolean translateGestureHandled = translateGestureDetector.onTouchEvent(event);
00064     final boolean rotateGestureHandled = rotateGestureDetector.onTouchEvent(event);
00065     final boolean zoomGestureHandled = zoomGestureDetector.onTouchEvent(event);
00066     return translateGestureHandled || rotateGestureHandled || zoomGestureHandled
00067         || super.onTouchEvent(view, event);
00068   }
00069 
00070   @Override
00071   public void onStart(final VisualizationView view, ConnectedNode connectedNode) {
00072     view.post(new Runnable() {
00073       @Override
00074       public void run() {
00075         translateGestureDetector =
00076             new GestureDetectorCompat(view.getContext(), new GestureDetector.SimpleOnGestureListener() {
00077               @Override
00078               public boolean onDown(MotionEvent e) {
00079                 // This must return true in order for onScroll() to trigger.
00080                 return true;
00081               }
00082 
00083               @Override
00084               public boolean onScroll(MotionEvent event1, MotionEvent event2,
00085                   final float distanceX, final float distanceY) {
00086                 view.getCamera().translate(-distanceX, distanceY);
00087                 listeners.signal(new SignalRunnable<CameraControlListener>() {
00088                   @Override
00089                   public void run(CameraControlListener listener) {
00090                     listener.onTranslate(-distanceX, distanceY);
00091                   }
00092                 });
00093                 return true;
00094               }
00095 
00096               @Override
00097               public boolean onDoubleTap (final MotionEvent e) {
00098                 listeners.signal(new SignalRunnable<CameraControlListener>() {
00099                   @Override
00100                   public void run(CameraControlListener listener) {
00101                     listener.onDoubleTap(e.getX(), e.getY());
00102                   }
00103                 });
00104                 return true;
00105               }
00106             });
00107         rotateGestureDetector =
00108             new RotateGestureDetector(new RotateGestureDetector.OnRotateGestureListener() {
00109               @Override
00110               public boolean onRotate(MotionEvent event1, MotionEvent event2,
00111                   final double deltaAngle) {
00112                 final float focusX = (event1.getX(0) + event1.getX(1)) / 2;
00113                 final float focusY = (event1.getY(0) + event1.getY(1)) / 2;
00114                 view.getCamera().rotate(focusX, focusY, deltaAngle);
00115                 listeners.signal(new SignalRunnable<CameraControlListener>() {
00116                   @Override
00117                   public void run(CameraControlListener listener) {
00118                     listener.onRotate(focusX, focusY, deltaAngle);
00119                   }
00120                 });
00121                 return true;
00122               }
00123             });
00124         zoomGestureDetector =
00125             new ScaleGestureDetector(view.getContext(),
00126                 new ScaleGestureDetector.SimpleOnScaleGestureListener() {
00127                   @Override
00128                   public boolean onScale(ScaleGestureDetector detector) {
00129                     if (!detector.isInProgress()) {
00130                       return false;
00131                     }
00132                     final float focusX = detector.getFocusX();
00133                     final float focusY = detector.getFocusY();
00134                     final float factor = detector.getScaleFactor();
00135                     view.getCamera().zoom(focusX, focusY, factor);
00136                     listeners.signal(new SignalRunnable<CameraControlListener>() {
00137                       @Override
00138                       public void run(CameraControlListener listener) {
00139                         listener.onZoom(focusX, focusY, factor);
00140                       }
00141                     });
00142                     return true;
00143                   }
00144                 });
00145       }
00146     });
00147   }
00148 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07