#!/usr/bin/env python
"""python_fu_make_achievement: Create Steam-style achievement graphics in The GIMP
Copyright 2009 Michael Farrell <http://micolous.id.au/>
License: GNU General Public License 2

To install, copy to ~/.gimp-2.4/plug-ins/

Make sure you have python-fu support available first.  Otherwise it'll stuff up.

To use, open an image you want to use as an icon.  Then go Filters > Python-Fu > Make Steam Achievement.  Enter parameters and a new image will be made with your achievement graphics.  Bear in mind your original icon image will get munged a bit as I haven't learned all of Python-Fu yet so that I can do it non-destructively.  You should only have to press undo twice to restore the icon.

Inspired by this web-based PHP version by Deleted <http://teamfortress2.fr/achievements.php?eng>.  You can also find the TF2, Portal and HL2 achievement icons on that page.  Otherwise, Google (and your own creativity) is your friend for icons.

Thanks to James Henstridge for writing the GIMP Python Documentation.  <http://www.gimp.org/docs/python/index.html>
"""
from gimpfu import *

def python_make_achievement(timg, tdrawable, title_text, description_text, completed, progress, progress_numerator, progress_denominator):
	## constants
	img_width = 772
	img_height = 72
	title_font_face="Arial"
	title_font_size=18.
	description_font_face="Tahoma Bold"
	description_font_size=11.
	
	if progress: # error checks that only apply to progress bar mode.
		if progress_numerator > progress_denominator:
			raise Exception, "Numerator cannot exceed denominator."
		if progress_denominator <= 0:
			raise Exception, "Denominator must be at least 1."
	
	## create target image
	gimp.progress_init("Creating target image")
	gimp.progress_update(1./8.)
	achimg = gimp.Image(img_width, img_height, RGB)
	
	## resize icon and export it
	# TODO: make this not destroy the original image
	gimp.progress_init("Munging icon")
	gimp.progress_update(2./8.)
	timg.scale(64, 64)
	icon = timg.flatten()#.copy()
	icon_pixels = icon.get_pixel_rgn(0, 0, 64, 64, False)[0:64, 0:64]
	
	## make layers
	# background layer
	gimp.progress_init("Creating background layer")
	gimp.progress_update(3./8.)
	bg_layer = gimp.Layer(achimg, "grey box bg", img_width, img_height, RGB_IMAGE, 100, NORMAL_MODE)
	achimg.add_layer(bg_layer)
	bg_layer.add_alpha()
	pdb.gimp_edit_clear(bg_layer)
	gimp.set_background(79, 79, 79)
	pdb.script_fu_selection_rounded_rectangle(achimg, bg_layer, 10., False)
	pdb.gimp_edit_bucket_fill(bg_layer, BG_BUCKET_FILL, NORMAL_MODE, 100., 255., False, 10., 10.)
	pdb.gimp_selection_none(achimg)
	
	# icon layer
	gimp.progress_init("Copying icon")
	gimp.progress_update(4./8.)
	icon_layer = gimp.Layer(achimg, "icon", 64, 64, RGB_IMAGE, 100, NORMAL_MODE)
	achimg.add_layer(icon_layer)
	icon_layer.get_pixel_rgn(0, 0, 64, 64, True)[0:64, 0:64] = icon_pixels
	icon_layer.translate(4, 4)
	
	# title text
	gimp.progress_init("Creating title text")
	gimp.progress_update(5./8.)
	if completed:
		gimp.set_foreground(157, 195, 76)
	else:
		gimp.set_foreground(144, 144, 144)
	title_text_layer = gimp.Layer(achimg, "title", img_width, img_height, RGB_IMAGE, 100, NORMAL_MODE)
	achimg.add_layer(title_text_layer)
	title_text_layer.add_alpha()
	pdb.gimp_edit_clear(title_text_layer)
	title_text_layer_text = pdb.gimp_text_fontname(achimg, title_text_layer, 86, 6, title_text, 0, True, title_font_size, PIXELS, title_font_face)
	pdb.gimp_image_merge_down(achimg, title_text_layer_text, CLIP_TO_IMAGE)
	
	# description text
	gimp.progress_init("Creating description text")
	gimp.progress_update(6./8.)
	if completed: # we only need to change the fgcolour if we're a completed achievement; otherwise it is the same.
		gimp.set_foreground(194, 194, 194)
	description_text_layer = gimp.Layer(achimg, "description", img_width, img_height, RGB_IMAGE, 100, NORMAL_MODE)
	achimg.add_layer(description_text_layer)
	description_text_layer.add_alpha()
	pdb.gimp_edit_clear(description_text_layer)
	description_text_layer_text = pdb.gimp_text_fontname(achimg, description_text_layer, 86, 30, description_text, 0, True, description_font_size, PIXELS, description_font_face)
	pdb.gimp_image_merge_down(achimg, description_text_layer_text, CLIP_TO_IMAGE)
	
	## make progress bar thinger
	if progress:
		gimp.progress_init("Creating progress bar")
		gimp.progress_update(7./8.)
		
		# create a layer for the barstuff
		bar_layer = gimp.Layer(achimg, "progress bar", img_width, img_height, RGB_IMAGE, 100, NORMAL_MODE)
		achimg.add_layer(bar_layer)
		bar_layer.add_alpha()
		pdb.gimp_edit_clear(bar_layer)
		
		# draw the first part of the bar
		first_width = 300. * (float(progress_numerator) / float(progress_denominator))
		if progress_numerator > 0:
			pdb.gimp_rect_select(achimg, 86., 50., first_width, 15., CHANNEL_OP_REPLACE, False, 0)
			gimp.set_foreground(200, 185, 150)
			pdb.gimp_edit_bucket_fill(bar_layer, FG_BUCKET_FILL, NORMAL_MODE, 100., 255., False, 86., 50.)
		
		# draw the end of the bar
		if progress_numerator != progress_denominator:
			second_width = 300. - first_width
			pdb.gimp_rect_select(achimg, 86. + first_width, 50., second_width, 15., CHANNEL_OP_REPLACE, False, 0)
			gimp.set_foreground(26, 26, 26)
			pdb.gimp_edit_bucket_fill(bar_layer, FG_BUCKET_FILL, NORMAL_MODE, 100., 255., False, 86. + first_width, 50.)
		
		# draw the text label
		gimp.set_foreground(144, 144, 144)
		progress_text = pdb.gimp_text_fontname(achimg, bar_layer, 390, 49, u"%s/%s" % (progress_numerator, progress_denominator), 0, True, description_font_size, PIXELS, description_font_face)
		pdb.gimp_image_merge_down(achimg, progress_text, CLIP_TO_IMAGE)
	
	## show new images
	gimp.progress_init("Pushing to display")
	gimp.progress_update(8./8.)
	gimp.Display(achimg)
	gimp.displays_flush()

register(
        "python_fu_make_achievement",
        "Make the image an achievement icon for an achievement",
        "Make the image an achievement icon for an achievement",
        "Michael Farrell",
        "Michael Farrell",
        "2009",
        "<Image>/Filters/Python-Fu/Make Steam Achievement...",
        "RGB*",
        [
        		(PF_STRING, "title_text", "Title", u""),
        		(PF_STRING, "text_text", "Description", u""),
        		(PF_TOGGLE, "completed", "Completed Achievement", True),
        		(PF_TOGGLE, "progress", "Show Progress Bar", False),
        		(PF_INT, "progress_numerator", "Progress Parts Done (numerator)", 1),
        		(PF_INT, "progress_denominator", "Progress Parts Total (denominator)", 2),
        ],
        [],
        python_make_achievement)

main()
