Android Open Source - YOGOSec Button






From Project

Back to project page YOGOSec.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project YOGOSec listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package YOGOSec.core.gui;
/*w w  w .j a  v a 2  s  . co  m*/
import YOGOSec.core.Game;
import YOGOSec.core.render.Render;
import YOGOSec.core.render.YUpPixmap;
import YOGOSec.core.util.Point2i;
import YOGOSec.core.util.Rectanglef;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;

/**
 * @author Aritz Lopez
 * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
 */
public class Button extends GUIComponent {

    private final String text;
    private final IActionListener listener;
    private boolean pressed, hovered;
    private int pressPointer = -1;
    private YUpPixmap pixmap;
    private Texture texture;

    public Button(Rectanglef bounds, String text, IActionListener listener) {
        super(bounds);
        this.text = text;
        this.listener = listener;

        this.pixmap = new YUpPixmap(Game.INSTANCE.getRender().getWidth(), Game.INSTANCE.getRender().getHeight(), Pixmap.Format.RGBA8888);

        pixmap.setColor(Color.RED);
        pixmap.fillRectangle(this.bounds.getX().intValue(), this.bounds.getY().intValue(), this.bounds.getWidth().intValue(), this.bounds.getHeight().intValue());
        pixmap.setColor(Color.BLACK);
        Gdx.app.log("YOGOSec", "Created button: " + this.bounds);
        this.texture = new Texture(this.pixmap);
    }

    @Override
    public void render(Render render) {
        super.render(render);
        render.draw(this.texture, 0, 0);
        render.drawBigCenteredString(this.text, (int) (this.bounds.getX() + (this.bounds.getWidth() / 2)), (int) (this.bounds.getY() + (this.bounds.getHeight() / 2)));
    }

    @Override
    public boolean touchDown(Point2i point, int pointer, int button) {
        Gdx.app.log("YOGOSec", "Touched at: " + point);
        if (this.bounds.contains(point, true)) {
            Gdx.app.log("YOGOSec", "Touched down!");
            this.pressed = true;
            this.pressPointer = pointer;
            return true;
        }
        return false;
    }

    @Override
    public boolean touchUp(Point2i point, int pointer, int button) {
        if (this.bounds.contains(point, true)) {
            this.pressed = false;
            this.hovered = false;
            Gdx.app.debug("YOGOSec", "Clicked with text: " + this.text);
            if (this.listener != null) listener.actionPerformed(this);
            return true;
        }
        return false;
    }

    @Override
    public boolean touchDragged(Point2i point, int pointer) {
        if (pointer == this.pressPointer && !this.bounds.contains(point)) {
            this.hovered = false;
            this.pressed = false;
        } else if (this.bounds.contains(point)) {
            this.pressed = true;
            this.pressPointer = pointer;
            return true;
        }
        return false;
    }

    @Override
    public boolean mouseMoved(Point2i point) {
        return this.hovered = this.bounds.contains(point);
    }

    @Override
    public void dispose() {
        super.dispose();
        this.pixmap.dispose();
        this.texture.dispose();
    }

    @Override
    public void onGUIResized(int width, int height) {
        super.onGUIResized(width, height);
        this.pixmap.dispose();
        this.pixmap = new YUpPixmap(width, height, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.RED);
        pixmap.fillRectangle(this.bounds.getX().intValue(), this.bounds.getY().intValue(), this.bounds.getWidth().intValue(), this.bounds.getHeight().intValue());
        pixmap.setColor(Color.BLACK);
    }
}




Java Source Code List

YOGOSec.android.GameActivity.java
YOGOSec.core.Game.java
YOGOSec.core.gui.Button.java
YOGOSec.core.gui.GUIComponent.java
YOGOSec.core.gui.GUI.java
YOGOSec.core.gui.IActionListener.java
YOGOSec.core.gui.InputListener.java
YOGOSec.core.gui.ProgressBar.java
YOGOSec.core.input.Input.java
YOGOSec.core.render.Render.java
YOGOSec.core.render.YUpPixmap.java
YOGOSec.core.screens.MainMenuScreen.java
YOGOSec.core.screens.MyScreen.java
YOGOSec.core.util.Point2f.java
YOGOSec.core.util.Point2i.java
YOGOSec.core.util.Point.java
YOGOSec.core.util.Rectangle.java
YOGOSec.core.util.Rectanglef.java
YOGOSec.core.util.Rectanglei.java
YOGOSec.java.GameDesktop.java