Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / modules / mod_noise / valuenode_random.cpp
index b49b57b..0488189 100644 (file)
@@ -6,7 +6,7 @@
 **
 **     \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
@@ -60,7 +60,7 @@ ValueNode_Random::ValueNode_Random(const ValueBase &value):
        set_link("radius",ValueNode_Const::create(Real(1)));
        set_link("seed",ValueNode_Const::create(random.get_seed()));
        set_link("speed",ValueNode_Const::create(Real(1)));
-       set_link("smooth",ValueNode_Const::create(int(0)));
+       set_link("smooth",ValueNode_Const::create(int(Random::SMOOTH_CUBIC)));
 
        switch(get_type())
        {
@@ -83,7 +83,7 @@ ValueNode_Random::ValueNode_Random(const ValueBase &value):
                set_link("link",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()));
        }
 
        DCAST_HACK_ENABLE();
@@ -173,31 +173,17 @@ ValueNode_Random::get_local_name()const
 }
 
 bool
-ValueNode_Random::set_link_vfunc(int i,ValueNode::Handle x)
+ValueNode_Random::set_link_vfunc(int i,ValueNode::Handle value)
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
-       case 0:
-               link_=x;
-               signal_child_changed()(i);signal_value_changed()();
-               return true;
-       case 1:
-               radius_=x;
-               signal_child_changed()(i);signal_value_changed()();
-               return true;
-       case 2:
-               seed_=x;
-               signal_child_changed()(i);signal_value_changed()();
-               return true;
-       case 3:
-               speed_=x;
-               signal_child_changed()(i);signal_value_changed()();
-               return true;
-       case 4:
-               smooth_=x;
-               signal_child_changed()(i);signal_value_changed()();
-               return true;
+       case 0: CHECK_TYPE_AND_SET_VALUE(link_,   get_type());
+       case 1: CHECK_TYPE_AND_SET_VALUE(radius_, ValueBase::TYPE_REAL);
+       case 2: CHECK_TYPE_AND_SET_VALUE(seed_,   ValueBase::TYPE_INTEGER);
+       case 3: CHECK_TYPE_AND_SET_VALUE(speed_,  ValueBase::TYPE_REAL);
+       case 4: CHECK_TYPE_AND_SET_VALUE(smooth_, ValueBase::TYPE_INTEGER);
        }
        return false;
 }
@@ -206,6 +192,7 @@ ValueNode::LooseHandle
 ValueNode_Random::get_link_vfunc(int i)const
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
        case 0: return link_;
@@ -227,6 +214,7 @@ String
 ValueNode_Random::link_name(int i)const
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
        case 0: return "link";
@@ -242,6 +230,7 @@ String
 ValueNode_Random::link_local_name(int i)const
 {
        assert(i>=0 && i<link_count());
+
        switch(i)
        {
        case 0: return _("Link");
@@ -275,3 +264,25 @@ ValueNode_Random::check_type(ValueBase::Type type)
                type==ValueBase::TYPE_TIME              ||
                type==ValueBase::TYPE_VECTOR    ;
 }
+
+ValueNode*
+ValueNode_Random::clone(const GUID& deriv_guid)const
+{
+       ValueNode_Random* ret = (ValueNode_Random*)LinkableValueNode::clone(deriv_guid);
+       ret->randomize_seed();
+       return ret;
+}
+
+void
+ValueNode_Random::randomize_seed()
+{
+       int i = get_link_index_from_name("seed");
+       ValueNode::Handle link = get_link_vfunc(i);
+       if(!link->is_exported() && link->get_name() == "constant")
+       {
+               int seed = time(NULL) + rand();
+               if (seed < 0) seed = -seed;
+               random.set_seed(seed);
+               set_link(i, ValueNode_Const::create(seed));
+       }
+}