; See  http://gimpchat.com/viewtopic.php?f=9&t=6205&sid=d7f9fe8c77454db326c62382d9ff588f

(define selffilename "\n- akovia-3d-r2.scm")

; This finds the newly added shadow layer and re-names it
(define (akovia_3d_rename_shadow_layer Image before-layer-list New-Name)
  (let*
    (
      ; get the list of layers after the shadow layer was added
      (after-layer-list (vector->list (car (cdr (gimp-image-get-layers Image)))))
      (shadow-layer 0)
    )
    ; Search through the new layers to find which layer isn't in the list of layers before the shadow layer was added
    (for-each
      (lambda (layer-id)
        (if (not (list? (member layer-id before-layer-list))) 
          (set! shadow-layer layer-id)
        )
      ) after-layer-list
    )
    (gimp-layer-set-name shadow-layer New-Name) ; Set the name of the shadow layer
    shadow-layer  ; for possible future use - return the id of the shadow layer
  )
)

; this is the main routine
;(define (script-fu_akovia_3d 
;                Image 
;                Drawable
;                hl-color
;                hl-o
;                sh-color
;                sh-o
;                int-x
;                int-y
;                int-b 
;                dsh
;                ds-color 
;                ds-x 
;                ds-y 
;                ds-b 
;                ds-o
;                )
(define (script-fu_akovia_3d 
                Image 
                Drawable
                hl-color
                hl-o
                sh-color
                sh-o
                int-offset
                int-b 
                dsh
                ds-color 
                ds-offset 
                ds-b 
                ds-o
                sh-angle
        )
  (if (= (car (gimp-selection-is-empty Image)) TRUE)
    (begin
      (gimp-message "The current image doesn't have a SELECTION.\n\nThis plugin works on a \nSELECTED AREA of the image.")
    )
    (begin; START OF PROCESSING
      (let*  (
               (before-layer-list '()) ; we will need to know what layers there are before adding a new shadow layer
               (sh-angle-radians (* (/ sh-angle 180) *pi*))     ; trigonometry is done using radians not degrees
               (ds-x (* (cos sh-angle-radians) ds-offset))
               (ds-y (* (sin sh-angle-radians) ds-offset))
               (int-x (* (cos sh-angle-radians) int-offset))
               (int-y (* (sin sh-angle-radians) int-offset))               
             )
        (gimp-image-undo-group-start Image) ; make all the following steps into a single undo step

        (gimp-selection-layer-alpha Drawable)
        (gimp-selection-invert Image)
      
        (set! before-layer-list (vector->list (car (cdr (gimp-image-get-layers Image))))) ; what layers are there now?
        (script-fu-drop-shadow Image Drawable int-x int-y int-b hl-color sh-o FALSE)      ; add the new shadow/highlight layer
        (akovia_3d_rename_shadow_layer Image before-layer-list "Internal highlight")      ; find and re-name the new shadow layer
   
        (set! before-layer-list (vector->list (car (cdr (gimp-image-get-layers Image)))))          ; what layers are there now?
        (script-fu-drop-shadow Image Drawable (* -1 int-x) (* -1 int-y) int-b sh-color hl-o FALSE) ; add the new shadow/highlight layer
        (akovia_3d_rename_shadow_layer Image before-layer-list "Internal shadow")                  ; find and re-name the new shadow layer

        ;(gimp-selection-invert Image)
        (gimp-selection-none Image)
        ; Create shadow layer on request
        (if (= dsh TRUE)
          (begin
            (set! before-layer-list (vector->list (car (cdr (gimp-image-get-layers Image)))))  ; what layers are there now?
            (script-fu-drop-shadow Image Drawable ds-x ds-y ds-b ds-color ds-o FALSE)          ; add the new shadow/highlight layer
            (akovia_3d_rename_shadow_layer Image before-layer-list "External shadow")          ; find and re-name the new shadow layer
          )
        )
        ;(gimp-selection-none Image)
   
        (gimp-image-undo-group-end Image) ; Finished the script so close the undo buffer
        (gimp-displays-flush)
      )
    ); end of BEGIN
  ); end of first IF (checking for a selection)
); end of DEFINE

;(script-fu-register "script-fu_akovia_3d"
;   _"akovia 3d r2..."
;  (string-append "akovia 3D" selffilename)
;  "GimpChat"
;  "GPL License"
;  "15/01/2013"
;  "RGB* GRAY*"
;  SF-IMAGE        "Input Image" 0
;  SF-DRAWABLE     "Input Drawable" 0
;  SF-COLOR       _"Internal Highlight Color"     '(255 255 255)
;  SF-ADJUSTMENT  _"Internal Highlight Opacity"   '(35 0 100 1 10 0 0)
;  SF-COLOR       _"Internal Shadow Color"        '(0 0 0)
;  SF-ADJUSTMENT  _"Internal Shadow Opacity"      '(80 0 100 1 10 0 0)
;  SF-ADJUSTMENT  _"Internal Offset X"            '(3 1 1000 1 10 0 1)
;  SF-ADJUSTMENT  _"Internal Offset Y"            '(3 1 1000 1 10 0 1)
;  SF-ADJUSTMENT  _"Internal Blur Radius"         '(4 0 100 1 10 0 1)
;  SF-TOGGLE      _"Include Drop Shadow ?"         TRUE
;  SF-COLOR       _"Drop Shadow Color"            '(0 0 0)
;  SF-ADJUSTMENT  _"Drop Shadow X"                '(4 1 1000 1 10 0 1)
;  SF-ADJUSTMENT  _"Drop Shadow Y"                '(4 1 1000 1 10 0 1)
;  SF-ADJUSTMENT  _"Drop Shadow Blur Radius"      '(5 0 100 1 10 0 1)
;  SF-ADJUSTMENT  _"Drop Shadow Opacity"          '(80 0 100 1 10 0 0)
;)

(script-fu-register "script-fu_akovia_3d"
   _"akovia 3d r2..."
  (string-append "akovia 3D" selffilename)
  "GimpChat"
  "GPL License"
  "15/01/2013"
  "RGB* GRAY*"
  SF-IMAGE        "Input Image" 0
  SF-DRAWABLE     "Input Drawable" 0
  SF-COLOR       _"Internal Highlight Color"     '(255 255 255)
  SF-ADJUSTMENT  _"Internal Highlight Opacity"   '(35 0 100 1 10 0 0)
  SF-COLOR       _"Internal Shadow Color"        '(0 0 0)
  SF-ADJUSTMENT  _"Internal Shadow Opacity"      '(80 0 100 1 10 0 0)
  SF-ADJUSTMENT  _"Internal Offset"              '(5 1 1000 1 10 0 1)
  SF-ADJUSTMENT  _"Internal Blur Radius"         '(4 0 100 1 10 0 1)
  SF-TOGGLE      _"Include Drop Shadow ?"         TRUE
  SF-COLOR       _"Drop Shadow Color"            '(0 0 0)
  SF-ADJUSTMENT  _"Drop Shadow Offset"           '(6 1 1000 1 10 0 1)
  SF-ADJUSTMENT  _"Drop Shadow Blur Radius"      '(5 0 100 1 10 0 1)
  SF-ADJUSTMENT  _"Drop Shadow Opacity"          '(80 0 100 1 10 0 0)
  SF-ADJUSTMENT  _"Drop Shadow Angle"            '(45 -180 180 1 10 0 1)
)

(script-fu-menu-register "script-fu_akovia_3d" "<Image>/Script-Fu/Effects Selection")