1 /* === S Y N F I G ========================================================= */
2 /*!	\file valuenodedynamiclistloop.cpp
3 **	\brief Template File
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **	Copyright (c) 2008 Chris Moore
10 **	Copyright (c) 2012 Carlos López
11 **
12 **	This package is free software; you can redistribute it and/or
13 **	modify it under the terms of the GNU General Public License as
14 **	published by the Free Software Foundation; either version 2 of
15 **	the License, or (at your option) any later version.
16 **
17 **	This package is distributed in the hope that it will be useful,
18 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **	General Public License for more details.
21 **	\endlegal
22 */
23 /* ========================================================================= */
24 
25 /* === H E A D E R S ======================================================= */
26 
27 #ifdef USING_PCH
28 #	include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #	include <config.h>
32 #endif
33 
34 #include <synfig/general.h>
35 
36 #include "valuenodedynamiclistloop.h"
37 #include <synfigapp/canvasinterface.h>
38 
39 #include <synfigapp/localization.h>
40 #include <synfig/valuenodes/valuenode_composite.h>
41 
42 #endif
43 
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
49 
50 /* === M A C R O S ========================================================= */
51 
52 ACTION_INIT(Action::ValueNodeDynamicListLoop);
53 ACTION_SET_NAME(Action::ValueNodeDynamicListLoop,"ValueNodeDynamicListLoop");
54 ACTION_SET_LOCAL_NAME(Action::ValueNodeDynamicListLoop,N_("Loop"));
55 ACTION_SET_TASK(Action::ValueNodeDynamicListLoop,"loop");
56 ACTION_SET_CATEGORY(Action::ValueNodeDynamicListLoop,Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
57 ACTION_SET_PRIORITY(Action::ValueNodeDynamicListLoop,0);
58 ACTION_SET_VERSION(Action::ValueNodeDynamicListLoop,"0.0");
59 ACTION_SET_CVS_ID(Action::ValueNodeDynamicListLoop,"$Id$");
60 
61 /* === G L O B A L S ======================================================= */
62 
63 /* === P R O C E D U R E S ================================================= */
64 
65 /* === M E T H O D S ======================================================= */
66 
ValueNodeDynamicListLoop()67 Action::ValueNodeDynamicListLoop::ValueNodeDynamicListLoop():
68 	old_loop_value()
69 { }
70 
71 Action::ParamVocab
get_param_vocab()72 Action::ValueNodeDynamicListLoop::get_param_vocab()
73 {
74 	ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
75 	ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
76 		.set_local_name(_("ValueNode"))
77 	);
78 	return ret;
79 }
80 
81 bool
is_candidate(const ParamList & x)82 Action::ValueNodeDynamicListLoop::is_candidate(const ParamList &x)
83 {
84 	if (!candidate_check(get_param_vocab(),x))
85 		return false;
86 	ValueNode::Handle value_node;
87 	ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
88 	if(value_desc.parent_is_value_node())
89 	{
90 		value_node = value_desc.get_parent_value_node();
91 		// let's check if the parent is a composite (if user clicked on tangent duck)
92 		if(ValueNode_Composite::Handle::cast_dynamic(value_node))
93 		{
94 			ValueNode_Composite::Handle compo(ValueNode_Composite::Handle::cast_dynamic(value_node));
95 			ValueNode_BLine::Handle bline=NULL;
96 			std::set<Node*>::iterator iter;
97 			// now check if the grand parent is a dynamic list 'bline' type
98 			for(iter=compo->parent_set.begin();iter!=compo->parent_set.end();++iter)
99 				{
100 					bline=ValueNode_BLine::Handle::cast_dynamic(*iter);
101 					if(bline)
102 						break;
103 				}
104 			if(bline)
105 				value_node=bline;
106 		}
107 	}
108 	else
109 		value_node = x.find("value_node")->second.get_value_node();
110 	// We need a dynamic list.
111 	return (ValueNode_DynamicList::Handle::cast_dynamic(value_node) &&
112 			// We need the list not to be looped.
113 			!ValueNode_DynamicList::Handle::cast_dynamic(value_node)->get_loop());
114 }
115 
116 bool
set_param(const synfig::String & name,const Action::Param & param)117 Action::ValueNodeDynamicListLoop::set_param(const synfig::String& name, const Action::Param &param)
118 {
119 	if(!value_node && name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
120 	{
121 		ValueDesc value_desc(param.get_value_desc());
122 		if(!value_desc.parent_is_value_node())
123 			return false;
124 		// Let's check if it is a dynamic list
125 		value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
126 		if (!value_node)
127 		{
128 			// we didn't found a dynamic list, let's check wheter the parent is a composite
129 			if(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_parent_value_node()))
130 			{
131 				ValueNode_Composite::Handle compo(ValueNode_Composite::Handle::cast_dynamic(value_desc.get_parent_value_node()));
132 				ValueNode_BLine::Handle bline=NULL;
133 				std::set<Node*>::iterator iter;
134 				// now check if the grand parent is a 'bline' type
135 				for(iter=compo->parent_set.begin();iter!=compo->parent_set.end();++iter)
136 					{
137 						bline=ValueNode_BLine::Handle::cast_dynamic(*iter);
138 						if(bline)
139 							break;
140 					}
141 				if(bline)
142 					value_node=bline;
143 			}
144 		}
145 		if(!value_node)
146 			return false;
147 		return true;
148 	}
149 	if(!value_node && name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
150 	{
151 		value_node=ValueNode_DynamicList::Handle::cast_dynamic(param.get_value_node());
152 		if(!value_node)
153 			return false;
154 		return true;
155 	}
156 	return Action::CanvasSpecific::set_param(name,param);
157 }
158 
159 bool
is_ready() const160 Action::ValueNodeDynamicListLoop::is_ready()const
161 {
162 	if(!value_node)
163 		return false;
164 	return Action::CanvasSpecific::is_ready();
165 }
166 
167 void
perform()168 Action::ValueNodeDynamicListLoop::perform()
169 {
170 	old_loop_value=value_node->get_loop();
171 	if(old_loop_value==true)
172 	{
173 		set_dirty(false);
174 		return;
175 	}
176 	set_dirty(true);
177 	value_node->set_loop(true);
178 	value_node->changed();
179 }
180 
181 void
undo()182 Action::ValueNodeDynamicListLoop::undo()
183 {
184 	if(old_loop_value==value_node->get_loop())
185 	{
186 		set_dirty(false);
187 		return;
188 	}
189 	set_dirty(true);
190 	value_node->set_loop(old_loop_value);
191 	value_node->changed();
192 }
193