World Wind Java Fenggui

From World Wind Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 06:08, 24 September 2011 (edit)
108.62.101.249 (Talk)

← Previous diff
Revision as of 13:17, 24 September 2011 (edit) (undo)
Mkpl (Talk | contribs)
m (Reverted edits by 108.62.101.249 (Talk); changed back to last version by Kjohnston)
Next diff →
Line 344: Line 344:
} }
</pre> </pre>
- 
-[http://africanmangofreetrial.org african mango reviews] 

Revision as of 13:17, 24 September 2011


FengGUI Layer in WorldWind

Here is the code for a simple FengGUI layer. You should be able to use any of the FengGUI widgets within it. I included a small sample creating a layer panel using FengGUI.

Screenshot:

Image:fenggui_worldwind.jpg

Sample code using the layer:

      	GUIBuilder builder = new FengGUILayer.GUIBuilder() {
	public void buildGUI(Display display)
	{
		Window w = new Window(false, false, false);
		w.setTitle("Layers");

		w.setXY(50, 50);

		w.getContentContainer().setLayoutManager(new RowLayout(false));

		for (final Layer layer : wwd.getModel().getLayers())
		{
			if (!layer.getName().equals("Renderable"))
			{
				CheckBox fcb = new CheckBox(layer.getName());
				fcb.setSelected(layer.isEnabled());
				fcb.addSelectionChangedListener(new ISelectionChangedListener() {
					public void selectionChanged(
							SelectionChangedEvent evt)
					{
						layer.setEnabled(evt.isSelected());
						wwd.redraw();
					}
				});
				w.getContentContainer().addWidget(fcb);
			}
		}

		w.pack();
		display.addWidget(w);
		display.layout();
	}
};

FengGUILayer:

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.event.InputHandler;
import gov.nasa.worldwind.render.DrawContext;
import gov.nasa.worldwind.render.OrderedRenderable;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;

import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.render.jogl.EventHelper;

public class FengGUILayer implements GLEventListener, OrderedRenderable
{
	private class OrderedIcon implements OrderedRenderable
	{
		// Set distance to -1 so FengGUI windows will appear in front
		// of the WorldWind compass
		public double getDistanceFromEye()
		{
			return -1;
		}

		public void pick(DrawContext dc, Point pickPoint)
		{
			// Not implemented
		}

		public void render(DrawContext dc)
		{
			FengGUILayer.this.display.display();
		}
	}

	// the canvas on which the OpenGL will draw his stuff. We keep
	// it as a field because we need the canvas to instantiate the
	// JOGL binding.
	private GLCanvas canvas = null;

	// The root of the Widget tree.
	private Display display = null;

	private WorldWindow wwd;

	private OrderedIcon orderedImage = new OrderedIcon();

	private GUIBuilder builder;

	public FengGUILayer(WorldWindow wwd, GUIBuilder builder)
	{
		this.wwd = wwd;
		this.canvas = (GLCanvas) wwd;
		this.builder = builder;

		canvas.addGLEventListener(this);
	}

	private void buildGUI()
	{
		display = FengGUI.createDisplay(new MyJOGLBinding(canvas));
		display.setDepthTestEnabled(true);

		new EventBinding(wwd.getInputHandler());

		builder.buildGUI(display);
	}

	public void display(GLAutoDrawable arg0)
	{
		// Display is done in the render method
	}

	public void render(DrawContext dc)
	{
		dc.addOrderedRenderable(this.orderedImage);
	}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width,
			int height)
	{
		// does nothing
	}

	/**
	 * JOGL callback method
	 */
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2)
	{
		// does nothing
	}

	/**
	 * JOGL callback method
	 */
	public void init(GLAutoDrawable drawable)
	{
		buildGUI();
	}

	public double getDistanceFromEye()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public void pick(DrawContext dc, Point pickPoint)
	{
		// TODO Auto-generated method stub

	}

	private class EventBinding implements KeyListener, MouseMotionListener,
			MouseListener, MouseWheelListener
	{
		private boolean mouseDragging = false;
		private boolean mousePressed = false;
		private boolean mouseReleased = false;

		/**
		 * Creates a new Binding.
		 * @param c the JOGLUI canvas
		 * @param d the FengGUI Display
		 */
		public EventBinding(InputHandler handler)
		{
			// makes FengGUI listen to tab keys
			// http://answers.google.com/answers/threadview?id=126916
			canvas.setFocusTraversalKeysEnabled(false);

			handler.addMouseListener(this);
			handler.addMouseMotionListener(this);
			handler.addMouseWheelListener(this);
			canvas.addKeyListener(this);
		}

		/**
		 * Forwards the key typed event to the Display.
		 * @param e the event
		 */
		public void keyTyped(KeyEvent e)
		{
			if (display.fireKeyTypedEvent(e.getKeyChar()))
			{
				e.consume();
			}
		}

		/**
		 * Forwards the key pressed event to the Display.
		 * @param e the event
		 */
		public void keyPressed(KeyEvent e)
		{
			if (display.fireKeyPressedEvent(e.getKeyChar(), EventHelper
					.getKeyPressed(e)))
			{
				e.consume();
			}
		}

		/**
		 * Forwards the key released event to the Display.
		 * @param e the event
		 */
		public void keyReleased(KeyEvent e)
		{
			if (display.fireKeyReleasedEvent(e.getKeyChar(), EventHelper
					.getKeyPressed(e)))
			{
				e.consume();
			}
		}

		/**
		 * Forwards the mouse dragged event to the Display.
		 * @param e the event
		 */
		public void mouseDragged(MouseEvent e)
		{
			if (display.fireMouseDraggedEvent(e.getX(), display.getHeight()
					- e.getY(), EventHelper.getMouseButton(e)))
			{
				mouseDragging = true;
			}
			if (mouseDragging)
			{
				e.consume();
			}
		}

		/**
		 * Forwards the mouse moved event to the Display.
		 * @param e the event
		 */
		public void mouseMoved(MouseEvent e)
		{
			if (display.fireMouseMovedEvent(e.getX(), display.getHeight()
					- e.getY()))
			{
				e.consume();
			}
		}

		/**
		 * Does nothing.
		 * @param arg0 the event
		 */
		public void mouseClicked(MouseEvent e)
		{
			if (mousePressed && mouseReleased)
			{
				e.consume();
			}
			mousePressed = false;
			mouseReleased = false;
		}

		/**
		 * Forwards the mouse pressed event to the Display.
		 * @param e the event
		 */
		public void mousePressed(MouseEvent e)
		{
			if (display.fireMousePressedEvent(e.getX(), display.getHeight()
					- e.getY(), EventHelper.getMouseButton(e), e
					.getClickCount()))
			{
				e.consume();
				mousePressed = true;
			}
		}

		/**
		 * Forwards the mouse released event to the Display.
		 * @param e the event
		 */
		public void mouseReleased(MouseEvent e)
		{
			if (display.fireMouseReleasedEvent(e.getX(), display.getHeight()
					- e.getY(), EventHelper.getMouseButton(e), e
					.getClickCount()))
			{
				e.consume();
				mouseReleased = true;
			}
			mouseDragging = false;
		}

		/**
		 * Does nothing. 
		 * @param e the event
		 */
		public void mouseEntered(MouseEvent e)
		{
		}

		/**
		 * Does nothing. 
		 * @param e the event
		 */
		public void mouseExited(MouseEvent e)
		{
		}

		/* (non-Javadoc)
		 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
		 */
		public void mouseWheelMoved(MouseWheelEvent e)
		{
			if (display.fireMouseWheel(e.getX(), e.getY(),
					e.getWheelRotation() < 0, Math.abs(e.getWheelRotation())))
			{
				e.consume();
			}
		}
	}

	public static interface GUIBuilder
	{
		public void buildGUI(Display display);
	}
}
Personal tools