Search in sources :

Example 1 with GradientDrawable

use of com.ray3k.skincomposer.GradientDrawable in project skin-composer by raeleus.

the class DialogColorPicker method populate.

public void populate() {
    content.clear();
    content.defaults().padLeft(10.0f);
    Image cursor = new Image(skin.getDrawable("color-picker"));
    cursor.setTouchable(Touchable.enabled);
    Image hueKnob = new Image(skin, "color-scale");
    hueKnob.setTouchable(Touchable.enabled);
    Image hueKnob2 = new Image(skin, "color-scale-flipped");
    hueKnob2.setTouchable(Touchable.enabled);
    Image alphaKnob = new Image(skin, "color-scale");
    alphaKnob.setTouchable(Touchable.enabled);
    Image alphaKnob2 = new Image(skin, "color-scale-flipped");
    alphaKnob2.setTouchable(Touchable.enabled);
    Container selectedColorCont = new Container();
    selectedColorCont.setBackground(skin.getDrawable("white"));
    selectedColorCont.setColor(selectedColor);
    Vector3 v = rgbToHsb(selectedColor.r, selectedColor.g, selectedColor.b);
    Spinner greenSpinner, blueSpinner, alphaSpinner, hueSpinner, saturationSpinner, brightnessSpinner;
    hueSpinner = new Spinner(v.x * 359.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    hueSpinner.setMinimum(0.0);
    hueSpinner.setMaximum(359.0);
    hueSpinner.getTextField().addListener(main.getIbeamListener());
    hueSpinner.getButtonMinus().addListener(main.getHandListener());
    hueSpinner.getButtonPlus().addListener(main.getHandListener());
    saturationSpinner = new Spinner(v.y * 100.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    saturationSpinner.setMinimum(0.0);
    saturationSpinner.setMaximum(100.0);
    saturationSpinner.getTextField().addListener(main.getIbeamListener());
    saturationSpinner.getButtonMinus().addListener(main.getHandListener());
    saturationSpinner.getButtonPlus().addListener(main.getHandListener());
    brightnessSpinner = new Spinner(v.z * 100.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    brightnessSpinner.setMinimum(0.0);
    brightnessSpinner.setMaximum(100.0);
    brightnessSpinner.getTextField().addListener(main.getIbeamListener());
    brightnessSpinner.getButtonMinus().addListener(main.getHandListener());
    brightnessSpinner.getButtonPlus().addListener(main.getHandListener());
    redSpinner = new Spinner(selectedColor.r * 255.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    redSpinner.setMinimum(0.0);
    redSpinner.setMaximum(255.0);
    redSpinner.getTextField().addListener(main.getIbeamListener());
    redSpinner.getButtonMinus().addListener(main.getHandListener());
    redSpinner.getButtonPlus().addListener(main.getHandListener());
    greenSpinner = new Spinner(selectedColor.g * 255.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    greenSpinner.setMinimum(0.0);
    greenSpinner.setMaximum(255.0);
    greenSpinner.getTextField().addListener(main.getIbeamListener());
    greenSpinner.getButtonMinus().addListener(main.getHandListener());
    greenSpinner.getButtonPlus().addListener(main.getHandListener());
    blueSpinner = new Spinner(selectedColor.b * 255.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    blueSpinner.setMinimum(0.0);
    blueSpinner.setMaximum(255.0);
    blueSpinner.getTextField().addListener(main.getIbeamListener());
    blueSpinner.getButtonMinus().addListener(main.getHandListener());
    blueSpinner.getButtonPlus().addListener(main.getHandListener());
    alphaSpinner = new Spinner(selectedColor.a * 255.0f, 1.0, true, Orientation.HORIZONTAL, skin);
    alphaSpinner.setMinimum(0.0);
    alphaSpinner.setMaximum(255.0);
    alphaSpinner.getTextField().addListener(main.getIbeamListener());
    alphaSpinner.getButtonMinus().addListener(main.getHandListener());
    alphaSpinner.getButtonPlus().addListener(main.getHandListener());
    final TextField hexField = new TextField(selectedColor.toString(), skin);
    hexField.setMaxLength(8);
    hexField.setTextFieldFilter(new TextField.TextFieldFilter() {

        @Override
        public boolean acceptChar(TextField textField, char c) {
            return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
        }
    });
    hexField.addListener(main.getIbeamListener());
    redSpinner.setTransversalNext(greenSpinner.getTextField());
    greenSpinner.setTransversalNext(blueSpinner.getTextField());
    blueSpinner.setTransversalNext(alphaSpinner.getTextField());
    alphaSpinner.setTransversalNext(hueSpinner.getTextField());
    hueSpinner.setTransversalNext(saturationSpinner.getTextField());
    saturationSpinner.setTransversalNext(brightnessSpinner.getTextField());
    brightnessSpinner.setTransversalNext(redSpinner.getTextField());
    redSpinner.setTransversalPrevious(brightnessSpinner.getTextField());
    greenSpinner.setTransversalPrevious(redSpinner.getTextField());
    blueSpinner.setTransversalPrevious(greenSpinner.getTextField());
    alphaSpinner.setTransversalPrevious(blueSpinner.getTextField());
    hueSpinner.setTransversalPrevious(alphaSpinner.getTextField());
    saturationSpinner.setTransversalPrevious(hueSpinner.getTextField());
    brightnessSpinner.setTransversalPrevious(saturationSpinner.getTextField());
    ChangeListener rgbListener = new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            selectedColor.set((float) redSpinner.getValue() / 255.0f, (float) greenSpinner.getValue() / 255.0f, (float) blueSpinner.getValue() / 255.0f, (float) alphaSpinner.getValue() / 255.0f);
            Vector3 v = rgbToHsb(selectedColor.r, selectedColor.g, selectedColor.b);
            hueSpinner.setValue(v.x * 359.0f);
            saturationSpinner.setValue(v.y * 100.0f);
            brightnessSpinner.setValue(v.z * 100.0f);
            selectedColorCont.setColor(selectedColor);
            Color color = hsbToRgb((float) hueSpinner.getValue(), 1.0f, 1.0f);
            gradientS.setCol2(color);
            gradientS.setCol3(color);
            gradientAlpha.setCol3(color);
            gradientAlpha.setCol4(color);
            color = new Color(color);
            color.a = 0.0f;
            gradientAlpha.setCol1(color);
            gradientAlpha.setCol2(color);
            cursor.setX(v.y * SIZE - cursor.getWidth() / 2.0f);
            cursor.setY(v.z * SIZE - cursor.getHeight() / 2.0f);
            hueKnob.setY(v.x * SIZE - hueKnob.getHeight() / 2.0f);
            hueKnob2.setY(hueKnob.getY());
            hexField.setText(selectedColor.toString());
        }
    };
    redSpinner.addListener(rgbListener);
    greenSpinner.addListener(rgbListener);
    blueSpinner.addListener(rgbListener);
    ChangeListener hsbListener = new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            Color color = hsbToRgb((float) hueSpinner.getValue(), (float) saturationSpinner.getValue() / 100.0f, (float) brightnessSpinner.getValue() / 100.0f);
            color.a = (float) alphaSpinner.getValue() / 255.0f;
            redSpinner.setValue(color.r * 255.0f);
            greenSpinner.setValue(color.g * 255.0f);
            blueSpinner.setValue(color.b * 255.0f);
            selectedColor.set(color);
            selectedColorCont.setColor(selectedColor);
            color = hsbToRgb((float) hueSpinner.getValue(), 1.0f, 1.0f);
            gradientS.setCol2(color);
            gradientS.setCol3(color);
            gradientAlpha.setCol3(color);
            gradientAlpha.setCol4(color);
            color = new Color(color);
            color.a = 0.0f;
            gradientAlpha.setCol1(color);
            gradientAlpha.setCol2(color);
            cursor.setX((float) saturationSpinner.getValue() / 100.0f * SIZE - cursor.getWidth() / 2.0f);
            cursor.setY((float) brightnessSpinner.getValue() / 100.0f * SIZE - cursor.getHeight() / 2.0f);
            hueKnob.setY((float) hueSpinner.getValue() / 359.0f * SIZE - hueKnob.getHeight() / 2.0f);
            hueKnob2.setY(hueKnob.getY());
            hexField.setText(selectedColor.toString());
        }
    };
    hueSpinner.addListener(hsbListener);
    saturationSpinner.addListener(hsbListener);
    brightnessSpinner.addListener(hsbListener);
    ChangeListener hexListener = new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            if (hexField.getText().length() == 6 || hexField.getText().length() == 8) {
                Color color = Color.valueOf(hexField.getText());
                redSpinner.setValue(color.r * 255);
                greenSpinner.setValue(color.g * 255);
                blueSpinner.setValue(color.b * 255);
                alphaSpinner.setValue(color.a * 255);
                if (hexField.getText().length() == 6) {
                    selectedColor.set(color.r, color.g, color.b, (float) alphaSpinner.getValue() / 255.0f);
                } else {
                    selectedColor.set(color.r, color.g, color.b, color.a);
                }
                Vector3 v = rgbToHsb(selectedColor.r, selectedColor.g, selectedColor.b);
                hueSpinner.setValue(v.x * 359.0f);
                saturationSpinner.setValue(v.y * 100.0f);
                brightnessSpinner.setValue(v.z * 100.0f);
                selectedColorCont.setColor(selectedColor);
                color = hsbToRgb((float) hueSpinner.getValue(), 1.0f, 1.0f);
                gradientS.setCol2(color);
                gradientS.setCol3(color);
                gradientAlpha.setCol3(color);
                gradientAlpha.setCol4(color);
                color = new Color(color);
                color.a = 0.0f;
                gradientAlpha.setCol1(color);
                gradientAlpha.setCol2(color);
                cursor.setX(v.y * SIZE - cursor.getWidth() / 2.0f);
                cursor.setY(v.z * SIZE - cursor.getHeight() / 2.0f);
                hueKnob.setY(v.x * SIZE - hueKnob.getHeight() / 2.0f);
                hueKnob2.setY(hueKnob.getY());
                alphaKnob.setY(selectedColor.a * SIZE - alphaKnob.getHeight() / 2.0f);
                alphaKnob2.setY(alphaKnob.getY());
            }
        }
    };
    hexField.addListener(hexListener);
    alphaSpinner.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            selectedColor.set((float) redSpinner.getValue() / 255.0f, (float) greenSpinner.getValue() / 255.0f, (float) blueSpinner.getValue() / 255.0f, (float) alphaSpinner.getValue() / 255.0f);
            selectedColorCont.setColor(selectedColor);
            alphaKnob.setY(selectedColor.a * SIZE - alphaKnob.getHeight() / 2.0f);
            alphaKnob2.setY(alphaKnob.getY());
            hexField.setText(selectedColor.toString());
        }
    });
    Table panel = new Table(skin);
    panel.setBackground("color-box");
    Table t = new Table(skin);
    t.setClip(true);
    t.setBackground(gradientSB);
    t.setTouchable(Touchable.enabled);
    cursor.setPosition(v.y * SIZE - cursor.getWidth() / 2.0f, v.z * SIZE - cursor.getHeight() / 2.0f);
    t.addActor(cursor);
    DragListener dragListener = new DragListener() {

        @Override
        public void drag(InputEvent event, float x, float y, int pointer) {
            saturationSpinner.setValue(MathUtils.clamp(x / SIZE * 100.0f, 0, 100));
            brightnessSpinner.setValue(MathUtils.clamp(y / SIZE * 100.0f, 0, 100));
            saturationSpinner.fire(new ChangeListener.ChangeEvent());
        }
    };
    dragListener.setTapSquareSize(1.0f);
    t.addListener(dragListener);
    t.addListener(new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            saturationSpinner.setValue(MathUtils.clamp(x / SIZE * 100.0f, 0, 100));
            brightnessSpinner.setValue(MathUtils.clamp(y / SIZE * 100.0f, 0, 100));
            saturationSpinner.fire(new ChangeListener.ChangeEvent());
            return false;
        }
    });
    panel.add(t).size(SIZE, SIZE);
    content.add(panel);
    panel = new Table(skin);
    panel.setBackground("color-box");
    t = new Table(skin);
    t.setTouchable(Touchable.enabled);
    t.setClip(true);
    for (GradientDrawable gradient : hueGradient) {
        Container container = new Container();
        container.background(gradient);
        t.add(container).growX().height(50.0f);
        t.row();
    }
    t.addActor(hueKnob);
    t.addActor(hueKnob2);
    hueKnob.setY(v.x * SIZE - hueKnob.getHeight() / 2.0f);
    hueKnob2.setX(30.0f - hueKnob2.getWidth());
    hueKnob2.setY(v.x * SIZE - hueKnob2.getHeight() / 2.0f);
    dragListener = new DragListener() {

        @Override
        public void drag(InputEvent event, float x, float y, int pointer) {
            hueSpinner.setValue(MathUtils.clamp(y / SIZE * 359.0f, 0.0f, 359.0f));
            hueSpinner.fire(new ChangeListener.ChangeEvent());
        }
    };
    dragListener.setTapSquareSize(1.0f);
    t.addListener(dragListener);
    t.addListener(new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            hueSpinner.setValue(MathUtils.clamp(y / SIZE * 359.0f, 0.0f, 359.0f));
            hueSpinner.fire(new ChangeListener.ChangeEvent());
            return false;
        }
    });
    panel.add(t).minWidth(30.0f).height(SIZE);
    content.add(panel);
    panel = new Table(skin);
    panel.setBackground("color-box");
    t = new Table();
    t.setTouchable(Touchable.enabled);
    t.setBackground(alphaStack);
    t.setClip(true);
    t.addActor(alphaKnob);
    t.addActor(alphaKnob2);
    alphaKnob.setY(selectedColor.a * SIZE - alphaKnob.getHeight() / 2.0f);
    alphaKnob2.setX(30.0f - alphaKnob2.getWidth());
    alphaKnob2.setY(selectedColor.a * SIZE - alphaKnob2.getHeight() / 2.0f);
    dragListener = new DragListener() {

        @Override
        public void drag(InputEvent event, float x, float y, int pointer) {
            alphaSpinner.setValue(MathUtils.clamp(y / SIZE * 255.0f, 0.0f, 255.0f));
            alphaSpinner.fire(new ChangeListener.ChangeEvent());
        }
    };
    dragListener.setTapSquareSize(1.0f);
    t.addListener(dragListener);
    t.addListener(new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            alphaSpinner.setValue(MathUtils.clamp(y / SIZE * 255.0f, 0.0f, 255.0f));
            alphaSpinner.fire(new ChangeListener.ChangeEvent());
            return false;
        }
    });
    panel.add(t).minWidth(30.0f).height(SIZE);
    content.add(panel);
    t = new Table();
    t.defaults().pad(10.0f);
    Table table = new Table(skin);
    Label label = new Label("new", skin);
    label.setAlignment(Align.center);
    table.add(label).growX();
    table.row();
    Container bg = new Container();
    bg.setBackground(checker);
    Stack stack = new Stack(bg, selectedColorCont);
    panel = new Table(skin);
    panel.setBackground("color-box");
    panel.add(stack).grow();
    table.add(panel).grow();
    if (previousColor != null) {
        Container cont = new Container();
        cont.setBackground(skin.getDrawable("white"));
        cont.setColor(previousColor);
        bg = new Container();
        bg.setBackground(checker);
        stack = new Stack(bg, cont);
        panel.row();
        panel.add(stack).grow();
        table.row();
        label = new Label("current", skin);
        label.setAlignment(Align.center);
        table.add(label).growX();
        t.add(table).minWidth(80.0f).minHeight(150.0f);
    } else {
        t.add(table).minWidth(80.0f).minHeight(100.0f);
    }
    table = new Table();
    table.setName("confirmTable");
    table.defaults().space(10.0f).minWidth(80.0f);
    TextButton textButton = new TextButton("OK", skin);
    textButton.addListener(main.getHandListener());
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            if (listener != null) {
                listener.handle(new ColorListener.ColorEvent(selectedColor));
            }
            hide();
        }
    });
    table.add(textButton);
    textButton = new TextButton("Cancel", skin);
    textButton.addListener(main.getHandListener());
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            if (listener != null) {
                listener.handle(new ColorListener.ColorEvent(null));
            }
            hide();
        }
    });
    table.add(textButton);
    t.add(table);
    table.row();
    table = new Table();
    ((Table) t.findActor("confirmTable")).add(table).colspan(2);
    table.defaults().space(10.0f);
    label = new Label("#", skin, "required");
    table.add(label);
    table.add(hexField).width(75.0f);
    t.row();
    table = new Table();
    label = new Label("R", skin, "required");
    table.add(label);
    table.add(redSpinner).padLeft(10.0f).minWidth(90.0f);
    t.add(table);
    table = new Table();
    label = new Label("H", skin, "required");
    table.add(label);
    table.add(hueSpinner).padLeft(10.0f).minWidth(90.0f);
    t.add(table);
    t.row();
    table = new Table();
    label = new Label("G", skin, "required");
    table.add(label);
    table.add(greenSpinner).padLeft(10.0f).minWidth(90.0f);
    t.add(table);
    table = new Table();
    label = new Label("S", skin, "required");
    table.add(label);
    table.add(saturationSpinner).padLeft(10.0f).minWidth(90.0f);
    t.add(table);
    t.row();
    table = new Table();
    label = new Label("B", skin, "required");
    table.add(label);
    table.add(blueSpinner).padLeft(10.0f).minWidth(90.0f);
    t.add(table);
    table = new Table();
    label = new Label("B", skin, "required");
    table.add(label);
    table.add(brightnessSpinner).padLeft(10.0f).minWidth(90.0f);
    t.add(table);
    t.row();
    table = new Table();
    label = new Label("A", skin, "required");
    table.add(label);
    t.add(table);
    table.add(alphaSpinner).padLeft(10.0f).minWidth(90.0f);
    content.add(t).growY();
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Table(com.badlogic.gdx.scenes.scene2d.ui.Table) Spinner(com.ray3k.skincomposer.Spinner) Color(com.badlogic.gdx.graphics.Color) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) Vector3(com.badlogic.gdx.math.Vector3) Image(com.badlogic.gdx.scenes.scene2d.ui.Image) GradientDrawable(com.ray3k.skincomposer.GradientDrawable) Stack(com.badlogic.gdx.scenes.scene2d.ui.Stack) Container(com.badlogic.gdx.scenes.scene2d.ui.Container) DragListener(com.badlogic.gdx.scenes.scene2d.utils.DragListener) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) Actor(com.badlogic.gdx.scenes.scene2d.Actor) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent)

Aggregations

Color (com.badlogic.gdx.graphics.Color)1 Vector3 (com.badlogic.gdx.math.Vector3)1 Actor (com.badlogic.gdx.scenes.scene2d.Actor)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)1 Container (com.badlogic.gdx.scenes.scene2d.ui.Container)1 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)1 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)1 Stack (com.badlogic.gdx.scenes.scene2d.ui.Stack)1 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)1 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)1 TextField (com.badlogic.gdx.scenes.scene2d.ui.TextField)1 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)1 DragListener (com.badlogic.gdx.scenes.scene2d.utils.DragListener)1 GradientDrawable (com.ray3k.skincomposer.GradientDrawable)1 Spinner (com.ray3k.skincomposer.Spinner)1