//  DrawingTools
//
//  This is a set of drawing tools similar to the pencil, paintbrush, 
//  eraser and flood fill (paint bucket) tools in NIH Image. The 
//  pencil and paintbrush draw in the current foreground color 
//  and the eraser draws in the current background color. The
//  flood fill tool fills the selected area using the foreground color.
//  Hold down the alt key to have the pencil and paintbrush draw 
//  using the background color or to have the flood fill tool fill 
//  using the background color. Set the foreground and background 
//  colors by double-clicking on the flood fill tool or on the eye  
//  dropper tool.  Double-click on the pencil, paintbrush or eraser 
//  tool  to set the drawing width for that tool.
//
// Rename as "StartupMacros.txt" and move to the macros folder 
// to have these tools automatically loaded when ImageJ starts.
//
// Icons contributed by Tony Collins.

  var pencilWidth=1, brushWidth=10, eraserWidth=10, leftClick=16, alt=8;

  macro "Unused Tool-1 - " {}  // leave slot between text tool and magnifying glass unused
  macro "Unused Tool-2 - " {}  // leave slot between dropper and pencil unused
 
 macro "Pencil Tool - C037L494fL4990L90b0Lc1c3L82a4Lb58bL7c4fDb4L5a5dL6b6cD7b" {
        getCursorLoc(x, y, z, flags);
        if (flags&alt!=0)
              setColorToBackgound();
        draw(pencilWidth);
   }

   macro "Paintbrush Tool - C037F6036F3699CfffD71F4771D5eD7eD9e" {
        getCursorLoc(x, y, z, flags);
        if (flags&alt!=0)
              setColorToBackgound();
        draw(brushWidth);
   }

   macro "Eraser Tool - C037R0aa4 P0a61f1aa0Pbef5f1" { 
        setColorToBackgound();
        draw(eraserWidth);
   }

    macro "Flood Fill Tool -C037B21P085373b75d0L4d1aL3135L4050L6166D57D77D68La5adLb6bcD09D94" {
        requires("1.34j");
        setupUndo();
        getCursorLoc(x, y, z, flags);
        if (flags&alt!=0) setColorToBackgound();
        floodFill(x, y);
    }

   function draw(width) {
        requires("1.32g");
        setupUndo();
        getCursorLoc(x, y, z, flags);
        setLineWidth(width);
        moveTo(x,y);
        x2=-1; y2=-1;
        while (true) {
            getCursorLoc(x, y, z, flags);
            if (flags&leftClick==0) exit();
            if (x!=x2 || y!=y2)
                lineTo(x,y);
            x2=x; y2 =y;
            wait(10);
        }
   }

   function setColorToBackgound() {
       savep = getPixel(0, 0);
       makeRectangle(0, 0, 1, 1);
       run("Clear");
       background = getPixel(0, 0);
       run("Select None");
       setPixel(0, 0, savep);
       setColor(background);
   }

  // Runs when the user double-clicks on the pencil tool icon
  macro 'Pencil Tool Options...' {
       pencilWidth = getNumber("Pencil Width (pixels):", pencilWidth);
  }

  // Runs when the user double-clicks on the paint brush tool icon
  macro 'Paintbrush Tool Options...' {
      brushWidth = getNumber("Brush Width (pixels):", brushWidth);
  }

  // Runs when the user double-clicks on the eraser tool icon
  macro 'Eraser Tool Options...' {
      eraserWidth = getNumber("Eraser Width (pixels):", eraserWidth);
  }

  // Runs when the user double-clicks on the flood fill tool icon
  macro 'Flood Fill Tool Options...' {
      requires("1.34j");
      restorePreviousTool();
      run("Color Picker...");
  }




