#
######################################################
#
#  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
#
######################################################
#
# init.tcl
#
# $Id: init.tcl,v 1.21 2005/02/16 01:02:51 lloydwood Exp $

proc geomview_init {} {
    global satellites_flag box_sat_flag planes_sat_flag fancy_sat_flag \
	   orbits_flag cones_flag footprints_flag \
	   axes_flag plane_flag stars_flag \
	   earth_flag fancy_earth_flag texture_flag \
	   geomview_flag

    trace variable satellites_flag w flag_change
    trace variable box_sat_flag w flag_change
    trace variable planes_sat_flag w flag_change
    trace variable fancy_sat_flag w flag_change

    trace variable orbits_flag w flag_change
    trace variable footprints_flag w flag_change
    trace variable cones_flag w flag_change

    trace variable axes_flag w flag_change
    trace variable plane_flag w flag_change
    trace variable stars_flag w flag_change

    trace variable earth_flag w flag_change
    trace variable fancy_earth_flag w flag_change
    trace variable texture_flag w flag_change

    trace variable geomview_flag w flag_change

    set satellites_flag 1
    set box_sat_flag 0
    set planes_sat_flag 0
    set fancy_sat_flag 0

    set orbits_flag 1
    set footprints_flag 0
    set cones_flag 0

    set axes_flag 1
    set plane_flag 0
    set stars_flag 0

    set earth_flag 1
    set fancy_earth_flag 1
    set texture_flag 0

    set geomview_dynamic_texture_flag 0
    set geomview_flag 1
}

proc app_init {} {
    global TITLE FONT COLOR COLDIV COLDEC
    global Load Save first_filename last_filename env geomview_module \
	geomview_dynamic_texture_flag
    global coverage_flag no_access_flag print_access_flag coverage_proj_flag \
	map_flag fisheye_flag fisheye_vapors_flag fisheye_reverse_view_flag \
	fisheye_print_inview_flag fisheye_labels_flag oe Fisheye \
        tracks_flag project_tracks_flag coverage_angle_flag realtime_flag \
	coverage_flag_dismissed no_access_flag_dismissed tracks_flag_dismissed \
	fisheye_flag_dismissed coverage_projection last_coverage_projection \
	playbar

    global PI
    set PI 3.14159265358979324

    set coverage_projection 0
    set last_coverage_projection $coverage_projection

    set playbar 0

    # set colors for windows
    set COLOR(bg) #acf
    set COLOR(abg) #67f
    set COLOR(sbg) #33f
    set COLOR(entry) white
    set FONT(button) -Adobe-Helvetica-Bold-R-Normal--*-120-*
    set FONT(label) -Adobe-Helvetica-Medium-R-Normal--*-120-*
    set FONT(fixed) -Adobe-Courier-Medium-R-Normal--*-120-*

    # set colors everywhere
    tk_setPalette background $COLOR(bg) \
	 activeBackground $COLOR(abg) selectBackground $COLOR(abg)

    # close the default toplevel window
    wm withdraw .

    # set up string for title bar
    set TITLE "SaVi"

    if {$geomview_module == 1} {
	geomview_init
    }
    # build top window
    main(build)

    # setup traces for option variables
    trace variable geomview_dynamic_texture_flag w flag_change
    trace variable coverage_flag w flag_change
    trace variable no_access_flag w flag_change
    trace variable print_access_flag w flag_change
    trace variable tracks_flag w flag_change
    trace variable map_flag w flag_change
    trace variable project_tracks_flag w flag_change
    trace variable coverage_proj_flag w flag_change
    trace variable coverage_angle_flag w flag_change
    trace variable fisheye_flag w flag_change
    trace variable fisheye_labels_flag w flag_change
    trace variable fisheye_vapors_flag w flag_change
    trace variable fisheye_reverse_view_flag w flag_change
    trace variable fisheye_print_inview_flag w flag_change
    trace variable realtime_flag w flag_change

    # initialize load directory
    # first set to old current working directory
    if {[info exists env(OLDCWD)]} {
	set Load(directory) "$env(OLDCWD)"
    }
    set Load(filename) ""

    # initialize save directory
    if {[info exists env(OLDCWD)]} {
	set Save(directory) "$env(OLDCWD)"
    }
    set Save(filename) ""



    set realtime_flag 0
    set print_access_flag 0
    set fisheye_labels_flag 1
    set fisheye_reverse_view_flag 0
    set fisheye_print_inview_flag 0
    set project_tracks_flag 0
    set coverage_proj_flag 0
    set coverage_angle_flag 0

    coverage(init_colors)

    # for texturemap demonstration - blue world in Geomview
    set no_access_flag 1
    set map_flag 1

    set coverage_flag_dismissed 1
    set tracks_flag_dismissed 1
    set fisheye_flag_dismissed 1

    # Equator (0,0) - increases chances of seeing a satellite
    set Fisheye(lat_degrees) 0
    set Fisheye(lat_minutes) 0
    set Fisheye(long_degrees) 0
    set Fisheye(long_minutes) 0
    fisheye(apply)

    if { [string compare "$first_filename" "NULL"] } {
        main(load_file) "$first_filename"
    } else {
      set last_filename "a LEO satellite."
    }

    # initialize the display
    update
}

proc flag_change {name element op} {
    global geomview_module box_sat_flag planes_sat_flag fancy_sat_flag
    upvar #0 $name flag

   if {$geomview_module == 1} {
    set not_geomview_flag 0
    if {$name == "satellites_flag"} {
	if {$flag == 1} {satellites SATELLITES_ON} else {satellites SATELLITES_OFF}
    } elseif {$name == "box_sat_flag"} {
        if {$flag == 1} {
            satellites BOX_SAT_ON
            set planes_sat_flag 0
	    set fancy_sat_flag 0
        } else {satellites BOX_SAT_OFF}
    } elseif {$name == "planes_sat_flag"} {
	if {$flag == 1} {
	    satellites PLANES_SAT_ON
	    set box_sat_flag 0
	    set fancy_sat_flag 0
	} else {satellites PLANES_SAT_OFF}
    } elseif {$name == "fancy_sat_flag"} {
        if {$flag == 1} {
	    satellites FANCY_SAT_ON
	    set box_sat_flag 0
	    set planes_sat_flag 0
	} else {satellites FANCY_SAT_OFF}
    } elseif {$name == "orbits_flag"} {
	if {$flag == 1} {satellites ORBITS_ON} else {satellites ORBITS_OFF}
    } elseif {$name == "footprints_flag"} {
	if {$flag == 1} {satellites FOOTPRINTS_ON} else {satellites FOOTPRINTS_OFF}
    } elseif {$name == "cones_flag"} {
	if {$flag == 1} {satellites CONES_ON} else {satellites CONES_OFF}
    } elseif {$name == "axes_flag"} {
	if {$flag == 1} {satellites AXES_ON} else {satellites AXES_OFF}
    } elseif {$name == "plane_flag"} {
	if {$flag == 1} {satellites PLANE 0.0 0.0} else {satellites PLANE_OFF}
    } elseif {$name == "stars_flag"} {
	if {$flag == 1} {satellites STARS_ON} else {satellites STARS_OFF}
    } elseif {$name == "earth_flag"} {
	if {$flag == 1} {satellites EARTH_ON} else {satellites EARTH_OFF}
    } elseif {$name == "fancy_earth_flag"} {
	if {$flag == 1} {satellites FANCY_EARTH_ON} else {satellites FANCY_EARTH_OFF}
    } elseif {$name == "texture_flag"} {
	if {$flag == 1} {satellites TEXTURE_ON} else {satellites TEXTURE_OFF}
    } elseif {$name == "geomview_flag"} {
	if {$flag == 1} {satellites GEOMVIEW_ON} else {satellites GEOMVIEW_OFF}
    } elseif {$name == "geomview_dynamic_texture_flag"} {
	if {$flag == 1} {satellites GEOMVIEW_DYNAMIC_TEXTURE_ON} else {satellites GEOMVIEW_DYNAMIC_TEXTURE_OFF}	
    } else {
	set not_geomview_flag 1
    }
    if {$not_geomview_flag == 0} return
   }

    if {$name == "realtime_flag"} {
	if {$flag == 1} {satellites REALTIME_ON} else {satellites REALTIME_OFF}
    } elseif {$name == "coverage_flag"} {
	if {$flag == 1} {satellites COVERAGE_ON} else {satellites COVERAGE_OFF}
    } elseif {$name == "no_access_flag"} {
	if {$flag == 1} {satellites NO_ACCESS_ON} else {satellites NO_ACCESS_OFF}
    } elseif {$name == "print_access_flag"} {
	if {$flag == 1} {satellites PRINT_ACCESS_ON} else {satellites PRINT_ACCESS_OFF}
    } elseif {$name == "map_flag"} {
	if {$flag == 1} {satellites MAP_ON} else {satellites MAP_OFF}
    } elseif {$name == "fisheye_reverse_view_flag"} {
	if {$flag == 1} {satellites FISHEYE_REVERSE_VIEW_ON} else {satellites FISHEYE_REVERSE_VIEW_OFF}
    } elseif {$name == "fisheye_print_inview_flag"} {
	if {$flag == 1} {satellites FISHEYE_PRINT_INVIEW_ON} else {satellites FISHEYE_PRINT_INVIEW_OFF}
    } elseif {$name == "tracks_flag"} {
	if {$flag == 1} {satellites TRACKS_ON} else {satellites TRACKS_OFF}
    } elseif {$name == "project_tracks_flag"} {
	if {$flag == 1} {satellites PROJECT_TRACKS_ON} else {satellites PROJECT_TRACKS_OFF}
    } elseif {$name == "coverage_proj_flag"} {
	if {$flag == 1} {satellites COVERAGE_PROJ 1} else {satellites COVERAGE_PROJ 0}
    } elseif {$name == "coverage_angle_flag"} {
	if {$flag == 1} {satellites COVERAGE_ANGLE_TYPE 1} else {satellites COVERAGE_ANGLE_TYPE 0}
    } elseif {$name == "fisheye_flag"} {
	if {$flag == 1} {satellites FISHEYE_ON} else {satellites FISHEYE_OFF}
    } elseif {$name == "fisheye_labels_flag"} {
        if {$flag == 1} {satellites FISHEYE_LABELS_ON} else {satellites FISHEYE_LABELS_OFF}
    } elseif {$name == "fisheye_vapors_flag"} {
	if {$flag == 1} {satellites FISHEYE_VAPORS_ON} else {satellites FISHEYE_VAPORS_OFF}
    } else {
	puts "Unrecognized flag: $name"
    }
}
