package j3deditor.bin.editor;

import j3deditor.bin.components.ButtonPanelLayout;
import j3deditor.bin.hierarchy.util.HierarchyEvent;
import j3deditor.bin.hierarchy.util.HierarchyEventListener;
import j3deditor.bin.components.SimpleButton;
import j3deditor.bin.components.SimpleButtonEvent;
import j3deditor.bin.components.SimpleButtonListener;

import j3deditor.bin.hierarchy.util.VirtualScene;

import j3deditor.bin.hierarchy.util.VirtualScene3D;

import j3deditor.bin.util.Utilities;

import javax.swing.JPanel;


/**
 * Contains buttons for moving the camera
 * of the specified scene.
 *
 * @author Risto Seene
 */
public class CameraPanel extends JPanel implements SimpleButtonListener{


	/** the scene the camera is located in */
	private VirtualScene scene;

	/** listener that listens hierarchy manipulation events */
	private HierarchyEventListener listener;


	/** button for rotating camera */
	private SimpleButton rotateCam;

	/** button for moving camera */
	private SimpleButton translateCam;

	/** button for zooming camera */
	private SimpleButton zoomCam;


	/** infopanel that shows active button info */
	private InfoPanel infoPanel = null;


	/**
	 * Creates an instance of <code>CameraPanel</code>.
	 * 
	 * @param scene
	 * 	scene which camera is maniputated by this panel
	 * @param width
	 * 	width of each button
	 * @param height
	 * 	height of each button
	 * @param vspace
	 * 	vertical space between buttons
	 * @param hspace
	 * 	horizontal space between buttons
	 * @param orient
	 * 	panel orientation.<br/>
	 *        <code>ButtonPanelLayout.POSITIVE</code> or
	 *        <code>ButtonPanelLayout.NEGATIVE</code>
	 */
	public CameraPanel(VirtualScene scene, int width, int height, int vspace, int hspace, int orient){
		this.scene = scene;

		setOpaque(false);
		setLayout(new ButtonPanelLayout(width, height, vspace, hspace, ButtonPanelLayout.VERTICAL, orient));
		initButtons();
	}


	/**
	 * Initializes all buttons.
	 */
	private void initButtons(){
		if(scene != null && scene instanceof VirtualScene3D){
			rotateCam = new SimpleButton(SimpleButton.MULTIPLE_DRAG);
			rotateCam.setButtonInfo(Utilities.translator.translate(this.getClass(), "rotCam"));
			rotateCam.setImage("cam_rot.png", SimpleButton.NORMAL_IMAGE);
			rotateCam.setImage("cam_rot_1.png", SimpleButton.MOUSE_OVER_IMAGE);
			rotateCam.setImage("cam_rot_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
			rotateCam.setImage("cam_rot_off.png", SimpleButton.DISABLED_IMAGE);
			rotateCam.addSimpleButtonListener(this);
			add(rotateCam);
		}

		translateCam = new SimpleButton(SimpleButton.MULTIPLE_DRAG);
		translateCam.setButtonInfo(Utilities.translator.translate(this.getClass(), "moveCam"));
		translateCam.setImage("cam_move.png", SimpleButton.NORMAL_IMAGE);
		translateCam.setImage("cam_move_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		translateCam.setImage("cam_move_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		translateCam.setImage("cam_move_off.png", SimpleButton.DISABLED_IMAGE);
		translateCam.addSimpleButtonListener(this);
		add(translateCam);

		zoomCam = new SimpleButton(SimpleButton.VERTICAL_DRAG);
		zoomCam.setButtonInfo(Utilities.translator.translate(this.getClass(), "zoomCam"));
		zoomCam.setImage("cam_zoom.png", SimpleButton.NORMAL_IMAGE);
		zoomCam.setImage("cam_zoom_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		zoomCam.setImage("cam_zoom_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		zoomCam.setImage("cam_zoom_off.png", SimpleButton.DISABLED_IMAGE);
		zoomCam.addSimpleButtonListener(this);
		add(zoomCam);
	}


	/**
	 * Sets the listener this panel can send hierarchy manipulation events to.
	 *
	 * @param listener
	 *	object which listens hierarchy manipulation events sent by this panel
	 */
	public void setEditorEventListener(HierarchyEventListener listener){
		this.listener = listener;
	}


	/**
	 * Not in use.
	 * 
	 * @param e
	 * 	doesn't do anything
	 */
	public void buttonClicked(SimpleButtonEvent e){
	}


	/**
	 * Listens the mouse drag events sent by <code>SimpleButton</code> objects.
	 *
	 * @param e
	 *	event sent by <code>SimpleButton</code>
	 */
	public void buttonDragged(SimpleButtonEvent e){
		if(scene != null && e.getSource() == rotateCam){
			scene.getCamera().rotateCamera(0.5f * e.getX(), 0.5f * e.getY());
		}
		if(scene != null && e.getSource() == translateCam){
			scene.getCamera().moveFocus(0.01f * e.getX(), 0.01f * e.getY());
		}
		if(scene != null && e.getSource() == zoomCam){
			scene.getCamera().zoomCamera(0.01f * e.getY());
		}

		if(listener != null){
			listener.hierarchyEventOccured(new HierarchyEvent(this, HierarchyEvent.CAMERA_TRANSFORMATION));
		}
	}


	/**
	 * Listens the mouse enter events sent by <code>SimpleButton</code> objects.
	 *
	 * @param e
	 *	event sent by <code>SimpleButton</code>
	 */
	public void buttonEntered(SimpleButtonEvent e){
		if(infoPanel != null){
			infoPanel.setInfo(e.getSource().getButtonInfo());
		}
	}


	/**
	 * Listens the mouse exit events sent by <code>SimpleButton</code> objects.
	 *
	 * @param e
	 *	event sent by <code>SimpleButton</code>
	 */
	public void buttonExited(SimpleButtonEvent e){
		if(infoPanel != null){
			infoPanel.setInfo(null, null);
		}
	}


	/**
	 * Not in use.
	 * 
	 * @param e
	 * 	doesn't do anything
	 */
	public void buttonDragEnded(SimpleButtonEvent e){
	}


	/**
	 * Sets the infopanel which shows information about the buttons of this panel.
	 *
	 * @param infoPanel
	 *	infopanel which shows information about the buttons of this panel
	 */
	public void setInfoListener(InfoPanel infoPanel){
		this.infoPanel = infoPanel;
	}

}
