Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with guide position
#2
(11-05-2022, 12:19 AM)alvk Wrote: Hello everyone!

I'm using gimp to make figures for my scientific papers. Often I need to place a guide in a certain position relative to a selected layer. I'm trying to write a pretty simple script, that do the job. However, I have a problem with it. I want to have a checkbox, with indicates whether the guide should be placed within the selected layer or not. But it doesn't work. Every time, the script return negative value for guide position. If I have a multilayer image, this means that the guide is placed outside the active layer, regardless of the state of the checkbox. If I have a single layer image, I get an error, that the guide is outside the image. Could anyone help me to solve this problem? The full script is below. Thank you!

Code:
(define (script-fu-custom-guide image
                        drawable
                        orientation
                        position
                        in_toggle)

 (let* (
        (lwidth (car (gimp-drawable-get-width image)))
        (lheight (car (gimp-drawable-get-height image)))

        (offx (car (gimp-drawable-get-offsets drawable)))
        (offy (cadr (gimp-drawable-get-offsets drawable)))

        (if (= in_toggle TRUE) (position (* -1 position)))

        (guide_position
          (cond ((= orientation 0) (- offx position)) ; guide on left
                ((= orientation 1) (+ offx lwidth position)) ; guide on right
                ((= orientation 2) (- offy position)) ; guide on top
                ((= orientation 3) (+ offy lheight position)) ; guide on bottom
                )
          )
        )

   (if (< 1 orientation)
     (gimp-image-add-hguide image guide_position)
     )
   (if (> 2 orientation)
     (gimp-image-add-vguide image guide_position)
     )
   (gimp-displays-flush)
   )
 )

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"    0
 SF-DRAWABLE    "Input drawable" 0
 SF-OPTION _"_Orientation"         '(_"Left" _"Right" _"Top" _"Bottom")
 SF-ADJUSTMENT _"_Position"         (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?"  FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                        "<Image>/Figures")

Hello alvk,

There's quite a lot wrong so here's a new version which I think is doing what you wanted:

Code:
(define (script-fu-custom-guide image drawable orientation position in-toggle)

  (let ((image-width (car (gimp-image-width image)))
        (image-height (car (gimp-image-height image)))
        (layer-width (car (gimp-drawable-width drawable)))
        (layer-height (car (gimp-drawable-height drawable)))
        (offx (car (gimp-drawable-offsets drawable)))
        (offy (cadr (gimp-drawable-offsets drawable)))
        (guide-position 0))

    (if (= in-toggle TRUE)
      (set! position (- position)))

    (set! guide-position
      (case orientation
        ((0) (- offx position))                ; guide on left
        ((1) (+ offx layer-width position))    ; guide on right
        ((2) (- offy position))                ; guide on top
        ((3) (+ offy layer-height position)))) ; guide on bottom

    (case orientation
      ((0 1) (if (<= 0 guide-position image-width)
                 (gimp-image-add-vguide image guide-position)))
      ((2 3) (if (<= 0 guide-position image-height)
                 (gimp-image-add-hguide image guide-position))))

    (gimp-displays-flush)))

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"     0
 SF-DRAWABLE    "Input drawable"  0
 SF-OPTION     _"_Orientation"   '("_Left" "_Right" "_Top" "_Bottom")
 SF-ADJUSTMENT _"_Position"       (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?" FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                         "<Image>/Figures")
Reply


Messages In This Thread
Troubles with guide position - by alvk - 11-05-2022, 12:19 AM
RE: Troubles with guide position - by teapot - 11-05-2022, 08:49 AM
RE: Troubles with guide position - by alvk - 11-05-2022, 10:00 AM
RE: Troubles with guide position - by Ofnuts - 11-05-2022, 10:41 AM
RE: Troubles with guide position - by alvk - 11-05-2022, 10:57 AM
RE: Troubles with guide position - by Ofnuts - 11-05-2022, 09:20 AM

Forum Jump: