;;; Description: chewed-text_RD.scm
;;; Creates a chewed effect on a user created alpha or text layer.
;;; -----------------------------------------------------------------------------
;;; Change Log:
;;; Version 1.1
;;; added slider for offset of mask pattern (chewyness)

;;; Version 1.2 - bug fix

;;; Version 1.3 - bug fix

;;; Version-1.4
;;; added ability to have user select pattern mask for cutout of text or other on alpha layer

; Information can be found at GimpChat.com
;
; License: GPLv3
; 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 3 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.
;
; To view a copy of the GNU General Public License
; visit: http://www.gnu.org/licenses/gpl.html


(define (script-fu-chewed-text_RD image drawable mycolor myshadowcolor mypattern maskpattern bevel chewyness)
(gimp-image-undo-group-start image)

  ; save user settings
  (gimp-context-push)
 
  ; begin
 
  (let*
    ( 
	(width (car (gimp-image-width image)))
    (height (car (gimp-image-height image)))
    (original (car (gimp-layer-new-from-visible image image "Text-Copy")))
    (original-copy (car (gimp-layer-new-from-visible image image "Shadow")))	
    (pattern-fill (car (gimp-layer-new image width height RGBA-IMAGE "Pattern-Fill" 100 0)))	
    (bg-fill (car (gimp-layer-new image width height RGBA-IMAGE "Background Layer" 100 0)))	
    )


	 ; add the layers	
  (gimp-selection-layer-alpha drawable)	 
  (gimp-context-set-pattern mypattern)
  (gimp-edit-fill drawable PATTERN-FILL)

  (gimp-selection-none image)  
  
  (gimp-image-add-layer image original 2)
  (gimp-selection-layer-alpha original)	 
  (gimp-context-set-pattern mypattern)
  (gimp-edit-fill original PATTERN-FILL) 

    (script-fu-add-bevel image original bevel FALSE FALSE)
    (script-fu-add-bevel image original bevel FALSE FALSE)
    (script-fu-add-bevel image original bevel FALSE FALSE)	  
  
  (gimp-image-add-layer image original-copy 1)
  (gimp-selection-layer-alpha original-copy)  
  (gimp-context-set-foreground myshadowcolor)
  (gimp-edit-fill original-copy FOREGROUND-FILL)   
  
 
  
  
  (gimp-image-add-layer image pattern-fill 0)
  (gimp-context-set-pattern maskpattern)
  (gimp-drawable-fill pattern-fill PATTERN-FILL)
  (gimp-selection-layer-alpha pattern-fill)

  (gimp-image-remove-layer image pattern-fill)
  (gimp-edit-cut original)
  (gimp-selection-layer-alpha original)  
  ;(gimp-image-add-layer image chewed-fill 0)
  (gimp-edit-clear drawable)
  (gimp-layer-resize-to-image-size drawable)

  (gimp-selection-layer-alpha original-copy)
  (gimp-selection-invert image)
  (gimp-edit-clear drawable)
  (gimp-image-raise-item-to-top image drawable)
  
     ; Offset our pattern filled text layer
  (gimp-drawable-offset drawable 1 1 chewyness 0)
  
  (gimp-drawable-set-visible original-copy FALSE)
  (gimp-selection-none image)
  
   ; add bevel to both chewed layers
    ;(script-fu-add-bevel image original bevel FALSE FALSE)
	(script-fu-add-bevel image drawable bevel FALSE FALSE)	
    ;(script-fu-add-bevel image original bevel FALSE FALSE)
    (script-fu-add-bevel image drawable bevel FALSE FALSE)	
    ;(script-fu-add-bevel image original bevel FALSE FALSE)	
    (script-fu-add-bevel image drawable bevel FALSE FALSE)
	


  
  (gimp-image-add-layer image bg-fill 0)
  (gimp-image-lower-item-to-bottom image bg-fill)  
  (gimp-context-set-foreground mycolor)
  (gimp-drawable-fill bg-fill FOREGROUND-FILL)
  
  (gimp-image-raise-item image original)	
  (gimp-drawable-set-visible original-copy TRUE)	
  (plug-in-gauss RUN-NONINTERACTIVE image original-copy 4 4 1) 

  ; (plug-in-lighting RUN-NONINTERACTIVE
                    ; image            ; input image id
                    ; drawable          ; input layer id
                    ; original      ; load bumpmap? 0 = DISABLED name = enabled                     
                    ; 0                ; load env map? 0 = DISABLED name = enabled
                    ; TRUE             ; enable bumpmaping (TRUE/FALSE)
                    ; FALSE            ; enable env maping (TRUE/FALSE)
                    ; 0                ; Type of mapping (0=linear,1=log, 2=sinusoidal, 3=spherical
                    ; 0                ; type of light src 0 = point
                    ; '(255 255 255)   ; light src color is white   
                    ; 0.57 -1.27 1.00   ; light position 1 xyz - alt 0.57 -1.27 1.00 or 0.53 2.20 1.00 works
                    ; -1.00 -1.00 1.00 ; light direction 1 ijk - alt
                    ; 1.00             ; Material ambient intensity (0..1)
                    ; 0.20             ; Material diffuse intensity (0..1)
                    ; 0.50             ; Material diffuse reflectivity (0..1)
                    ; 0.50             ; Material specular reflectivity (0..1)
                    ; 27.0             ; Material highlight (0..->), note: it's expotential
                    ; TRUE             ; Apply antialiasing (TRUE/FALSE)                                   
                    ; FALSE            ; Create a new image (TRUE/FALSE)
                    ; FALSE            ; Make background transparent (TRUE/FALSE)        	
   ; )

          ; end
  (gimp-selection-none image)    
  ; display new image and return user settings
  (gimp-context-pop)
  (gimp-image-undo-group-end image)
  (gimp-displays-flush image) 
     
  )
)


    ;register menu location and information   

(script-fu-register
"script-fu-chewed-text_RD"
"<Image>/Rod/Alpha to Logo/Chewed Text Effect"
"creates a Chewed Effect on an alpha or text layer  -  script name: chewed-text_RD.scm"
"Rod Detmer"
"Rod Detmer"
"December 2015"
"RGB* GRAY*"
SF-IMAGE       "The Image"            0
SF-DRAWABLE    "The Drawable"         0  
SF-COLOR       "Background Color"        '(255 255 255)
SF-COLOR       "Shadow Color"        '(70 30 20)
SF-PATTERN      "Select Pattern"          "Seamless-burger_RD.png"   
SF-PATTERN      "Select Mask Pattern"     "Shredded-Text-Mask"   
SF-ADJUSTMENT "Bevel Amount"         '(20 0 30 1 10 0 SF-SLIDER)
SF-ADJUSTMENT "Chewyness"         '(-3 -1200 1200 1 10 0 SF-SLIDER)                             
)