tilo.modules.graphics.java Source code

Java tutorial

Introduction

Here is the source code for tilo.modules.graphics.java

Source

package tilo.modules;

/*
 The MIT License (MIT)
    
 Copyright (c) 2015 copyright Muresan Vlad 
    
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
    
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
    
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

import java.util.ArrayList;
import java.util.List;

import tilo.Quad;
import tilo.Tilo;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class graphics {
    public String author() {
        return "Muresan Vlad";
    }

    public String license() {
        return "MIT";
    }

    public String description() {
        return "Plugin for drawing and rendering of images and text.";
    }

    public String[] dependencies() {
        return new String[] { "math" };
    }

    public List<Sprite> addTextures = new ArrayList<Sprite>();

    private SpriteBatch batch;
    private ShapeRenderer shapes;
    private OrthographicCamera camera;
    private BitmapFont curFont;
    private float rotation, scale;

    public Class<?> imageLoader = Texture.class;
    public Class<?> fontLoader = BitmapFont.class;

    public ShapeRenderer getShape() {
        return shapes;
    }

    public SpriteBatch getBatch() {
        return batch;
    }

    public OrthographicCamera getCamera() {
        return camera;
    }

    public BitmapFont getFont() {
        return curFont;
    }

    public graphics setFont(BitmapFont fnt) {
        curFont = fnt;
        return this;
    }

    public graphics setShader(ShaderProgram shader) {
        batch.setShader(shader);
        return this;
    }

    public graphics setBlending(int src, int dest) {
        batch.setBlendFunction(src, dest);
        return this;
    }

    public void plugin_load() {
        batch = new SpriteBatch();
        shapes = new ShapeRenderer();
        curFont = new BitmapFont();
        camera = new OrthographicCamera();
        camera.setToOrtho(true);
        shapes.setProjectionMatrix(camera.combined);
        batch.setProjectionMatrix(camera.combined);

    }

    public void destory() {
        batch.dispose();
        curFont.dispose();
    }

    public void plugin_update_before() {
        reset().flush();
    }

    public Color color(String name) {
        if (name.startsWith("#"))
            return Color.valueOf(name.replace("#", ""));
        if ("clear".equalsIgnoreCase(name))
            return Color.CLEAR;
        else if ("white".equalsIgnoreCase(name))
            return Color.WHITE;
        else if ("black".equalsIgnoreCase(name))
            return Color.BLACK;
        else if ("red".equalsIgnoreCase(name))
            return Color.RED;
        else if ("green".equalsIgnoreCase(name))
            return Color.GREEN;
        else if ("blue".equalsIgnoreCase(name))
            return Color.BLUE;
        else if ("lightgray".equalsIgnoreCase(name))
            return Color.LIGHT_GRAY;
        else if ("gray".equalsIgnoreCase(name))
            return Color.GRAY;
        else if ("darkgray".equalsIgnoreCase(name))
            return Color.DARK_GRAY;
        else if ("pink".equalsIgnoreCase(name))
            return Color.PINK;
        else if ("orange".equalsIgnoreCase(name))
            return Color.ORANGE;
        else if ("yellow".equalsIgnoreCase(name))
            return Color.YELLOW;
        else if ("magenta".equalsIgnoreCase(name))
            return Color.MAGENTA;
        else if ("cyan".equalsIgnoreCase(name))
            return Color.CYAN;
        return Color.CLEAR;
    }

    public Color color(float r, float g, float b) {
        return color(r, g, b, 1);
    }

    public Color color(float r, float g, float b, float a) {
        return new Color(r, g, b, a);
    }

    public ShaderProgram shader(String vert, String frag) {
        return new ShaderProgram(Tilo.file(vert), Tilo.file(frag));
    }

    public BitmapFont font(String file) {
        return (Tilo.assets.isLoaded(file)) ? (BitmapFont) Tilo.assets.get(file, fontLoader)
                : new BitmapFont(Tilo.file(file));
    }

    public Vector2 project(Vector2 pos) {
        return project(pos.x, pos.y);
    }

    public Vector2 project(float x, float y) {
        Vector3 temp = new Vector3(x, y, 0);
        camera.project(new Vector3(x, y, 0));
        return new Vector2(temp.x, temp.y);
    }

    public Vector2 unproject(Vector2 pos) {
        return project(pos.x, pos.y);
    }

    public Vector2 unproject(float x, float y) {
        Vector3 temp = new Vector3(x, y, 0);
        camera.unproject(new Vector3(x, y, 0));
        return new Vector2(temp.x, temp.y);
    }

    public graphics clear(float r, float g, float b) {
        return clear(color(r, g, b));
    }

    public graphics clear(float r, float g, float b, float a) {
        return clear(color(r, g, b, a));
    }

    public graphics clear(String color) {
        return clear(color(color));
    }

    public graphics clear(Color color) {
        Gdx.gl.glClearColor(color.r, color.g, color.b, color.a);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        return this;
    }

    public graphics tint(float r, float g, float b) {
        return tint(color(r, g, b));
    }

    public graphics tint(float r, float g, float b, float a) {
        return tint(color(r, g, b, a));
    }

    public graphics tint(String color) {
        return tint(color(color));
    }

    public graphics tint(Color color) {
        shapes.setColor(color);
        batch.setColor(color);
        curFont.setColor(color);
        return this;
    }

    public graphics rotate(float degrees) {
        return rotate(degrees, 0, 0, 1);
    }

    public graphics rotate(float degrees, float x, float y, float z) {
        if (rotation != degrees) {
            rotation = degrees;
            camera.rotate(rotation, x, y, z);
            camera.update();
        }

        return this;
    }

    public graphics scale(float factor) {
        factor = 1 / factor;
        if (scale != factor) {
            scale = factor;
            camera.zoom = scale;
            camera.update();
        }

        return this;
    }

    public graphics translate(Vector2 pos) {
        return translate(pos.x, pos.y);
    }

    public graphics translate(float x, float y) {
        camera.translate(-x, -y);
        camera.update();

        return this;
    }

    public graphics reset() {
        return resetColor().resetTransform();
    }

    public graphics flush() {
        return this;
    }

    public graphics resetColor() {
        shapes.setColor(Color.WHITE);
        batch.setColor(Color.WHITE);
        curFont.setColor(Color.WHITE);
        return this;
    }

    public graphics resetTransform() {
        scale = 1;
        camera.translate(0, 0);
        rotation = 0;
        camera.zoom = scale;
        camera.setToOrtho(true);
        return this;
    }

    public graphics print(String text, int x, int y) {
        return print(text, x, y, 1);
    }

    public graphics print(String text, int x, int y, float scale) {
        return print(text, x, y, scale, scale);
    }

    public graphics print(String text, int x, int y, float sx, float sy) {
        curFont.setScale(sx, -sx);
        curFont.draw(batch, text, x, y);
        return this;
    }

    public graphics printf(String text, int x, int y, int limit) {
        return printf(text, x, y, limit, "left");
    }

    public graphics printf(String text, int x, int y, int limit, String align) {
        curFont.setScale(1, -1);

        if ("left".equalsIgnoreCase(align))
            curFont.drawMultiLine(batch, text, x, y, limit, BitmapFont.HAlignment.LEFT);
        else if ("right".equalsIgnoreCase(align))
            curFont.drawMultiLine(batch, text, x, y, limit, BitmapFont.HAlignment.RIGHT);
        else if ("center".equalsIgnoreCase(align))
            curFont.drawMultiLine(batch, text, x, y, limit, BitmapFont.HAlignment.CENTER);

        return this;
    }

    public void addTexture(String file, float x, float y) {
        Sprite s = new Sprite(new Texture(Gdx.files.internal(file)));
        s.setX(x);
        s.setY(y);
        addTextures.add(s);
    }

    public Texture image(String file) {
        return (Tilo.assets.isLoaded(file)) ? (Texture) Tilo.assets.get(file, imageLoader)
                : new Texture(Tilo.file(file));
    }

    public graphics draw(Texture img, float x, float y) {

        return draw(img, x, y, 0);
    }

    public graphics draw(Texture img, float x, float y, float r) {

        return draw(img, x, y, r, 1, 1);
    }

    public graphics draw(Texture img, float x, float y, float r, float sx, float sy) {
        return draw(img, x, y, r, sx, sy, 0, 0);
    }

    public graphics draw(Texture img, float x, float y, float r, float sx, float sy, float ox, float oy) {
        int w = img.getWidth();
        int h = img.getHeight();
        writeQuad(img, x, y, ox, oy, w, h, sx, sy, r, 0, 0, w, h);
        return this;
    }

    public graphics drawq(Texture img, Quad q, float x, float y) {
        return drawq(img, q, x, y, 0);
    }

    public graphics drawq(Texture img, Quad q, float x, float y, float r) {
        return drawq(img, q, x, y, r, 1, 1);
    }

    public graphics drawq(Texture img, Quad q, float x, float y, float r, float sx, float sy) {
        return drawq(img, q, x, y, r, sx, sy, 0, 0);
    }

    public graphics drawq(Texture img, Quad q, float x, float y, float r, float sx, float sy, float ox, float oy) {
        writeQuad(img, x, y, ox, oy, q.w, q.h, sx, sy, r, q.sx, q.sy, q.sw, q.sh);
        return this;
    }

    private void writeQuad(Texture img, float x, float y, float originX, float originY, float width, float height,
            float scaleX, float scaleY, float rotation, int sourceX, int sourceY, int sourceW, int sourceH) {

        batch.draw(img, x, y, originX, originY, width, height, scaleX, scaleY, rotation, sourceX, sourceY, sourceW,
                sourceH, false, true);
    }

    public void line(float x, float y, float x2, float y2) {
        shapes.begin(ShapeType.Line);
        shapes.line(x, y, x2, y2);
        shapes.end();
    }

    public void rectfill(float x, float y, int width, int height) {
        shapes.begin(ShapeType.Filled);
        shapes.rect(x, y, width, height);
        shapes.end();

    }

    public void rectline(float x, float y, int width, int height) {
        shapes.begin(ShapeType.Line);
        shapes.rect(x, y, width, height);
        shapes.end();

    }

    public void circlefill(float x, float y, float radius) {
        shapes.begin(ShapeType.Filled);
        shapes.circle(x, y, radius);
        shapes.end();
    }

    public void circleline(float x, float y, float radius) {
        shapes.begin(ShapeType.Line);
        shapes.circle(x, y, radius);
        shapes.end();
    }
}