format
This commit is contained in:
parent
7e27eeaae1
commit
36651b8faa
1 changed files with 94 additions and 69 deletions
163
traceur.xsl
163
traceur.xsl
|
@ -6,162 +6,187 @@
|
|||
<xsl:output method="xml" indent="yes" />
|
||||
|
||||
<!-- Déclaration des variables initiales -->
|
||||
<xsl:variable name="initial-x" select="0" as="xs:double" />
|
||||
<xsl:variable
|
||||
<xsl:variable name="initial-x" select="0" as="xs:double" />
|
||||
<xsl:variable
|
||||
name="initial-y" select="0" as="xs:double" />
|
||||
<xsl:variable name="initial-angle" select="0"
|
||||
<xsl:variable name="initial-angle" select="0"
|
||||
as="xs:double" />
|
||||
|
||||
<xsl:template match="/tortue">
|
||||
<xsl:template match="/tortue">
|
||||
<xsl:processing-instruction name="xml-model">href="traceur.xsd"</xsl:processing-instruction>
|
||||
<traceur>
|
||||
<!-- Appel du template pour traiter les commandes avec les valeurs initiales des
|
||||
paramètres -->
|
||||
<traceur>
|
||||
<!-- Appel du template pour traiter les commandes
|
||||
avec les valeurs initiales des paramètres -->
|
||||
<xsl:call-template name="process-commands">
|
||||
<xsl:with-param name="commands" select="*" />
|
||||
<xsl:with-param name="x"
|
||||
<xsl:with-param name="commands"
|
||||
select="*" />
|
||||
<xsl:with-param name="x"
|
||||
select="$initial-x" />
|
||||
<xsl:with-param
|
||||
name="y" select="$initial-y" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param name="y"
|
||||
select="$initial-y" />
|
||||
<xsl:with-param
|
||||
name="angle" select="$initial-angle" />
|
||||
<xsl:with-param name="states" select="()" />
|
||||
<xsl:with-param name="states"
|
||||
select="()" />
|
||||
</xsl:call-template>
|
||||
</traceur>
|
||||
</xsl:template>
|
||||
|
||||
<!-- Template récursif pour traiter les commandes individuelles -->
|
||||
<xsl:template
|
||||
<xsl:template
|
||||
name="process-commands">
|
||||
<xsl:param name="commands" />
|
||||
<xsl:param name="x" />
|
||||
<xsl:param name="y" />
|
||||
<xsl:param
|
||||
<xsl:param
|
||||
name="commands" />
|
||||
<xsl:param
|
||||
name="x" />
|
||||
<xsl:param
|
||||
name="y" />
|
||||
<xsl:param
|
||||
name="angle" />
|
||||
<xsl:param
|
||||
<xsl:param
|
||||
name="states" />
|
||||
|
||||
<!-- Si il reste des commandes à traiter -->
|
||||
<xsl:if test="$commands">
|
||||
<xsl:if test="$commands">
|
||||
<!-- Sélection de la première commande à traiter -->
|
||||
<xsl:variable
|
||||
name="current-command" select="$commands[1]" />
|
||||
<xsl:choose>
|
||||
<xsl:variable name="current-command"
|
||||
select="$commands[1]" />
|
||||
<xsl:choose>
|
||||
<!-- Si la commande est une ligne -->
|
||||
<xsl:when test="local-name($current-command) = 'line'">
|
||||
<!-- Calcul de la distance à parcourir et conversion de l'angle en radians -->
|
||||
<xsl:variable name="distance"
|
||||
<xsl:variable name="distance"
|
||||
select="number($current-command)" />
|
||||
<xsl:variable name="radians"
|
||||
<xsl:variable name="radians"
|
||||
select="$angle * math:pi() div 180" />
|
||||
<xsl:variable name="new-x"
|
||||
<xsl:variable name="new-x"
|
||||
select="$x + $distance * math:cos($radians)" />
|
||||
<xsl:variable name="new-y"
|
||||
<xsl:variable name="new-y"
|
||||
select="$y + $distance * math:sin($radians)" />
|
||||
|
||||
<!-- Création de l'élément LINETO avec les nouvelles coordonnées calculées -->
|
||||
<LINETO
|
||||
<LINETO
|
||||
x="{format-number($new-x, '#.######')}"
|
||||
y="{format-number($new-y, '#.######')}" />
|
||||
|
||||
<!-- Rappel récursif du template avec la mise à jour des coordonnées et des
|
||||
commandes restantes -->
|
||||
<xsl:call-template
|
||||
<xsl:call-template
|
||||
name="process-commands">
|
||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
name="x" select="$new-x" />
|
||||
<xsl:with-param name="y" select="$new-y" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="y" select="$new-y" />
|
||||
<xsl:with-param
|
||||
name="angle" select="$angle" />
|
||||
<xsl:with-param name="states" select="$states" />
|
||||
<xsl:with-param
|
||||
name="states" select="$states" />
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
|
||||
<!-- Si la commande est un turn -->
|
||||
<xsl:when test="local-name($current-command) = 'turn'">
|
||||
<!-- Calcul du nouvel angle avec modulo 360 pour rester dans un cercle complet -->
|
||||
<xsl:variable
|
||||
name="new-angle" select="($angle + number($current-command)) mod 360" />
|
||||
<xsl:variable name="new-angle"
|
||||
select="($angle + number($current-command)) mod 360" />
|
||||
|
||||
<!-- Rappel récursif du template sans changer de position mais en mettant à jour
|
||||
l'angle -->
|
||||
<xsl:call-template
|
||||
<xsl:call-template
|
||||
name="process-commands">
|
||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
name="x" select="$x" />
|
||||
<xsl:with-param name="y" select="$y" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="y" select="$y" />
|
||||
<xsl:with-param
|
||||
name="angle" select="$new-angle" />
|
||||
<xsl:with-param name="states"
|
||||
<xsl:with-param name="states"
|
||||
select="$states" />
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
|
||||
<!-- Si la commande est un MOVE -->
|
||||
<xsl:when test="local-name($current-command) = 'move'">
|
||||
<!-- Calcul de la distance à parcourir et conversion de l'angle en radians -->
|
||||
<xsl:variable name="distance"
|
||||
<xsl:variable name="distance"
|
||||
select="number($current-command)" />
|
||||
<xsl:variable name="radians"
|
||||
<xsl:variable name="radians"
|
||||
select="$angle * math:pi() div 180" />
|
||||
<xsl:variable name="new-x"
|
||||
<xsl:variable name="new-x"
|
||||
select="$x + $distance * math:cos($radians)" />
|
||||
<xsl:variable name="new-y"
|
||||
<xsl:variable name="new-y"
|
||||
select="$y + $distance * math:sin($radians)" />
|
||||
|
||||
<!-- Création de l'élément MOVETO avec les nouvelles coordonnées calculées -->
|
||||
<MOVETO
|
||||
<MOVETO
|
||||
x="{format-number($new-x, '#.######')}"
|
||||
y="{format-number($new-y, '#.######')}" />
|
||||
|
||||
<!-- Rappel récursif du template avec la mise à jour des coordonnées et des
|
||||
commandes restantes -->
|
||||
<xsl:call-template
|
||||
<xsl:call-template
|
||||
name="process-commands">
|
||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
name="x" select="$new-x" />
|
||||
<xsl:with-param name="y" select="$new-y" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="y" select="$new-y" />
|
||||
<xsl:with-param
|
||||
name="angle" select="$angle" />
|
||||
<xsl:with-param name="states" select="$states" />
|
||||
<xsl:with-param name="states"
|
||||
select="$states" />
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
|
||||
<!-- Si la commande est un store -->
|
||||
<xsl:when test="local-name($current-command) = 'store'">
|
||||
<!-- On enregistre le nouvelle état-->
|
||||
<xsl:variable name="new-state">
|
||||
<xsl:variable name="new-state">
|
||||
<state x="{$x}" y="{$y}" angle="{$angle}" />
|
||||
</xsl:variable>
|
||||
<!-- On l'ajoute au précédent -->
|
||||
<xsl:variable
|
||||
<xsl:variable
|
||||
name="new-states" select="($states, $new-state)" />
|
||||
<xsl:call-template
|
||||
<xsl:call-template
|
||||
name="process-commands">
|
||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
name="x" select="$x" />
|
||||
<xsl:with-param name="y" select="$y" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="y" select="$y" />
|
||||
<xsl:with-param
|
||||
name="angle" select="$angle" />
|
||||
<xsl:with-param name="states"
|
||||
<xsl:with-param name="states"
|
||||
select="$new-states" />
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
|
||||
<!-- Si la commande est un restore et que au moins 1 état est enregistré : -->
|
||||
<xsl:when test="local-name($current-command) = 'restore' and count($states)>=1">
|
||||
<xsl:when test="local-name($current-command) = 'restore' and count($states) >= 1">
|
||||
<!-- On séléctionne notre state-->
|
||||
<xsl:variable
|
||||
<xsl:variable
|
||||
name="state" select="$states/state[1]" />
|
||||
<xsl:variable
|
||||
<xsl:variable
|
||||
name="remaining-states"
|
||||
select="subsequence($states/state, 1, count($states/state) - 1)" />
|
||||
|
||||
<xsl:call-template
|
||||
name="process-commands">
|
||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
<xsl:with-param
|
||||
name="commands" select="$commands[position() > 1]" />
|
||||
<xsl:with-param
|
||||
name="x" select="($state/@x)[last()]" />
|
||||
<xsl:with-param name="y"
|
||||
<xsl:with-param name="y"
|
||||
select="($state/@y)[last()]" />
|
||||
<xsl:with-param name="angle"
|
||||
<xsl:with-param name="angle"
|
||||
select="($state/@angle)[last()]" />
|
||||
<xsl:with-param name="states"
|
||||
<xsl:with-param name="states"
|
||||
select="$remaining-states" />
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
|
|
Reference in a new issue