Skip to content
Christian Freischlag edited this page Jul 17, 2020 · 5 revisions

Hi just a short code example. this class is from the first pull request for libGDX. I decided to put it on a seperate library. You can just replace GdxTest with ApplicationListener and rename package etc. This should give you enough information how the lib is used.

    package de.ixeption.ixengine.transitions;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Logger;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import de.ixeption.ixengine.transitions.impl.*;

import java.util.ArrayList;


public class TransitionExample implements ApplicationListener {
    final String tag = TransitionExample.class.getSimpleName();

    FadingGame fadingGame;
    Screen firstScreen;
    Screen secondScreen;

    boolean toggle = false;
    int index = 0;

    ArrayList<ScreenTransition> transitions = new ArrayList<ScreenTransition>();

    public class SimpleScreen extends ScreenAdapter {

        Stage simpleStage;
        String name;
        boolean paused = false;

        public SimpleScreen(String name, String bgImage, final float offset) {
            this.name = name;
            simpleStage = new Stage(new StretchViewport(640, 480));
            final TextureRegion region = new TextureRegion(new Texture(bgImage));
            final Actor actor = new Actor() {

                public void draw(Batch batch, float parentAlpha) {
                    Color color = getColor();
                    batch.setColor(color.r, color.g, color.b, parentAlpha);
                    batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(),
                            getRotation());
                }

                @Override
                public void act(float delta) {
                    if (!paused) {
                        setPosition(getX() + 50 * delta, offset);
                        if (getX() > Gdx.graphics.getWidth()) setX(0);
                    }
                }
            };

            actor.setBounds(15, offset, 200, 200);
            actor.setOrigin(50, 50);

            final TextureRegion regionBG = new TextureRegion(new Texture("test/egg.png"));
            final Actor bg = new Actor() {

                public void draw(Batch batch, float parentAlpha) {
                    Color color = getColor();
                    batch.setColor(color.r, color.g, color.b, parentAlpha);
                    batch.draw(regionBG, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(),
                            getScaleY(), getRotation());
                }
            };

            bg.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

            simpleStage.addActor(bg);
            simpleStage.addActor(actor);

            simpleStage.addListener(new ClickListener(Input.Buttons.LEFT) {
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.debug(tag, "Toggle Screen");
                    fadingGame.setScreen(getNextScreen());

                }
            });

            simpleStage.addListener(new ClickListener(Input.Buttons.RIGHT) {
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.debug(tag, "Change Transition");
                    if (fadingGame.setTransition(getNextTransition(), 2))
                        Gdx.app.debug(tag, "Sucess");
                    else
                        Gdx.app.error(tag, "failed");

                }
            });

            simpleStage.addListener(new ClickListener(Input.Buttons.MIDDLE) {
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Gdx.app.debug(tag, "Clear Listeners");
                    fadingGame.clearTransitionListeners();

                }
            });

        }

        @Override
        public void render(float delta) {
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            simpleStage.act(delta);
            simpleStage.draw();
        }

        @Override
        public void resize(int width, int height) {
            Gdx.app.debug(tag, "Resize W:" + width + " H: " + height + " " + name);
            simpleStage.getViewport().update(width, height, true);
        }

        @Override
        public void show() {
            Gdx.app.debug(tag, "Show " + name);
            Gdx.input.setInputProcessor(simpleStage);
        }

        @Override
        public void hide() {
            Gdx.app.debug(tag, "Hide " + name);
        }

        @Override
        public void resume() {
            Gdx.app.debug(tag, "Resume " + name);
            paused = false;

        }

        @Override
        public void pause() {
            Gdx.app.debug(tag, "Pause " + name);
            paused = true;
        }

    }

    @Override
    public void create() {
        Gdx.app.setLogLevel(Logger.DEBUG);
        fadingGame = new FadingGame(new SpriteBatch());
        fadingGame.create();
        fadingGame.setTransition(new ColorFadeTransition(Color.BLACK, Interpolation.exp10), 3.0f);
        firstScreen = new SimpleScreen("Screen1", "test/particle-fire.png", 10);
        secondScreen = new SimpleScreen("Screen2", "test/badlogic-with-whitespace.png", 200);
        transitions.add(new AlphaFadingTransition());
        transitions.add(new SlidingTransition(SlidingTransition.Direction.LEFT, Interpolation.linear, true));
        transitions.add(new SlidingTransition(SlidingTransition.Direction.UP, Interpolation.bounce, false));
        transitions.add(new SlicingTransition(SlicingTransition.Direction.UPDOWN, 128, Interpolation.pow4));
        transitions.add(new SlicingTransition(SlicingTransition.Direction.DOWN, 8, Interpolation.bounce));
        transitions.add(new RotatingTransition(Interpolation.pow2Out, 720, RotatingTransition.TransitionScaling.IN));
        transitions.add(new RotatingTransition(Interpolation.bounce, 360, RotatingTransition.TransitionScaling.IN));
        transitions.add(new ColorFadeTransition(Color.WHITE, Interpolation.sine));

        fadingGame.addTransitionListener(new TransitionListener() {

            @Override
            public void onTransitionStart() {
                Gdx.app.debug(tag, "TransitionListener: Start detected");

            }

            @Override
            public void onTransitionFinished() {
                Gdx.app.debug(tag, "TransitionListener: Finish detected");

            }
        });

        fadingGame.setScreen(firstScreen);

    }

    @Override
    public void render() {
        fadingGame.render();
    }

    @Override
    public void resize(int width, int height) {
        fadingGame.resize(width, height);
    }

    @Override
    public void dispose() {
        fadingGame.dispose();
    }

    @Override
    public void pause() {
        fadingGame.pause();
    }

    @Override
    public void resume() {
        fadingGame.resume();
    }

    Screen getNextScreen() {
        toggle = !toggle;
        if (toggle) {
            return secondScreen;
        } else
            return firstScreen;

    }

    ScreenTransition getNextTransition() {
        if (++index >= transitions.size()) index = 0;
        return transitions.get(index);

    }

}
Clone this wiki locally