#
######################################################
#
#  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
#
######################################################
#
# main.tcl
#
# $Id: main.tcl,v 1.27 2005/02/07 19:22:01 lloydwood Exp $

proc window(raise) {window} {
    if [winfo exists .$window] {
	wm deiconify .$window
	raise .$window
    }
}

proc main(build) {} {
    global main_name COLOR FONT TITLE geomview_module last_filename use_X

    set main_name [build_Toplevel main]

    wm protocol $main_name WM_DELETE_WINDOW main(exit)

    wm title $main_name "$TITLE - satellite constellation visualization"

    build_Menubar $main_name mbar \
	{"Help" \
             {"about SaVi..." "about(build)"} } \
	{"File" \
	     {"Load satellites..." "load(build)"} \
	     {"Save satellites as Tcl script..." "save(build)"} \
	     {} \
	     {"Quit" "main(exit)"} } \
	{"Edit" \
	     {"Current satellite..." "edit(build)" } \
	     {"New satellite..." "main(new)" } \
	     {} \
	     {"Unselect" "main(unselect)" } \
	     {"Copy" "main(copy)" } \
	     {"Cut" "main(delete)" } \
	     {} \
	     {"Simulation constants..." "params(build)"} } \
	{"Views"} \
	{"Constellations"} \
	{"Rendering"}

    if {$use_X == 1} {
	set fisheye_option {"Local fisheye..." "fisheye(build)"}
    } else {
	set fisheye_option {"X and fisheye use disabled" {} }
    }

    build_Menu $main_name.mbar.b2 menu \
	    {"Global coverage..." "coverage(build)"} \
	    $fisheye_option \
	    {} \
	    {"> Step forwards" "main(forw_step)"} \
	    {"< Step back" "main(back_step)"} \
	    {">> Forwards..." "main(forwards)"} \
	    {"<< Reverse..." "main(backwards)"} \
	    {"Freeze views" "main(stop)"} \
	    {} \
	    {"Restart views at time zero" "main(restart)"}

    main(constellations_menu) $main_name.mbar.b3

    if {$geomview_module == 1} {
	build_options_menu $main_name.mbar.b4 \
	    {"Real-time mode" realtime_flag} \
	    {} \
	    {"Mark satellite positions" satellites_flag} \
	    {"Box markers" box_sat_flag} \
	    {"Planes markers" planes_sat_flag} \
            {"Satellite markers" fancy_sat_flag} \
	    {} \
	    {"Show satellite orbits" orbits_flag} \
	    {"Show footprints" footprints_flag} \
	    {"Show coverage cones" cones_flag} \
	    {} \
	    {"Show stationary axes" axes_flag} \
	    {"Show equatorial plane" plane_flag} \
	    {"Show star field" stars_flag} \
	    {} \
	    {"Show central body" earth_flag} \
	    {"Use detailed Earth model" fancy_earth_flag} \
	    {"Texture mapping" texture_flag} \
	    {} \
    	    {"Animate in Geomview" geomview_flag}
    } else {
	build_options_menu $main_name.mbar.b4 \
	    {"Real-time mode" realtime_flag}
    }

    set cmd [build_StdFrame $main_name cmd]

    label $cmd.l -text \
"no. {semi-major axis  eccentricity  inclination   long. asc. node  arg. periapsis   time to periapsis}" \
	-font $FONT(label)
    listbox $cmd.lb -yscrollcommand "$cmd.scroll set" \
	-height 2 -width 70 -selectmode single \
	-font $FONT(fixed) -bg $COLOR(entry)
    pack $cmd.l -side top -anchor w
    pack $cmd.l -fill none
    pack $cmd.lb -side left
    pack $cmd.lb -fill both -expand 1
    bind $cmd.lb <Button-1> {main(select) [%W nearest %y]}
    bind $cmd.lb <Double-Button-1> {main(dblselect) [%W nearest %y]}
    scrollbar $cmd.scroll -command "$cmd.lb yview"
    pack $cmd.scroll -side right -fill y

    pack $cmd -fill both -expand 1

    set cmd0 [build_CmdFrame $main_name cmd0]

    build_LabelEntryColumns $cmd0 le0 \
        {dentry "" {delta_t}} \
	{text "" {"seconds per simulation interval."}} \
	{label "" {time}} \
	{label "" {last_filename}}

    pack $cmd0 -fill x

    build_Buttonbar $main_name bb1 \
        {"Restart" {main(restart)}} \
        {"  <  " {main(back_step)}} \
        {"  >  " {main(forw_step)}} \
        {"  STOP  " main(stop)} \
        {"   < <   " main(backwards)} \
        {"   > >   " main(forwards)}


}

proc main(reset_title) {} {
  global TITLE main_name

  wm title $main_name "$TITLE - satellite constellation visualization"
}

proc main(title) {filename} {
  global TITLE main_name

  wm title $main_name "$TITLE - $filename - satellite constellation visualization"
}

proc main(constellations_menu) {submenu} {

    build_Menu $submenu menu \
	{"Empty space" "main(delete_all)"} \
	{} \
	{"Clarke geostationary" "main(constellation) clarke.tcl"} \
	{"Draim tetrahedral" "main(constellation) draim-4.tcl"} \
	{"Molnya" "main(constellation) molnya.tcl"} \
	{"Tundra" "main(constellation) tundra.tcl"} \
	{} \
	{"Globalstar" "main(constellation) globalstar.tcl"} \
	{"Iridium" "main(constellation) iridium-66.tcl"} \
	{"ICO" "main(constellation) ico.tcl"} \
	{"Orbcomm" "main(constellation) orbcomm.tcl"} \
	{} \
	{"GPS" "main(constellation) gps.tcl"} \
	{"Galileo" "main(constellation) galileo.tcl"} \
	{} \
	{"Skybridge (64)" "main(constellation) skybridge-64.tcl"} \
	{"Teledesic (288)" "main(constellation) teledesic-288.tcl"} \
	{"Teledesic (840)" "main(constellation) teledesic-840.tcl"} \
	{} \
	{"Aries" "main(constellation) aries.tcl"} \
	{"@contact" "main(constellation) atcontact.tcl"} \
	{"Celestri" "main(constellation) celestri.tcl"} \
	{"Ellipso" "main(constellation) ellipso.tcl"} \
	{"GS2" "main(constellation) gs2.tcl"} \
	{"LEq0" "main(constellation) leqo.tcl"} \
	{"Macrocell" "main(constellation) macrocell.tcl"} \
	{"Millimeter" "main(constellation) mm.tcl"} \
	{"NUONCE" "main(constellation) nuonce.tcl"} \
	{"Odyssey" "main(constellation) odyssey.tcl"} \
	{"Orblink" "main(constellation) orblink.tcl"} \
	{"Sirius Radio" "main(constellation) sirius-radio.tcl"} \
	{"Spaceway NGSO" "main(constellation) spaceway-ngso.tcl"} \
	{} \
	{"Geosynchronous belt (TLE)" "main(constellation) geo.tle"} \
	{"GPS (TLE)" "main(constellation) gps.tle"} \
	{"LEO satellites (TLE)" "main(constellation) visual.tle"}

}

proc main(forw_step) {} {

    main(stop)
    satellites FORW_STEP

}

proc main(back_step) {} {

    main(stop)
    satellites BACK_STEP

}


proc main(forwards) {} {
    global COLOR playbar

    satellites STOP
    if {("OK" == [satellites FORWARDS]) && ([winfo exists .main])} {
	.main.bb1.b3 configure -bg $COLOR(bg)
	.main.bb1.b4 configure -bg $COLOR(bg)
	.main.bb1.b5 configure -bg $COLOR(sbg)
    }
    set flag $playbar
    set playbar 1
    if {$flag == 0} {
	coverage(forwards)
    }
    set playbar 0
}

proc main(backwards) {} {
    global COLOR playbar

    satellites STOP
    if {("OK" == [satellites BACKWARDS]) && ([winfo exists .main])} {
	.main.bb1.b3 configure -bg $COLOR(bg)
	.main.bb1.b4 configure -bg $COLOR(sbg)
	.main.bb1.b5 configure -bg $COLOR(bg)
    }
    set flag $playbar
    set playbar 1
    if {$flag == 0} {
	coverage(backwards)
    }
    set playbar 0
}

proc main(stop) {} {
    global COLOR playbar

    if {("OK" == [satellites STOP]) && ([winfo exists .main])} {
	.main.bb1.b3 configure -bg $COLOR(sbg)
	.main.bb1.b4 configure -bg $COLOR(bg)
	.main.bb1.b5 configure -bg $COLOR(bg)
    }

    set flag $playbar
    set playbar 1
    if {$flag == 0} {
	coverage(stop)
    }
    set playbar 0
}

proc main(restart) {} {

    main(stop)
    satellites RESET

}

proc main(exit) {} {
    satellites EXIT
    exit
}

proc main(update) {} {
    set i 0
    set n [.main.cmd.lb size]
    set noe [satellites GET $i]
    while {$noe != ""} {
	.main.cmd.lb insert $i [list $i [string range $noe 0 64]]
	incr i
	set noe [satellites GET $i]
    }
    if {$n > 0} {
	.main.cmd.lb delete $i [expr $i+$n-1]
    }

    edit(update)
    fisheye(update)
    coverage(update)
}

proc main(update_one) {n} {
    set noe [satellites GET $n]
    .main.cmd.lb insert $n [list $n [string range $noe 0 64]]
    .main.cmd.lb select set $n
    incr n
    .main.cmd.lb delete $n
}


proc main(select) {n} {
    if {[llength $n] > 0} {
        .main.cmd.lb select set $n
	edit(set) [lindex $n 0]
	satellites TAG [lindex $n 0]
    }
}

proc main(dblselect) {n} {
    edit(build)
    main(select) $n
}

proc main(new) {} {
    set nn [satellites NEW]
    main(update)
    main(select) [list $nn]
    edit(build)
}

proc main(copy) {} {
    set n [.main.cmd.lb curselection]
    if {[llength $n] > 0} {
	set nn [satellites COPY [lindex $n 0]]
	main(select) [list $nn]
	main(update)
    }
}

proc main(delete) {} {
    set n [.main.cmd.lb curselection]
    if {[llength $n] > 0} {
	satellites DELETE [lindex $n 0]
	coverage(update_decay)
	main(update)
    }
}

proc main(unselect) {} {
    set n [.main.cmd.lb curselection]
    if {[llength $n] > 0} {
	satellites UNTAG [lindex $n 0]
	main(update)
    }
}

proc main(delete_all) {} {
    global project_tracks_flag last_filename main_name

    set project_tracks_flag 0
    satellites DELETE_ALL
    set last_filename " "
 
    main(reset_title)

    coverage(update_decay)
    main(update)
}


proc main(constellation) {filename} {
    global geomview_module last_filename PI params

    # We could choose not to delete all satellites when loading
    # in an additional constellation, but since mask angle is
    # set for all rather than for each satellite, the result
    # would be very confusing coverage. Load... gets around this.
    satellites DELETE_ALL
    coverage(update_decay)

    main(load_file) "data/$filename"
}

proc main(source_file) {filename} {
    global coverage_angle coverage_angle_flag PI params

    set coverage_angle_old $coverage_angle

    # interpret as a tcl file. Set up constants.
    set MU $params(Mu)
    set RADIUS_OF_EARTH $params(Radius)

    source "$filename"

    puts stderr "SaVi: loaded $filename"
    if {$coverage_angle != $coverage_angle_old} {
	puts stderr "      changed coverage angle from $coverage_angle_old to $coverage_angle."
	if {$coverage_angle_flag} {
	    puts stderr "      Coverage angle probably describes mask elevation."
	}
    }
}
 
proc main(load_file) {filename} {
    global geomview_module last_filename no_access_flag

    if {$geomview_module == 1} {
        satellites GV_SEND "(ui-freeze on)"
    }
    # if filename ends in ".tle" then interpret as a two-line-element file
    if {[string_ends "$filename" ".tle"]} {
	# tle file
	tle_file_input "$filename"
    } elseif {[string_ends "$filename" ".tcl"]} {
	main(source_file) "$filename"
    } else {
	puts stderr "SaVi: $filename is not a recognised .tle/.tcl satellites file."
	puts stderr "      No satellites loaded."
    }

    if {$geomview_module == 1} {
        satellites GV_SEND "(ui-freeze off)"
    }

    set last_filename "$filename"

    main(title) "$filename"
    main(update)
}
