# AVSLib :: 'base' package :: 'convert' module
# Copyright (c) 2005 George Zarkadas (gzarkadas@users.sourceforge.net)

# 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 the "Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA"

DefineModule("avslib", "base", "conversion")

global __load__ = LoadModule("avslib", "base", "core")

# x is made optional to allow high-level functions to pass undefined variables
# without the need to check them.
# The 'force' optional argument converts (if true) undefined variables passed
# as arguments to default-value variables. The default behavior (false or ommited)
# is to propagate undefined variables.
# Converting clips to other types and vice-versa is not very meaningfull. 
# Nevertheless, a conversion choice has been made in order to provide a 
# complete type conversion interface.


# CClip returns an RGB32 clip (same as  BlankClip() without arguments) 
# unless x is a clip also, (it then returns x)

Function CClip(val "x", bool "force")
{
	force = Default(force, false)
	_c = BlankClip()
	vt = VarType(x)
	# Cannot use Select() because ALL expressions inside it 
	# are evaluated, thus leading to errors
	return Defined(x) \
		? (vt == 0 \
			? _c \
		: (vt == 1 \
			? x \
		: _c.SubTitle(String(x)) )) \
	: (force ? _c : Undef())
}

Function CInt(val "x", bool "force")
{
	force = Default(force, false)
	vt = VarType(x)
	# Cannot use Select() because ALL expressions inside it 
	# are evaluated, thus leading to errors
	return Defined(x) \
		? (vt <= 1 \
			? 0 \
		: (vt == 2 \
			? x \
		: (vt == 3 \
			? Int(x) \
		: (vt == 4 \
			? (x ? 1 : 0) \
		: Int(Value(x)) )))) \
	: (force ? 0 : Undef())
}

Function CFloat(val "x", bool "force")
{
	force = Default(force, false)
	vt = VarType(x)
	# Cannot use Select() because ALL expressions inside it 
	# are evaluated, thus leading to errors
	return Defined(x) \
		? (vt <= 1 \
			? 0.0 \
		: (vt == 2 \
			? Float(x) \
		: (vt == 3 \
			? x \
		: (vt == 4 \
			? (x ? 1.0 : 0.0) \
		: Float(Value(x)) )))) \
	: (force ? 0.0 : Undef())
}

Function CBool(val "x", bool "force")
{
	force = Default(force, false)
	vt = VarType(x)
	# Cannot use Select() because ALL expressions inside it 
	# are evaluated, thus leading to errors
	return Defined(x) \
		? (vt == 0 \
			? false \
		: (vt == 1 \
			? x.Framecount > 0 \
		: (vt == 2 \
			? x != 0 \
		: (vt == 3 \
			? x != 0.0 \
		: (vt == 4 \
			? x \
		: x != "" ))))) \
	: (force ? false : Undef())
}

Function CString(val "x", bool "force")
{
	force = Default(force, false)
	return Defined(x) \
		? String(x) \
		: (force ? "" : Undef())
}
