Package transientlibs.preui.objects.gui.elements

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

/*
* 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.BitmapFont;
import transientlibs.preui.objects.gui.interfaces.INode;
import transientlibs.preui.objects.gui.interfaces.IImage;
import transientlibs.preui.objects.gui.interfaces.IMarker;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
//import org.lwjgl.input.Mouse;


import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.datasets.Action;

import transientlibs.processors.misc.ClickProcessor;
import transientlibs.processors.misc.Detonator;
import transientlibs.preui.gdx.gui.MarkerList;
import transientlibs.objects.primitives.Coords;
import transientlibs.objects.general.Node;
import transientlibs.preui.gdx.gui.MouseHack;
import transientlibs.preui.objects.gui.interfaces.IColour;
import transientlibs.preui.objects.gui.interfaces.IFont;
import transientlibs.preui.objects.gui.misc.ButtonInterval;
import transientlibs.preui.objects.gui.misc.RenderList;

import transientlibs.tex.ActList;
import transientlibs.tex.ReqList;

/**
*
* @author kibertoad
*/
public class Marker extends Node implements IMarker {

    @Override
    public Action getRelatedAction() {
        return (Action) relatedAction();
    }

    @Override
    public void setNoHoverOnTransparent(boolean b) {
        noHoverOnTransparent = b;
    }

    @Override
    public void setOriginalImage(IImage iImage) {
        originalImage = iImage;
    }

    @Override
    public void draw(SpriteBatch spriteBatch) {
        if ((isDrawn) && (getImage() != null)) {
            //Log.info("has image");
            if (!isHovered()) {
                spriteBatch.draw(getImage().getAtlasRegion(), screenCoords.x, Detonator.INSTANCE.currentGenericGame.getScreenHeight() - screenCoords.y - getImage().getHeight());
            } else {
                spriteBatch.draw(getHoveredImage().getAtlasRegion(), screenCoords.x, Detonator.INSTANCE.currentGenericGame.getScreenHeight() - screenCoords.y - getImage().getHeight());
            }
        }
    }

    @Override
    public void drawTextureRegion(SpriteBatch spriteBatch) {
        if ((isDrawn) && (getImage() != null)) {
            //Log.info("has image");
            spriteBatch.draw(getImage().getTextureRegion(), screenCoords.x, Detonator.INSTANCE.currentGenericGame.getScreenHeight() - screenCoords.y - getImage().getHeight());
        }
    }

    @Override
    public BitmapFont getFont() {
        return null;
    }

    public enum MarkerType {

        NullMarker, IconMarker, ButtonMarker, MenuMarker, TextWindowMarker, CheckBoxMarker,
        SliderMarker, TextLine, UnitMarker, BoardUnitMarker, TileMarker, PlusMinusMarker, LandmarkMarker,
        CardMarker;
    }
    public boolean wasReloaded = false; //used for interrupting payload processing
    public boolean noHoverOnTransparent = true;
    public RenderList inRenderList = null;
    public String stringID = null;

    @Override
    public String getStringID() {
        return stringID;
    }

    @Override
    public void setStringID(String stringID) {
        this.stringID = stringID;
    }
    protected Menu inGroup = null;
    protected INode relatedNode = null;
    private ArrayList<ClickProcessor> payload = new ArrayList<ClickProcessor>();
    public IImage originalImage = null;
    private IImage imageLink = null;
    private IImage hoveredImageLink = null;
    public IImage focusedImageLink = null;
    public static int ScreenHeight = 768; //set it when initializing game for correct calculations
    public int imageHeight = 0;
    public int imageWidth = 0;
    public boolean isActive = true; //only relevant for buttons, work only if active
    public boolean isDrawn = true; //is it drawn on the next cycle?
    public boolean isOnScreen = true; //if not on screen, it will not be auto-enabled
    //is set true on SetButton\SetInfo command
    public boolean isFocused = false; //e. g. selected
    public ButtonGroup togglesGroup = null;
    public ReqList reqs = new ReqList();
    public ActList acts = new ActList();
    public int addressInGroup = -1;
    public boolean infoOnHover = false;
    public int maxWidth = 300;
    public Coords screenCoords = new Coords();
    public static IMarker lastMarker;

    @Override
    public boolean isOnScreen() {
        return isOnScreen;
    }

    @Override
    public boolean isDrawn() {
        return isDrawn;
    }

    @Override
    public void processTex(ArrayList<String> tokens) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public RenderList getRenderList() {
        return inRenderList;
    }

    @Override
    public void setRenderList(RenderList toList) {
        inRenderList = toList;
    }

    @Override
    public boolean getIsFocused() {
        return isFocused;
    }

    @Override
    public void setIsFocused(boolean toValue) {
        isFocused = toValue;
    }

//    @Override
//    public int getID() {
//        return ID;
//    }
    @Override
    public INode getRelatedNode() {
        return relatedNode;
    }

    @Override
    public void setRelatedNode(INode setNode) {
        relatedNode = setNode;
    }

    @Override
    public Menu getInGroup() {
        return inGroup;
    }

    @Override
    public void setInGroup(Menu setGroup) {
        inGroup = setGroup;
    }

    @Override
    public void setImage(IImage toImage) {
        imageLink = toImage;
        recountImageSize();
    }

    @Override
    public final IImage getImage() {
        return imageLink;
    }

    @Override
    public Coords getScreenCoords() {
        return screenCoords;
    }

    @Override
    public boolean isMenu() {
        return false;
    }

    @Override
    public void addPayload(ClickProcessor cp) {
        payload.add(cp);
    }

    public void resetPayload() {
        payload.clear();
    }

//draw from another list
    //public void relocate(SlickStateTemplate fromState, MarkerList toList) {
    //    relocate(fromState.drawStorage, toList);
    //}
    public void relocate(MarkerList fromList, MarkerList toList) {

        if (toList != null) {

            if (fromList.contains(this)) {
                fromList.remove(this);
            }
            if (!toList.contains(this)) {
                toList.add(this);
            }

        }
    }

    public void increaseCompositionPriority(MarkerList inList) {

        int compositionPosition;

        compositionPosition = inList.indexOf(this);
        if (compositionPosition < inList.size() - 1) {
            compositionPosition++;
        }
        inList.remove(this);
        inList.add(compositionPosition, this);

    }

    @Override
    public void minimizeCompositionPriority(MarkerList inList) {
        //inList.remove(this);
        //inList.add(0, this);
        inList.minimizeCompositionPriority(this);
    }

    @Override
    public void maximizeCompositionPriority(MarkerList inList) {

//                    inList.remove(this);
//                    int setSize = inList.size()-1;
//                    inList.add(setSize, this);
        inList.maximizeCompositionPriority(this);
    }

    public void decreaseCompositionPriority(MarkerList inList) {

        int compositionPosition;

        compositionPosition = inList.indexOf(this);
        if (compositionPosition > 1) {
            compositionPosition--;
        }
        inList.remove(this);

        //Log.info("Position: "+compositionPosition);
        if (compositionPosition == -1) {
            compositionPosition = 0;
        }

        inList.add(compositionPosition, this);

    }

    @Override
    public boolean returnsInfoOnHover() {
        return infoOnHover;
    }

    @Override
    public void saveToFile(Writer out) {
        StringBuilder outputString = new StringBuilder();
        outputString.append("element marker ");
        outputString.append(stringID);
        outputString.append(" ");
        outputString.append(screenCoords.getIntX());
        outputString.append(" ");
        outputString.append(screenCoords.getIntY());
        outputString.append(" ");
        outputString.append(originalImage.getName());
        outputString.append("\n");
        outputString.append("\n");


        Log.info("String was written: " + outputString.toString());
        //Log.info("TransientImage ID: "+imageID);

        try {
            out.write(outputString.toString());
        } catch (IOException ex) {
            Logger.getLogger(Marker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean isClickable() {
        return true;
    }

    public Action relatedAction() {
        return (Action) relatedNode;
    }

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

    @Override
    public void fillInfo(InfoGroup infoTable) {
        if (relatedNode != null) {
            relatedNode.fillInfo(infoTable);
        }
    }

    @Override
    public void reEvaluate() {
        if (relatedNode != null) {
            relatedNode.reEvaluate();
        }
    }

    public final void recountImageHeight() {

        if (imageLink != null) {
            imageHeight = getImage().getHeight();

        } else if (inGroup != null) {
            imageHeight = textHeight();
        }


    }

    public final void recountImageWidth() {

        if (imageLink != null) {
            imageWidth = getImage().getWidth();
        } else if (inGroup != null) {
            imageWidth = inGroup.maxWidth;
        } else {
            if (this.markerID() == MarkerType.TextLine) {
                imageWidth = Detonator.INSTANCE.ScreenSizeX;
            }
        }

        //else
        //{
        //}


    }

    public final void recountImageSize() {
        recountImageWidth();
        recountImageHeight();
    }

    public int textHeight() {
        return 0;
    }

    public Marker(float setX, float setY) {

        screenCoords.x = setX;
        screenCoords.y = setY;
        screenCoords.z = 1;

        imageHeight = 0;
        imageWidth = 0;
    }

    @Override
    public String name() {
        return "Marker";
    }

    /*
     public void setBothImages(SlickImageCatalog image) {
     if (image != null) {
     setBothImages(image.image);
     } else {
     setBothImages((TransientSlickImage) null);
     }
     }
     */
    @Override
    public void setBothImages(IImage image) {
        setImage(image);
        setHoveredImage(image);
    }

//    public void setImage (IImage image) {
//        imageLink = image;
//        recountImageSize();
//    }
    @Override
    public void setFocusedImage(IImage image) {
        focusedImageLink = image;
        recountImageSize();
    }

    @Override
    public void setHoveredImage(IImage image) {
        hoveredImageLink = image;
    }

    public Marker(IImage assignImage, IImage setHoveredImage, int setx, int sety) {

        screenCoords.x = setx;
        screenCoords.y = sety;
        screenCoords.z = 1;

        imageLink = assignImage;
        hoveredImageLink = setHoveredImage;
        focusedImageLink = setHoveredImage;

        recountImageSize();
    }

    public Marker(IImage assignImage, int setx, int sety) {
        screenCoords.x = setx;
        screenCoords.y = sety;
        screenCoords.z = 1;

        imageLink = assignImage;
        hoveredImageLink = assignImage;
        focusedImageLink = assignImage;

        recountImageSize();
    }

    //public final IImage getImage() {
    //    return imageLink;
    //}
    public final IImage getHoveredImage() {

        //Log.info("Hovered is null: "+(hoveredImageLink == null));
        //Log.info("Main is null: "+(imageLink == null));

        if (hoveredImageLink != null) {
            return hoveredImageLink;
        } else {
            return (imageLink);
        }
    }

    public static void setLast(int setType, int setID) {
        lastMarker.setType(setType);
        lastMarker.setID(setID);
    }

    public void assignImage(IImage toImage) {
        imageLink = toImage;
        hoveredImageLink = toImage;
        recountImageSize();
    }

    public boolean isTransparentAreaMouseHovered() {
        return imageLink.getColour(MouseHack.getX() - screenCoords.getIntX(), ScreenHeight - MouseHack.getY() - screenCoords.getIntY()).a() == 0f;
    }

    public boolean isTransparentAreaMouseHovered(int mouseX, int mouseY) {
        //Log.info("TransientImage size:"+imageLink.getWidth()+"/"+imageLink.getHeight());
        //Log.info("Check spot:"+(mouseX - screenCoords.getIntX())+"/"+(mouseY - screenCoords.getIntY()));
        //Log.info("Check spot RAW:"+(mouseX)+"/"+(mouseY));


        //uncomment later
        //return imageLink.getColour(mouseX - screenCoords.getIntX(), mouseY - screenCoords.getIntY()).a() == 0f;
        //return false;

        return imageLink.isTransparent(mouseX - screenCoords.getIntX(), mouseY - screenCoords.getIntY());
    }

    public boolean isTransparentAreaHovered(int X, int Y) {
        //Color c = imageLink.getColour(X, Y);
        //return c.a == 0f;

        //return (imageLink.getColour(X, Y).a() == 0f);

        return imageLink.isTransparent(X, Y);
    }

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

        mouseY = ScreenHeight - mouseY;

        //Log.info ("Mouse 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));

        return ((((mouseX >= screenCoords.x) && (mouseX < (screenCoords.x + imageWidth)))
                && ((mouseY >= screenCoords.getIntY()) && (mouseY < (screenCoords.getIntY() + imageHeight))))
                && ((noHoverOnTransparent == false) || (imageLink == null) || (!isTransparentAreaMouseHovered(mouseX, mouseY))));
    }

    @Override
    public int getWidth() {
        return imageWidth;
    }

    public int getImageEndX() {
        return (screenCoords.getIntX() + imageWidth);
    }

    @Override
    public int getHeight() {
        return imageHeight;
    }

    public int getImageEndY() {
        return (screenCoords.getIntY() + imageHeight);
    }

    public int getImageMiddleY() {
        return (screenCoords.getIntY() + (imageHeight / 2));
    }

    public int getImageMiddleX() {
        return (screenCoords.getIntX() + (imageWidth / 2));
    }

    /*
     public void drawOnSurface(Graphics g) {


     if (isFocused) {
     //Log.info("draw highlighted");
     g.drawImage((Image) focusedImageLink, screenCoords.getIntX(), screenCoords.getIntY());
     } else if (!(isHovered())) {
     g.drawImage((Image) getImage(), screenCoords.getIntX(), screenCoords.getIntY());
     } else {
     g.drawImage((Image) getHoveredImage(), screenCoords.getIntX(), screenCoords.getIntY());
     //g.drawImage(getImage(), screenCoords.getIntX(), screenCoords.getIntY());           
     }
     }
     */
    @Override
    public void draw() {

        //Log.info("Type: "+this.markerID());


        if (isFocused) {
            //Log.info("draw highlighted");
            focusedImageLink.draw(screenCoords.getIntX(), screenCoords.getIntY());
        } else if (!(isHovered())) {
            getImage().draw(screenCoords.getIntX(), screenCoords.getIntY());
        } else {
            getHoveredImage().draw(screenCoords.getIntX(), screenCoords.getIntY());
        }

    }

    public void draw(int onx, int ony) {

        //Log.info("Type: "+this.markerID());


        if (isFocused) {
            //Log.info("draw highlighted");
            focusedImageLink.draw(onx, ony);
        } else if (!(isHovered())) {
            getImage().draw(onx, ony);
        } else {
            getHoveredImage().draw(onx, ony);
        }

    }

    public void drawBorders(IColour colour) {
        Detonator.INSTANCE.geometryAgent.drawRectangle(screenCoords.x-1, screenCoords.y, imageWidth+1, imageHeight+1, colour);
    }

    /*
     public void drawBorders(Graphics graphics, Color withColor) {
     Color oldColor = graphics.getColor();

     graphics.setColor(withColor);
     graphics.drawRect((float) screenCoords.x, (float) screenCoords.y,
     imageWidth, imageHeight);

     graphics.setColor(oldColor);
     }

     public void drawBorders(Graphics graphics) {
     Color oldColor = graphics.getColor();

     graphics.setColor(Color.red);
     graphics.drawRect((float) screenCoords.x, (float) screenCoords.y,
     imageWidth, imageHeight);

     graphics.setColor(oldColor);
     }
     */
    @Override
    public boolean processClick() {

        Log.info("Process click yarr");

        wasReloaded = false;

        if (!acts.isEmpty()) {
            acts.process();
            Log.info("ActList not empty: " + acts.size());
        }

        if (relatedNode != null) {
            relatedNode.processActivators();
        }


        boolean result;
        if (togglesGroup != null) {
            result = (ID == 0);
        } else {
            result = false;
        }

        if (togglesGroup != null) {
            togglesGroup.setAll(true);
        }


        int iterationIndex = 0;
        ClickProcessor cp;

        Log.info("Payload size: " + payload.size());
        while ((iterationIndex < payload.size()) && (!(wasReloaded))) {

            cp = payload.get(iterationIndex);
            cp.process();
            iterationIndex++;
        }


        if ((iterationIndex < payload.size()) && (wasReloaded)) {
            Log.error("ERROR. There still were some ClickProcessors to process, but button got reloaded. Please check ClickProcessor sequence for the clicked button, button reload ClickButton should be the last one.");
        }

        /*
         if (!(payload.isEmpty())) {

         for (ClickProcessor cp : payload){
         cp.process();
         }
         }
         */


        return result;
    }

    public int returnClickedIntervalID() {
        if ((inGroup != null) && (inGroup.intervals != null)) {
            return inGroup.returnIntervalID(ID);
        } else {
            return -1;
        }
    }

    @Override
    public ButtonInterval returnClickedInterval() {
        if ((inGroup != null) && (inGroup.intervals != null)) {
            return inGroup.findInterval(ID);
        } else {
            return null;
        }
    }

    public void centerImage() {
        screenCoords.x = (Detonator.INSTANCE.ScreenSizeX - imageWidth) / 2;
        screenCoords.y = (Detonator.INSTANCE.ScreenSizeY - imageHeight) / 2;
    }

    @Override
    public void moveMarker(int toX, int toY) {
        screenCoords.x = toX;
        screenCoords.y = toY;
    }

    @Override
    public void moveMarkerByDelta(int byX, int byY) {
        screenCoords.x = screenCoords.x + byX;
        screenCoords.y = screenCoords.y + byY;
    }

    @Override
    public void setIsDrawn(boolean setValue) {
        isDrawn = setValue;
    }

    @Override
    public void setIsOnScreen(boolean setValue) {
        isOnScreen = setValue;
    }

    @Override
    public ArrayList<ClickProcessor> getPayload() {
        return payload;
    }

    @Override
    public ReqList getReqList() {
        return reqs;
    }

    @Override
    public ActList getActList() {
        return acts;
    }

    /*
     @Override
     public void drawBorders(Graphics gx, IColour red) {
     this.drawBorders(gx, red.getSlickColor());
     }
     */
    @Override
    public void reCalc() {
    }

    @Override
    public void drawBorders() {
        this.drawBorders(Detonator.INSTANCE.colourFactory.red);
    }

    @Override
    public void setText(String toText) {
    }

    @Override
    public String getText() {
        return null;
    }

    @Override
    public void setFont(IFont defaultFont) {
    }

    @Override
    public void savetoYaml(Writer out) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
   
   
}
TOP

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

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.