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 effectsplayground.control;

import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.stage.*;
import javafx.util.*;

//@author campbell

class Preview extends CustomNode {
    def w = 26;
    def h = 16;
    var picker:ColorPicker;

    override protected function create():Node {
        Rectangle {
            width: w
            height: h
            fill: bind picker.selectedColor;
            stroke: Color.BLACK
        }
    }
}

class ColorPalette extends CustomNode {
    var picker:ColorPicker;
    var iv:ImageView;
    var mouseOver = false;
    var selectedColor = Color.BLACK;

    function colorAtLocation(x:Integer, y:Integer):Color {
        // TODO: this is a temporary workaround until RT-1355 is resolved
        var bimg = iv.image.platformImage as java.awt.image.BufferedImage;
        if (x < 0 or x >= bimg.getWidth() or y < 0 or y >= bimg.getHeight()) {
            return null;
        }
        var rgb = bimg.getRGB(x, y);
        var r = Bits.bitAnd(Bits.shiftRight(rgb, 16), 0xff);
        var g = Bits.bitAnd(Bits.shiftRight(rgb,  8), 0xff);
        var b = Bits.bitAnd(Bits.shiftRight(rgb,  0), 0xff);
        Color.rgb(r, g, b)
    }

    function updateSelectedColor(e:MouseEvent) {
        var rgb = colorAtLocation(e.x, e.y);
        if (rgb != null) {
            picker.selectedColor = rgb;
        }
    }

    override protected function create():Node {
        Group {
            content: [
                iv = ImageView {
                    cursor: Cursor.HAND
                    image:Image { url: "{__DIR__}colors.png" }
                    x: 1
                    y: 1
                    onMouseEntered:function(e) {
                        mouseOver = true;
                    }
                    onMouseExited:function(e) {
                        mouseOver = false;
                    }
                    onMousePressed:function(e) {
                        updateSelectedColor(e);
                    }
                    onMouseDragged: function(e) {
                        if (mouseOver) {
                            updateSelectedColor(e);
                        }
                    }
                },
                Rectangle {
                    width: bind iv.layoutBounds.width+1
                    height: bind iv.layoutBounds.height+1
                    fill: null
                    stroke: bind if (mouseOver) Color.YELLOW else Color.BLACK
                }
            ]
        }
    }
}

class GrayItem extends CustomNode {
    def w = 16;
    def h = 16;

    var picker:ColorPicker;
    var fill = Color.BLACK;
    var stroke = Color.BLACK;
    public-init var gray = 0.0 on replace {
        fill = Color.color(gray, gray, gray);
    };

    override protected function create():Node {
        Rectangle {
            cursor: Cursor.HAND
            width: w
            height: h
            fill: bind fill
            stroke: bind stroke

            onMouseEntered:function(e){
                stroke = Color.YELLOW;
            }
            onMouseExited:function(e){
                stroke = Color.BLACK;
            }
            onMousePressed:function(e){
                picker.selectedColor = fill;
            }
        }
    }
}

class GrayPalette extends CustomNode {
    var picker:ColorPicker;

    override protected function create():Node {
        HBox {
            spacing: 2
            content: [
                for (i in [0..9]) {
                    GrayItem {
                        gray: (9-i)/9.0
                        picker: bind picker
                    }
                }
            ]
        }
    }
}

public class ColorPicker extends CustomNode {
    public var selectedColor = Color.BLACK;

    def w = 20;
    def h = 16;

    override protected function create():Node {
        HBox {
            spacing: 25
            content: [
                Preview { picker: this },
                ColorPalette { picker: this },
                GrayPalette { picker: this }
            ]
        }
    }
}

function run() {
    Stage {
        width: 500
        height: 100
        scene:Scene {
            content: [
                ColorPicker {}
            ]
        }
    }
}