-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding transitions effects for screens
- Loading branch information
Showing
11 changed files
with
532 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
client/src/game/screens/transitions/AlphaFadingTransition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Source code recreated from a .class file by IntelliJ IDEA | ||
// (powered by Fernflower decompiler) | ||
// | ||
|
||
package game.screens.transitions; | ||
|
||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.g2d.Batch; | ||
import com.badlogic.gdx.math.Interpolation; | ||
|
||
public class AlphaFadingTransition implements ScreenTransition { | ||
public AlphaFadingTransition() { | ||
} | ||
|
||
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float alpha) { | ||
alpha = Interpolation.fade.apply(alpha); | ||
batch.begin(); | ||
batch.setColor(1.0F, 1.0F, 1.0F, 1.0F); | ||
batch.draw(currentScreenTexture, 0.0F, 0.0F, 0.0F, 0.0F, (float) currentScreenTexture.getWidth(), (float) currentScreenTexture.getHeight(), 1.0F, 1.0F, 0.0F, 0, 0, currentScreenTexture.getWidth(), currentScreenTexture.getHeight(), false, true); | ||
batch.setColor(1.0F, 1.0F, 1.0F, alpha); | ||
batch.draw(nextScreenTexture, 0.0F, 0.0F, 0.0F, 0.0F, (float) nextScreenTexture.getWidth(), (float) nextScreenTexture.getHeight(), 1.0F, 1.0F, 0.0F, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true); | ||
batch.end(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
client/src/game/screens/transitions/ColorFadeTransition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package game.screens.transitions; | ||
|
||
import com.badlogic.gdx.graphics.Color; | ||
import com.badlogic.gdx.graphics.Pixmap; | ||
import com.badlogic.gdx.graphics.Pixmap.Format; | ||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.g2d.Batch; | ||
import com.badlogic.gdx.math.Interpolation; | ||
|
||
/** | ||
* A color fading transition | ||
* | ||
* @author iXeption | ||
*/ | ||
public class ColorFadeTransition implements ScreenTransition { | ||
|
||
private final Color color; | ||
private final Interpolation interpolation; | ||
private final Texture texture; | ||
|
||
/** | ||
* @param color the {@link Color} to fade to | ||
* @param interpolation the {@link Interpolation} method | ||
*/ | ||
public ColorFadeTransition(Color color, Interpolation interpolation) { | ||
this.color = new Color(Color.WHITE); | ||
this.interpolation = interpolation; | ||
|
||
texture = new Texture(1, 1, Format.RGBA8888); | ||
Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888); | ||
pixmap.setColor(color); | ||
pixmap.fillRectangle(0, 0, 1, 1); | ||
texture.draw(pixmap, 0, 0); | ||
} | ||
|
||
@Override | ||
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float percent) { | ||
float width = currentScreenTexture.getWidth(); | ||
float height = currentScreenTexture.getHeight(); | ||
float x = 0; | ||
float y = 0; | ||
|
||
if (interpolation != null) percent = interpolation.apply(percent); | ||
|
||
batch.begin(); | ||
|
||
float fade = percent * 2; | ||
|
||
if (fade > 1.0f) { | ||
fade = 1.0f - (percent * 2 - 1.0f); | ||
color.a = 1.0f - fade; | ||
batch.setColor(color); | ||
|
||
batch.draw(nextScreenTexture, 0, 0, width / 2, height / 2, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), | ||
1, 1, 0, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true); | ||
|
||
} else { | ||
|
||
color.a = 1.0f - fade; | ||
batch.setColor(color); | ||
|
||
batch.draw(currentScreenTexture, 0, 0, width / 2, height / 2, width, height, 1, 1, 0, 0, 0, (int) width, (int) height, | ||
false, true); | ||
|
||
} | ||
|
||
color.a = fade; | ||
|
||
batch.setColor(color); | ||
batch.draw(texture, 0, 0, width, height); | ||
batch.end(); | ||
batch.setColor(Color.WHITE); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
// | ||
// Source code recreated from a .class file by IntelliJ IDEA | ||
// (powered by Fernflower decompiler) | ||
// | ||
|
||
package game.screens.transitions; | ||
|
||
import com.badlogic.gdx.Game; | ||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.Screen; | ||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.Pixmap.Format; | ||
import com.badlogic.gdx.graphics.g2d.Batch; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.badlogic.gdx.graphics.glutils.FrameBuffer; | ||
import com.badlogic.gdx.utils.Array; | ||
import java.util.Iterator; | ||
|
||
public class FadingGame extends Game { | ||
protected Batch batch; | ||
private final Array<TransitionListener> listeners; | ||
protected FrameBuffer currentScreenFBO; | ||
protected FrameBuffer nextScreenFBO; | ||
protected Screen nextScreen; | ||
private float transitionDuration; | ||
private float currentTransitionTime; | ||
private boolean transitionRunning; | ||
private ScreenTransition screenTransition; | ||
|
||
public FadingGame() { | ||
this.listeners = new Array(); | ||
} | ||
|
||
public void create() { | ||
this.batch = new SpriteBatch(); | ||
this.currentScreenFBO = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false); | ||
this.nextScreenFBO = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false); | ||
} | ||
|
||
public void dispose() { | ||
if (this.screen != null) { | ||
this.screen.hide(); | ||
} | ||
|
||
if (this.nextScreen != null) { | ||
this.nextScreen.hide(); | ||
} | ||
|
||
this.currentScreenFBO.dispose(); | ||
this.nextScreenFBO.dispose(); | ||
} | ||
|
||
public void pause() { | ||
if (this.screen != null) { | ||
this.screen.pause(); | ||
} | ||
|
||
if (this.nextScreen != null) { | ||
this.nextScreen.pause(); | ||
} | ||
|
||
} | ||
|
||
public void resume() { | ||
if (this.screen != null) { | ||
this.screen.resume(); | ||
} | ||
|
||
if (this.nextScreen != null) { | ||
this.nextScreen.resume(); | ||
} | ||
|
||
} | ||
|
||
public void render() { | ||
float delta = Gdx.graphics.getDeltaTime(); | ||
if (this.nextScreen == null) { | ||
this.screen.render(delta); | ||
} else if (this.transitionRunning && this.currentTransitionTime >= this.transitionDuration) { | ||
this.screen.dispose(); | ||
this.screen = this.nextScreen; | ||
this.screen.resume(); | ||
this.transitionRunning = false; | ||
this.nextScreen = null; | ||
this.notifyFinished(); | ||
this.screen.render(delta); | ||
} else if (this.screenTransition != null) { | ||
this.currentScreenFBO.begin(); | ||
this.screen.render(delta); | ||
this.currentScreenFBO.end(); | ||
this.nextScreenFBO.begin(); | ||
this.nextScreen.render(delta); | ||
this.nextScreenFBO.end(); | ||
float percent = this.currentTransitionTime / this.transitionDuration; | ||
this.screenTransition.render(this.batch, this.currentScreenFBO.getColorBufferTexture(), this.nextScreenFBO.getColorBufferTexture(), percent); | ||
this.currentTransitionTime += delta; | ||
} | ||
|
||
} | ||
|
||
public void resize(int width, int height) { | ||
if (this.screen != null) { | ||
this.screen.resize(width, height); | ||
} | ||
|
||
if (this.nextScreen != null) { | ||
this.nextScreen.resize(width, height); | ||
} | ||
|
||
this.currentScreenFBO.dispose(); | ||
this.nextScreenFBO.dispose(); | ||
this.currentScreenFBO = new FrameBuffer(Format.RGBA8888, width, height, false); | ||
this.nextScreenFBO = new FrameBuffer(Format.RGBA8888, width, height, false); | ||
} | ||
|
||
public boolean setTransition(ScreenTransition screenTransition, float duration) { | ||
if (this.transitionRunning) { | ||
return false; | ||
} else { | ||
this.screenTransition = screenTransition; | ||
this.transitionDuration = duration; | ||
return true; | ||
} | ||
} | ||
|
||
public Screen getScreen() { | ||
return this.screen; | ||
} | ||
|
||
public void setScreen(Screen screen) { | ||
screen.show(); | ||
if (this.transitionRunning) { | ||
Gdx.app.log(FadingGame.class.getSimpleName(), "Changed Screen while transition in progress"); | ||
} | ||
|
||
if (this.screen == null) { | ||
this.screen = screen; | ||
} else if (this.screenTransition == null) { | ||
this.screen.hide(); | ||
this.screen = screen; | ||
} else { | ||
this.nextScreen = screen; | ||
this.screen.pause(); | ||
this.nextScreen.pause(); | ||
this.currentTransitionTime = 0.0F; | ||
this.transitionRunning = true; | ||
this.notifyStarted(); | ||
} | ||
|
||
this.screen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | ||
} | ||
|
||
public Screen getNextScreen() { | ||
return this.nextScreen; | ||
} | ||
|
||
public void addTransitionListener(TransitionListener listener) { | ||
this.listeners.add(listener); | ||
} | ||
|
||
public boolean removeTransitionListener(TransitionListener listener) { | ||
return this.listeners.removeValue(listener, true); | ||
} | ||
|
||
public void clearTransitionListeners() { | ||
this.listeners.clear(); | ||
} | ||
|
||
private void notifyFinished() { | ||
Iterator var1 = this.listeners.iterator(); | ||
|
||
while(var1.hasNext()) { | ||
TransitionListener transitionListener = (TransitionListener)var1.next(); | ||
transitionListener.onTransitionFinished(); | ||
} | ||
|
||
} | ||
|
||
private void notifyStarted() { | ||
Iterator var1 = this.listeners.iterator(); | ||
|
||
while(var1.hasNext()) { | ||
TransitionListener transitionListener = (TransitionListener)var1.next(); | ||
transitionListener.onTransitionStart(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.