#
######################################################
#
#  SaVi by Robert Thurman (thurman@geom.umn.edu) and
#          Patrick Worfolk (worfolk@alum.mit.edu).
#
#  Copyright (c) 1997 by The Geometry Center.
#  This file is part of SaVi.  SaVi is free software;
#  you can redistribute it and/or modify it only under
#  the terms given in the file COPYRIGHT which you should
#  have received along with this file.  SaVi may be
#  obtained from:
#  http://savi.sourceforge.net/
#  http://www.geom.uiuc.edu/locate/SaVi
#
######################################################
#
# fisheye.tcl
#
# $Id: fisheye.tcl,v 1.5 2004/01/06 03:57:56 lloydwood Exp $


proc fisheye(build) {} {
    global fisheye_flag fisheye_flag_dismissed Fisheye

    if [winfo exists .fisheye] {
	wm deiconify .fisheye
	raise .fisheye
	return
    }

    set name [build_Toplevel fisheye]

    wm protocol $name WM_DELETE_WINDOW fisheye(dismiss)

    build_Title $name "Fisheye view of the local sky"

    set cmd [build_CmdFrame $name cmd]

    set p "$cmd.p"
    plot $p -bg black -height 300 -width 300
    pack $p -side right
    pack $p -fill both -expand 1

    build_Optionslist $cmd olist \
	{"Update display" fisheye_flag}

    build_Optionslist $cmd olist1 \
	{"Show mask elevation" fisheye_print_inview_flag} \
	{"Show satellite labels" fisheye_labels_flag}

    build_Optionslist $cmd olist2 \
	{"Reverse fisheye view" fisheye_reverse_view_flag} \
	{"Track satellite passes" fisheye_vapors_flag}

    build_LabelEntryColumns $cmd le1 \
        {label "" {time fisheye_lat_lon}}

    build_LabelEntryColumns $cmd le0 \
	{text " " {"Lat:" "Long:"}} \
	      {ientry "degrees" {Fisheye(lat_degrees) Fisheye(long_degrees)} } \
	{ientry "minutes" {Fisheye(lat_minutes) Fisheye(long_minutes)} }

    pack $cmd -fill both -expand 1

    # an explicit Dismiss Window button halts computation.
    build_DismissButtonbar $name dbbar fisheye(dismiss) \
	{"Use new coordinates" fisheye(apply)}
    fisheye(update)
    fisheye(draw)
    set fisheye_flag $fisheye_flag_dismissed
}

proc fisheye(dismiss) {} {
    global fisheye_flag fisheye_flag_dismissed

    set fisheye_flag_dismissed $fisheye_flag
    set fisheye_flag 0
    destroy .fisheye
}

proc fisheye(draw) {} {
    global FS_D

    # initialize display

    set p ".fisheye.cmd.p"
    if [winfo exists $p] {

      $p clear
      fisheye(resize)

      set r [expr $FS_D/2-1]
      set offset [expr $r+2]
      $p plot $offset $offset -symbol disk -size $r -color white
      $p plot $offset $offset -symbol circle -size $r -color grey
      $p plot $offset $offset -symbol circle -size [expr $r*2/3] -color grey
      $p plot $offset $offset -symbol circle -size [expr $r/3] -color grey
      $p plot $offset $offset -symbol point -size small -color grey
      if {$r>100} {
        $p plot $offset $offset -symbol circle -size [expr $r/2] -color grey
        $p plot $offset $offset -symbol circle -size [expr $r/6] -color grey
        $p plot $offset $offset -symbol circle -size [expr $r*5/6] -color grey
      }
   }
}


proc fisheye(resize) {} {

    # update C with current smallest dimension of window.

    set p ".fisheye.cmd.p"
    set diam [eval winfo width $p]
    set height [eval winfo height $p]
    if {$diam > $height} {
        set diam $height
    }
    upvar #0 FS_D FS_D
    if {$diam != $FS_D} {
	set FS_D $diam
    }
}

proc fisheye(update) {} {

    set flag fisheye_flag
    set fisheye_flag 1
    if {$flag == 0} {
	set fisheye_flag 0
    }
    update
}

proc fisheye(apply) {} {
    global Fisheye
    satellites FISHEYE_SET $Fisheye(lat_degrees) $Fisheye(lat_minutes) \
	$Fisheye(long_degrees) $Fisheye(long_minutes)
    fisheye(draw)
}
