#               P I C T U R E T Y P E F . I T C L
# BRL-CAD
#
# Copyright (c) 2004-2021 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this file; see the file named COPYING for more
# information.
#
###
#
# This class defines the process necessary to build a BRL-CAD
# picture of Type "F". This is a Ghost Image with a full-color
# insert and lines.
#
# This is the most complicated picture type.
#

#
# Required packages
#
package require Itcl
package require Itk
package require Iwidgets

namespace eval RtWizard {

    package require PictureTypeBase 1.0
    package provide PictureTypeF 1.0

    #
    # Required packages
    #

    # None!

    #
    # The PictureTypeF class supports making a Ghost view image overlaid
    # with a full-color insert and lines.
    #
    ::itcl::class PictureTypeF {
	inherit ::RtWizard::PictureTypeBase

	public {
	    constructor {args} {
		PictureTypeBase::constructor $args
	    } {}

	    #
	    # Methods that implement abstract base class methods.
	    # Unfortunately, I have to redeclare them here.
	    #
	    method activate {}
	    method deactivate {}
	    method rtimage {w h}
	    method preview {}
	    method fullSize {}
	}

	private {
	    variable gp
	    variable fp
	    variable lp
	}
    }

    #--------------------#
    #   Public Methods   #
    #--------------------#
    #
    # activate - called when this picture type is selected.
    #
    itcl::body PictureTypeF::activate { } {
	#
	# Open a new steps menu
	#
	$::wizardInstance openSteps

	#
	# Update the introductory text
	#
	$::introInstance configure -greeting "$introText"

	#
	# Activate the pages that are necessary for this picture
	# type.
	#
	set gp [$::wizardInstance enable ghost]
	set fp [$::wizardInstance enable fullColor]
	set lp [$::wizardInstance enable lines]
    }

    #
    # deactivate - called when this picture type is deselected.
    #
    itcl::body PictureTypeF::deactivate { } {
	#
	# Deactivate the pages.
	#
	$::wizardInstance disable ghost
	$::wizardInstance disable fullColor
	$::wizardInstance disable lines

	#
	# Close the current steps menu
	#
	$::wizardInstance closeSteps
    }

   #
    # rtimage - generates a picture.
    #
    itcl::body PictureTypeF::rtimage {w h} {

	#
	# get a framebuffer
	#
	set fb [$::fbp getFrameBuffer $w $h]
	set fbarray [split $fb " "]
	if { [llength $fbarray] > 1 } {
	   set fb [lindex $fbarray 0]
	   set fbpid [lindex $fbarray 1]
	}
	if {[info exists  ::RtWizard::wizard_state(output_filename)]} {
	    set filename $::RtWizard::wizard_state(output_filename)
	}

	#
	# Get the view info
	#
	set viewInfo [$::mgedObj gedCmd get_eyemodel]
	set view_info [regsub -all ";" $viewInfo ""]
	set vdata [split $view_info "\n"]
	set viewsize [lindex [lindex $vdata 0] 1]
	set orientation [lrange [lindex $vdata 1] 1 end]
	set eye_pt [lrange [lindex $vdata 2] 1 end]

	#
	# Get the background color
	#
	set bgHex [$fp getBackgroundColor]
	set bg [hexToRGB $bgHex]

	# Get the ghosting intensity
	set gamma [$gp getIntensity]

	#
	# get occlusion mode
	#
	set occMode [$lp getOcclusionMode]
	if {! $occMode} {set occMode 1}

	#
	# Get the edge color color
	#
	set objPage $lp
	set lineHex [$objPage getLineColor]
	set l [hexToRGB $lineHex]

	#
	# Get the non-line color
	#
	set nonLineHex [$objPage getNonLineColor]
	set nl [hexToRGB $nonLineHex]

	#
	# Get the object lists, the file is global
	#
	set objPage $gp
	set ghost_objects [$objPage getObjects]
	set objPage $fp
	set color_objects [$objPage getObjects]
	set objPage $lp
	set line_objects [$objPage getObjects]

	# Get the perspective angle
	#
	set perspective [$::mgedObj gedCmd perspective]

	if {$::tcl_platform(platform) == "windows"} {
	    set pid_filename "NUL"
	} else {
	    set pid_filename "/dev/null"
	}

	if {$::tcl_platform(platform) == "windows"} {
	    set log_file "NUL"
	} else {
	    set log_file "/dev/null"
	}

        set rtimage_dict [dict create \
	    _dbfile $::RtWizard::wizard_state(dbFile) \
            _port $fb \
	    _w $w \
            _n $h \
	    _viewsize $viewsize \
            _orientation $orientation \
            _eye_pt $eye_pt \
            _perspective $perspective \
	    _bgcolor $bg \
            _ecolor $l \
            _necolor $nl \
	    _occmode $occMode \
            _gamma $gamma \
	    _color_objects $color_objects \
	    _ghost_objects $ghost_objects \
	    _edge_objects $line_objects \
           _pid_filename $pid_filename \
           _log_file $log_file]

	::cadwidgets::rtimage $rtimage_dict

	# If we're outputting a file, write it out and close down
	# the in-memory fbserv
	if {[info exists filename]} {
	   if {[string length $filename] > 0 } {
	       set output_generated 0
	       if {[file extension $filename] == ".png"} {
		  if { [catch {exec [file join [bu_dir bin] fb-png] -w $w -n $h -F $fb $filename}] } {
		      set output_generated 0
		  } else {
		      set output_generated 1
		  }
	       }
	       if {!$output_generated} {
		   if { [ catch {exec [file join [bu_dir bin] fb-pix] -w $w -n $h -F $fb $filename} ] } {
		       set output_generated 0
		   } else {
		       set output_generated 1
		   }
	       }
	       if {!$output_generated} {
		   tk_messageBox -message "Generation of image $filename failed!  If a file with the same name exists, it may be marked read-only." -type ok
	       }
	       if {$::tcl_platform(platform) == "windows"} {
		   set kill_cmd "[auto_execok taskkill] /F /PID"
	       } else {
		   set kill_cmd [auto_execok kill]
	       }

	       if {$kill_cmd != ""} {
		   eval exec $kill_cmd $fbpid
	       }
	   }
	}
    }


    #
    # preview - generates a 1/4 size preview image, limited to on-screen
    #
    itcl::body PictureTypeF::preview { } {
	#
	# get height and width
	#
	set w [$::fbp getWidth]
	set w2 [ expr $w / 2 ]

	set h [$::fbp getHeight]
	set h2 [ expr $h / 2 ]

	rtimage $w2 $h2
    }

    #
    # fullSize - generates a fullSize picture.
    #
    itcl::body PictureTypeF::fullSize { } {
	#
	# get height and width
	#
	set w [$::fbp getWidth]
	set h [$::fbp getHeight]

	rtimage $w $h
    }

}; # end namespace


# Local Variables:
# mode: Tcl
# tab-width: 8
# c-basic-offset: 4
# tcl-indent-level: 4
# indent-tabs-mode: t
# End:
# ex: shiftwidth=4 tabstop=8
