/*******************************************************************************
 * Copyright 2012 bmanuel
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.bitfire.postprocessing.effects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.math.Vector2;
import com.bitfire.postprocessing.PostProcessorEffect;
import com.bitfire.postprocessing.filters.RadialBlur;
import com.bitfire.postprocessing.filters.Zoom;

/** Implements a zooming effect: either a radial blur filter or a zoom filter is used. */
public final class Zoomer extends PostProcessorEffect {
	private boolean doRadial = false;
	private RadialBlur radialBlur = null;
	private Zoom zoom = null;
	private float oneOnW, oneOnH;
	private float userOriginX, userOriginY;

	/** Creating a Zoomer specifying the radial blur quality will enable radial blur */
	public Zoomer( RadialBlur.Quality quality ) {
		radialBlur = new RadialBlur( quality );
		zoom = null;
		oneOnW = 1f / (float)Gdx.graphics.getWidth();
		oneOnH = 1f / (float)Gdx.graphics.getHeight();

		doRadial = true;
	}

	/** Creating a Zoomer without any parameter will use plain simple zooming */
	public Zoomer() {
		radialBlur = null;
		zoom = new Zoom();

		doRadial = false;
	}

	/** Specify the zoom origin, in screen coordinates. */
	public void setOrigin( Vector2 o ) {
		userOriginX = o.x;
		userOriginY = o.y;

		if( doRadial ) {
			radialBlur.setOrigin( o.x * oneOnW, 1f - o.y * oneOnH );
		} else {
			zoom.setOrigin( o.x * oneOnW, 1f - o.y * oneOnH );
		}
	}

	/** Specify the zoom origin, in screen coordinates. */
	public void setOrigin( float x, float y ) {
		userOriginX = x;
		userOriginY = y;

		if( doRadial ) {
			radialBlur.setOrigin( x * oneOnW, 1f - y * oneOnH );
		} else {
			zoom.setOrigin( x * oneOnW, 1f - y * oneOnH );
		}
	}

	public void setBlurStrength( float strength ) {
		if( doRadial ) {
			radialBlur.setStrength( strength );
		}
	}

	public void setZoom( float zoom ) {
		if( doRadial ) {
			radialBlur.setZoom( 1f / zoom );
		} else {
			this.zoom.setZoom( 1f / zoom );
		}
	}

	public float getZoom() {
		if( doRadial ) {
			return 1f / radialBlur.getZoom();
		} else {
			return 1f / zoom.getZoom();
		}
	}

	public float getBlurStrength() {
		if( doRadial ) {
			return radialBlur.getStrength();
		}

		return -1;
	}

	public float getOriginX() {
		return userOriginX;
	}

	public float getOriginY() {
		return userOriginY;
	}

	@Override
	public void dispose() {
		if( radialBlur != null ) {
			radialBlur.dispose();
			radialBlur = null;
		}

		if( zoom != null ) {
			zoom.dispose();
			zoom = null;
		}
	}

	@Override
	public void rebind() {
		radialBlur.rebind();
	}

	@Override
	public void render( FrameBuffer src, FrameBuffer dest ) {
		if( doRadial ) {
			radialBlur.setInput( src ).setOutput( dest ).render();
		} else {
			zoom.setInput( src ).setOutput( dest ).render();
		}
	}
}

@@ -1519,28 +1519,22 @@
 y ) %7B%0A%09%09
-radialBlur =
+setup(
  new Rad
@@ -1555,594 +1555,563 @@
 ty )
-;%0A%09%09zoom = null;%0A%09%09oneOnW = 1f / (float)Gdx.graphics.getWidth();%0A%09%09oneOnH = 1f / (float)Gdx.graphics.getHeight();%0A%0A%09%09doRadial = true;%0A%09%7D%0A%0A%09/** Creating a Zoomer without any parameter will use plain simple zooming */%0A%09public Zoomer() %7B%0A%09%09radialBlur = null;%0A%09%09zoom = new Zoom();%0A%0A%09%09doRadial = false;%0A%09%7D%0A%0A%09/** Specify the zoom origin, in screen coordinates. */%0A%09public void setOrigin( Vector2 o ) %7B%0A%09%09userOriginX = o.x;%0A%09%09userOriginY = o.y;%0A%0A%09%09if( doRadial ) %7B%0A%09%09%09radialBlur.setOrigin( o.x * oneOnW, 1f - o.y * oneOnH );%0A%09%09%7D else %7B%0A%09%09%09zoom.setOrigin( o.x * oneOnW, 1f - o.y * oneOnH );%0A%09%09%7D
+ );%0A%09%7D%0A%0A%09/** Creating a Zoomer without any parameter will use plain simple zooming */%0A%09public Zoomer() %7B%0A%09%09setup( null );%0A%09%7D%0A%0A%09private void setup( RadialBlur radialBlurFilter ) %7B%0A%09%09radialBlur = radialBlurFilter;%0A%09%09if( radialBlur != null ) %7B%0A%09%09%09doRadial = true;%0A%09%09%09zoom = null;%0A%09%09%7D else %7B%0A%09%09%09doRadial = false;%0A%09%09%09zoom = new Zoom();%0A%09%09%7D%0A%0A%09%09oneOnW = 1f / (float)Gdx.graphics.getWidth();%0A%09%09oneOnH = 1f / (float)Gdx.graphics.getHeight();%0A%09%7D%0A%0A%09/** Specify the zoom origin, in screen coordinates. */%0A%09public void setOrigin( Vector2 o ) %7B%0A%09%09setOrigin( o.x, o.y );
 %0A%09%7D%0A

Fix inverse dimensions initialization