Legal Terms and Copyright Notice
/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
 * Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.
 * 
 * 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, 
        trademark notice, this list of conditions, and the following disclaimer.

 *   *  Redistributions in binary form must reproduce the above copyright notice, 
        trademark 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 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 fximageviewport;

import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.effect.DropShadow;

import javafx.stage.StageStyle;



function adjustMagnification(amount : Number) {
    var newMagnification = magnification + amount / 4;
    if (newMagnification < .5) {
        newMagnification = .5;
    } else if (newMagnification > 10) {
        newMagnification = 10;
    }
    magnification = newMagnification;
}

def image = Image {
    url: "http://lh4.ggpht.com/_uryd8W9bs_g/SJFMdmfUxoI/AAAAAAAABHM/QZDm-wkZZr4/SANY0194.JPG"
}
var scene : Scene;
var magnification: Number = 1.5;
// define the size of the glass in terms of viewable image
var GLASS_SIZE : Number = bind bgImageView.boundsInLocal.width / 4;
var GLASS_CENTER = bind GLASS_SIZE / 2;
var centerX : Number;
var centerY : Number;
// ratio of original image vs what is displayed by the bgImageView
var factor = bind image.height / bgImageView.boundsInLocal.height;
var viewportCenterX : Number = bind centerX * factor;
var viewportCenterY : Number = bind centerY * factor;
// viewport size is inversely proportional to magnification factor
var viewportSize : Number = bind GLASS_SIZE * factor / magnification;

def bgImageView : ImageView = ImageView {
    fitWidth: bind scene.width
    fitHeight: bind scene.height
    image: image
    // only cache if we're scaling
    cache: bind factor != 1.0
    smooth: true
    preserveRatio: true
    onMouseMoved: function(me: MouseEvent) {
        centerX = me.sceneX;
        centerY = me.sceneY;
    }
    onKeyPressed: function(ke: KeyEvent) {
        if (ke.code == KeyCode.VK_EQUALS or ke.code == KeyCode.VK_PLUS) {
            adjustMagnification(1.0);
        } else if (ke.code == KeyCode.VK_MINUS) {
            adjustMagnification(-1.0);
        }
    }
    onMouseWheelMoved: function(me: MouseEvent) {
        adjustMagnification(me.wheelRotation);
    }
    onMouseClicked: function(me: MouseEvent) {
        if (me.button != MouseButton.PRIMARY) {
            magGlass.smooth = not magGlass.smooth;
        }
        bgImageView.requestFocus();
    }
}

def magGlass: ImageView = ImageView {
    image: image
    preserveRatio: true
    fitWidth: bind GLASS_SIZE
    fitHeight: bind GLASS_SIZE
    smooth: true
    viewport: bind Rectangle2D {
        minX: viewportCenterX - viewportSize / 2
        minY: viewportCenterY - viewportSize / 2
        width: viewportSize
        height: viewportSize
    }
    clip: bind Circle {
        centerX: GLASS_CENTER
        centerY: GLASS_CENTER
        radius: GLASS_CENTER - 5
    }
}

def glassGroup : Group = Group {
    translateX: bind centerX - GLASS_CENTER
    translateY: bind centerY - GLASS_CENTER
    content: [
        magGlass,
        Text {
            x: bind GLASS_CENTER + GLASS_CENTER / 2
            y: bind GLASS_SIZE
            content: bind "x{%2.2f magnification}"
        }
        Circle {
            centerX: bind GLASS_CENTER
            centerY: bind GLASS_CENTER
            radius: bind GLASS_CENTER-2
            stroke: Color.GREEN
            strokeWidth: 4
            fill: null
        }
    ]
    effect: DropShadow { offsetY: 4 }
}

def desc = Text {
    x: 10 y: 15 
    content: bind if (not bgImageView.focused) "Click image to focus" else
                  "Use the +/- or mouse wheel to zoom. Right-click to make the magnification "
                  "{if (magGlass.smooth) "less smooth." else "more smooth."}"
    font: Font { size: 12 } 
}

Stage {
    style: StageStyle.UNDECORATED
    width: image.width / 2
    height: image.height / 2
    title: "Magnifying Glass"
    scene: scene = Scene { content: [ bgImageView, glassGroup, desc ] }
}
bgImageView.requestFocus();