# AC3D
# (C) Copyright 2003 by Inivis


# some tcl code to make the frames resizeable
# very flickery but it works

proc frame_press { win } {
global viewsetting 
global prefs_window_3d_width_percent
global prefs_window_3d_height_percent
global frame_move_horiz
global frame_move_vert
global frame

    if { $viewsetting != "all" } { return }

set col white
$frame(xy) configure -bg $col
$frame(zy) configure -bg $col
$frame(xz) configure -bg $col
$frame(xyz) configure -bg $col


    set rx [winfo rootx .views]
    set ry [winfo rooty .views]
    set x [winfo pointerx $win]
    set y [winfo pointery $win]
    set xd [expr $x-$rx]
    set yd [expr $y-$ry]

    set w [winfo width .views]
    set h [winfo height .views ]

    set w2 [expr 100 - int (($xd.0/$w.0) *100)]
    set h2 [expr 100 - int (($yd.0/$h.0) *100)]

    set ha [expr abs( $prefs_window_3d_width_percent - $w2) ]
    set va [expr abs( $prefs_window_3d_height_percent - $h2) ]

    set frame_move_horiz false
    set frame_move_vert false


    if { ( $ha < 5 ) && ($va < 5) } {
        set frame_move_horiz true
        set frame_move_vert true
    } elseif { ($ha < 5) } {
        set frame_move_horiz true
    } elseif { ($va < 5) } {
        set frame_move_vert true
    }
}

proc frame_drag {win} {
global prefs_window_3d_width_percent
global prefs_window_3d_height_percent
global viewsetting 
global frame_move_horiz
global frame_move_vert
global frame

    if { $viewsetting != "all" } { return }

    set rx [winfo rootx .views]
    set ry [winfo rooty .views]
    set x [winfo pointerx $win]
    set y [winfo pointery $win]
    set xd [expr $x-$rx]
    set yd [expr $y-$ry]

    set w [winfo width .views]
    set h [winfo height .views ]

    set w2 [expr 100 - int (($xd.0/$w.0) *100)]
    set h2 [expr 100 - int (($yd.0/$h.0) *100)]

    if { ($w2 > 5) && ($w2 < 95) } {
        if { ($frame_move_horiz) } {
            set prefs_window_3d_width_percent $w2
        }
    }
    if { ($h2 > 5) && ($h2 < 95) } {
        if { $frame_move_vert } {
            set prefs_window_3d_height_percent $h2
        }
    }

    configure_views
}

proc frame_release {win} {
global frame

set col grey
$frame(xy) configure -bg $col
$frame(zy) configure -bg $col
$frame(xz) configure -bg $col
$frame(xyz) configure -bg $col

}


proc bind_frame_resize {} {
global frame

bind $frame(xy) <1> { frame_press %W }
bind $frame(zy) <1> { frame_press %W }
bind $frame(xz) <1> { frame_press %W }
bind $frame(xyz) <1> { frame_press %W }

bind $frame(xy) <B1-Motion> { frame_drag %W }
bind $frame(zy) <B1-Motion> { frame_drag %W }
bind $frame(xz) <B1-Motion> { frame_drag %W }
bind $frame(xyz) <B1-Motion> { frame_drag %W }

bind $frame(xy) <ButtonRelease-1> { frame_release %W }
bind $frame(zy) <ButtonRelease-1> { frame_release %W }
bind $frame(xz) <ButtonRelease-1> { frame_release %W }
bind $frame(xyz) <ButtonRelease-1> { frame_release %W }

}


proc configure_views {} {
global viewsetting
global prefs_window_3d_width_percent
global prefs_window_3d_height_percent
global frame

    set w [winfo width .views]
    set h [winfo height .views ]
    set w2 [expr $w/2]
    set h2 [expr $h/2]


   set w3 [expr ($w/100.0)*$prefs_window_3d_width_percent]
   set h3 [expr ($h/100.0)*$prefs_window_3d_height_percent]
   set fw [expr $w-$w3]
   set fh [expr $h-$h3]

    switch $viewsetting {
        "all" {
            .views itemconfigure xy -width $fw -height $fh  
            .views coords xy 0 0

            .views itemconfigure zy -width $w3 -height $fh
            .views coords zy $fw 0

            .views itemconfigure xz -width $fw -height $h3
            .views coords xz 0 $fh

            .views itemconfigure xyz -width $w3 -height $h3 
            .views coords xyz $fw $fh
         }
       "xy" {
            .views coords xy 0 0 
            .views coords zy 5000 5000 
            .views coords xz 5000 5000 
            .views coords xyz 5000 5000 
            .views itemconfigure xy -width $w -height $h
        }
       "zy" {
            .views coords zy 0 0 
            .views coords xy 5000 5000 
            .views coords xz 5000 5000 
            .views coords xyz 5000 5000 
            .views itemconfigure zy -width $w -height $h
        }
       "xz" {
            .views coords xz 0 0 
            .views coords zy 5000 5000 
            .views coords xy 5000 5000 
            .views coords xyz 5000 5000 
            .views itemconfigure xz -width $w -height $h
        }
       "xyz" {
            .views coords xyz 0 0 
            .views coords zy 5000 5000 
            .views coords xy 5000 5000 
            .views coords xz 5000 5000 
            .views itemconfigure xyz -width $w -height $h
        }
    }

}


proc view_max_min { viewid winname } {
global viewsetting

	if { $viewsetting == "all" } {
		#  all windows are now displayed - set view to current window
		if { $viewid == 0 } {
		set viewsetting xy ; configure_views ; display_message "Front XY"
		} elseif {$viewid == 1 } {
			set viewsetting zy ; configure_views ; display_message "Side ZY"
		} elseif {$viewid == 2 } {
			set viewsetting xz ; configure_views ; display_message "Plan XZ"
		} elseif {$viewid == 3 } {
			set viewsetting xyz ; configure_views ; display_message "3D XYZ"
		}

	} else {
		# single view is currently displayed - go back to all
		set viewsetting all ; configure_views ; display_message "All views"
	}

}




# the window names are stored in ToglWinName - AC3D pulls the full names
# from this and makes these the view windows

proc make_views {} {
global viewsetting copycontext
global frame ToglWinName
global prefs_share_opengl_contexts

	# MAKE THE 3D WINDOW
	frame .views.xyzframe -bd 2 -relief sunken
	frame .views.xyzframe.f 
	pack .views.xyzframe.f  -fill both -expand 1
	togl .views.xyzframe.f.win -rgba true -double true -depth true 
	pack .views.xyzframe.f.win  -fill both -expand 1
	set frame(xyz) .views.xyzframe
	set ToglWinName(xyz) .views.xyzframe.f.win



	# note that the FRONT XY window has the main context - this is shared with 
	#the other two orth windows

	set copycontext contextwin

	frame .views.xyframe -bd 2 -relief sunken
	frame .views.xyframe.f 
	pack .views.xyframe.f  -fill both -expand 1
	if { ($prefs_share_opengl_contexts) } {
		puts "OpenGL windows - sharing orth contexts"
		togl .views.xyframe.f.win -rgba true -double true -depth true -ident $copycontext
	} else {
		puts "OpenGL windows - unique orth contexts"
		togl .views.xyframe.f.win -rgba true -double true -depth true
	}

	pack .views.xyframe.f.win  -fill both -expand 1
	set frame(xy) .views.xyframe
	set ToglWinName(xy) .views.xyframe.f.win


	frame .views.zyframe -bd 2 -relief sunken
	frame .views.zyframe.f 
	pack .views.zyframe.f  -fill both -expand 1
	if {$prefs_share_opengl_contexts} {
		togl .views.zyframe.f.win -rgba true -double true -depth true -sharelist $copycontext -sharecontext $copycontext
	} else {
		togl .views.zyframe.f.win -rgba true -double true -depth true
	}

	pack .views.zyframe.f.win  -fill both -expand 1
	set frame(zy) .views.zyframe
	set ToglWinName(zy) .views.zyframe.f.win



	frame .views.xzframe  -bd 2 -relief sunken
	frame .views.xzframe.f 
	pack .views.xzframe.f  -fill both -expand 1
	if {$prefs_share_opengl_contexts} {
		togl .views.xzframe.f.win -rgba true -double true -depth true -sharelist $copycontext -sharecontext $copycontext
	} else {
		togl .views.xzframe.f.win -rgba true -double true -depth true
	}

	pack .views.xzframe.f.win  -fill both -expand 1
	set frame(xz) .views.xzframe
	set ToglWinName(xz) .views.xzframe.f.win


        bind $frame(xy) <Enter> {focus .}
        bind $frame(zy) <Enter> {focus .}
        bind $frame(xz) <Enter> {focus .}
        bind $frame(xyz) <Enter> {focus .}


	set viewsetting all


	.views create window 0 0 -anchor nw -window $frame(xy) -tags  xy
	.views create window 400 0 -window $frame(zy) -anchor nw -tags zy
	.views create window 0 400 -window $frame(xz) -anchor nw -tags xz 
	.views create window 400 400  -window $frame(xyz) -anchor nw -tags xyz

	bind .views <Configure> { configure_views }

	#bind all <Escape> {ac3d clear_selection}


	bind all <MouseWheel> {
		set x %X
		set y %Y
		set d %D
		set over [winfo containing $x $y]
		set z [expr $d/4]
	#puts "mousewheel $over"

		if {$over == $ToglWinName(xy) } { ac3d zoomview 0 $z }
		if {$over == $ToglWinName(zy) } { ac3d zoomview 1 $z }
		if {$over == $ToglWinName(xz) } { ac3d zoomview 2 $z }
		if {$over == $ToglWinName(xyz) } { ac3d zoomview 3 $z }

		# zoom the uv editor if necc
		catch {	if {$over == $UI(uveditview) } { uvedit_zoom $z } }
	}

	# stop window delete winodw message from alt-f4 causing a keypress
	bind . <Alt-F4> {  }

	bind . <KeyPress> {
		set x %X
		set y %Y
		set d %D
		set over [winfo containing $x $y]
#		puts "TCLTK key $over Keycode: %k   KeySym: %K %s" 

		if { $over == $ToglWinName(xy) } {
			set view 0 
		} elseif { $over == $ToglWinName(zy) } {
			 set view 1 
		} elseif { $over == $ToglWinName(xz) } {
			 set view 2 
		} elseif { $over == $ToglWinName(xyz) } {
			 set view 3 
		} else {
			set view -1
		}

		if { $view != -1 } {
			set key %k
			set keysym %K
			if { $keysym == "space" } {
				 view_max_min $view $over
			} else {
				ac3d keypress $view %K %s
			}
		} else {
#			puts "key not over a view"

		}
	}


	bind_frame_resize

}






















