Package transientlibs.preui.objects.gui.elements

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

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

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import transientlibs.preui.objects.gui.interfaces.IFont;
import transientlibs.objects.primitives.SmallPoints;
import transientlibs.preui.utils.layout.HorizontalAlign;
import transientlibs.preui.utils.layout.VerticalAlign;


//import org.lwjgl.input.Mouse;

import transientlibs.objects.primitives.SmallPointsSlot;
import transientlibs.preui.gdx.gui.MouseHack;
import transientlibs.preui.objects.gui.misc.ButtonEffect;
import transientlibs.processors.misc.Detonator;

/**
*
* @author kibertoad
*/
public class PlusMinus extends TextMarker {

    public String storedText;
    public Marker PlusImage = null;
    public Marker MinusImage;
    public TextInfo label;
    int hoverType = 0;
    public static final int selfHover = 1;
    public static final int plusHover = 2;
    public static final int minusHover = 3;
    public SmallPoints pointPool; //pool for all Plus Minuses in a group
    public SmallPoints value = new SmallPoints(0, 100);
    public int max;
    public SmallPointsSlot linkedValue;

    public PlusMinus(SmallPointsSlot usePool, String name, int maxValue) {
        super(null, null, 0, 0, 0);

        storedText = name;

        linkedValue = usePool;
        relinkValue();
        value.max.value = 100000;
        max = maxValue;
    }

    @Override
    public void reCalc() {
        //Log.info("Recalc");
       
        //Log.info("Pool is: "+pointPool.now);

        while ((pointPool.now.value) < 0) {
            pointPool.increase(1);
            value.decrease(1);
        }

        while ((value.now.value) > max) {
            pointPool.increase(1);
            value.decrease(1);
        }

        while ((value.now.value) < 0) {
            pointPool.decrease(1);
            value.increase(1);
        }   
       
        updateText();
    }

    public final void relinkValue() {
        value = linkedValue.referencedPoints;

        if (PlusImage != null) {
            if (PlusImage.imageHeight > imageHeight) {
                imageHeight = PlusImage.imageHeight;
            }
        }

        if (PlusImage != null) {                     
            PlusImage.getPayload().clear();
            PlusImage.addPayload(new ButtonEffect(value, ButtonEffect.EffectType.IncrementEffect));
            PlusImage.addPayload(new ButtonEffect(pointPool, ButtonEffect.EffectType.DecrementEffect));
            PlusImage.addPayload(new ButtonEffect(this, ButtonEffect.EffectType.reCalcMarker));          
        }

        if (MinusImage != null) {
            MinusImage.getPayload().clear();
            MinusImage.addPayload(new ButtonEffect(value, ButtonEffect.EffectType.DecrementEffect));
            MinusImage.addPayload(new ButtonEffect(pointPool, ButtonEffect.EffectType.IncrementEffect));
            MinusImage.addPayload(new ButtonEffect(this, ButtonEffect.EffectType.reCalcMarker));
        }

    }

    public PlusMinus(String withText, IFont setFont, int setX, int setY, SmallPoints usePool, int maxValue) {

        super(null, null, setX, setY, 0);

        label = new TextInfo(setX, setY, storedText + " " + value.now, -1, setFont);
       

        storedText = withText;
        label.setFont(setFont);
        label.setText(storedText + " " + value.now);
        //textCapsule.Text = storedText + " " + value.now;
        //rewrap();

        label.textCapsule.textHAlign = HorizontalAlign.left;
        label.textCapsule.textVAlign = VerticalAlign.center;

        pointPool = usePool;



        PlusImage = new Marker(Detonator.INSTANCE.imageProvider.getImage("plus"), Detonator.INSTANCE.imageProvider.getImage("plus"), setX + 130, setY);
        if (PlusImage.imageHeight > imageHeight) {
            imageHeight = PlusImage.imageHeight;
        }

        MinusImage = new Marker(Detonator.INSTANCE.imageProvider.getImage("minus"), Detonator.INSTANCE.imageProvider.getImage("minus"), (int) PlusImage.screenCoords.x + PlusImage.imageWidth + 20, setY);
        value.max.value = maxValue;



    }

    @Override
    public final void setText(String newText) {
        label.textCapsule.text = newText;
        label.rewrap();
    }

    @Override
    public final void rewrap() {
        label.expandConstraints();
        label.textCapsule.cut();
    }

    public final void expandConstraints() {
        imageHeight = textCapsule.font.getHeight(textCapsule.text);
        imageWidth = textCapsule.font.getWidth(textCapsule.text);

        label.textCapsule.maxWidth = imageWidth;
    }

    public void updateText() {
        //label.setText(storedText + " " + value.now + " /" + pointPool.now);
        label.setText(storedText + " " + value.now);
    }

    public void setValue(int toValue) {
        value.now.value = toValue;
        updateText();
    }

    @Override
    public boolean isHovered() {
        int mouseX = MouseHack.getX();
        int mouseY = MouseHack.getY();

        mouseY = ScreenHeight - mouseY;

        //Log.info ("Check coords: "+mouseX+"/"+(mouseY));
        //Log.info ("TransientImage size: "+ImageWidth+"/"+ImageHeight);
        //Log.info  ("Must be between "+screenCoords.x+"/"+screenCoords.getIntY()+" and "+(screenCoords.x + ImageWidth)+'/'+ (screenCoords.getIntY() + ImageHeight));

        if (((mouseX >= label.screenCoords.x) && (mouseX <= (label.screenCoords.x + imageWidth)))
                && ((mouseY >= label.screenCoords.getIntY()) && (mouseY <= (label.screenCoords.getIntY() + imageHeight)))) {
            hoverType = selfHover;
        } else if (((mouseX >= PlusImage.screenCoords.x) && (mouseX <= (PlusImage.screenCoords.x + PlusImage.imageWidth)))
                && ((mouseY >= PlusImage.screenCoords.getIntY()) && (mouseY <= (PlusImage.screenCoords.getIntY() + PlusImage.imageHeight)))) {
            hoverType = plusHover;
        } else if (((mouseX >= MinusImage.screenCoords.x) && (mouseX <= (MinusImage.screenCoords.x + MinusImage.imageWidth)))
                && ((mouseY >= MinusImage.screenCoords.getIntY()) && (mouseY <= (MinusImage.screenCoords.getIntY() + MinusImage.imageHeight)))) {
            hoverType = minusHover;
        } else {
            hoverType = 0;
        }


        return (hoverType != 0);

    }

    @Override
    public boolean processClick() {

        if ((hoverType == minusHover) && (value.now.value > 1)) {
            if (value.decrease(1)) {
                pointPool.increase(1);
            }
        }
        if ((hoverType == plusHover) && (pointPool.now.value > 0)) {
            if (value.increase(1)) {
                pointPool.decrease(1);
            }
        }

        setText(storedText + " " + value.now);

        return true;
    }

    @Override
    public void draw() {
        //super.draw ();

        if (!" ".equals(label.textCapsule.text)) {

            //Log.info(textCapsule.Text+" inside, hoho.");
            label.textCapsule.draw();
        }
        //textCapsule.draw();

        //Log.info(textCapsule.Text+" inside, hoho.");
        //Log.info(textCapsule.x+"/"+textCapsule.y+" xy, hoho.");


        PlusImage.draw();
        MinusImage.draw();
    }

    @Override
    public void draw(SpriteBatch spriteBatch) {
        /*if (!" ".equals(label.textCapsule.text)) {
         
         label.textCapsule.draw(spriteBatch);
         }

         PlusImage.draw(spriteBatch);
         MinusImage.draw(spriteBatch);
         */
    }

    @Override
    public MarkerType markerID() {
        return MarkerType.PlusMinusMarker;
    }
}
TOP

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

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.