# Nameplate Functions
# 
# This file contains functions to render nameplates.
#
# TODO: determine whether Cruise holds copyright
# Copyright (C) 1999-2002 David Gale <cruise@openverse.com>
# For more information visit http://www.openverse.com/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
# USA.

namespace eval nameplate {}
# Create a nameplate tagged $tag on canvas $cnv at $x, $y
# args:	-min-width: minimum nameplate width
#	-min-height: minimum nameplate height
#	-text: text placed inside nameplate
#	-font: font used for text
#	-icon: icon placed alongside text
#	-light: color for highlighted region of 3d border
#	-dark: color for shaded region of 3d border
#	-fill: color used for inside 3d border
#	-outline: color used for outer flat border
#	-relief: either raised, sunken, or flat
#	-foreground: text color
#	-shadow: text shadow color
#	-opacity: shadow opacity; from 0.0 to 1.0
#	-extra-tags: what else, but extra tags
# Components:
#	$tag-t1 through $tag-t4: the text
#	$tag-i: the icon
#	$tag-f: the background
proc nameplate::create {x y cnv tag args} {
	# Defaults
	set plate_w 5
	set plate_h 5
	set name ""
	set plate_font ""
	set icon ""
	set fill "#236582"
	set outline "black"
	set foreground "white"
	set ex_tags {}

	# Parse extra arguments
	while 1 {
		if ![arg_pop arg args] break
		switch -- $arg {
		"-min-width" {arg_pop plate_w args}
		"-min-height" {arg_pop plate_h args}
		"-text" {arg_pop name args}
		"-font" {arg_pop fnt args; set plate_font "-font [list $fnt]"}
		"-icon" {arg_pop icon args}
		"-fill" {arg_pop fill args}
		"-outline" {arg_pop outline args}
		"-foreground" {arg_pop foreground args}
		"-extra-tags" {arg_pop ex_tags args}
		}
	}

	# Render the text (offscreen)
	if {$name != ""} {
		# First, create the text
		eval "\$cnv create text -1000 -1000 -text \$name\
				$plate_font -tags \[lunion \$tag\
				\$ex_tags \"\$tag-t\"\] -fill $foreground"

		# Now gauge the size of the text (and therefore the nameplate)
		set box [$cnv bbox "$tag-t"]
		set text_w [expr [lindex $box 2] - [lindex $box 0]]
		set text_h [expr [lindex $box 3] - [lindex $box 1]]
		set plate_w [max $plate_w [expr $text_w + 12]]
		set plate_h [max $plate_h [expr $text_h + 2]]
	} else {
		set text_w 0
		set text_h 0
	}

	# Render the icon (offscreen)
	if {$icon != ""} {
		$cnv create image -1000 -1000 -image $icon -tag [lunion $tag\
				$ex_tags "$tag-i"]
		set box [$cnv bbox "$tag-i"]
		set icon_w [expr [lindex $box 2] - [lindex $box 0]]
		set icon_h [expr [lindex $box 3] - [lindex $box 1]]
		if {$name != ""} {
			incr plate_w [expr $icon_w + 2]
		} else {
			set plate_w [max $plate_w [expr $icon_w + 5]]
		}
		set plate_h [max $plate_h [expr $icon_h + 5]]
	} else {
		set icon_w 0
		set icon_h 0
	}

	# Decide coordinates
	set l [expr $x - $plate_w / 2]
	set t [expr $y - $plate_h / 2]
	set r [expr $x + $plate_w / 2]
	set b [expr $y + $plate_h / 2]

	# Fill
	$cnv create rectangle $l $t $r $b -outline $outline -fill $fill\
			-tag [lunion $tag $ex_tags "$tag-f"]

	# Find the center
	set text_x $x
	set text_y $y

	# Put the icon in place
	if {$icon != ""} {
		incr text_x [expr $icon_w / 2]
		set icon_x [expr $l + 2]
		set anchor "w"
		$cnv itemconfigure "$tag-i" -anchor $anchor
		$cnv coords "$tag-i" $icon_x $text_y
		$cnv raise "$tag-i"
	}

	# Put the text in place
	if {$name != ""} {
		$cnv coords "$tag-t" $text_x $text_y
		$cnv raise "$tag-t"
	}
}

# Adjust the background of an existing nameplate
proc nameplate::set_bg {cnv tag args} {
	while 1 {
		if ![arg_pop arg args] break
		switch -- $arg {
		"-fill" {
			arg_pop c args
			$cnv itemconfigure "$tag-f" -fill $c
		} "-outline" {
			arg_pop c args
			$cnv itemconfigure "$tag-f" -outline $c
		}}
	}
}

