VisualizationView.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;
00018 
00019 import com.google.common.collect.Lists;
00020 
00021 import android.content.Context;
00022 import android.graphics.PixelFormat;
00023 import android.opengl.GLSurfaceView;
00024 import android.util.AttributeSet;
00025 import android.view.MotionEvent;
00026 import org.ros.android.view.visualization.layer.Layer;
00027 import org.ros.exception.RosRuntimeException;
00028 import org.ros.message.MessageListener;
00029 import org.ros.namespace.GraphName;
00030 import org.ros.node.ConnectedNode;
00031 import org.ros.node.Node;
00032 import org.ros.node.NodeMain;
00033 import org.ros.node.topic.Subscriber;
00034 import org.ros.rosjava_geometry.FrameTransformTree;
00035 
00036 import java.util.List;
00037 import java.util.concurrent.CountDownLatch;
00038 
00043 public class VisualizationView extends GLSurfaceView implements NodeMain {
00044 
00045   private static final boolean DEBUG = false;
00046 
00047   private final FrameTransformTree frameTransformTree = new FrameTransformTree();
00048   private final Camera camera = new Camera(frameTransformTree);
00049   private final XYOrthographicRenderer renderer = new XYOrthographicRenderer(camera);
00050   private final List<Layer> layers = Lists.newArrayList();
00051   private final CountDownLatch attachedToWindow = new CountDownLatch(1);
00052 
00053   private ConnectedNode connectedNode;
00054 
00055   public VisualizationView(Context context) {
00056     super(context);
00057     init();
00058   }
00059 
00060   public VisualizationView(Context context, AttributeSet attrs) {
00061     super(context, attrs);
00062     init();
00063   }
00064 
00065   private void init() {
00066     if (DEBUG) {
00067       // Turn on OpenGL error-checking and logging.
00068       setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
00069     }
00070     setEGLConfigChooser(8, 8, 8, 8, 0, 0);
00071     getHolder().setFormat(PixelFormat.TRANSLUCENT);
00072     setRenderer(renderer);
00073   }
00074 
00075   @Override
00076   public GraphName getDefaultNodeName() {
00077     return GraphName.of("android_honeycomb_mr2/visualization_view");
00078   }
00079 
00080   @Override
00081   public boolean onTouchEvent(MotionEvent event) {
00082     for (Layer layer : Lists.reverse(layers)) {
00083       if (layer.onTouchEvent(this, event)) {
00084         return true;
00085       }
00086     }
00087     return false;
00088   }
00089 
00090   public XYOrthographicRenderer getRenderer() {
00091     return renderer;
00092   }
00093 
00094   public Camera getCamera() {
00095     return camera;
00096   }
00097 
00105   public void addLayer(Layer layer) {
00106     layers.add(layer);
00107   }
00108 
00109   public void removeLayer(Layer layer) {
00110     layer.onShutdown(this, connectedNode);
00111     layers.remove(layer);
00112   }
00113 
00114   public void hideLayer(Layer layer) {
00115     layers.remove(layer);
00116   }
00117 
00118   @Override
00119   public void onStart(ConnectedNode connectedNode) {
00120     this.connectedNode = connectedNode;
00121     startTransformListener();
00122     try {
00123       attachedToWindow.await();
00124     } catch (InterruptedException e) {
00125       throw new RosRuntimeException(e);
00126     }
00127     // startLayers() must be called after we've attached to the window in order
00128     // to ensure that getHandler() will not return null.
00129     startLayers();
00130   }
00131 
00132   @Override
00133   protected void onAttachedToWindow() {
00134     super.onAttachedToWindow();
00135     attachedToWindow.countDown();
00136   }
00137 
00138   private void startTransformListener() {
00139     Subscriber<tf2_msgs.TFMessage> tfSubscriber = connectedNode.newSubscriber("tf", tf2_msgs.TFMessage._TYPE); // tf.tfMessage
00140     tfSubscriber.addMessageListener(new MessageListener<tf2_msgs.TFMessage>() {
00141       @Override
00142       public void onNewMessage(tf2_msgs.TFMessage message) {
00143         for (geometry_msgs.TransformStamped transform : message.getTransforms()) {
00144           frameTransformTree.update(transform);
00145         }
00146       }
00147     });
00148   }
00149 
00150   private void startLayers() {
00151     for (Layer layer : layers) {
00152       layer.onStart(connectedNode, getHandler(), frameTransformTree, camera);
00153     }
00154     renderer.setLayers(layers);
00155   }
00156 
00157   @Override
00158   public void onShutdown(Node node) {
00159     renderer.setLayers(null);
00160     for (Layer layer : layers) {
00161       layer.onShutdown(this, node);
00162     }
00163     this.connectedNode = null;
00164   }
00165 
00166   @Override
00167   public void onShutdownComplete(Node node) {
00168   }
00169 
00170   @Override
00171   public void onError(Node node, Throwable throwable) {
00172   }
00173 }


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