//      zoom.gib
//
//      zoom script for GIB in QuakeForge 0.5.3
//
//      Copyright (C) 2002,2003  Brian Koropoff
//
//      This program is free software; you can redistribute it and/or
//      modify it under the terms of the GNU General Public License
//      as published by the Free Software Foundation; either version 2
//      of the License, or (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
//      See the GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to:
//
//              Free Software Foundation, Inc.
//              59 Temple Place - Suite 330
//              Boston, MA  02111-1307, USA
//

domain zoom

global rcsid = "$Id: zoom.gib,v 1.8 2003/02/14 08:36:39 snax Exp $"

global amp
global fov
global min
global max
global step
global mult
global zoomed

// zoom::clamp returns the second argument clamped
// between the first and third
function zoom::clamp {
	if ($args[2] < $args[1]) {
		return $args[1]
	} else if ($args[2] > $args[3]) {
		return $args[3]
	} else return $args[2]
}

function zoom::init { // Initialize basic options
	if (#args != 6) {
		print "Usage: ", $args[0], " base_amp base_fov min_zoom max_zoom zoom_step\n"
		return
	}
	amp = $args[1]
	fov = $args[2]
	min = $args[3]
	max = $args[4]
	step = $args[5]
	mult = $min
	zoomed = 0
	set fov $fov
	set in_amp $amp
}

// Default initial values
zoom::init 1 90 1.15 90 1.15

function zoom::adjust { // Adjust fov and sensitivity to match zoom factor
	if $zoomed {
		set fov ($fov/$mult)
		set in_amp ($amp/$mult)
	} else {
		set fov $fov
		set in_amp $amp
	}
}

function zoom::in {
	zoomed = 1
	zoom::adjust
}

function zoom::out {
	zoomed = 0
	zoom::adjust
}

function zoom::toggle {
	zoomed = (!$zoomed)
	zoom::adjust
}

function zoom::increase {
	ifnot $zoomed {
		return
	}
	mult = `zoom::clamp $min ($mult*$step) $max`
	zoom::adjust
}

function zoom::decrease {
	ifnot $zoomed {
		return
	}
	mult = `zoom::clamp $min ($mult/$step) $max`
	zoom::adjust
}

function::export zoom::init zoom::increase zoom::decrease zoom::in zoom::out zoom::toggle
