1 ## Copyright (C) 2005-2012 Etienne Grossmann <etienne@egdn.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## s = vrml_TimeSensor (...) - Low-level vrml TimeSensor node
18 ## s is a vrml node with possible fields :
19 ## ------------------------------------------------------------------
21 ## exposedField SFTime cycleInterval 1 # (0,inf)
22 ## exposedField SFBool enabled TRUE
23 ## exposedField SFBool loop FALSE
24 ## exposedField SFTime startTime 0 # (-inf,inf)
25 ## exposedField SFTime stopTime 0 # (-inf,inf)
26 ## eventOut SFTime cycleTime
27 ## eventOut SFFloat fraction_changed # [0, 1]
28 ## eventOut SFBool isActive
29 ## eventOut SFTime time
31 ## ------------------------------------------------------------------
34 ## Beyond all the fields of the node, it is also possible to use the option
36 ## "DEF", name : The created node will be preceded by 'DEF name ', so that
37 ## it is further possible to refer to it.
41 function s = vrml_TimeSensor (varargin)
45 tpl = struct ("cycleInterval", "SFTime",\
46 "startTime", "SFTime",\
47 "stopTime", "SFTime",\
55 # Transform varargin into key-value pairs
56 i = j = k = 1; # i:pos in new varargin, j:pos in headpar,
57 # k:pos is old varargin.
58 while i <= length (varargin) && \
59 ! (ischar (varargin{i}) && isfield (tpl, varargin{i}))
61 if j <= length (headpar)
64 printf ("vrml_TimeSensor : Assume arg %i is '%s'\n",k,headpar{j});
67 ##varargin = splice (varargin, i, 0, headpar(j));
68 varargin = {varargin{1:i-1}, headpar(j), varargin{i:end}};
73 error ("vrml_TimeSensor : Argument %i should be string, not '%s'",\
74 k,typeinfo (varargin{i}));
80 if rem (length (varargin), 2), error ("vrml_TimeSensor : Odd n. of arguments"); end
82 l = {"TimeSensor {\n"};
84 while i < length (varargin)
86 k = varargin{i++}; # Read key
89 error ("vrml_TimeSensor : Arg n. %i should be a string, not a %s.",\
92 if ! isfield (tpl, k) && ! strcmp (k,"DEF")
93 error ("vrml_TimeSensor : Unknown field '%s'. Should be one of :\n%s",\
94 k, sprintf (" '%s'\n",fieldnames (tpl)'{:}));
97 v = varargin{i++}; # Read value
101 if verbose, printf ("vrml_TimeSensor : Defining node '%s'\n",v); end
103 if DEF, error ("vrml_TimeSensor : Multiple DEFs found"); end
104 l = {sprintf("DEF %s ", v), l{:}};
107 else # Add data field
110 printf ("vrml_TimeSensor : Adding '%s' of type %s, with arg of type %s\n",\
111 k,getfield(tpl,k),typeinfo (v));
113 tmp = getfield(tpl,k);
114 if strcmp (tmp(2:end), "FNode")
116 if verbose, printf ("vrml_TimeSensor : Trying to learn type of node\n"); end
118 if iscell (v) # v is list of arguments
120 # Check whether 1st arg is node type's name.
123 if all (exist (["vrml_",tn]) != [2,3,5])
124 # If it isn't type's name, use default type.
125 if isfield (dnode, k)
127 printf ("vrml_TimeSensor : Using default type : %s\n",getfield(dnode,k));
129 v = {getfield(dnode,k), v{:}};
131 error ("vrml_TimeSensor : Can't determine type of node '%s'",k);
135 printf ("vrml_TimeSensor : 1st list element is type : %s\n",tn);
138 # If v is not a list, maybe it has a default
139 # node type type (otherwise, it's be sent
141 elseif isfield (dnode, k)
143 printf ("vrml_TimeSensor : Using default type : %s\n",dnode.(k));
145 v = {getfield(dnode,k), v{:}};
148 l = {l{:}, k, " ", data2vrml(getfield(tpl,k),v),"\n"};
157 s = [s, sprintf(l{i})];
159 ### Stupid strcat removes trailing spaces in l's elements
160 ### s = strcat (l{:});