#!/usr/bin/X11/wafe --f

set order {3 1 6 2 5 7 15 13 4 11 8 9 14 10 12}
set width 30

mergeResources topLevel *shadowWidth 2 *background wheat
form f topLevel translations "<Key>q: exec(quit)"

for {set i 0} {$i < 15} {set i [expr $i+1]} {
  set num [lindex $order $i]
  set xpos($num) [expr ($i%4)*$width+4]
  set ypos($num) [expr ($i/4)*$width+4]
  command c$num f { 
      label $num callback "move $num" 
      horizDistance $xpos($num) vertDistance $ypos($num) 
      width $width height $width
  }
}
set xpos(space) [expr 3*$width+4]
set ypos(space) [expr 3*$width+4]

proc move {num} {
    global xpos ypos width
    if {(($ypos($num) >= $ypos(space))
            && ($ypos($num) <= $ypos(space))
            && ($xpos($num) >= $xpos(space) - $width)
            && ($xpos($num) <= $xpos(space) + $width))
            || (($xpos($num) >= $xpos(space))
            && ($xpos($num) <= $xpos(space) )
            && ($ypos($num) >= ($ypos(space) - $width))
            && ($ypos($num) <= ($ypos(space) + $width)))} {
        set tmp $xpos(space); set xpos(space) $xpos($num); set xpos($num) $tmp
        set tmp $ypos(space); set ypos(space) $ypos($num); set ypos($num) $tmp
        sV c$num horizDistance [expr $xpos($num)-1] vertDistance [expr $ypos($num)-1]
        sV c$num horizDistance $xpos($num) vertDistance $ypos($num)
    }
}

realize


