Package transientlibs.preui.objects.gui.elements

Source Code of transientlibs.preui.objects.gui.elements.ButtonGroup

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.preui.objects.gui.elements;

import transientlibs.preui.objects.gui.interfaces.IFont;
import transientlibs.preui.objects.gui.interfaces.IImage;
import transientlibs.objects.general.Node;
import transientlibs.objects.general.PointSet;
import transientlibs.objects.primitives.SmallPoints;



import java.util.ArrayList;
import java.util.Iterator;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.datasets.Action;
import transientlibs.preui.objects.gui.misc.ButtonEffect;
import transientlibs.processors.misc.ClickProcessor;
import transientlibs.processors.misc.Detonator;
import transientlibs.preui.objects.states.IState;
import transientlibs.preui.utils.layout.HorizontalAlign;

/**
*
* @author kibertoad
*/
public class ButtonGroup extends Menu {

    public String title = ""; //header
    public String groupOfElements = "-"; //for collecting from actions.dat
    public IState currentState;
    public static ButtonGroup lastButtonGroup;
    public TextMarker lastLine; //last added option

   
   
   
  public ButtonGroup(TextMarker fromMarker, IState setState) {
        super(null, fromMarker.screenCoords.getIntX(), fromMarker.screenCoords.getIntY(), fromMarker.font);

        setDefaultValues(setState);
        //setImage(fromMarker.getImage());

        isDrawn = true;
    }

    public ButtonGroup(int setX, int setY, IImage useImage, IFont setFont, IState setState) {
        super(null, setX, setY, setFont);

        setDefaultValues(setState);
        setImage(useImage);
        isDrawn = true;

        //Log.info("TransientImage was set!");
    }

    public ButtonGroup(int setX, int setY, IFont setFont, IState setState) {
        super(null, setX, setY, setFont);

        setDefaultValues(setState);
        setImage(null);
    }   
   
   
   
    public void collectAllActionsInGroupWithTag(String checkGroup, String withTag) {

        TextMarker m;

        for (Action a : Action.container.elements) {

            if ((a.group.equals(checkGroup)) && (a.reqs.isFulfilled())
                    && (a.hasTag(withTag))) {
                m = addNodeOption(a.LName.value, a);
                a.reEvaluate();
                if (!(a.reqsToActivate.isFulfilled())) {
                    m.isActive = false;
                } else {
                    m.isActive = true;
                }

                if (!(m.isActive)) {
                    m.textCapsule.colour = Detonator.INSTANCE.colourFactory.getColour("red");
                    m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.getColour("red");
                }
                if ((m.isActive)) {
                    m.textCapsule.colour = Detonator.INSTANCE.colourFactory.getColour("white");
                    m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.getColour("red");
                }
            }
        }

    }

    public void setLeftPositioning (){
        textHAlign = HorizontalAlign.left;
    }
   
    public void collectAllActionsWithTag(String withTag) {

        TextMarker m;

        for (Action a : Action.container.elements) {

            //Log.info("Ingroup: "+(a.group.equals(inGroup)));
            //Log.info("fulfilled: "+(a.reqs.isFulfilled()));
            //Log.info("name: "+a.LName);

            if ((a.hasTag(withTag)) && (a.reqs.isFulfilled())) {
                m = addNodeOption(a.LName.value, a);
                a.reEvaluate();
                if (!(a.reqsToActivate.isFulfilled())) {
                    m.isActive = false;
                } else {
                    m.isActive = true;
                }

                if (!(m.isActive)) {
                    m.textCapsule.colour = Detonator.INSTANCE.colourFactory.getColour("red");
                    m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.getColour("red");
                }
                if ((m.isActive)) {
                    m.textCapsule.colour = Detonator.INSTANCE.colourFactory.getColour("white");
                    m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.getColour("red");
                }
            }
        }

    }

   
    public void collectAllActionsInGroup(String checkGroup) {


        TextMarker m;

        for (Action a : Action.container.elements) {

            //Log.info("Ingroup: "+(a.group.equals(inGroup)));
            //Log.info("fulfilled: "+(a.reqs.isFulfilled()));
            //Log.info("name: "+a.LName);

            if ((a.group.equals(checkGroup)) && (a.reqs.isFulfilled())) {
                m = addNodeOption(a.LName.value, a);
                a.reEvaluate();
                if (!(a.reqsToActivate.isFulfilled())) {
                    m.isActive = false;
                } else {
                    m.isActive = true;
                }

                if (!(m.isActive)) {
                    m.textCapsule.colour = Detonator.INSTANCE.colourFactory.getColour("red");
                    m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.getColour("red");
                }
                if ((m.isActive)) {
                    m.textCapsule.colour = Detonator.INSTANCE.colourFactory.getColour("white");
                    m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.getColour("red");
                }
            }
        }


    }

    public final void setDefaultValues(IState setState) {
        isDrawn = true;
        this.highlightedColour = Detonator.INSTANCE.colourFactory.defaultHoveredButtonColour;

        currentState = setState;

        if (Detonator.INSTANCE.useGDXImages == false){
       
        if (Detonator.INSTANCE.imageProvider.lastUsedButtonImage != null) {
            buttonImage = Detonator.INSTANCE.imageProvider.lastUsedButtonImage;
        }

        if (Detonator.INSTANCE.imageProvider.lastUsedHoveredButtonImage != null) {
            hoveredButtonImage = Detonator.INSTANCE.imageProvider.lastUsedHoveredButtonImage;
        }
        }
       
       
        if (buttonImage == null) {
            buttonImage = Detonator.INSTANCE.imageProvider.defaultButtonImage;
            hoveredButtonImage = Detonator.INSTANCE.imageProvider.defaultHoveredButtonImage;
        }
       
        buttonFont = Detonator.INSTANCE.fontProvider.getDefaultFont();

    }

    public TextMarker returnLastButton() {
        return elements.get(elements.size() - 1);
    }

 

    @Override
    public void restartFilling() {



        for (TextMarker m : elements) {
            m.acts.clear();
            m.reqs.clear();
            m.getPayload().clear();

            m.wasReloaded = true;

            //m.textCapsule.colour = Detonator.INSTANCE.colourFactory.produceColour(colour);
            //m.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.produceColour(highlightedColour);

            m.textCapsule.colour = colour;
            m.textCapsule.highlightedColour = highlightedColour;
}

        lastButtonGroup = this;
        Menu.LastMenu = this;
        setOptionsCount = 0;

    }

    public void maximizeButtonHoverPriority(IState inState) {
        for (Marker m : elements) {
            m.minimizeCompositionPriority(inState.getHoverStorage());
        }

    }


    /*
     * Disable unused buttons
     */
    @Override
    public void complete() {

        //Log.info("FillCounter: "+fillCounter);
        //Log.info("ElementSize: "+elements.size());


        while (setOptionsCount < elements.size()) {

            elements.get(setOptionsCount).isDrawn = false;
            elements.get(setOptionsCount).isOnScreen = false;
            setOptionsCount++;
        }

    }

    public void fillFromArray(String[] fromArray) {
        int counter;
        for (counter = 0; counter < fromArray.length; counter++) {

            addOption(fromArray[counter], counter);
        }
    }

    public void fillFromNamedArray(ArrayList fromArray) {
        int counter;
        Node x;
        for (counter = 0; counter < fromArray.size(); counter++) {

            x = (Node) fromArray.get(counter);
            addOption(x.name(), counter);
        }
    }

    public void bindPlusMinusToArrayList(PointSet<SmallPoints> fromArray) {

        SmallPoints nowPoints;
        int x = 0;
        Iterator<SmallPoints> i = fromArray.iterator();

        while (i.hasNext()) {
            nowPoints = i.next();
            returnPlusMinus(x).value = nowPoints;
            returnPlusMinus(x).updateText();
            //setText(storedText + " " + value.now);
            x++;
        }

    }

    public void fillPlusMinusFromArrayList(ArrayList fromArray, SmallPoints usePool, int maxValue) {
        Node nowNode;

        Iterator<Node> i = fromArray.iterator();

        while (i.hasNext()) {

            nowNode = i.next();
            addPlusMinusOption(nowNode.name(), nowNode, usePool, maxValue);

        }

    }

    public void deselectButton() {
        selectButton (null);
    }
   
    public void selectButton(TextMarker selectedButton) {

        TextMarker nowText;

        if (elements.size() > 0) {

            Iterator<TextMarker> i = elements.iterator();
            while (i.hasNext()) {

                nowText = i.next();

                if (nowText.isFocused == true) {
                    nowText.isFocused = false;
                }
            }

        }

        if (selectedButton != null) {selectedButton.isFocused = true;}
    }

    public void fillFromArrayList(ArrayList fromArray) {

        Node nowNode;

        Iterator<Node> i = fromArray.iterator();

        while (i.hasNext()) {

            nowNode = i.next();
            addNodeOption(nowNode.name(), nowNode);

        }
    }

    public void assignGroupToggle(ButtonGroup[] assignButtonGroups) {

        int counter;
        for (counter = 0; counter <= assignButtonGroups.length; counter++) {
            elements.get(counter).togglesGroup = assignButtonGroups[counter];
        }
    }

    public void assignToggle(int elementCount, ButtonGroup assignButtonGroups) {
        elements.get(elementCount).togglesGroup = assignButtonGroups;
    }

    public PlusMinus returnPlusMinus(int withIndex) {
        return (PlusMinus) elements.get(withIndex);
    }

    /**
     * Add new button.
     */
    public TextMarker addNodeButton(String setText, Node setNode, int setID, ButtonGroup setToggleGroup) {

        //call real one
        if (elements.isEmpty()) {
            lastLine = addNodeButton(screenCoords.getIntX(), screenCoords.getIntY(), setText, setNode, setID, setToggleGroup);
        } else {

            TextMarker.lastTextMarker = elements.get(setOptionsCount - 1);
            lastLine = addNodeButton(TextMarker.lastTextMarker.screenCoords.getIntX(), TextMarker.calculateNextYPosition(buttonYOffset), setText, setNode, setID, setToggleGroup);
        }

        return lastLine;
    }

    public void addNodeOption(IImage setImage, Node setNode) {
        addOption(setImage).relatedNode = setNode;

    }

    public TextMarker addOption(IImage setImage) {
        return addOption(setImage, setImage);
    }

    public TextMarker addOption(IImage setImage, IImage hoverImage) {

        IImage oldButton = buttonImage;
        IImage oldHButton = hoveredButtonImage;

        buttonImage = setImage;
        hoveredButtonImage = hoverImage;
        addOption("");

        buttonImage = oldButton;
        hoveredButtonImage = oldHButton;

        return elements.get(elements.size() - 1);
    }

    public void setOptionImage (IImage setImage) {
        this.buttonImage = setImage;
        this.hoveredButtonImage = setImage;
    }

    public void setOptionImage (IImage setImage, IImage setHoveredImage) {
        this.buttonImage = setImage;
        this.hoveredButtonImage = setHoveredImage;
    }


    public TextMarker addNodeButton(int setX, int setY, String setText, Node setNode, int setID, ButtonGroup setToggleGroup) {

        if (buttonImage == null) {
            //Log.warn("No button image set!!!");
            //buttonImage = Images.Button1_Black;
            //hoveredButtonImage = buttonImage;
        }

        if (buttonFont == null) {
            Log.error("No font set!!!");
        }

        if (currentState == null) {
            Log.error("No state set!!!");
        }


        if (elements.isEmpty()) {
            currentState.addTextMarker(buttonFont, buttonImage, hoveredButtonImage, setText, setX + ButtonStartXOffset, setY + ButtonStartYOffset, -1);
        } else {
            currentState.addTextMarker(buttonFont, buttonImage, hoveredButtonImage, setText, setX, setY + ButtonStartYOffset, -1);
        }

        TextMarker.lastTextMarker.addressInGroup = elements.size();
        elements.add(TextMarker.lastTextMarker);

        //Log.info("OK, TIME!!");

        //TextMarker.lastTextMarker.textCapsule.colour = Detonator.INSTANCE.colourFactory.produceColour(colour);
        TextMarker.lastTextMarker.textCapsule.colour = colour;

        //Log.info("Colour: "+colour);

        //TextMarker.lastTextMarker.textCapsule.highlightedColour = Detonator.INSTANCE.colourFactory.produceColour(highlightedColour);
        TextMarker.lastTextMarker.textCapsule.highlightedColour = highlightedColour;

        TextMarker.lastTextMarker.textCapsule.textHAlign = textHAlign;
        TextMarker.lastTextMarker.textCapsule.textVAlign = textVAlign;

        TextMarker.lastTextMarker.inGroup = this;
        TextMarker.lastTextMarker.isFocused = false;

        setNodeButton(TextMarker.lastTextMarker, setText, setNode, setID, setToggleGroup);
       
        TextMarker.lastTextMarker.textCapsule.x = TextMarker.lastTextMarker.textCapsule.x + TextStartXOffset;
        TextMarker.lastTextMarker.textCapsule.y = TextMarker.lastTextMarker.textCapsule.y + TextStartYOffset;



        //Log.info("TEXT: " + TextMarker.lastTextMarker.textCapsule.textLines[0]);
        //Log.info("TEXT2: " + TextMarker.lastTextMarker.textCapsule.text);

        //TextMarker.lastTextMarker.recountImageSize();


        //TextMarker.lastTextMarker.relatedNode = setNode;
        //TextMarker.lastTextMarker.ID = setID;
        //TextMarker.lastTextMarker.togglesGroup = setToggleGroup;


        this.lastY = setY;

        TextMarker.lastTextMarker.relocate(currentState.getDrawStorage(), inList);

        return TextMarker.lastTextMarker;
    }

    /**
     * Add new button with coords.
     */
    public void addNodeButton(int setX, int setY, String setText, Node setNode) {
        addNodeButton(setX, setY, setText, setNode, -1, null);
    }

    private TextMarker addButton(int setX, int setY, String setText, ButtonGroup setToggleGroup, int setID) {

        return addNodeButton(setX, setY, setText, null, setID, setToggleGroup);
    }

    /**
     * Add new button with coords.
     */
    private TextMarker addButton(int setX, int setY, String setText, int setID) {

        return addButton(setX, setY, setText, null, setID);
    }

    private TextMarker addButton(String setText, int setID) {
        return addButton(TextMarker.calculateNextXPosition(), TextMarker.calculateNextYPosition(buttonYOffset), setText, setID);
    }

    private TextMarker addButton(String setText, ButtonGroup setToggleGroup) {
        return addButton(TextMarker.calculateNextXPosition(), TextMarker.calculateNextYPosition(buttonYOffset), setText, setToggleGroup, -1);
    }

    public void addPlusMinusButton(int setX, int setY, String setText, Node setNode, int setID, SmallPoints usePool, int maxValue) {

        currentState.addPlusMinusMarker(buttonFont, null, setText, setX, setY, setID, usePool, maxValue);
        elements.add(TextMarker.lastTextMarker);
        TextMarker.lastTextMarker.inGroup = this;

        TextMarker.lastTextMarker.relatedNode = setNode;
        TextMarker.lastTextMarker.ID = setID;
    }

    public void addPlusMinusButton(String setText, Node setNode, int setID, SmallPoints usePool, int maxValue) {

        if (!elements.isEmpty()) {
            addPlusMinusButton(TextMarker.lastTextMarker.screenCoords.getIntX(), TextMarker.calculateNextYPosition(buttonYOffset), setText, setNode, setID, usePool, maxValue);
        } else {
            addPlusMinusButton(screenCoords.getIntX(), (int) screenCoords.getIntY(), setText, setNode, setID, usePool, maxValue);
        }
    }

    public void setNodeButton(TextMarker theButton, String setText, Node setNode, int setID, ButtonGroup setToggleGroup) {

        if (theButton.addressInGroup > 0) {
            theButton.screenCoords.y = elements.get(theButton.addressInGroup - 1).screenCoords.getIntY() + elements.get(theButton.addressInGroup - 1).imageHeight + buttonYOffset;
        }

        theButton.recountImageWidth();
        //Log.info("set text: "+setText);
        theButton.setText(setText);
        theButton.relatedNode = setNode;
        theButton.ID = setID;

        //needed?..
        theButton.isFocused = false;
        //hmm

        //Log.info("Set text: "+setText);

        if ((theButton.getImage() == null) && (theButton.textCapsule.textLines.length > 1)) {
            theButton.calculateTotalHeight();
            theButton.imageHeight = theButton.totalTextSize;

            //Log.info("RESET ImageHeight: " + theButton.ImageHeight);
        } else {
            theButton.recountImageHeight();

            //Log.info("RECOUNT ImageHeight: " + theButton.ImageHeight);
        }


        theButton.isOnScreen = true;
        theButton.isDrawn = isDrawn;
        //theButton.textCapsule.colour = Detonator.INSTANCE.colourFactory.produceColour(colour);
        theButton.textCapsule.colour = colour;
                       
        theButton.togglesGroup = setToggleGroup;
    }

    public void setNodeButton(TextMarker theButton, String setText, Node setNode) {
        setNodeButton(theButton, setText, setNode, -1, null);
    }

    public void setButton(TextMarker theButton, String setText, int setID) {
        setNodeButton(theButton, setText, null, setID, null);
    }

    public void setButton(TextMarker theButton, String setText, ButtonGroup setToggleGroup) {
        setNodeButton(theButton, setText, null, -1, null);
        theButton.togglesGroup = setToggleGroup;
    }

    /**
     * Add new option - if no free buttons available, create new.
     */
    public void addPlusMinusOption(String setText, Node setNode, SmallPoints usePool, int maxValue) {

        addPlusMinusButton(setText, setNode, -1, usePool, maxValue);
    }

    public TextMarker addNodeOption(String setText, Node setNode) {

        return addNodeOption(setText, setNode, -1, null);

    }

    public TextMarker addNodeOption(String setText, Node setNode, int setID, ButtonGroup setToggleGroup) {

        if (setOptionsCount >= elements.size()) {

            lastLine = addNodeButton(setText, setNode, setID, setToggleGroup);
            setOptionsCount++;
        } else {
            //result = addNodeOption(screenCoords.x, screenCoords.getIntY(), setText, setNode, setID, setToggleGroup);
            setNodeButton(elements.get(setOptionsCount), setText, setNode, setID, setToggleGroup);
            lastLine = elements.get(setOptionsCount);
            setOptionsCount++;
        }
//            addConsequentNodeOption(setText, setNode, setID);
        //      }

        return lastLine;
    }

    public void addNodeOption(int setX, int setY, String setText, Node setNode) {
        //addConsequentNodeOption(setX, setY, setText, setNode, -1);
        addNodeOption(setX, setY, setText, setNode, -1, null);
    }

    public TextMarker addNodeOption(int setX, int setY, String setText, Node setNode, int setID, ButtonGroup setToggleGroup) {
        if (setOptionsCount >= elements.size()) {

            //Log.info("create new");

            lastLine = addNodeButton(setX, setY, setText, setNode, setID, setToggleGroup);
            setOptionsCount++;

        } else {
            //Log.info("reuse");
            setNodeButton(elements.get(setOptionsCount), setText, setNode, setID, setToggleGroup);
            lastLine = elements.get(setOptionsCount);
            setOptionsCount++;
        }
        //  addConsequentNodeOption(setX, setY, setText, setNode, setID);
        return lastLine;
    }

    public TextMarker addOption(int setX, int setY, String setText, int setID) {

        //result = addConsequentOption(setX, setY, setText, setID);
        lastLine = addNodeOption(setX, setY, setText, null, setID, null);

        return lastLine;
    }

    public TextMarker addOption(int setX, int setY, String setText, ButtonGroup setToggleGroup) {

        lastLine = addNodeOption(setX, setY, setText, null, -1, setToggleGroup);

        return lastLine;
    }

    public TextMarker addOption(String setText, ButtonGroup setToggleGroup) {




        return addNodeOption(setText, null, -1, setToggleGroup);
        //}

//        return result;
    }

    public TextMarker addStateOption(String setText, int setStateID) {
        //System.out.println("to state "+setStateID);

        TextMarker tm;
        tm = addOption(setText, -1);
        tm.addPayload(new ButtonEffect (setStateID));
        //tm.acts.addStateTransition(setStateID);
        return tm;
    }

    public TextMarker addOption(int setX, int setY, String setText) {
        addOption(setX, setY, setText, -1);
        return elements.get(elements.size() - 1);
    }

    @Override
    public TextMarker addOption(String setText) {
        return addOption(setText, -1);
    }

    public TextMarker addStateOption(int setX, int setY, String setText, int setStateID) {
        //System.out.println("to state "+setStateID);

        addOption(setX, setY, setText, -1).acts.addStateTransition(setStateID);
        return elements.get(elements.size() - 1);
    }

    public TextMarker addOption(String setText, ClickProcessor setPayload) {
        TextMarker result = addOption(setText, -1);
        result.addPayload(setPayload);
        return result;
    }

    public TextMarker addOption(String setText, int setID) {
        if (this.setOptionsCount == 0) {
            //if (elements.isEmpty()) {
            //Log.info("adding first button");
            lastLine = addNodeOption(screenCoords.getIntX(), screenCoords.getIntY(), setText, null, setID, null);
        } else {
            //Log.info("adding additional button");
            //lastLine = addNodeOption(TextMarker.lastTextMarker.screenCoords.x, TextMarker.calculateNextYPosition(ButtonYOffset), setText, null, setID, null);
            lastLine = addNodeOption(elements.get(elements.size() - 1).screenCoords.getIntX(), TextMarker.calculateNextYPosition(buttonYOffset), setText, null, setID, null);
        }

        //not needed
        //if (inRenderList != null) {
            //inRenderList.add(lastLine);
        //}
        return lastLine;
    }

    public void setXOffsetToTheMiddle(int imageWidth) {
        ButtonStartXOffset = ((maxWidth - imageWidth) / 2);
    }

    public int getYOffsetToPercentage(int percentage) {
        return (screenCoords.getIntY()) + (imageHeight * percentage / 100);
    }

    public int getXOffsetToPercentage(int percentage) {
        return (screenCoords.getIntX()) + (imageWidth * percentage / 100);
    }

    public void setYOffsetToPercentage(int percentage) {
        ButtonStartYOffset = ((imageHeight - buttonImage.getHeight()) * percentage / 100);
    }

    public void setXOffsetToTheMiddle() {

        ButtonStartXOffset = ((maxWidth - buttonImage.getWidth()) / 2);
    }
}
TOP

Related Classes of transientlibs.preui.objects.gui.elements.ButtonGroup

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.