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.base.Preconditions;
00020 import com.google.common.collect.Lists;
00021 
00022 import android.app.Activity;
00023 import android.content.Context;
00024 import android.graphics.PixelFormat;
00025 import android.opengl.GLSurfaceView;
00026 import android.os.Bundle;
00027 import android.util.AttributeSet;
00028 import android.view.MotionEvent;
00029 
00030 import org.ros.android.RosActivity;
00031 import org.ros.android.view.visualization.layer.Layer;
00032 import org.ros.message.MessageListener;
00033 import org.ros.namespace.GraphName;
00034 import org.ros.node.ConnectedNode;
00035 import org.ros.node.Node;
00036 import org.ros.node.NodeMain;
00037 import org.ros.node.NodeMainExecutor;
00038 import org.ros.node.topic.Subscriber;
00039 import org.ros.rosjava_geometry.FrameTransformTree;
00040 
00041 import java.util.Collections;
00042 import java.util.List;
00043 
00048 public class VisualizationView extends GLSurfaceView implements NodeMain {
00049 
00050   private static final boolean DEBUG = false;
00051 
00052   private final Object mutex = new Object();
00053   private final FrameTransformTree frameTransformTree = new FrameTransformTree();
00054   private final XYOrthographicCamera camera = new XYOrthographicCamera(frameTransformTree);
00055 
00056   private List<Layer> layers;
00057   private XYOrthographicRenderer renderer;
00058   private ConnectedNode connectedNode;
00059 
00060   public VisualizationView(Context context) {
00061     super(context);
00062   }
00063 
00064   public VisualizationView(Context context, AttributeSet attrs) {
00065     super(context, attrs);
00066   }
00067 
00073   public void onCreate(List<Layer> layers) {
00074     this.layers = layers;
00075     setDebugFlags(DEBUG_CHECK_GL_ERROR);
00076     if (DEBUG) {
00077       // Turn on OpenGL logging.
00078       setDebugFlags(getDebugFlags() | DEBUG_LOG_GL_CALLS);
00079     }
00080     setEGLConfigChooser(8, 8, 8, 8, 0, 0);
00081     getHolder().setFormat(PixelFormat.TRANSLUCENT);
00082     renderer = new XYOrthographicRenderer(this);
00083     setRenderer(renderer);
00084   }
00085 
00091   public void init(NodeMainExecutor nodeMainExecutor) {
00092     Preconditions.checkNotNull(layers);
00093     for (Layer layer : layers) {
00094       layer.init(nodeMainExecutor);
00095     }
00096   }
00097 
00098   @Override
00099   public GraphName getDefaultNodeName() {
00100     return GraphName.of("android_15/visualization_view");
00101   }
00102 
00103   @Override
00104   public boolean onTouchEvent(MotionEvent event) {
00105     for (Layer layer : Lists.reverse(layers)) {
00106       if (layer.onTouchEvent(this, event)) {
00107         return true;
00108       }
00109     }
00110     return super.onTouchEvent(event);
00111   }
00112 
00113   public XYOrthographicRenderer getRenderer() {
00114     return renderer;
00115   }
00116 
00117   public XYOrthographicCamera getCamera() {
00118     return camera;
00119   }
00120 
00121   public FrameTransformTree getFrameTransformTree() {
00122     return frameTransformTree;
00123   }
00124 
00125   public List<Layer> getLayers() {
00126     return Collections.unmodifiableList(layers);
00127   }
00128 
00129   @Override
00130   public void onStart(ConnectedNode connectedNode) {
00131     this.connectedNode = connectedNode;
00132     startTransformListener();
00133     startLayers();
00134   }
00135 
00136   private void startTransformListener() {
00137     final Subscriber<tf2_msgs.TFMessage> tfSubscriber =
00138         connectedNode.newSubscriber("tf", tf2_msgs.TFMessage._TYPE);
00139     tfSubscriber.addMessageListener(new MessageListener<tf2_msgs.TFMessage>() {
00140       @Override
00141       public void onNewMessage(tf2_msgs.TFMessage message) {
00142         synchronized (mutex) {
00143           for (geometry_msgs.TransformStamped transform : message.getTransforms()) {
00144             frameTransformTree.update(transform);
00145           }
00146         }
00147       }
00148     });
00149     final Subscriber<tf2_msgs.TFMessage> tfStaticSubscriber =
00150         connectedNode.newSubscriber("tf_static", tf2_msgs.TFMessage._TYPE);
00151     tfStaticSubscriber.addMessageListener(new MessageListener<tf2_msgs.TFMessage>() {
00152       @Override
00153       public void onNewMessage(tf2_msgs.TFMessage message) {
00154         synchronized (mutex) {
00155           for (geometry_msgs.TransformStamped transform : message.getTransforms()) {
00156             frameTransformTree.update(transform);
00157           }
00158         }
00159       }
00160     });
00161   }
00162 
00163   private void startLayers() {
00164     for (Layer layer : layers) {
00165       layer.onStart(this, connectedNode);
00166     }
00167   }
00168 
00169   public void addLayer(Layer layer) {
00170         layers.add(layer);
00171   }
00172 
00173   @Override
00174   public void onShutdown(Node node) {
00175     for (Layer layer : layers) {
00176       layer.onShutdown(this, node);
00177     }
00178     this.connectedNode = null;
00179   }
00180 
00181   @Override
00182   public void onShutdownComplete(Node node) {
00183   }
00184 
00185   @Override
00186   public void onError(Node node, Throwable throwable) {
00187   }
00188 }


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