# AC3D
# (C) Copyright 2003 by Inivis

# AC3D UV Editor

# uvmode is surface or vertex
set uvmode surface


proc uv_do_sensitivity { } {
global uvmode
global UI

	set numvert [ac3d uv_get_selected_vertices]
	set numsurf [ac3d uv_get_selected_surfaces]

#	puts "num selected $numvert $numsurf";

	# if nothing is selected - disable everything
	if { ( $numvert == 0 ) && ($numsurf == 0) } {
		set_sensitive false $UI(uvsnap)
		set_sensitive false $UI(uvremap0) $UI(uvremap1) $UI(uvremap2) $UI(uvremap3)
		set_sensitive false $UI(uvflip0) $UI(uvflip1)
		set_sensitive false $UI(uvrotate0) $UI(uvrotate1)
		return; 
	}
	
	# something is selected

	# don't allow flip or rotate > 0 surfaces or more than one vertex is selected
	if { ( $numvert >1 ) || ($numsurf >0) } {
		set_sensitive true $UI(uvflip0) $UI(uvflip1)
		set_sensitive true $UI(uvrotate0) $UI(uvrotate1)
	} else {
		set_sensitive false $UI(uvflip0) $UI(uvflip1)
		set_sensitive false $UI(uvrotate0) $UI(uvrotate1)
	}

	if { $uvmode == "surface" } {
		set_sensitive true $UI(uvremap0) $UI(uvremap1) $UI(uvremap2) $UI(uvremap3)
		set_sensitive false $UI(uvsnap)
	} else {
		set_sensitive false $UI(uvremap0) $UI(uvremap1) $UI(uvremap2) $UI(uvremap3)
		if { $numvert > 1} {
			set_sensitive true $UI(uvsnap)
		} else {
			set_sensitive false $UI(uvsnap)
		}
	}
}


proc menu_uveditor { } {
global uvmode
global prefs_uveditor_bgcol prefs_uveditor_edgecol
global UI tcl_platform

	if [winfo exists .uvedit] { return }

	toplevel .uvedit
	wm transient .uvedit [winfo toplevel [winfo parent .uvedit ]]


	wm title .uvedit "AC3D Texture Coordinate Editor"


	wm protocol .uvedit WM_DELETE_WINDOW { ac3d closeuv; destroy .uvedit }

	menu .uvedit.mbar -tearoff 0 -type normal
	menubutton .uvedit.mbar.file -text "Window" -menu .mbar.file.menu -underline 0
	.uvedit.mbar add cascade -menu .uvedit.mbar.file.menu -label "Window" -underline 0

	menu .uvedit.mbar.file.menu -tearoff 0

	if { $tcl_platform(platform) == "windows" } {
		.uvedit.mbar.file.menu add command -label "Copy image to windows clipboard" -command "update; ac3d uv_copy_to_clipboard"
		.uvedit.mbar.file.menu add separator
	}


	.uvedit.mbar.file.menu add command -label "Close" -command "ac3d closeuv; destroy .uvedit"


	menubutton .uvedit.mbar.edit -text "Edit" -menu .mbar.file.menu -underline 0
	.uvedit.mbar add cascade -menu .uvedit.mbar.edit.menu -label "Edit" -underline 0

	menu .uvedit.mbar.edit.menu -tearoff 0

	.uvedit.mbar.edit.menu add command -label "Select all" -command "ac3d uv_select_all" -accelerator Ctrl+A
    .uvedit.mbar.edit.menu add separator
    .uvedit.mbar.edit.menu add command -label "Undo" -command "ac3d undo" -accelerator Ctrl+Z -underline 0
    .uvedit.mbar.edit.menu add command -label "Redo" -command "ac3d redo" -accelerator Ctrl+Shift+Z -underline 0


	menubutton .uvedit.mbar.view -text "View" -menu .mbar.view.menu -underline 0
	.uvedit.mbar add cascade -menu .uvedit.mbar.view.menu -label "View" -underline 0

	menu .uvedit.mbar.view.menu -tearoff 0

	.uvedit.mbar.view.menu add command -label "Reset" -command "ac3d uv_view_reset"
	.uvedit.mbar.view.menu add checkbutton -label "Antialias lines" -variable prefs_uveditor_antialias -command "ac3d uv_redisplay"


	.uvedit configure -menu .uvedit.mbar -bd 1



	set cp [frame .uvedit.cp]
	set gl [ frame .uvedit.gl -border 2 -relief sunken]
	set togl [togl $gl.win -rgba true -double true]

	set UI(uveditview) $togl

	set uvinfolab [ label .uvedit.info -text info]
	pack $uvinfolab -side bottom -fill x -expand 0

	set uvinfolab2 [ label .uvedit.info2 -text info]
	pack $uvinfolab2 -side bottom -fill x -expand 0

	pack $cp -side left -fill y
	pack $gl -fill both -expand 1
	pack $togl -fill both -expand 1


	set UI(uv_coor) [label $cp.uvcoor -text ""]
	pack $UI(uv_coor) -side top -fill x

	set sb [section_frame $cp.uvmodetitle "Select by" ]
	pack $cp.uvmodetitle

	set surfacemodebutton [radiobutton $sb.select_mode_surface  -text "Surface" -variable uvmode -value surface -indicatoron true -padx 0 -pady 0 -command \
		{ ac3d uv_selectmode 0 ; uv_do_sensitivity } ]
	pack $surfacemodebutton -anchor w
	set vertexmodebutton [radiobutton $sb.select_mode_vertex  -text "Vertex" -variable uvmode -value vertex -indicatoron true -padx 0 -pady 0 -command "ac3d uv_selectmode 0" -indicatoron true -padx 0 -pady 0 -command \
		{ ac3d uv_selectmode 1 ; uv_do_sensitivity } ]
	pack $vertexmodebutton -anchor w


	set self [section_frame $cp.sel "Select" ]
	pack $cp.sel -fill x
	cbutton $self.b  "All" "ac3d uv_select_all " "select everything"
	cbutton $self.b2  "None" "ac3d uv_clear_selection " "select nothing"
	pack $self.b $self.b2 -side left -fill both -expand 1



	set fp [ section_frame $cp.flip "Flip" ]
	pack $cp.flip -fill x 
	set UI(uvflip0) [cbutton $fp.b  "Horiz" "ac3d uv_flip 1" "flip selection horizontaly"]

	set UI(uvflip1) [cbutton $fp.b2  "Vert" "ac3d uv_flip 0" "flip selection vertically"]
	pack $fp.b $fp.b2 -side left -fill both -expand 1


	set rp [ section_frame $cp.rotate "Rotate" ]
	pack $cp.rotate -fill x

	set UI(uvrotate0) [cbutton $rp.b3  "CW" "ac3d uv_rotate 0" "rotate the selection 90 degrees clockwise"]
	pack $rp.b3 -side left -fill both -expand 1

	set UI(uvrotate1) [cbutton $rp.b4  "CCW" "ac3d uv_rotate 1" "rotate the selection 90 degrees counter-clockwise"]
	pack $rp.b4 -side left -fill both -expand 1






	set remf [section_frame $cp.remap "Remap" ]
	pack $cp.remap -fill x

	set UI(uvremap0) [cbutton $remf.b6  "front (XY)" "ac3d uv_remap 0" "map selected surfaces from front"]
	pack $remf.b6 -fill both -expand 1

	set UI(uvremap1) [cbutton $remf.b7  "side (ZY)" "ac3d uv_remap 1 " "map selected surfaces side"]
	pack $remf.b7 -fill both -expand 1

	set UI(uvremap2) [cbutton $remf.b8  "top (XZ)" "ac3d uv_remap 2" "map selected surfaces top"]
	pack $remf.b8 -fill both -expand 1

	set UI(uvremap3) [cbutton $remf.b9  "spherical" "ac3d uv_remap 3" "map selected surfaces spherically"]
	pack $remf.b9 -fill both -expand 1


	set UI(uvsnap) [cbutton $cp.b5  "Snap together" "ac3d uv_snap_together" "move all selected uvs to the same point"]
	pack $cp.b5



#	set b1 [ coledit_button $cp.bgbutton2 prefs_uveditor_edgecol "Edges" ]
#	pack $b1

#	set b [ coledit_button $cp.bgbutton prefs_uveditor_selectedcol "Selected" ]
#	pack $b

	# tell AC3D about this UV editor
	ac3d inituv $togl

	# set the mode in the C code
	if {$uvmode == "surface" } {
		ac3d uv_selectmode 0
	} else {
		ac3d uv_selectmode 1
	}

	bind .uvedit <Control-z> "ac3d undo"
	bind .uvedit <Control-Z> "ac3d redo"

    bind .uvedit <Control-t> {ac3d uv_snap_together} 
    bind .uvedit <Control-a> {ac3d uv_select_all}

bind .uvedit <Enter> {focus .uvedit}

#	bind $gl <Configure> { puts "conf"}

	centre_window_on_screen .uvedit
	uv_do_sensitivity
}



# zoom called from mousewheel binding in ac3dview.tcl

proc uvedit_zoom { a} {
	ac3d uv_zoom $a
}

