Commit 2a013baa authored by Matyáš Latner's avatar Matyáš Latner

#7 Preloader for In the Darkness game. Tablexia atlas manager can handle animations

parent 6b82852e
......@@ -118,6 +118,7 @@ task prepareSoundAssets(dependsOn: prepareAssets) {
eachFile { FileCopyDetails fcd ->
fcd.setPath(fcd.getRelativePath().toString().replace(File.separator, '.'))
}
outputs.upToDateWhen { false }
}.execute()
}
}
......
......@@ -14,6 +14,7 @@ import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
......@@ -49,8 +50,7 @@ public abstract class TablexiaApplication implements ApplicationListener {
private static final Color BACKGROUND_COLOR = new Color(0f, 0f, 0f, 1f);
private static final int DIMMER_TRANSACTION_SPEED = 1;
private static final float LOADER_DIALOG_SIZE_RATIO = 2f / 3;
private static final float PRELOADER_SIZE_RATIO = 2f / 3;
private TablexiaAtlasManager preloaderAtlasManager;
private ScreenTransaction currentScreenTransaction;
......@@ -234,6 +234,11 @@ public abstract class TablexiaApplication implements ApplicationListener {
private abstract class PreloaderDialog extends TablexiaDialog {
private static final float PRELOADER_TITLE_PADDING = 1f / 40;
private static final float PRELOADER_CONTENT_PADDING = 1f / 40;
private final ApplicationFontManager.FontType PRELOADER_TITLE_FONT = ApplicationFontManager.FontType.ROBOTO_BOLD_26;
private final Color PRELOADER_TITLE_COLOR = Color.BLACK;
private TablexiaButton textButton;
public PreloaderDialog(float x, float y, float width, float height, BackGroundType backGroundType) {
......@@ -243,11 +248,14 @@ public abstract class TablexiaApplication implements ApplicationListener {
@Override
protected void prepareContent() {
super.prepareContent();
Label.LabelStyle titleLableStyle = new Label.LabelStyle(ApplicationFontManager.getInstance().getFont(ApplicationFontManager.FontType.ROBOTO_BOLD_20), Color.BLACK);
Label.LabelStyle titleLableStyle = new Label.LabelStyle(ApplicationFontManager.getInstance().getFont(PRELOADER_TITLE_FONT), PRELOADER_TITLE_COLOR);
Label titleLabel = new Label(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.PRELOADER_TITLE), titleLableStyle);
getContentTable().add(titleLabel);
getContentTable().add(titleLabel).pad(getHeight() * PRELOADER_TITLE_PADDING);
getContentTable().row();
screen.preparePreloaderContent(getContentTable(), preloaderAtlasManager, ApplicationTextManager.getInstance(), ApplicationFontManager.getInstance());
Table preloaderContent = new Table();
preloaderContent.setSize(getWidth(), getHeight());
screen.preparePreloaderContent(preloaderContent, preloaderAtlasManager, ApplicationTextManager.getInstance(), ApplicationFontManager.getInstance());
getContentTable().add(preloaderContent).expandY().fillY().pad(getWidth() * PRELOADER_CONTENT_PADDING);
getContentTable().row();
textButton = new TablexiaButton(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.CONFIRM_BUTTON), TablexiaButton.ButtonType.GREEN);
textButton.addListener(new ClickListener() {
......@@ -296,11 +304,11 @@ public abstract class TablexiaApplication implements ApplicationListener {
private void preloaderComplete() {
preloaderStart = false;
float width = getStage().getHeight() * LOADER_DIALOG_SIZE_RATIO;
float width = getStage().getHeight() * PRELOADER_SIZE_RATIO;
float height = width;
loadingDialog = new PreloaderDialog(TablexiaSettings.getViewportWidth(getStage()) / 2 - width / 2,
TablexiaSettings.getViewportHeight(getStage()) / 2 - height / 2,
width, height, TablexiaDialog.BackGroundType.DIALOG_SQUARE) {
width, height, TablexiaDialog.BackGroundType.DIALOG_SQUARE_BORDERLINES) {
@Override
protected void buttonAction() {
......
......@@ -39,6 +39,7 @@ import cz.nic.tablexia.loader.application.ApplicationTextManager;
import cz.nic.tablexia.model.game.Game;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.ScaleUtil;
import cz.nic.tablexia.util.ui.AnimatedImage;
import cz.nic.tablexia.util.ui.TablexiaButton;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.alpha;
......@@ -136,8 +137,12 @@ public class InTheDarknessGame extends AbstractTablexiaGame<List<MapWidget>> imp
private static final Color PRELOADER_TEXT_COLOR = Color.BLACK;
private static final String PRELOADER_INFO_IMAGE = "preloader_info";
private static final String PRELOADER_ANIM_IMAGE = "preloader_anim";
private static final int PRELOADER_ANIM_FRAMES = 15;
private static final float PRELOADER_ANIM_FRAME_DURATION = 0.5f;
private static final String PRELOADER_TEXT1_KEY = ApplicationTextManager.ApplicationTextsAssets.GAME_IN_THE_DARKNESS_PRELOADER_TEXT1;
private static final String PRELOADER_TEXT2_KEY = ApplicationTextManager.ApplicationTextsAssets.GAME_IN_THE_DARKNESS_PRELOADER_TEXT2;
private static final float PRELOADER_TEXT_PADDING_RATIO = 1f / 40;
private static final float PRELOADER_IMAGE_WIDTH_RATIO = 1f / 3;
private static final String SCORE_KEY_PLANNING_DURATION = "planning_duration";
public static final String SCORE_KEY_ERRORS_COUNT = "errors_count";
......@@ -377,18 +382,34 @@ public class InTheDarknessGame extends AbstractTablexiaGame<List<MapWidget>> imp
@Override
public void preparePreloaderContent(Table contentTable, TablexiaAtlasManager preloaderAtlasManager, ApplicationTextManager applicationTextManager, ApplicationFontManager applicationFontManager) {
contentTable.setDebug(TablexiaSettings.getInstance().isShowBoundingBoxes(), true);
float textPadding = contentTable.getWidth() * PRELOADER_TEXT_PADDING_RATIO;
float imageWidth = contentTable.getWidth() * PRELOADER_IMAGE_WIDTH_RATIO;
float textWidth = contentTable.getWidth() - imageWidth;
// HELP IMAGE
Table preloaderContent1 = new Table();
Image preloaderInfoImage = new Image(getPreloaderTextureRegion(preloaderAtlasManager, PRELOADER_INFO_IMAGE));
contentTable.add(preloaderInfoImage);
ScaleUtil.setImageDrawableMinSizeForWidth(preloaderInfoImage, imageWidth);
preloaderContent1.add(preloaderInfoImage);
// HELP TEXT 1
Label.LabelStyle textLabelStyle = new Label.LabelStyle(applicationFontManager.getFont(PRELOADER_TEXT_FONT), PRELOADER_TEXT_COLOR);
Label preloaderText1 = new Label(applicationTextManager.getText(PRELOADER_TEXT1_KEY), textLabelStyle);
preloaderText1.setWrap(true);
contentTable.add(preloaderText1);
preloaderContent1.add(preloaderText1).width(textWidth).pad(textPadding);
contentTable.add(preloaderContent1).expandX().fillX();
contentTable.row();
// HELP TEXT 2
Table preloaderContent2 = new Table();
Label preloaderText2 = new Label(applicationTextManager.getText(PRELOADER_TEXT2_KEY), textLabelStyle);
preloaderText2.setWrap(true);
contentTable.add(preloaderText2);
contentTable.add(new Image(getPreloaderTextureRegion(preloaderAtlasManager, PRELOADER_ANIM_IMAGE)));
preloaderContent2.add(preloaderText2).width(textWidth).pad(textPadding);
// HELP ANIMATION
AnimatedImage preloaderAnimImage = new AnimatedImage(getPreloaderAnimation(preloaderAtlasManager, PRELOADER_ANIM_IMAGE, PRELOADER_ANIM_FRAMES, PRELOADER_ANIM_FRAME_DURATION, false), false);
preloaderAnimImage.startAnimationLoop();
ScaleUtil.setImageDrawableMinSizeForWidth(preloaderAnimImage, imageWidth);
preloaderContent2.add(preloaderAnimImage);
contentTable.add(preloaderContent2).expandX().fillX();
}
......
......@@ -23,19 +23,22 @@ import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Array;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
import cz.nic.tablexia.TablexiaSettings;
import cz.nic.tablexia.game.games.in_the_darkness.InTheDarknessGame;
import cz.nic.tablexia.game.games.in_the_darkness.assets.InTheDarknessAssets;
import cz.nic.tablexia.game.games.in_the_darkness.map.TileMap;
import cz.nic.tablexia.game.games.in_the_darkness.map.TileMap.TileMapPosition;
import cz.nic.tablexia.game.games.in_the_darkness.map.tile.TileType;
import cz.nic.tablexia.util.ui.AnimatedImage;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.moveTo;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.parallel;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.rotateTo;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.run;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.scaleTo;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.sequence;
/**
* Player sprite
......@@ -77,59 +80,6 @@ public class Player extends Group {
}
}
public class AnimatedImage extends Image {
protected Animation animation;
private float stateTime = 0;
private boolean started;
private boolean loop;
private Runnable finishHandler;
public AnimatedImage(Animation animation, boolean started) {
super(animation.getKeyFrame(0));
this.animation = animation;
this.started = started;
this.loop = false;
}
public void startAnimation(Runnable finishHandler) {
this.finishHandler = finishHandler;
stateTime = 0;
started = true;
loop = false;
}
public void startAnimationLoop() {
stateTime = 0;
started = true;
loop = true;
}
public void stopAnimation() {
started = false;
loop = false;
}
@Override
public void act(float delta) {
if (started) {
stateTime = stateTime + delta;
if (loop) {
((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime += delta, true));
} else {
((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime, false));
if (finishHandler != null && animation.isAnimationFinished(stateTime)) {
finishHandler.run();
}
}
} else {
((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(0));
}
super.act(delta);
}
}
private static final int PLAYER_WIDTH = 55;
private static final int PLAYER_HEIGHT = 55;
......
......@@ -3,6 +3,7 @@ package cz.nic.tablexia.loader;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
......@@ -84,6 +85,14 @@ public class TablexiaAtlasManager extends TablexiaAbstractFileManager {
}
}
public Animation getAnimationFromAtlas(String atlasName, String regionName, int framesCount, float frameDuration, boolean useBleedingFix) {
TextureRegion[] textureRegions = new TextureRegion[framesCount];
for (int i = 0; i < framesCount; i++) {
textureRegions[i] = getTextureRegionFromAtlas(atlasName, regionName, i, useBleedingFix);
}
return new Animation(frameDuration, textureRegions);
}
public NinePatch getPatchFromAtlas(String atlasName, String patchName) {
TextureAtlas atlas = getAsset(atlasName, TextureAtlas.class);
if (atlas == null) {
......
......@@ -7,6 +7,7 @@ import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
......@@ -635,6 +636,10 @@ public abstract class AbstractTablexiaScreen<T> extends ScreenAdapter {
return preloaderAtlasManager.getTextureRegionFromAtlas(preparePreloaderAtlasPath(), regionName, null);
}
public Animation getPreloaderAnimation(TablexiaAtlasManager preloaderAtlasManager, String regionName, int framesCount, float frameDuration, boolean useBleedingFix) {
return preloaderAtlasManager.getAnimationFromAtlas(preparePreloaderAtlasPath(), regionName, framesCount, frameDuration, useBleedingFix);
}
public TextureRegion getTextureRegionForAtlas(String atlasName, String regionName) {
return atlasManager.getTextureRegionFromAtlas(atlasName, regionName, null);
}
......
package cz.nic.tablexia.util;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
......@@ -154,6 +153,16 @@ public class ScaleUtil {
image.setSize(getWidth(image.getWidth(), image.getHeight(), height), height);
}
public static void setImageDrawableMinSizeForHeight(Image image, float height) {
image.getDrawable().setMinHeight(height);
image.getDrawable().setMinWidth(getWidth(image.getWidth(), image.getHeight(), height));
}
public static void setImageDrawableMinSizeForWidth(Image image, float width) {
image.getDrawable().setMinWidth(width);
image.getDrawable().setMinHeight(getHeight(image.getWidth(), image.getHeight(), width));
}
public static Actor setBackgroundBounds(Actor actor) {
actor.setPosition(0, -TablexiaSettings.getMinWorldHeight() / 2);
actor.setSize(TablexiaSettings.getWorldSize(), TablexiaSettings.getMinWorldHeight() * 2);
......
package cz.nic.tablexia.util.ui;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
/**
* Created by Matyáš Latner.
*/
public class AnimatedImage extends Image {
protected Animation animation;
private float stateTime = 0;
private boolean started;
private boolean loop;
private Runnable finishHandler;
public AnimatedImage(Animation animation, boolean started) {
super(animation.getKeyFrame(0));
this.animation = animation;
this.started = started;
this.loop = false;
}
public void startAnimation(Runnable finishHandler) {
this.finishHandler = finishHandler;
stateTime = 0;
started = true;
loop = false;
}
public void startAnimationLoop() {
stateTime = 0;
started = true;
loop = true;
}
public void stopAnimation() {
started = false;
loop = false;
}
@Override
public void act(float delta) {
if (started) {
stateTime = stateTime + delta;
if (loop) {
((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime += delta, true));
} else {
((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime, false));
if (finishHandler != null && animation.isAnimationFinished(stateTime)) {
finishHandler.run();
}
}
} else {
((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(0));
}
super.act(delta);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment