#!/usr/bin/env wish

lappend auto_path  $::env(LUSTRE_INSTALL)/tcl
package require Luciole 2.0
namespace import luc::*

# uncomment for debugging
#luc::set_dbg 1

# change font magnification (%)
ezfont::scale 20

# Does not use luc main gui,
# just luc support for access rpexe inputs/outputs

ecexe game "./game.ec" {
   { "c_0_0" "bool" }
   { "c_0_1" "bool" }
   { "c_0_2" "bool" }
   { "c_1_0" "bool" }
   { "c_1_1" "bool" }
   { "c_1_2" "bool" }
   { "c_2_0" "bool" }
   { "c_2_1" "bool" }
   { "c_2_2" "bool" }
	{ "start" "bool" }
} {
   { "x_0_0" "int" }
   { "x_0_1" "int" }
   { "x_0_2" "int" }
   { "x_1_0" "int" }
   { "x_1_1" "int" }
   { "x_1_2" "int" }
   { "x_2_0" "int" }
   { "x_2_1" "int" }
   { "x_2_2" "int" }
	{ "win" "bool" }
}
# Var acces:
#    luc::InputVars(c_i_j)
#    luc::OutputVars(x_i_j)
# Rpexe acces:
#    luc::Prg(user_step)
#    luc::Prg(user_reset)

proc dbg { } {
	puts "Inputs :"
	puts "start=$luc::InputVars(start)"
   for {set x 0} {$x < 3} {incr x} {
   for {set y 0}  {$y < 3} {incr y} {
		puts -nonewline "$luc::InputVars(c_${x}_${y}) "
	}
	puts ""
	}
	puts "Outputs :"
	puts "win=$luc::OutputVars(win)"
   for {set x 0} {$x < 3} {incr x} {
   for {set y 0}  {$y < 3} {incr y} {
		puts -nonewline "$luc::OutputVars(x_${x}_${y}) "
	}
	puts ""
	}
}

proc doclic { x y } {
   set luc::InputVars(c_${x}_${y}) 1
	eval $luc::Prg(user_step)
   set luc::InputVars(c_${x}_${y}) 0
	#dbg
}

proc dostart { } {
   set luc::InputVars(start) 1
	eval $luc::Prg(user_step)
   set luc::InputVars(start) 0
}

proc mkcell { title x y } {
   ptk::begin switch luc::OutputVars(x_${x}_${y}) {0 1 2 3}
      for {set d 0} {$d < 4} {incr d} {
         set imgfile "_imgs/${title}${x}${y}${d}.gif"
         set img "img${x}${y}${d}"
         image create photo $img -file $imgfile
#puts "    mkcell $x $y '$imgfile'"
         ptk::add button -image "$img" -command "doclic $x $y"
      }
   ptk::end switch
}

proc mkcol { title x } {
   ptk::begin col
   for {set y 0}  {$y < 3} {incr y} {
#puts "mkcol $x "
      mkcell $title $x $y
   }
   ptk::end col
}


ptk::begin main "Puzz"
   ptk::begin line
   for {set x 0} {$x < 3} {incr x} {
      mkcol liberte $x
   }
   ptk::end line
	ptk::begin line
		ptk::add button -text "Start" -command dostart
		# mimics std luc outbool
		ptk::begin toggle luc::OutputVars(win)
			ptk::add label -text "win" -fg {#ffffff} -bg {#ff0000}
			ptk::add label -text "win" -fg {#202020} -bg {#efefef}
		ptk::end toggle
	ptk::end line
	# first step to init outputs
	eval $luc::Prg(user_reset)
	eval $luc::Prg(user_step)
	#dbg
ptk::end main

