package viewer.gui.transformation;

import java.awt.Graphics;
import java.awt.Graphics2D;

import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.ui.OverlayRenderer;
import net.imglib2.ui.TransformListener;
import viewer.TextOverlayAnimator;
import viewer.render.DisplayMode;
import viewer.render.SourceState;
import viewer.render.TransformedSource;
import viewer.render.ViewerState;

public class ManualTransformationEditor implements TransformListener< AffineTransform3D >, OverlayRenderer
{

	private boolean active = false;

	private final ViewerState state;

	private TextOverlayAnimator animatedOverlay;

	private final AffineTransform3D frozenTransform;

	private final AffineTransform3D liveTransform;

	public ManualTransformationEditor( final ViewerState state )
	{
		this.state = state;
		this.frozenTransform = new AffineTransform3D();
		this.liveTransform = new AffineTransform3D();
	}

	public synchronized void toggle()
	{
		if ( !active )
		{ // Enter manual edit mode
			if ( state.getDisplayMode() != DisplayMode.FUSED )
			{
				if ( state.getDisplayMode() != DisplayMode.FUSED )
				{
					animatedOverlay = new TextOverlayAnimator( "Can only do manual transformation when in FUSED mode.", 1000 );
					return;
				}
			}
			else
			{

				active = true;
				state.getViewerTransform( frozenTransform );
			}
		}
		else
		{ // Exit manual edit mode.
			active = false;
		}
	}

	@Override
	public void transformChanged( final AffineTransform3D transform )
	{
		if ( !active ) { return; }

		final int currentSource = state.getCurrentSource();
		final SourceState< ? > sourceState = state.getSources().get( currentSource );
		final TransformedSource< ? > source = ( TransformedSource< ? > ) sourceState.getSpimSource();

		state.getViewerTransform( liveTransform );
		liveTransform.preConcatenate( frozenTransform.inverse() );

		source.setTransform( liveTransform.inverse() );

	}

	@Override
	public void drawOverlays( final Graphics g )
	{
		if ( animatedOverlay != null )
		{
			animatedOverlay.paint( ( Graphics2D ) g, System.currentTimeMillis() );
			if ( animatedOverlay.isComplete() )
			{
				animatedOverlay = null;
			}
		}
	}

	@Override
	public void setCanvasSize( final int width, final int height )
	{}

}

@@ -1057,70 +1057,8 @@
 %09%09%7B%0A
-%09%09%09%09if ( state.getDisplayMode() != DisplayMode.FUSED )%0A%09%09%09%09%7B%0A%09
 %09%09%09%09
@@ -1173,17 +1173,16 @@
 %09%09%09%09
-%09
 return;%0A
 %09%09%09%09
@@ -1181,14 +1181,8 @@
 rn;%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -1309,16 +1309,546 @@
 it mode.
+%0A%0A%09%09%09final int currentSource = state.getCurrentSource();%0A%09%09%09final SourceState%3C ? %3E sourceState = state.getSources().get( currentSource );%0A%09%09%09final TransformedSource%3C ? %3E source = ( TransformedSource%3C ? %3E ) sourceState.getSpimSource();%0A%09%09%09final AffineTransform3D tmp = new AffineTransform3D();%0A%09%09%09source.getIncrementalTransform( frozenTransform );%0A%09%09%09source.getFixedTransform( tmp );%0A%09%09%09frozenTransform.concatenate( tmp );%0A%09%09%09tmp.identity();%0A%09%09%09source.setIncrementalTransform( tmp );%0A%09%09%09source.setFixedTransform( frozenTransform );
 %0A%09%09%09acti
@@ -2328,16 +2328,27 @@
 urce.set
+Incremental
 Transfor
@@ -2377,17 +2377,16 @@
 se() );%0A
-%0A
 %09%7D%0A%0A%09@Ov

fix transform after leaving transform edit mode.