; token.scm
; by Rob Antonishen
; http://ffaat.pointclark.net

; Version 1.0 (20090303)

; Description
; Two scripts.  One to make token blanks in a number of shapes, one to make them pretty afterword.
; File > Create > Blank Token
;   creates a blank token image, black on transparent to allow the use to cut out shapes
;   before running the make token script
;
; Filters > Decor > Token...
; makes a shiny token.  It leaves the layers separate to allow changes.  Alpha is locked on the main token.

; License:
;
; 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.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html

;---------------------------------------------------------------------------------------------------------------------                    

(define (script-fu-make-token-blank inX inY inSpace inCorner)
  
  (let* (
        (width (+ inX inSpace inSpace))
        (height (+ inY inSpace inSpace))
        (inCorner (min inCorner (/ inX 2) (/ inY 2)))
        (img (car (gimp-image-new width height RGB)))
        (layer (car (gimp-layer-new img width height RGBA-IMAGE "Token Blank" 100 LAYER-MODE-NORMAL-LEGACY)))
        )

    (gimp-context-push)
    (gimp-image-undo-disable img)
    
    ; Create token blank
    (gimp-image-add-layer img layer -1)
    (gimp-round-rect-select img inSpace inSpace inX inY inCorner inCorner CHANNEL-OP-REPLACE TRUE FALSE 0 0)
    (gimp-context-set-background (list 0 0 0))
    (gimp-edit-fill layer FILL-BACKGROUND)
    
    ; add guides to mark the center
    (gimp-image-add-hguide img (/ height 2))
    (gimp-image-add-vguide img (/ width 2))

    ; Done
    (gimp-selection-none img)
    (gimp-display-new img)

    (gimp-image-undo-enable img)
    (gimp-context-pop)
  )
)



(script-fu-register "script-fu-make-token-blank"
					_"_Blank Token..."
					_"Create a blank token state shape."
					"Rob Antonishen"
					"Rob Antonishen"
					"Mar 2009"
					""
					SF-ADJUSTMENT  "X Size"               (list 400 60 900 10 100 0 SF-SLIDER)
					SF-ADJUSTMENT  "Y Size"               (list 400 60 900 10 100 0 SF-SLIDER)
					SF-ADJUSTMENT  "Boundary Space"       (list 50 10 100 1 10 0 SF-SLIDER)
					SF-ADJUSTMENT  "Round Corner Size"    (list 50 0 450 10 100 0 SF-SLIDER)
					)

(script-fu-menu-register "script-fu-make-token-blank"
                    "<Image>/File/Create")




;---------------------------------------------------------------------------------------------------------------------                    

(define (script-fu-make-token img inLayer inBevel inGlow inTokenColour inGlowColour)
  
  (let* (
        (width (car (gimp-image-width img)))
        (height (car (gimp-image-height img)))
        )

    (gimp-context-push)
    (gimp-image-undo-group-start img)

    ; select by alpha
    (gimp-selection-layer-alpha inLayer)

    (let * ( 
           (select (car (gimp-selection-save img))) ; save to channel
           (glow (car (gimp-layer-new img width height RGBA-IMAGE "Glow" 100 LAYER-MODE-NORMAL-LEGACY)))
           (satin (car (gimp-layer-new img width height RGBA-IMAGE "Satin" 50 LAYER-MODE-MULTIPLY-LEGACY)))
           (bevel (car (gimp-layer-new img width height RGBA-IMAGE "Bevel" 85 LAYER-MODE-MULTIPLY-LEGACY)))
           (highlight (car (gimp-layer-new img width height RGBA-IMAGE "Highlight" 85 LAYER-MODE-SCREEN-LEGACY)))
           )
    
      (gimp-image-add-layer img glow -1)
      (gimp-image-add-layer img satin -1)
      (gimp-image-add-layer img bevel -1)
      (gimp-image-add-layer img highlight -1)
      (gimp-image-lower-layer img glow)
      
      ;glow
      (gimp-selection-load select)
      (gimp-selection-grow img (/ inGlow 2))
      (gimp-selection-feather img (/ inGlow 2))
      (gimp-context-set-background inGlowColour)
      (gimp-edit-fill glow FILL-BACKGROUND)     
      
      ;token
      (gimp-layer-set-lock-alpha inLayer TRUE)
      (gimp-selection-load select)
      (gimp-context-set-background inTokenColour)
      (gimp-edit-fill inLayer FILL-BACKGROUND)     

      ;satin
      (gimp-selection-load select)
      (gimp-context-set-background (list 0 0 0))
      (gimp-context-set-foreground (list 255 255 255))
      (gimp-edit-blend satin  BLEND-FG-BG-RGB LAYER-MODE-NORMAL-LEGACY GRADIENT-SHAPEBURST-ANGULAR 100 0 REPEAT-NONE FALSE TRUE 2 0.5 FALSE 0 0 0 0)
      (let* (
            (counter 10)
            )
        (while (> counter 0)
          (gimp-edit-blend satin  BLEND-FG-BG-RGB LAYER-MODE-DIFFERENCE-LEGACY GRADIENT-LINEAR 100 0 REPEAT-NONE FALSE TRUE 2 0.5 FALSE (rand width) (rand height) (rand width) (rand height))
          (set! counter (- counter 1))
        )
      )
      (plug-in-gauss RUN-NONINTERACTIVE img satin 20 20 0)
      (plug-in-edge RUN-NONINTERACTIVE img satin 2.0 1 0)
      (plug-in-gauss RUN-NONINTERACTIVE img satin 5 5 0)
      (gimp-invert satin)
      (gimp-levels satin HISTOGRAM-VALUE 128 255 0.5 0 255)      
      
      ;bevel
      (gimp-selection-load select)
      (gimp-context-set-background (list 0 0 0))
      (gimp-edit-fill bevel FILL-BACKGROUND)    
      (gimp-selection-shrink img (/ inBevel 2))
      (gimp-selection-feather img inBevel)
      (gimp-context-set-background (list 255 255 255))
      (gimp-edit-fill bevel FILL-BACKGROUND)     

      ;hightlight
      (gimp-selection-load select)
      (gimp-context-set-background (list 0 0 0))
      (gimp-edit-fill highlight FILL-BACKGROUND)    
      (gimp-selection-shrink img (/ inBevel 4))
      (gimp-selection-feather img (/ inBevel 2))
      (gimp-context-set-background (list 255 255 255))
      (gimp-edit-fill highlight FILL-BACKGROUND)     
      (gimp-layer-set-lock-alpha inLayer TRUE)
      (gimp-selection-none img)
      (plug-in-emboss RUN-NONINTERACTIVE img highlight 90 30 10 0)
      (plug-in-gauss RUN-NONINTERACTIVE img highlight 4 4 1)
      (gimp-levels highlight HISTOGRAM-VALUE 128 200 1 0 255)
      ;Soften the inner
      (gimp-fuzzy-select-full inLayer 0 0 0 CHANNEL-OP-REPLACE TRUE FALSE 0 0 FALSE TRUE SELECT-CRITERION-COMPOSITE)
      (gimp-selection-invert img)
      (gimp-selection-shrink img (/ inBevel 2))
      (gimp-selection-feather img (/ inBevel 2))
      (gimp-levels highlight HISTOGRAM-VALUE 0 255 5 0 255)
      (plug-in-gauss RUN-NONINTERACTIVE img highlight (/ inBevel 2) (/ inBevel 2) 1)
      (gimp-selection-none img)
      (plug-in-gauss RUN-NONINTERACTIVE img highlight 4 4 1)

      ; remove temp channel
      (gimp-image-remove-channel img select)
    )

    ; Done
    (gimp-selection-none img)
    
    (gimp-image-undo-group-end img)
    (gimp-context-pop)
    
    (gimp-image-set-active-layer img inLayer)
    (gimp-displays-flush)

  )
)


(script-fu-register "script-fu-make-token"
					_"_Token..."
					_"Turn a token state template into a pretty token."
					"Rob Antonishen"
					"Rob Antonishen"
					"Mar 2009"
					"RGBA"
                    SF-IMAGE       "Image"      0
                    SF-DRAWABLE    "Drawable"   0
					SF-ADJUSTMENT  "Bevel Size"  (list 50 1 100 1 10 0 SF-SLIDER)
					SF-ADJUSTMENT  "Glow Size"   (list 50 1 100 1 10 0 SF-SLIDER)
                    SF-COLOR       "Token Color" "red"
                    SF-COLOR       "Glow Color"  "white"
					)

(script-fu-menu-register "script-fu-make-token"
                    "<Image>/Filters/Decor")
