Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_add.cpp
index 3c08cef..7cd4ee9 100644 (file)
@@ -1,11 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file valuenode_add.cpp
-**     \brief Template File
+**     \brief Implementation of the "Add" 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
 #include "valuenode_const.h"
 #include <stdexcept>
 #include "color.h"
+#include "gradient.h"
 #include "vector.h"
 #include "angle.h"
 #include "real.h"
+#include <ETL/misc>
 
 #endif
 
@@ -57,7 +60,7 @@ using namespace synfig;
 synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value):
        LinkableValueNode(value.get_type())
 {
-       set_scalar(1.0);
+       set_link("scalar",ValueNode_Const::create(Real(1.0)));
        ValueBase::Type id(value.get_type());
 
        switch(id)
@@ -70,6 +73,10 @@ synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value):
                set_link("lhs",ValueNode_Const::create(value.get(Color())));
                set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
                break;
+       case ValueBase::TYPE_GRADIENT:
+               set_link("lhs",ValueNode_Const::create(value.get(Gradient())));
+               set_link("rhs",ValueNode_Const::create(Gradient()));
+               break;
        case ValueBase::TYPE_INTEGER:
                set_link("lhs",ValueNode_Const::create(value.get(int())));
                set_link("rhs",ValueNode_Const::create(int(0)));
@@ -88,14 +95,8 @@ synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value):
                break;
        default:
                assert(0);
-               throw runtime_error("synfig::ValueNode_Add:Bad type "+ValueBase::type_name(id));
+               throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
        }
-
-       assert(get_lhs()->get_type()==id);
-       assert(get_rhs()->get_type()==id);
-       assert(get_type()==id);
-
-       DCAST_HACK_ENABLE();
 }
 
 LinkableValueNode*
@@ -115,51 +116,6 @@ synfig::ValueNode_Add::~ValueNode_Add()
        unlink_all();
 }
 
-void
-ValueNode_Add::set_scalar(Real value)
-{
-       set_link("scalar",ValueNode_Const::create(value));
-}
-
-bool
-synfig::ValueNode_Add::set_scalar(ValueNode::Handle value)
-{
-       if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value))
-               return false;
-       scalar=value;
-       return true;
-}
-
-bool
-synfig::ValueNode_Add::set_lhs(ValueNode::Handle x)
-{
-       assert(get_type());
-
-       if(!x ||
-          (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
-          (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
-               return false;
-
-       ref_a=x;
-
-       return true;
-}
-
-bool
-synfig::ValueNode_Add::set_rhs(ValueNode::Handle x)
-{
-       assert(get_type());
-
-       if(!x ||
-          (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
-          (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
-               return false;
-
-       ref_b=x;
-
-       return true;
-}
-
 synfig::ValueBase
 synfig::ValueNode_Add::operator()(Time t)const
 {
@@ -171,12 +127,10 @@ synfig::ValueNode_Add::operator()(Time t)const
                return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
        case ValueBase::TYPE_COLOR:
                return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
+       case ValueBase::TYPE_GRADIENT:
+               return ((*ref_a)(t).get(Gradient())+(*ref_b)(t).get(Gradient()))*(*scalar)(t).get(Real());
        case ValueBase::TYPE_INTEGER:
-       {
-               Real ret = ((*ref_a)(t).get(int())+(*ref_b)(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(((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
        case ValueBase::TYPE_REAL:
                return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
        case ValueBase::TYPE_TIME:
@@ -193,28 +147,22 @@ synfig::ValueNode_Add::operator()(Time t)const
 bool
 ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value)
 {
-       assert(i>=0 && i<3);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
-               case 0:
-                       if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
-                       return false;
-               case 1:
-                       if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
-                       return false;
-               case 2:
-                       scalar=value;
-                       signal_child_changed()(i);signal_value_changed()();
-                       return true;
+       case 0: CHECK_TYPE_AND_SET_VALUE(ref_a,  get_type());
+       case 1: CHECK_TYPE_AND_SET_VALUE(ref_b,  get_type());
+       case 2: CHECK_TYPE_AND_SET_VALUE(scalar, ValueBase::TYPE_REAL);
        }
-
        return false;
 }
 
 ValueNode::LooseHandle
 ValueNode_Add::get_link_vfunc(int i)const
 {
-       assert(i>=0 && i<3);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
                case 0: return ref_a;
@@ -233,7 +181,8 @@ ValueNode_Add::link_count()const
 String
 ValueNode_Add::link_local_name(int i)const
 {
-       assert(i>=0 && i<3);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
                case 0: return _("LHS");
@@ -246,7 +195,8 @@ ValueNode_Add::link_local_name(int i)const
 String
 ValueNode_Add::link_name(int i)const
 {
-       assert(i>=0 && i<3);
+       assert(i>=0 && i<link_count());
+
        switch(i)
        {
                case 0: return "lhs";
@@ -259,7 +209,6 @@ ValueNode_Add::link_name(int i)const
 int
 ValueNode_Add::get_link_index_from_name(const String &name)const
 {
-       printf("%s:%d link_index_from_name\n", __FILE__, __LINE__);
        if(name=="lhs") return 0;
        if(name=="rhs") return 1;
        if(name=="scalar") return 2;
@@ -283,6 +232,7 @@ ValueNode_Add::check_type(ValueBase::Type type)
 {
        return type==ValueBase::TYPE_ANGLE
                || type==ValueBase::TYPE_COLOR
+               || type==ValueBase::TYPE_GRADIENT
                || type==ValueBase::TYPE_INTEGER
                || type==ValueBase::TYPE_REAL
                || type==ValueBase::TYPE_TIME