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

#11 Difficulty button snaps to position

parent a0904c46
Branches
Tags
No related merge requests found
......@@ -5,12 +5,13 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
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;
......@@ -25,6 +26,8 @@ import cz.nic.tablexia.util.ui.ViewPager;
public class GameMenuPage extends MenuPage implements ViewPager.ScrollListener {
private GameDefinition game;
private float diffEasyX, diffMediumX, diffHardX;
private float scrollOffset = TablexiaSettings.getDefaultScreenWidth(); // hack to keep paralax layers out of picture before scrolled for first time
public GameMenuPage(AbstractTablexiaScreen screen, GameDefinition game) {
......@@ -118,33 +121,65 @@ public class GameMenuPage extends MenuPage implements ViewPager.ScrollListener {
addActor(diffBarImage);
// Difficulty button
Texture diff = getScreen().getTexture(GameMenuAssets.DIFF_THUMB_MEDIUM);
final 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) (diffWidth * 0.1f);
float diffY = 0;
float diffHeight = screen.getStage().getHeight() * 0.15f;
float diffWidth = diffHeight * ((float) diff.getWidth() / (float) diff.getHeight());
diffEasyX = (screen.getStage().getWidth() / 2 - diffWidth / 2 + (int) (diffWidth * 0.1f)) - diffBarWidth / 2;
diffMediumX = screen.getStage().getWidth() / 2 - diffWidth / 2 + (int) (diffWidth * 0.1f);
diffHardX = (screen.getStage().getWidth() / 2 - diffWidth / 2 + (int) (diffWidth * 0.1f)) + diffBarWidth / 2;
float diffX = diffEasyX;
diffButton.setSize(diffWidth, diffHeight);
diffButton.setPosition(diffX, diffY);
addActor(diffButton);
diffButton.addListener(new DragListener() {
diffButton.addListener(new InputListener() {
float lastX;
@Override
public void dragStart(InputEvent event, float x, float y, int pointer) {
super.dragStart(event, x, y, pointer);
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
super.touchDown(event, x, y, pointer, button);
lastX = x;
event.handle();
event.stop();
return true;
}
@Override
public void drag(InputEvent event, float x, float y, int pointer) {
super.drag(event, x, y, pointer);
// example code below for origin and position
//diffButton.setPosition(lastX - x, diffButton.getY());
lastX = x;
event.handle();
public void touchDragged(InputEvent event, float x, float y, int pointer) {
super.touchDragged(event, x, y, pointer);
float bx = diffButton.getX() + (x - lastX);
if (bx >= diffEasyX && bx <= diffHardX) {
diffButton.setPosition(bx, diffButton.getY());
}
event.stop();
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
float bx = diffButton.getX() + (x - lastX);
MoveToAction ma = new MoveToAction();
if (bx < diffMediumX) {
if ((diffEasyX + ((diffMediumX - diffEasyX) / 2)) > bx) {
ma.setPosition(diffEasyX, diffButton.getY());
} else {
ma.setPosition(diffMediumX, diffButton.getY());
}
} else {
if ((diffMediumX + ((diffHardX - diffMediumX) / 2)) > bx) {
ma.setPosition(diffMediumX, diffButton.getY());
} else {
ma.setPosition(diffHardX, diffButton.getY());
}
}
diffButton.addAction(ma);
event.stop();
}
});
}
......
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