Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_scale.cpp
index b2f62b7..b272d7d 100644 (file)
@@ -1,12 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file valuenode_scale.cpp
-**     \brief Template File
+**     \brief Implementation of the "Scale" valuenode conversion.
 **
 **     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
-**     Copyright (c) 2007 Chris Moore
+**     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
 #include "valuenode_scale.h"
 #include "valuenode_const.h"
 #include <stdexcept>
-#include <cassert>
 #include "color.h"
 #include "vector.h"
 #include "time.h"
 #include "angle.h"
+#include <ETL/misc>
 
 #endif
 
@@ -59,7 +59,7 @@ using namespace synfig;
 ValueNode_Scale::ValueNode_Scale(const ValueBase &value):
        LinkableValueNode(value.get_type())
 {
-       set_scalar(1.0);
+       set_link("scalar",ValueNode::Handle(ValueNode_Const::create(Real(1.0))));
        ValueBase::Type id(value.get_type());
 
        switch(id)
@@ -84,11 +84,11 @@ ValueNode_Scale::ValueNode_Scale(const ValueBase &value):
                break;
        default:
                assert(0);
-               throw runtime_error("synfig::ValueNode_Scale:Bad type "+ValueBase::type_name(value.get_type()));
+               throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
        }
 
        assert(value_node);
-       assert(get_value_node()->get_type()==id);
+       assert(value_node->get_type()==id);
        assert(get_type()==id);
 }
 
@@ -109,59 +109,12 @@ synfig::ValueNode_Scale::~ValueNode_Scale()
        unlink_all();
 }
 
-void
-ValueNode_Scale::set_scalar(Real x)
-{
-       set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
-}
-
-bool
-ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
-{
-       if(!x
-               || x->get_type()!=ValueBase::TYPE_REAL
-               && !PlaceholderValueNode::Handle::cast_dynamic(x)
-       )
-               return false;
-       scalar=x;
-       return true;
-}
-
-ValueNode::Handle
-ValueNode_Scale::get_scalar()const
-{
-       return scalar;
-}
-
-bool
-ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
-{
-       assert(get_type());
-
-       // if this isn't a proper value
-       if(!x ||
-          // or we don't have a type, and this value isn't one of the types we accept
-          (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
-          // or we have a type and this value is a different type and (placeholder?)
-          (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
-               // then fail to set the value
-               return false;
-
-       value_node=x;
-
-       return true;
-}
-
-ValueNode::Handle
-ValueNode_Scale::get_value_node()const
-{
-       return value_node;
-}
-
-
 synfig::ValueBase
 synfig::ValueNode_Scale::operator()(Time t)const
 {
+       if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
+               printf("%s:%d operator()\n", __FILE__, __LINE__);
+
        if(!value_node || !scalar)
                throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
        else if(get_type()==ValueBase::TYPE_ANGLE)
@@ -176,11 +129,7 @@ synfig::ValueNode_Scale::operator()(Time t)const
                return ret;
        }
        else if(get_type()==ValueBase::TYPE_INTEGER)
-       {
-               Real ret = (*value_node)(t).get(int())*(*scalar)(t).get(Real()) + 0.5f;
-               if (ret < 0) return static_cast<int>(ret-1);
-               return static_cast<int>(ret);
-       }
+               return round_to_int((*value_node)(t).get(int())*(*scalar)(t).get(Real()));
        else if(get_type()==ValueBase::TYPE_REAL)
                return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
        else if(get_type()==ValueBase::TYPE_TIME)
@@ -192,28 +141,69 @@ synfig::ValueNode_Scale::operator()(Time t)const
        return ValueBase();
 }
 
-
-bool
-ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
+synfig::ValueBase
+synfig::ValueNode_Scale::get_inverse(Time t, const synfig::Vector &target_value) const
 {
-       if(!(i==0 || i==1))
-               return false;
+       Real scalar_value((*scalar)(t).get(Real()));
+       if(scalar_value==0)
+                       throw runtime_error(strprintf("ValueNode_Scale: %s",_("Attempting to get the inverse of a non invertible Valuenode")));
+       else
+               {
+                       switch (get_type())
+                       {
+                               case ValueBase::TYPE_REAL:
+                                       return target_value.mag() / scalar_value;
+                               case ValueBase::TYPE_ANGLE:
+                                       return Angle::tan(target_value[1] / scalar_value ,target_value[0] / scalar_value);
+                               default:
+                                       return target_value / scalar_value;
+                       }
+               }
+       return ValueBase();
+}
 
-       if(i==0 && !set_value_node(x))
-               return false;
+synfig::ValueBase
+synfig::ValueNode_Scale::get_inverse(Time t, const synfig::Angle &target_value) const
+{
+       Real scalar_value((*scalar)(t).get(Real()));
+       if(scalar_value==0)
+                       throw runtime_error(strprintf("ValueNode_Scale: %s",_("Attempting to get the inverse of a non invertible Valuenode")));
        else
-       if(i==1 && !set_scalar(x))
-               return false;
+               {
+                       switch (get_type())
+                       {
+                                       default:
+                                       return (*value_node)(t).get(Angle()) + target_value / scalar_value;
+                       }
+               }
+       return ValueBase();
+}
 
-       signal_child_changed()(i);signal_value_changed()();
+bool
+synfig::ValueNode_Scale::is_invertible(Time t) const
+{
+       Real scalar_value((*scalar)(t).get(Real()));
+       return (!scalar_value==0);
+}
+
+bool
+ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle value)
+{
+       assert(i>=0 && i<link_count());
 
-       return true;
+       switch(i)
+       {
+       case 0: CHECK_TYPE_AND_SET_VALUE(value_node, get_type());
+       case 1: CHECK_TYPE_AND_SET_VALUE(scalar,     ValueBase::TYPE_REAL);
+       }
+       return false;
 }
 
 ValueNode::LooseHandle
 ValueNode_Scale::get_link_vfunc(int i)const
 {
-       assert(i==0 || i==1);
+       assert(i>=0 && i<link_count());
+
        if(i==0)
                return value_node;
        else if(i==1)
@@ -230,7 +220,8 @@ ValueNode_Scale::link_count()const
 String
 ValueNode_Scale::link_local_name(int i)const
 {
-       assert(i==0 || i==1);
+       assert(i>=0 && i<link_count());
+
        if(i==0)
                return _("Link");
        else if(i==1)
@@ -241,7 +232,8 @@ ValueNode_Scale::link_local_name(int i)const
 String
 ValueNode_Scale::link_name(int i)const
 {
-       assert(i==0 || i==1);
+       assert(i>=0 && i<link_count());
+
        if(i==0)
                return "link";
        else if(i==1)