Process SMIL directives in XML data: Difference between revisions

(Added Go)
Line 190:
<Appearance>
<Material diffuseColor="0.20 0.56 0.80"/>
</Appearance>
</Shape>
</Scene>
</X3D>
</pre>
 
=={{header|Phix}}==
<lang Phix>include builtins\xml.e
 
constant xml = """
<?xml version="1.0" ?>
<smil>
<X3D>
<Scene>
<Viewpoint position="0 0 8" orientation="0 0 1 0"/>
<PointLight color='1 1 1' location='0 2 0'/>
<Shape>
<Box size='2 1 2'>
<animate attributeName="size" from="2 1 2"
to="1 2 1" begin="0s" dur="10s"/>
</Box>
<Appearance>
<Material diffuseColor='0.0 0.6 1.0'>
<animate attributeName="diffuseColor" from="0.0 0.6 1.0"
to="1.0 0.4 0.0" begin="0s" dur="10s"/>
</Material>
</Appearance>
</Shape>
</Scene>
</X3D>
</smil>
"""
 
function scan_all(sequence s, fmt)
for i=1 to length(s) do
{{s[i]}} = scanf(s[i],fmt)
end for
return s
end function
 
function animate_contents(sequence doc, atom t)
sequence a = xml_get_nodes(doc,"animate")
if a={} then
for i=1 to length(doc[XML_CONTENTS]) do
doc[XML_CONTENTS][i] = animate_contents(doc[XML_CONTENTS][i],t)
end for
else
for i=1 to length(doc[XML_CONTENTS]) do
if doc[XML_CONTENTS][i][XML_TAGNAME]="animate" then
string name = xml_get_attribute(doc[XML_CONTENTS][i],"attributeName"),
vfrm = xml_get_attribute(doc[XML_CONTENTS][i],"from"),
v_to = xml_get_attribute(doc[XML_CONTENTS][i],"to"),
sbeg = xml_get_attribute(doc[XML_CONTENTS][i],"begin"),
sdur = xml_get_attribute(doc[XML_CONTENTS][i],"dur")
sequence from = scan_all(split(vfrm),"%f"),
to_s = scan_all(split(v_to),"%f")
atom {{begin}} = scanf(sbeg,"%fs"),
{{durat}} = scanf(sdur,"%fs"),
fj = begin+durat-t,
tj = t-begin
-- plenty more error handling possible here...
if tj<0 or fj<0 or length(from)!=length(to_s) then ?9/0 end if
for j=1 to length(from) do
from[j] = sprintf("%.2f",(from[j]*fj+to_s[j]*tj)/durat)
end for
doc = xml_set_attribute(doc,name,join(from," "))
doc[XML_CONTENTS][i..i] = "" -- remove 'animate'
exit
end if
end for
end if
return doc
end function
 
function animate(sequence doc, atom t)
doc[XML_CONTENTS] = doc[XML_CONTENTS][XML_CONTENTS][1] -- remove smil
doc[XML_CONTENTS] = animate_contents(doc[XML_CONTENTS],t)
return doc
end function
 
sequence doc = xml_parse(xml)
if doc[XML_DOCUMENT]!="document"
or doc[XML_CONTENTS][XML_TAGNAME]!="smil"
or length(doc[XML_CONTENTS][XML_CONTENTS])!=1
or doc[XML_CONTENTS][XML_CONTENTS][1][XML_TAGNAME]!="X3D" then
?9/0
end if
printf(1,"At time = 0:\n\n")
puts(1,xml_sprint(animate(doc,0)))
printf(1,"At time = 2:\n\n")
puts(1,xml_sprint(animate(doc,2)))</lang>
{{out}}
<pre>
At time = 0:
 
<?xml version="1.0" ?>
<X3D>
<Scene>
<Viewpoint position="0 0 8" orientation="0 0 1 0" />
<PointLight color="1 1 1" location="0 2 0" />
<Shape>
<Box size="2.00 1.00 2.00" />
<Appearance>
<Material diffuseColor="0.00 0.60 1.00" />
</Appearance>
</Shape>
</Scene>
</X3D>
At time = 2:
 
<?xml version="1.0" ?>
<X3D>
<Scene>
<Viewpoint position="0 0 8" orientation="0 0 1 0" />
<PointLight color="1 1 1" location="0 2 0" />
<Shape>
<Box size="1.80 1.20 1.80" />
<Appearance>
<Material diffuseColor="0.20 0.56 0.80" />
</Appearance>
</Shape>
7,795

edits