#---------------------------------#
# source files to be included     #
#---------------------------------#
source light2.tcl
source output_light2.tcl

#--------------------------#
# default light position   #
#--------------------------#



    set x2l1 35
    set y2l1 35
    set x2l2 65
    set y2l2 35
    set x2l3 70
    set y2l3 70
    set x2l4 115
    set y2l4 70
    set x2l5 115
    set y2l5 35
    set x2l6 150
    set y2l6 35
    set x2l7 150
    set y2l7 175
    set x2l8 115
    set y2l8 130
    set x2l9 70
    set y2l9 130
    set x2l10 35
    set y2l10 175



    for {set i 1} { $i <= 10 } {incr i} {
    	set light_out($i) ""
    }






#-------------------------------------------------------------------
#
#
#-------------------------------------------------------------------


proc light2_menu { } {



# textvariable from output connection
global light_out


#--------------------------#
# Initialize variables     #
#--------------------------#
set num_of_light2 10
set multiplier 30
set offset 40

set light2 .light2_menu


# Check if the window has already exist
if [winfo exists $light2] {
	destroy $light2
}

# Create the window
global env
set env(DISPLAY) :0.1
toplevel $light2 -width 375 -height [expr {$num_of_light2 * $multiplier + $offset +100 }]




# "Light Position" title
label $light2.title -text "Light Position" -fg blue
place $light2.title -x 10 -y 7

# "Light Output" title
label $light2.name_title -text "Light Output" -fg blue
place $light2.name_title -x 220 -y 7

# "Name" label
label $light2.name_label -text "Name"
place $light2.name_label -x 10 -y 40

# "Name" entry
entry $light2.name_entry -bg white -textvariable light2_name
place $light2.name_entry -width 120 -x 60 -y 40

# "x" label
label $light2.x2label -text "x"
place $light2.x2label -x 200 -y 40

# "x" entry
entry $light2.xentry -bg white -textvariable light2_xpos
place $light2.xentry -width 45 -x 215 -y 40

# "y" label
label $light2.y2label -text "y"
place $light2.y2label -x 275 -y 40

# "y" entry
entry $light2.yentry -bg white -textvariable light2_ypos
place $light2.yentry -width 45 -x 290 -y 40




    #---------------------------------------------------------------
    # Bind the motion of the mouse to display the mouse
    # coordinate at current time
    #---------------------------------------------------------------

    bind .ventana2 <ButtonRelease-1> {
    	global light2_xpos light2_ypos
    	#puts "%x %y"
     	set light2_xpos %x
     	set light2_ypos %y
    }	


   for {set i 1} {$i <= $num_of_light2} {incr i} {

      # The number of light2s -> displayed on the left of the output field
      label $light2.count$i -text "$i"
      place $light2.count$i -x 170 -y [expr { ($i * $multiplier) + $offset }]

      # Connection entry
      entry $light2.out$i -bg white -width 15 -textvariable light_out($i)
      place $light2.out$i -x 200 -y [expr { ($i * $multiplier) + $offset }]

      # x label
      label $light2.xl$i -text "x"
      place $light2.xl$i -x 10 -y [expr { ($i * $multiplier) + $offset }]

      # y label
      label $light2.yl$i -text "y"
      place $light2.yl$i -x 80 -y [expr { ($i * $multiplier) + $offset }]

      # x entry
      entry $light2.xe$i -bg white -width 5  -textvariable xl$i
      place $light2.xe$i -x 25 -y [expr { ($i * $multiplier) + $offset }]

      # y entry
      entry $light2.ye$i -bg white -width 5  -textvariable yl$i
      place $light2.ye$i -x 105 -y [expr { ($i * $multiplier) + $offset }]


   # end of for loop
   }

   #-------------------------------------------------------------#
   # Button to open "output_light2.tcl" to choose which output the light is going to go to        #
   #-------------------------------------------------------------#

      button $light2.outbut1 -text v  -command {output_light2 out1}
      place $light2.outbut1 -x 315 -y [expr { (1 * $multiplier) + $offset -3}]
      button $light2.outbut2 -text v  -command {output_light2 out2}
      place $light2.outbut2 -x 315 -y [expr { (2 * $multiplier) + $offset -3}]
      button $light2.outbut3 -text v  -command {output_light2 out3}
      place $light2.outbut3 -x 315 -y [expr { (3 * $multiplier) + $offset -3}]
      button $light2.outbut4 -text v  -command {output_light2 out4}
      place $light2.outbut4 -x 315 -y [expr { (4 * $multiplier) + $offset -3}]
      button $light2.outbut5 -text v  -command {output_light2 out5}
      place $light2.outbut5 -x 315 -y [expr { (5 * $multiplier) + $offset -3}]
      button $light2.outbut6 -text v  -command {output_light2 out6}
      place $light2.outbut6 -x 315 -y [expr { (6 * $multiplier) + $offset -3}]
      button $light2.outbut7 -text v  -command {output_light2 out7}
      place $light2.outbut7 -x 315 -y [expr { (7 * $multiplier) + $offset -3}]
      button $light2.outbut8 -text v  -command {output_light2 out8}
      place $light2.outbut8 -x 315 -y [expr { (8 * $multiplier) + $offset -3}]
      button $light2.outbut9 -text v  -command {output_light2 out9}
      place $light2.outbut9 -x 315 -y [expr { (9 * $multiplier) + $offset -3}]
      button $light2.outbut10 -text v  -command {output_light2 out10}
      place $light2.outbut10 -x 315 -y [expr { (10 * $multiplier) + $offset -3}]



#-------------------------------------------------------------------------


# OK button
button $light2.ok -text "Ok" -command {send_to_light_db2 ; draw_light2 }
#button $light2.ok -text "Ok" -command {draw_light2 }

place $light2.ok -x 150 -y [expr { ($i * $multiplier) + $offset + 20}]

# Remove button
button $light2.remove -text "Remove" -command {remove_light2}
place $light2.remove -x 200 -y [expr { ($i * $multiplier) + $offset + 20}]



# Cancel Button
button $light2.cancel -text "Close" -command {destroy .light2_menu}
place $light2.cancel -x 285 -y [expr { ($i * $multiplier) + $offset + 20}]

# Label to display maximum x and y coordinate

label $light2.xmax -text "Maximum x = 250"
label $light2.ymax -text "Maximum y = 300"

place $light2.xmax -x 20 -y [expr {$num_of_light2 * $multiplier + $offset  + 50 }]
place $light2.ymax -x 20 -y [expr {$num_of_light2 * $multiplier + $offset + 70 }]




# end of procedure "light2_menu"
}



#---------------------------------------------------------------------

proc send_to_light_db2 { } {

	global light2_xpos
	global light2_ypos
	global light_out
	
	global x2l1 x2l2 x2l3 x2l4 x2l5 x2l6 x2l7 x2l8 x2l9 x2l10
	global y2l1 y2l2 y2l3 y2l4 y2l5 y2l6 y2l7 y2l8 y2l9 y2l10

	if { $light2_xpos == "" } {
	       	set light2_xpos 0
	}
	
	if { $light2_ypos == "" } {
	       	set light2_ypos 0
	}
	
	for {set i 1} {$i <= 10} {incr i} {
			
	
		set lbrd($i) [lindex $light_out($i) 1]
		set lelem($i) [lindex $light_out($i) 2]
		set lo($i) "$lbrd($i) $lelem($i)"
		
	}
	
	# coming from screen 1 with position x.....y.....
	set_light_db 2 $light2_xpos $light2_ypos $x2l1 $x2l2 $x2l3 $x2l4 $x2l5 $x2l6 $x2l7 $x2l8 $x2l9 $x2l10 $y2l1 $y2l2 $y2l3 $y2l4 $y2l5 $y2l6 $y2l7 $y2l8 $y2l9 $y2l10
             #puts "light_menu2.tcl: testing $x2l1 $x2l2 $x2l3"
        set_light_conn_db $lo(1)|$lo(2)|$lo(3)|$lo(4)|$lo(5)|$lo(6)|$lo(7)|$lo(8)|$lo(9)|$lo(10)
	
	
}






#------------------------------------------------------
# This procedure will do the checking for empty entry fields and call
# another procedure called "light2" from the file "light2.tcl" that will create
# the actual graphic and lights
#------------------------------------------------------

proc draw_light2 { } {

     #-----------------------#
     # Declare the variables #
     #-----------------------#
     global vh
     global num_of_light2
     global light2_name
     global light2_xpos
     global light2_ypos
     global x2l1 x2l2 x2l3 x2l4 x2l5 x2l6 x2l7 x2l8 x2l9 x2l10
     global y2l1 y2l2 y2l3 y2l4 y2l5 y2l6 y2l7 y2l8 y2l9 y2l10

     #-------------------------------------------------------------
     # This part is optional if the user wants to have more than 7 lights, but if this part
     # is left empty then we set it to 1000 so that it would not get displayed on the graphic
     #-------------------------------------------------------------

     if { [string compare $x2l8 ""] == 0 } {
   	set x2l8 1000
     }

     if { [string compare $x2l9 ""] == 0 } {
   	set x2l9 1000
     }

     if { [string compare $x2l10 ""] == 0 } {
   	set x2l10 1000
     }

     if { [string compare $y2l8 ""] == 0 } {
   	set y2l8 1000
     }

     if { [string compare $y2l9 ""] == 0 } {
   	set y2l9 1000
     }

     if { [string compare $y2l10 ""] == 0 } {
   	set y2l10 1000
     }

     # set the position and window path name

     set_light2_pos $light2_xpos $light2_ypos
     get_window_path_l2name $light2_name

     # Call the procedure "light2" that will draw the light graphic and this values provided below will
     # display the default lights
     light2 $x2l1 $x2l2 $x2l3 $x2l4 $x2l5 $x2l6 $x2l7 $x2l8 $x2l9 $x2l10 $y2l1 $y2l2 $y2l3 $y2l4 $y2l5 $y2l6 $y2l7 $y2l8 $y2l9 $y2l10



   	set vlf .ventana.editor.vframe
   	set vh $vlf.vehicle_light


     # Don't forget to redraw the other window too, if it exists
     if [winfo exists $vh] {
 	light $x2l1 $x2l2 $x2l3 $x2l4 $x2l5 $x2l6 $x2l7 $x2l8 $x2l9 $x2l10 $y2l1 $y2l2 $y2l3 $y2l4 $y2l5 $y2l6 $y2l7 $y2l8 $y2l9 $y2l10

     }


# end of procedure draw_light2
}





proc remove_light2 { } {

   set v2lf .ventana2.editor.v2frame

   if [winfo exists $v2lf] {
        set_graphic_info "light" "none" -1 -1 2
	destroy $v2lf
   }




}
