Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_repeat_gradient.cpp
index 30a4d01..79e4f66 100644 (file)
@@ -1,11 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file valuenode_repeat_gradient.cpp
-**     \brief Template File
+**     \brief Implementation of the "Repeat Gradient" valuenode conversion.
 **
 **     $Id: valuenode_repeat_gradient.cpp 604 2007-09-05 14:29:02Z dooglus $
 **
 **     \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
@@ -33,6 +34,7 @@
 #include "valuenode_repeat_gradient.h"
 #include "valuenode_const.h"
 #include <stdexcept>
+#include "color.h"
 #include "gradient.h"
 
 #endif
@@ -51,17 +53,21 @@ using namespace synfig;
 
 /* === M E T H O D S ======================================================= */
 
-synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient():LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
+synfig::ValueNode_Repeat_Gradient::ValueNode_Repeat_Gradient(const Gradient& x):LinkableValueNode(synfig::ValueBase::TYPE_GRADIENT)
 {
-       set_link("gradient",ValueNode_Const::create(Gradient()));
+       set_link("gradient",ValueNode_Const::create(x));
        set_link("count",count_=ValueNode_Const::create(int(3)));
        set_link("width",ValueNode_Const::create(0.5));
+       set_link("specify_start",ValueNode_Const::create(true));
+       set_link("specify_end",ValueNode_Const::create(true));
+       set_link("start_color",ValueNode_Const::create(Color::alpha()));
+       set_link("end_color",ValueNode_Const::create(Color::alpha()));
 }
 
 LinkableValueNode*
 ValueNode_Repeat_Gradient::create_new()const
 {
-       return new ValueNode_Repeat_Gradient();
+       return new ValueNode_Repeat_Gradient(Gradient());
 }
 
 ValueNode_Repeat_Gradient*
@@ -72,11 +78,10 @@ ValueNode_Repeat_Gradient::create(const ValueBase& x)
        if(id!=ValueBase::TYPE_GRADIENT)
        {
                assert(0);
-               throw runtime_error("synfig::ValueNode_Repeat_Gradient:Bad type "+ValueBase::type_name(id));
+               throw runtime_error(String(_("Repeat Gradient"))+_(":Bad type ")+ValueBase::type_local_name(id));
        }
 
-       ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient();
-       value_node->set_gradient(ValueNode_Const::create(x.get(Gradient())));
+       ValueNode_Repeat_Gradient* value_node=new ValueNode_Repeat_Gradient(x.get(Gradient()));
 
        assert(value_node->get_type()==id);
 
@@ -88,37 +93,6 @@ synfig::ValueNode_Repeat_Gradient::~ValueNode_Repeat_Gradient()
        unlink_all();
 }
 
-bool
-synfig::ValueNode_Repeat_Gradient::set_gradient(ValueNode::Handle a)
-{
-       if(a->get_type()!=ValueBase::TYPE_GRADIENT)
-               return false;
-
-       gradient_=a;
-
-       return true;
-}
-
-bool
-synfig::ValueNode_Repeat_Gradient::set_width(ValueNode::Handle x)
-{
-       if(x->get_type()!=ValueBase::TYPE_REAL)
-               return false;
-
-       width_=x;
-
-       return true;
-}
-
-bool
-synfig::ValueNode_Repeat_Gradient::set_count(ValueNode::Handle b)
-{
-       if(b->get_type()!=ValueBase::TYPE_INTEGER)
-               return false;
-       count_=b;
-       return true;
-}
-
 synfig::ValueBase
 synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
 {
@@ -131,12 +105,16 @@ synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
 
        const Gradient gradient((*gradient_)(t).get(Gradient()));
        const float width(max(0.0,min(1.0,(*width_)(t).get(Real()))));
+       const bool specify_start((*specify_start_)(t).get(bool()));
+       const bool specify_end((*specify_end_)(t).get(bool()));
 
        const float gradient_width_a(width/count);
        const float gradient_width_b((1.0-width)/count);
 
        Gradient::const_iterator iter;
        Gradient::const_reverse_iterator riter;
+       if (specify_start)
+               ret.push_back(Gradient::CPoint(0,(*start_color_)(t).get(Color())));
        for(i=0;i<count;i++)
        {
                float pos(float(i)/count);
@@ -148,26 +126,26 @@ synfig::ValueNode_Repeat_Gradient::operator()(Time t)const
                        for(riter=gradient.rbegin();riter!=gradient.rend();riter++)
                                ret.push_back(Gradient::CPoint(pos+gradient_width_b*(1-(riter->pos)),riter->color));
        }
+       if (specify_end)
+               ret.push_back(Gradient::CPoint(1,(*end_color_)(t).get(Color())));
        return ret;
 }
 
 bool
-ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle x)
+ValueNode_Repeat_Gradient::set_link_vfunc(int i,ValueNode::Handle value)
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
-               case 0:
-                       if(set_gradient(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
-                       else { return false; }
-               case 1:
-                       if(set_count(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
-                       else { return false; }
-               case 2:
-                       if(set_width(x)) { signal_child_changed()(i);signal_value_changed()(); return true; }
-                       else { return false; }
+       case 0: CHECK_TYPE_AND_SET_VALUE(gradient_,          get_type());
+       case 1: CHECK_TYPE_AND_SET_VALUE(count_,                 ValueBase::TYPE_INTEGER);
+       case 2: CHECK_TYPE_AND_SET_VALUE(width_,                 ValueBase::TYPE_REAL);
+       case 3: CHECK_TYPE_AND_SET_VALUE(specify_start_, ValueBase::TYPE_BOOL);
+       case 4: CHECK_TYPE_AND_SET_VALUE(specify_end_,   ValueBase::TYPE_BOOL);
+       case 5: CHECK_TYPE_AND_SET_VALUE(start_color_,   ValueBase::TYPE_COLOR);
+       case 6: CHECK_TYPE_AND_SET_VALUE(end_color_,     ValueBase::TYPE_COLOR);
        }
-
        return false;
 }
 
@@ -175,11 +153,16 @@ ValueNode::LooseHandle
 ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
                case 0:  return gradient_;
                case 1:  return count_;
                case 2:  return width_;
+               case 3:  return specify_start_;
+               case 4:  return specify_end_;
+               case 5:  return start_color_;
+               case 6:  return end_color_;
            default: return 0;
        }
 }
@@ -187,18 +170,23 @@ ValueNode_Repeat_Gradient::get_link_vfunc(int i)const
 int
 ValueNode_Repeat_Gradient::link_count()const
 {
-       return 3;
+       return 7;
 }
 
 String
 ValueNode_Repeat_Gradient::link_local_name(int i)const
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
                case 0:  return _("Gradient");
                case 1:  return _("Count");
                case 2:  return _("Width");
+               case 3:  return _("Specify Start");
+               case 4:  return _("Specify End");
+               case 5:  return _("Start Color");
+               case 6:  return _("End Color");
                default: return String();
        }
 }
@@ -207,11 +195,16 @@ String
 ValueNode_Repeat_Gradient::link_name(int i)const
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
                case 0:  return "gradient";
                case 1:  return "count";
                case 2:  return "width";
+               case 3:  return "specify_start";
+               case 4:  return "specify_end";
+               case 5:  return "start_color";
+               case 6:  return "end_color";
                default: return String();
        }
 }
@@ -222,6 +215,10 @@ ValueNode_Repeat_Gradient::get_link_index_from_name(const String &name)const
        if(name=="gradient") return 0;
        if(name=="count")    return 1;
        if(name=="width")    return 2;
+       if(name=="specify_start") return 3;
+       if(name=="specify_end")   return 4;
+       if(name=="start_color")   return 5;
+       if(name=="end_color")     return 6;
        throw Exception::BadLinkName(name);
 }