dorkbox.tweenengine.demo.SplashScreen.java Source code

Java tutorial

Introduction

Here is the source code for dorkbox.tweenengine.demo.SplashScreen.java

Source

/*
 * Copyright 2012 Aurelien Ribon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dorkbox.tweenengine.demo;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import dorkbox.accessors.SpriteAccessor;
import dorkbox.tweenengine.BaseTween;
import dorkbox.tweenengine.Timeline;
import dorkbox.tweenengine.Tween;
import dorkbox.tweenengine.TweenCallback;
import dorkbox.tweenengine.TweenEquations;
import dorkbox.tweenengine.TweenManager;
import dorkbox.tweenengine.UpdateAction;

/**
 * @author Aurelien Ribon | http://www.aurelienribon.com/
 */
public class SplashScreen {
    private static final int PX_PER_METER = 400;

    private final OrthographicCamera camera = new OrthographicCamera();
    private final SpriteBatch batch = new SpriteBatch();
    private final TweenManager tweenManager = new TweenManager();

    private final UpdateAction<Void> callback;
    private final TweenCallback tweenCallback;
    protected boolean finishedAnimation = false;

    private final Sprite universal;
    private final Sprite tween;
    private final Sprite engine;
    private final Sprite logo;
    private final Sprite strip;
    private final Sprite powered;
    private final Sprite gdx;
    private final Sprite veil;
    private final TextureRegion gdxTex;

    private final InputProcessor inputProcessor = new InputAdapter() {
        @Override
        public boolean touchUp(int x, int y, int pointer, int button) {
            Tween.to(veil, SpriteAccessor.OPACITY, 0.7f).target(1).addCallback(tweenCallback).start(tweenManager);
            return true;
        }
    };
    private final InputProcessor placeAssetsInputProcessor = new InputAdapter() {
        private Sprite draggedSprite;
        private float lastX, lastY;

        @Override
        public boolean touchDown(int x, int y, int pointer, int button) {
            Vector3 v = new Vector3(x, y, 0);
            camera.unproject(v);

            draggedSprite = null;

            Sprite[] sprites = new Sprite[] { powered, gdx };
            for (Sprite sp : sprites) {
                if (sp.getX() <= v.x && v.x <= sp.getX() + sp.getWidth() && sp.getY() <= v.y
                        && v.y <= sp.getY() + sp.getHeight()) {
                    draggedSprite = sp;
                    break;
                }
            }

            lastX = x;
            lastY = y;
            return true;
        }

        @Override
        public boolean touchDragged(int x, int y, int pointer) {
            if (draggedSprite != null) {
                float dx = (x - lastX) * camera.viewportWidth / Gdx.graphics.getWidth();
                float dy = (lastY - y) * camera.viewportHeight / Gdx.graphics.getHeight();
                draggedSprite.translate(dx, dy);
            }

            lastX = x;
            lastY = y;
            return true;
        }

        @Override
        public boolean touchUp(int x, int y, int pointer, int button) {
            System.out.println("powered: " + powered.getX() + " " + powered.getY());
            System.out.println("gdx: " + gdx.getX() + " " + gdx.getY());
            System.out.println();
            return true;
        }
    };

    public SplashScreen(UpdateAction<Void> callback) {
        this.callback = callback;

        tweenCallback = new TweenCallback() {
            @Override
            public void onEvent(final int type, final BaseTween<?> source) {
                finishedAnimation = true;
            }
        };

        TextureAtlas atlas = Assets.inst().get(Test.location + "splash/pack", TextureAtlas.class);
        universal = atlas.createSprite("universal");
        tween = atlas.createSprite("tween");
        engine = atlas.createSprite("engine");
        logo = atlas.createSprite("logo");
        strip = atlas.createSprite("white");
        powered = atlas.createSprite("powered");
        gdx = atlas.createSprite("gdxblur");
        veil = atlas.createSprite("white");
        gdxTex = atlas.findRegion("gdx");

        float wpw = 1.0f;
        float wph = wpw * Gdx.graphics.getHeight() / Gdx.graphics.getWidth();

        camera.viewportWidth = wpw;
        camera.viewportHeight = wph;
        camera.update();

        Gdx.input.setInputProcessor(inputProcessor);

        Sprite[] sprites = new Sprite[] { universal, tween, engine, logo, powered, gdx };
        for (Sprite sp : sprites) {
            sp.setSize(sp.getWidth() / PX_PER_METER, sp.getHeight() / PX_PER_METER);
            sp.setOrigin(sp.getWidth() / 2, sp.getHeight() / 2);
        }

        universal.setPosition(-0.325F, 0.028F);
        tween.setPosition(-0.320F, -0.066F);
        engine.setPosition(0.020F, -0.087F);
        logo.setPosition(0.238F, 0.022F);

        strip.setSize(wpw, wph);
        strip.setOrigin(wpw / 2, wph / 2);
        strip.setPosition(-wpw / 2, -wph / 2);

        powered.setPosition(-0.278F, -0.025F);
        gdx.setPosition(0.068F, -0.077F);

        veil.setSize(wpw, wph);
        veil.setPosition(-wpw / 2, -wph / 2);
        veil.setColor(1, 1, 1, 0);

        Timeline.createSequence()
                // init
                .push(Tween.set(tween, SpriteAccessor.POS_XY).targetRelative(-1, 0))
                .push(Tween.set(engine, SpriteAccessor.POS_XY).targetRelative(1, 0))
                .push(Tween.set(universal, SpriteAccessor.POS_XY).targetRelative(0, 0.5F))
                .push(Tween.set(logo, SpriteAccessor.SCALE_XY).target(7, 7))
                .push(Tween.set(logo, SpriteAccessor.OPACITY).target(0))
                .push(Tween.set(strip, SpriteAccessor.SCALE_XY).target(1, 0))
                .push(Tween.set(powered, SpriteAccessor.OPACITY).target(0))
                .push(Tween.set(gdx, SpriteAccessor.OPACITY).target(0))

                .push(Tween.to(strip, SpriteAccessor.SCALE_XY, 0.8F).target(1, 0.6F).ease(TweenEquations.Back_Out))
                .push(Tween.to(tween, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0)
                        .ease(TweenEquations.Quart_Out))
                .push(Tween.to(engine, SpriteAccessor.POS_XY, 0.5F).targetRelative(-1, 0)
                        .ease(TweenEquations.Quart_Out))
                .push(Tween.to(universal, SpriteAccessor.POS_XY, 0.6F).targetRelative(0, -0.5F)
                        .ease(TweenEquations.Bounce_Out))

                .pushPause(0.5F)

                .beginParallel().push(Tween.set(logo, SpriteAccessor.OPACITY).target(1))
                .push(Tween.to(logo, SpriteAccessor.SCALE_XY, 0.8F).target(1, 1).ease(TweenEquations.Back_Out))
                .end()

                .push(Tween.to(strip, SpriteAccessor.SCALE_XY, 0.5F).target(1, 1).ease(TweenEquations.Back_In))

                .pushPause(0.3F)

                .beginParallel()
                .push(Tween.to(tween, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0)
                        .ease(TweenEquations.Back_In))
                .push(Tween.to(engine, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0)
                        .ease(TweenEquations.Back_In))
                .push(Tween.to(universal, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0)
                        .ease(TweenEquations.Back_In))
                .push(Tween.to(logo, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0).ease(TweenEquations.Back_In))
                .end()

                .pushPause(0.3F)

                .push(Tween.to(powered, SpriteAccessor.OPACITY, 0.3F).target(1)).beginParallel()
                .push(Tween.to(gdx, SpriteAccessor.OPACITY, 1.5F).target(1).ease(TweenEquations.Cubic_In))
                .push(Tween.to(gdx, SpriteAccessor.ROTATION, 2.0F).target(360 * 15).ease(TweenEquations.Quad_Out))
                .end().pushPause(0.3f)

                .push(Tween.to(gdx, SpriteAccessor.SCALE_XY, 0.6F).waypoint(1.6F, 0.4F).target(1.2F, 1.2F)
                        .ease(TweenEquations.Cubic_Out))
                .pushPause(0.3f).beginParallel()
                .push(Tween.to(powered, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0)
                        .ease(TweenEquations.Back_In))
                .push(Tween.to(gdx, SpriteAccessor.POS_XY, 0.5F).targetRelative(1, 0).ease(TweenEquations.Back_In))
                .end().pushPause(0.3F)

                .addCallback(tweenCallback).start(tweenManager);
    }

    public void dispose() {
        tweenManager.killAll();
        batch.dispose();
    }

    public void render() {
        tweenManager.update(Gdx.graphics.getDeltaTime());

        if (gdx.getRotation() > 360 * 15 - 20) {
            gdx.setRegion(gdxTex);
        }

        GL20 gl = Gdx.gl20;
        gl.glClearColor(0, 0, 0, 1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        gl.glEnable(GL20.GL_BLEND);
        gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        strip.draw(batch);
        universal.draw(batch);
        tween.draw(batch);
        engine.draw(batch);
        logo.draw(batch);
        powered.draw(batch);
        gdx.draw(batch);

        if (veil.getColor().a > 0.1f) {
            veil.draw(batch);
        }
        batch.end();

        if (finishedAnimation) {
            callback.onEvent(null);
        }
    }
}