com.gmail.bleedobsidian.logicbuilder.utils.InvisibleButton.java Source code

Java tutorial

Introduction

Here is the source code for com.gmail.bleedobsidian.logicbuilder.utils.InvisibleButton.java

Source

/*
 *  Logic-Builder.
 *  Copyright (C) 2016 Jesse Prescott (BleedObsidian)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.gmail.bleedobsidian.logicbuilder.utils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Cursor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

/**
 * A utility class to create transparent buttons.
 * 
 * @author Jesse Prescott (BleedObsidian)
 */
public class InvisibleButton implements GameInterface {

    /**
     * The orthographic camera used for coordinate.
     */
    private final OrthographicCamera camera;

    /**
     * X Location of button.
     */
    private int locationX;

    /**
     * Y Location of button.
     */
    private int locationY;

    /**
     * X Size of button.
     */
    private final int sizeX;

    /**
     * Y Size of button.
     */
    private final int sizeY;

    /**
     * ClickListener.
     */
    private ClickListener clickListener;

    /**
     * If button is currently being touched.
     */
    private boolean isTouched = false;

    /**
     * A ShapeRenderer to draw borders of the button.
     */
    private final ShapeRenderer shapeRenderer = new ShapeRenderer();

    /**
     * If the button should render a black border when Render() is called.
     */
    private boolean shouldRenderBorder = false;

    /**
     * If the button is visible it accepts input.
     */
    private boolean isVisible = true;

    /**
     * New InvisibleButton.
     * 
     * @param camera The camera to use for coordinates.
     * @param x X Location of button.
     * @param y Y Location of button.
     * @param sizeX X Size of button.
     * @param sizeY Y Size of button.
     */
    public InvisibleButton(OrthographicCamera camera, int x, int y, int sizeX, int sizeY) {
        this.camera = camera;
        this.locationX = x;
        this.locationY = y;

        this.sizeX = sizeX;
        this.sizeY = sizeY;
    }

    @Override
    public void resume() {
    }

    @Override
    public void update(float delta) {
        if (Gdx.app.getInput().isTouched() && this.isVisible) {
            Vector3 touchLocation = this.camera
                    .unproject(new Vector3(Gdx.app.getInput().getX(), Gdx.app.getInput().getY(), 0));

            if (touchLocation.x >= this.locationX && touchLocation.x <= this.locationX + this.sizeX) {
                if (touchLocation.y >= this.locationY && touchLocation.y <= this.locationY + this.sizeY) {
                    if (!this.isTouched & this.clickListener != null) {
                        this.clickListener.clicked(new InputEvent(), touchLocation.x, touchLocation.y);
                        this.isTouched = true;
                    }
                } else {
                    if (this.isTouched == true) {
                        this.isTouched = false;
                    }
                }
            } else {
                if (this.isTouched == true) {
                    this.isTouched = false;
                }
            }
        } else {
            this.isTouched = false;
        }
    }

    @Override
    public void render(SpriteBatch spriteBatch) {
        if (this.shouldRenderBorder) {
            this.shapeRenderer.setProjectionMatrix(this.camera.combined);

            this.shapeRenderer.begin(ShapeType.Line);
            this.shapeRenderer.setColor(Color.BLACK);
            this.shapeRenderer.rect(this.locationX, this.locationY, this.sizeX, this.sizeY);
            this.shapeRenderer.end();
        }
    }

    @Override
    public void pause() {
    }

    /**
     * Add a ClickListener.
     * 
     * @param clickListener ClickListener.
     */
    public void addListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    /**
     * @return X Location of button.
     */
    public int getX() {
        return this.locationX;
    }

    /**
     * @param x X Location of button.
     */
    public void setX(int x) {
        this.locationX = x;
    }

    /**
     * @return Y Location of button.
     */
    public int getY() {
        return this.locationY;
    }

    /**
     * @param y Y Location of button.
     */
    public void setY(int y) {
        this.locationY = y;
    }

    /**
     * If the button should render a black border.
     * 
     * @param shouldRenderBorder Boolean.
     */
    public void setShouldRenderBorder(boolean shouldRenderBorder) {
        this.shouldRenderBorder = shouldRenderBorder;
    }

    /**
     * If the button is visible (accepts input).
     * 
     * @param isVisible Boolean.
     */
    public void setIsVisible(boolean isVisible) {
        this.isVisible = isVisible;
    }

    /**
     * If the button is visible (accepts input).
     * 
     * @return Boolean.
     */
    public boolean isVisible() {
        return this.isVisible;
    }
}