Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_timedswap.cpp
index b456bf9..378535a 100644 (file)
@@ -1,11 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file valuenode_timedswap.cpp
-**     \brief Template File
+**     \brief Implementation of the "Timed Swap" valuenode conversion.
 **
 **     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007, 2008 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -34,6 +35,7 @@
 #include "valuenode_const.h"
 #include <stdexcept>
 #include "color.h"
+#include <ETL/misc>
 
 #endif
 
@@ -56,14 +58,6 @@ ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
 {
        switch(get_type())
        {
-       case ValueBase::TYPE_REAL:
-               set_link("before",ValueNode_Const::create(value.get(Real())));
-               set_link("after",ValueNode_Const::create(value.get(Real())));
-               break;
-       case ValueBase::TYPE_VECTOR:
-               set_link("before",ValueNode_Const::create(value.get(Vector())));
-               set_link("after",ValueNode_Const::create(value.get(Vector())));
-               break;
        case ValueBase::TYPE_ANGLE:
                set_link("before",ValueNode_Const::create(value.get(Angle())));
                set_link("after",ValueNode_Const::create(value.get(Angle())));
@@ -76,12 +70,20 @@ ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
                set_link("before",ValueNode_Const::create(value.get(int())));
                set_link("after",ValueNode_Const::create(value.get(int())));
                break;
+       case ValueBase::TYPE_REAL:
+               set_link("before",ValueNode_Const::create(value.get(Real())));
+               set_link("after",ValueNode_Const::create(value.get(Real())));
+               break;
        case ValueBase::TYPE_TIME:
                set_link("before",ValueNode_Const::create(value.get(Time())));
                set_link("after",ValueNode_Const::create(value.get(Time())));
                break;
+       case ValueBase::TYPE_VECTOR:
+               set_link("before",ValueNode_Const::create(value.get(Vector())));
+               set_link("after",ValueNode_Const::create(value.get(Vector())));
+               break;
        default:
-               throw Exception::BadType(ValueBase::type_name(get_type()));
+               throw Exception::BadType(ValueBase::type_local_name(get_type()));
        }
 
        set_link("time",ValueNode_Const::create(Time(2)));
@@ -96,98 +98,23 @@ ValueNode_TimedSwap::create(const ValueBase& x)
        return new ValueNode_TimedSwap(x);
 }
 
-
 LinkableValueNode*
 ValueNode_TimedSwap::create_new()const
 {
        return new ValueNode_TimedSwap(get_type());
 }
 
-
 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
 {
        unlink_all();
 }
 
-
-
-bool
-ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
-{
-       if(!x || x->get_type()!=get_type()
-               && !PlaceholderValueNode::Handle::cast_dynamic(x))
-               return false;
-
-       before=x;
-
-       return true;
-}
-
-ValueNode::Handle
-ValueNode_TimedSwap::get_before()const
-{
-       return before;
-}
-
-
-bool
-ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
-{
-       if(!x || x->get_type()!=get_type()
-               && !PlaceholderValueNode::Handle::cast_dynamic(x))
-               return false;
-
-       after=x;
-
-       return true;
-}
-
-ValueNode::Handle
-ValueNode_TimedSwap::get_after()const
-{
-       return after;
-}
-
-
-bool
-ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
-{
-       if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
-                         !PlaceholderValueNode::Handle::cast_dynamic(x)))
-               return false;
-
-       swap_time=x;
-       return true;
-}
-
-ValueNode::Handle
-ValueNode_TimedSwap::get_swap_time()const
-{
-       return swap_time;
-}
-
-bool
-ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
-{
-       if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
-                         !PlaceholderValueNode::Handle::cast_dynamic(x)))
-               return false;
-
-       swap_length=x;
-       return true;
-}
-
-ValueNode::Handle
-ValueNode_TimedSwap::get_swap_length()const
-{
-       return swap_length;
-}
-
-
-
 synfig::ValueBase
 synfig::ValueNode_TimedSwap::operator()(Time t)const
 {
+       if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
+               printf("%s:%d operator()\n", __FILE__, __LINE__);
+
        Time swptime=(*swap_time)(t).get(Time());
        Time swplength=(*swap_length)(t).get(Time());
 
@@ -202,18 +129,6 @@ synfig::ValueNode_TimedSwap::operator()(Time t)const
 
                switch(get_type())
                {
-               case ValueBase::TYPE_REAL:
-                       {
-                               Real a=(*after)(t).get(Real());
-                               Real b=(*before)(t).get(Real());
-                               return (b-a)*amount+a;
-                       }
-               case ValueBase::TYPE_VECTOR:
-                       {
-                               Vector a=(*after)(t).get(Vector());
-                               Vector b=(*before)(t).get(Vector());
-                               return (b-a)*amount+a;
-                       }
                case ValueBase::TYPE_ANGLE:
                        {
                                Angle a=(*after)(t).get(Angle());
@@ -231,7 +146,13 @@ synfig::ValueNode_TimedSwap::operator()(Time t)const
                        {
                                float a=(float)(*after)(t).get(int());
                                float b=(float)(*before)(t).get(int());
-                               return static_cast<int>((b-a)*amount+a+0.5f);
+                               return round_to_int((b-a)*amount+a);
+                       }
+               case ValueBase::TYPE_REAL:
+                       {
+                               Real a=(*after)(t).get(Real());
+                               Real b=(*before)(t).get(Real());
+                               return (b-a)*amount+a;
                        }
                case ValueBase::TYPE_TIME:
                        {
@@ -239,12 +160,17 @@ synfig::ValueNode_TimedSwap::operator()(Time t)const
                                Time b=(*before)(t).get(Time());
                                return (b-a)*amount+a;
                        }
+               case ValueBase::TYPE_VECTOR:
+                       {
+                               Vector a=(*after)(t).get(Vector());
+                               Vector b=(*before)(t).get(Vector());
+                               return (b-a)*amount+a;
+                       }
                default:
                        break;
                }
        }
 
-
        /*! \todo this should interpolate from
        **      before to after over the period defined
        **      by swap_length */
@@ -252,17 +178,17 @@ synfig::ValueNode_TimedSwap::operator()(Time t)const
        return (*before)(t);
 }
 
-
 bool
-ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
+ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle value)
 {
-       assert(i>=0 && i<4);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
-       case 0: return set_before(x);
-       case 1: return set_after(x);
-       case 2: return set_swap_time(x);
-       case 3: return set_swap_length(x);
+       case 0: CHECK_TYPE_AND_SET_VALUE(before,      get_type());
+       case 1: CHECK_TYPE_AND_SET_VALUE(after,       get_type());
+       case 2: CHECK_TYPE_AND_SET_VALUE(swap_time,   ValueBase::TYPE_TIME);
+       case 3: CHECK_TYPE_AND_SET_VALUE(swap_length, ValueBase::TYPE_TIME);
        }
        return false;
 }
@@ -270,13 +196,14 @@ ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
 ValueNode::LooseHandle
 ValueNode_TimedSwap::get_link_vfunc(int i)const
 {
-       assert(i>=0 && i<4);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
-       case 0: return get_before();
-       case 1: return get_after();
-       case 2: return get_swap_time();
-       case 3: return get_swap_length();
+       case 0: return before;
+       case 1: return after;
+       case 2: return swap_time;
+       case 3: return swap_length;
        }
        return 0;
 }
@@ -290,7 +217,8 @@ ValueNode_TimedSwap::link_count()const
 String
 ValueNode_TimedSwap::link_local_name(int i)const
 {
-       assert(i>=0 && i<4);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
        case 0: return _("Before");
@@ -304,7 +232,8 @@ ValueNode_TimedSwap::link_local_name(int i)const
 String
 ValueNode_TimedSwap::link_name(int i)const
 {
-       assert(i>=0 && i<4);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
        case 0: return "before";
@@ -342,10 +271,10 @@ bool
 ValueNode_TimedSwap::check_type(ValueBase::Type type)
 {
        return
-               type==ValueBase::TYPE_REAL ||
-               type==ValueBase::TYPE_VECTOR ||
                type==ValueBase::TYPE_ANGLE ||
                type==ValueBase::TYPE_COLOR ||
                type==ValueBase::TYPE_INTEGER ||
-               type==ValueBase::TYPE_TIME;
+               type==ValueBase::TYPE_REAL ||
+               type==ValueBase::TYPE_TIME ||
+               type==ValueBase::TYPE_VECTOR;
 }