License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package effectsplaygroundmobile;

import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.*;
import javafx.util.Math;

public class PhotoView extends CustomNode {
    public var scaleFactor = 1.0;
    public var image:Image;

    var scale = bind zoomValue / 100.0;
    public var zoomValue = 100.0;

    public var cropActive = false;
    var clipping:Rectangle = null;
    var flipTransform:Transform = Transform.scale(1.0,1.0);
    var flipTransformTranslate:Transform = Transform.translate(0,0);
    var rotateAngle = 0.0;
    var horzFlip = 1.0;
    var vertFlip = 1.0;
    public var lightness = 0.0;
    public var darkness = 0.0;
    public var sepia = 0.0;

    public var frameWidth = 0;
    public var frameColor = Color.TRANSPARENT;
    public var photoWidth = bind image.width*scaleFactor;
    public var photoHeight = bind image.height*scaleFactor;
    var viewport:Rectangle = Rectangle { x: 0 y: 0 width: bind photoWidth height: bind photoHeight };
    public var frameImage:Image;
    public var funImage:Image;
    public var funActive = false;

    var startX = 0.0;
    var startY = 0.0;
    var cropX = 0.0;
    var cropY = 0.0;

    var iv: ImageView;

    public override function create():Node {
        Group {
            clip: bind viewport
            content: [
                iv = ImageView {
                    clip: bind clipping
                    transforms: bind [
                        Transform.translate(cropX,cropY),
                        Transform.scale(horzFlip * scale, vertFlip * scale, photoWidth / 2, photoHeight / 2),
                        Transform.rotate(rotateAngle, photoWidth/2, photoHeight/2)
                    ]
                    image: bind image
                    onMousePressed: function(e:MouseEvent) {
                        if(cropActive) {
                            startX = e.sceneX-cropX;
                            startY = e.sceneY-cropY;
                        }
                    }
                    onMouseDragged: function(e:MouseEvent) {
                        if(cropActive) {
                            cropX = e.sceneX-startX;
                            cropY = e.sceneY-startY;
                        }
                    }

                    fitWidth: bind image.width*scaleFactor
                    fitHeight: bind image.height*scaleFactor
                },
                //lightness overlay
                Rectangle { width: bind photoWidth height: bind photoHeight fill: bind Color.rgb(255,255,255,lightness/100.0) },
                //darkness overlay
                Rectangle { width: bind photoWidth height: bind photoHeight fill: bind Color.rgb(0,0,0,darkness/100.0) },
                //sepia overlay
                Rectangle { width: bind photoWidth height: bind photoHeight fill: bind Color.rgb(164,110,44,sepia/100.0) },
                // frame border
                Rectangle { width: bind photoWidth height: bind photoHeight fill: null stroke: bind frameColor strokeWidth: bind frameWidth },
                // frame image
                ImageView { 
                    image: bind frameImage
                    fitWidth: bind frameImage.width*scaleFactor
                    fitHeight: bind frameImage.height*scaleFactor
                },
                // fun image
                ImageView {
                    image: bind funImage
                    var sfix = 0.0;
                    var sfiy = 0.0;
                    var cfix = 0.0;
                    var cfiy = 0.0;
                    translateX: bind cfix
                    translateY: bind cfiy
                    onMousePressed: function(e:MouseEvent) {
                        if(funActive) {
                            sfix = e.sceneX-cfix;
                            sfiy = e.sceneY-cfiy;
                        }
                    }
                    onMouseDragged: function(e:MouseEvent) {
                        if(funActive) {
                            cfix = e.sceneX-sfix;
                            cfiy = e.sceneY-sfiy;
                        }
                    }
                    fitWidth: bind funImage.width*scaleFactor
                    fitHeight: bind funImage.height*scaleFactor
                }
            ]
        };
    }

    public function crop():Void {
        if (clipping == null) {
            clipping = Rectangle { x: 0, y: 0, width: photoWidth, height: photoHeight }
        }
        def b = iv.parentToLocal(viewport.boundsInLocal);
        // Workaround for RT-4302 parentToLocal(Bounds) returns bounds with negative width and/or height
        def b_minX = Math.min(b.minX, b.maxX);
        def b_maxX = Math.max(b.minX, b.maxX);
        def b_minY = Math.min(b.minY, b.maxY);
        def b_maxY = Math.max(b.minY, b.maxY);
        def x1 = Math.max(b_minX, clipping.x);
        def y1 = Math.max(b_minY, clipping.y);
        def x2 = Math.min(b_maxX, clipping.x + clipping.width);
        def y2 = Math.min(b_maxY, clipping.y + clipping.height);
        //println("x1 = {x1}, y1 = {y1} - x2 = {x2}, y2 = {y2}");
        clipping = Rectangle { x: x1, y: y1, width: x2 - x1, height: y2 - y1 }
        //println("{clipping.x},{clipping.y} - {clipping.x + clipping.width},{clipping.y + clipping.height}");
    }

    public function resetZoom():Void {
        zoomValue = 100;
    }

    public function resetCrop():Void {
        clipping = null;
        cropX = 0;
        cropY = 0;
    }

    public function flipHorz():Void {
        horzFlip = horzFlip * -1.0;
    }

    public function flipVert():Void {
        vertFlip = vertFlip * -1.0;
    }

    public function resetFlip():Void {
        horzFlip = 1.0;
        vertFlip = 1.0;
    }

    public function rotateCW():Void {
        rotateAngle += 90.0;
    }

    public function rotateCCW():Void {
        rotateAngle -= 90.0;
    }

    public function resetRotate():Void {
        rotateAngle = 0.0;
    }

}