Skip to content
Snippets Groups Projects
Commit d1a75ff2 authored by Luboš Horáček's avatar Luboš Horáček
Browse files

#11 Paralax foreground stretch and screen items as actors

parent 05d30971
No related branches found
No related tags found
No related merge requests found
package cz.nic.tablexia.screen.gamemenu.pages;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.DragListener;
import cz.nic.tablexia.TablexiaSettings;
import cz.nic.tablexia.game.GameDefinition;
import cz.nic.tablexia.screen.AbstractTablexiaScreen;
import cz.nic.tablexia.screen.gamemenu.GameMenuAssets;
......@@ -19,12 +23,24 @@ import cz.nic.tablexia.util.ui.ViewPager;
public class GameMenuPage extends MenuPage implements ViewPager.ScrollListener {
private GameDefinition game;
private float scrollOffset = 0;
private float scrollOffset = TablexiaSettings.getDefaultScreenWidth(); // hack to keep paralax layers out of picture before scrolled for first time
public GameMenuPage(AbstractTablexiaScreen screen, GameDefinition game) {
super(screen);
this.game = game;
// Title
Texture title = getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.TITLE));
Image titleImage = new Image(title);
int titleX = (int) (screen.getStage().getWidth() * 0.2);
int titleWidth = (int) (screen.getStage().getWidth() * 0.6);
int titleHeight = (int) (titleWidth * ((float) title.getHeight() / (float) title.getWidth()));
int titleY = (int) (screen.getStage().getHeight() - (screen.getStage().getHeight() * 0.05) - titleHeight);
titleImage.setPosition(titleX, titleY);
titleImage.setSize(titleWidth, titleHeight);
addActor(titleImage);
// Start button
Texture start = getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.STARTBUTTON));
Image startButton = new Image(start);
......@@ -43,13 +59,54 @@ public class GameMenuPage extends MenuPage implements ViewPager.ScrollListener {
}
});
// Start button
Texture startDown = getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.STARTBUTTON_PRESSED));
Image startDownButton = new Image(start);
int startDownY = (int) (screen.getStage().getWidth() * 0.1);
int startDownHeight = (int) (screen.getStage().getHeight() * 0.25);
int startDownWidth = (int) (startDownHeight * ((float) startDown.getWidth() / (float) startDown.getHeight()));
int startDownX = (int) screen.getStage().getWidth() / 2 - startDownWidth / 2;
startDownButton.setSize(startDownWidth, startDownHeight);
startDownButton.setPosition(startDownX, startDownY);
startDownButton.setVisible(false);
addActor(startDownButton);
// Labels for diffuculty slider
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
Label easy = new Label("Easy", skin);
Label medium = new Label("Medium", skin);
Label hard = new Label("Hard", skin);
easy.setPosition((screen.getStage().getWidth() / 2) - (screen.getStage().getWidth() * 0.1f) - easy.getWidth() / 2, screen.getStage().getHeight() * 0.0f);
medium.setPosition((screen.getStage().getWidth() / 2) - medium.getWidth() / 2, screen.getStage().getHeight() * 0.0f);
hard.setPosition((screen.getStage().getWidth() / 2) + (screen.getStage().getWidth() * 0.1f) - hard.getWidth() / 2, screen.getStage().getHeight() * 0.0f);
addActor(easy);
addActor(medium);
addActor(hard);
// Difficulty bar
Texture diffBar = getScreen().getTexture(GameMenuAssets.DIFF_BAR);
Image diffBarImage = new Image(diffBar);
int diffBarHeight = (int) (screen.getStage().getHeight() * 0.05);
int diffBarWidth = (int) (diffBarHeight * ((float) diffBar.getWidth() / (float) diffBar.getHeight()));
int diffBarX = (int) screen.getStage().getWidth() / 2 - diffBarWidth / 2;
int diffBarY = (int) (screen.getStage().getHeight() * 0.1 - diffBarHeight);
diffBarImage.setPosition(diffBarX, diffBarY);
diffBarImage.setSize(diffBarWidth, diffBarHeight);
addActor(diffBarImage);
// Difficulty button
Texture diff = getScreen().getTexture(GameMenuAssets.DIFF_THUMB_MEDIUM);
final Image diffButton = new Image(diff);
int diffY = 0;
int diffHeight = (int) (screen.getStage().getHeight() * 0.15);
int diffWidth = (int) (diffHeight * ((float) diff.getWidth() / (float) diff.getHeight()));
int diffX = (int) screen.getStage().getWidth() / 2 - diffWidth / 2;
int diffX = (int) screen.getStage().getWidth() / 2 - diffWidth / 2 + (int) (diffWidth * 0.1f);
diffButton.setSize(diffWidth, diffHeight);
diffButton.setPosition(diffX, diffY);
addActor(diffButton);
......@@ -78,32 +135,23 @@ public class GameMenuPage extends MenuPage implements ViewPager.ScrollListener {
@Override
public void draw(Batch batch, float parentAlpha) {
// Paralax layers
// Back layer travels half the speed of scroll
Texture back = getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.BACK));
float scrollHalf = (Math.abs(scrollOffset / 2) / getWidth()) * back.getWidth();
int srcX = (int) scrollHalf;
int srcWidth = (int) (back.getWidth() - (scrollHalf * 2));
int srcWidth = (int) (back.getWidth() - (scrollHalf));//(back.getWidth() - (scrollHalf * 2));
float x = getX() + Math.abs(Math.min(scrollOffset, 0));
float width = getWidth() - Math.abs(scrollOffset);
float width = getWidth() - Math.abs(scrollOffset / 2);
batch.draw(back, x, getY(), width, getHeight(), srcX, 0, srcWidth, back.getHeight(), false, false);
// Mid layer travels the same speed
batch.draw(getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.MID)), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.FORE)), getX() + (scrollOffset / 2), getY(), getWidth(), getHeight());
// Title
Texture title = getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.TITLE));
int titleX = (int) (getWidth() * 0.2);
int titleY = (int) (getHeight() - (getStage().getHeight() * 0.05));
int titleWidth = (int) (getWidth() * 0.6);
int titleHeight = (int) (titleWidth * ((float) title.getHeight() / (float) title.getWidth()));
batch.draw(title, getX() + titleX, getY() + titleY - titleHeight, titleWidth, titleHeight);
// Foreground layer travels faster and could be wider than screen. Needs to be clipped
// Difficulty bar
Texture diffBar = getScreen().getTexture(GameMenuAssets.DIFF_BAR);
int diffY = (int) (getHeight() * 0.1);
int diffHeight = (int) (getHeight() * 0.05);
int diffWidth = (int) (diffHeight * ((float) diffBar.getWidth() / (float) diffBar.getHeight()));
int diffX = (int) getWidth() / 2 - diffWidth / 2;
batch.draw(diffBar, getX() + diffX, getY() + diffY - diffHeight, diffWidth, diffHeight);
Texture fore = getScreen().getTexture(GameMenuAssets.getResourcePath(game, GameMenuAssets.GameMenuLayers.FORE));
float foreWidth = (getHeight() / fore.getHeight()) * fore.getWidth();
batch.draw(fore, getX() - ((foreWidth - getWidth()) / 2) + (scrollOffset / 2), getY(), foreWidth, getHeight());
super.draw(batch, parentAlpha);
}
......
......@@ -73,7 +73,7 @@ public class OfficeMenuPage extends MenuPage {
batch.disableBlending();
batch.draw(getScreen().getTexture(GameMenuAssets.OFFICE), getX(), getY(), getWidth(), getHeight());
batch.enableBlending();
batch.draw(getScreen().getTexture(GameMenuAssets.DESK), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.OFFICE_HELP), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.PROFILE_PRESSED), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.DOOR_PRESSED), getX(), getY(), getWidth(), getHeight());
......@@ -81,6 +81,8 @@ public class OfficeMenuPage extends MenuPage {
batch.draw(getScreen().getTexture(GameMenuAssets.STATISTICS_PRESSED), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.HALLOFFAME_PRESSED), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.DESK), getX(), getY(), getWidth(), getHeight());
batch.draw(getScreen().getTexture(GameMenuAssets.OFFICE_HELP), getX(), getY(), getWidth(), getHeight());
}
}
......@@ -72,7 +72,6 @@ public class ViewPager extends ScrollPane {
@Override
public void act(float delta) {
super.act(delta);
if (wasPanDragFling && !isPanning() && !isDragging() && !isFlinging()) {
wasPanDragFling = false;
scrollToPage();
......@@ -114,6 +113,7 @@ public class ViewPager extends ScrollPane {
}
}
lastX = getWidget().getX();
super.act(delta);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment