feat!: controls selector in SettingsScreen

This commit is contained in:
Laureηt 2021-05-27 10:51:03 +02:00
parent add04b141b
commit 3c351ac996
6 changed files with 158 additions and 46 deletions

View file

@ -1,8 +1,10 @@
package sagittarius;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.kotcrab.vis.ui.VisUI;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music;
import sagittarius.view.*;
public class SagittariusGame extends Game {
@ -10,11 +12,17 @@ public class SagittariusGame extends Game {
// Constants
public static float G = 100;
public static boolean debugMode = true;
public static Music music;
public static boolean disableMusic= false;
public static float musicVolume= 0.5f;
public static Music music;
public static boolean disableMusic = false;
public static float musicVolume = 0.5f;
public static boolean disableSounds = false;
public static float soundsVolume = 0.5f;
public static int moveLeftKey = Keys.LEFT;
public static int moveRightKey = Keys.RIGHT;
public static int zoomInKey = Keys.UP;
public static int zoomOutKey = Keys.DOWN;
public static int shootArrowButton = Buttons.LEFT;
private static Game game;
public SagittariusGame() {
@ -31,7 +39,7 @@ public class SagittariusGame extends Game {
@Override
public void create() {
// We load the VisUI library
VisUI.load();

View file

@ -1,7 +1,6 @@
package sagittarius.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
@ -37,10 +36,10 @@ public class Bow extends Actor {
@Override
public void act(float dt) {
super.act(dt);
if (GameScreen.playerCurrent.isActive() && Gdx.input.isButtonJustPressed(Buttons.RIGHT) && !pressed) {
if (GameScreen.playerCurrent.isActive() && Gdx.input.isButtonJustPressed(SagittariusGame.shootArrowButton) && !pressed) {
this.anchor = GameScreen.worldCursor.cpy();
pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.RIGHT) && pressed) {
} else if (Gdx.input.isButtonPressed(SagittariusGame.shootArrowButton) && pressed) {
aim = this.anchor.cpy().sub(GameScreen.worldCursor);
angle = aim.angleDeg();
power = MathUtils.clamp(aim.len(), 0, 1000);

View file

@ -3,13 +3,13 @@ package sagittarius.model;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import sagittarius.SagittariusGame;
public class Player extends EntityQuad {
@ -87,12 +87,10 @@ public class Player extends EntityQuad {
super.act(dt);
if (active) {
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
if (Gdx.input.isKeyPressed(SagittariusGame.moveLeftKey)) {
this.angle += 100.0f / home.getRadius();
}
if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
if (Gdx.input.isKeyPressed(SagittariusGame.moveRightKey)) {
this.angle -= 100.0f / home.getRadius();
}
}

View file

@ -0,0 +1,52 @@
package sagittarius.view;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.kotcrab.vis.ui.widget.VisTextButton;
public class ControllerListener extends InputListener {
private boolean capturing = false;
private VisTextButton button;
private VisTextButton[] other_buttons;
private Stage uiStage;
public ControllerListener(VisTextButton button, VisTextButton[] other_buttons, Stage uiStage) {
super();
this.button = button;
this.other_buttons = other_buttons;
this.uiStage = uiStage;
}
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (!this.button.isDisabled() && !capturing && button == Buttons.LEFT) {
capturing = true;
this.button.setText("capturing...");
for (VisTextButton other : other_buttons) {
if (other != this.button) {
other.setDisabled(true);
}
}
this.uiStage.setKeyboardFocus(this.button);
}
return super.touchDown(event, x, y, pointer, button);
}
@Override
public boolean keyDown(InputEvent event, int keycode) {
if (!this.button.isDisabled() && capturing) {
capturing = false;
this.button.setText(Keys.toString(keycode));
for (VisTextButton other : other_buttons) {
if (other != this.button) {
other.setDisabled(false);
}
}
this.uiStage.setKeyboardFocus(null);
}
return true;
}
}

View file

@ -133,24 +133,23 @@ public class GameScreen extends BaseScreen implements InputProcessor {
}
// camera zoom using keys
if (Gdx.input.isKeyPressed( Keys.DOWN)) {
if (Gdx.input.isKeyPressed(SagittariusGame.zoomInKey)) {
gameCam.zoom += dt;
}
if (Gdx.input.isKeyPressed( Keys.UP)) {
if (Gdx.input.isKeyPressed(SagittariusGame.zoomOutKey)) {
gameCam.zoom -= dt;
}
// Pause Menu
if (Gdx.input.isKeyPressed( Keys.ESCAPE)) {
SagittariusGame.setActiveScreen( new ResumeScreen(this) );
}
// clamp zoom
gameCam.zoom = MathUtils.clamp(gameCam.zoom, 1f, 3f);
// Pause Menu
if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
SagittariusGame.setActiveScreen( new ResumeScreen(this) );
}
// camera follow focus
mainCameraPosition.scl(ispeed);
mainCameraPosition.add(new Vector3(focus.getPosition(), 0).scl(speed));
}
/**

View file

@ -1,11 +1,16 @@
package sagittarius.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Align;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.widget.VisLabel;
import com.kotcrab.vis.ui.widget.VisTable;
import com.kotcrab.vis.ui.widget.VisTextButton;
import com.kotcrab.vis.ui.widget.spinner.SimpleFloatSpinnerModel;
import sagittarius.view.ButtonSounded.*;
@ -37,7 +42,6 @@ public class SettingsScreen extends BaseScreen {
// dubug mode checkbox
CheckBoxSounded debugModeBox = new CheckBoxSounded("debug mode");
debugModeBox.setChecked(SagittariusGame.debugMode);
// disable music checkbox
CheckBoxSounded disableMusic = new CheckBoxSounded("disable music");
@ -53,6 +57,51 @@ public class SettingsScreen extends BaseScreen {
// Change volume of the sound
SpinnerSounded musicSounds= new SpinnerSounded("Sounds Volume:", new SimpleFloatSpinnerModel(SagittariusGame.soundsVolume, 0f, 1.0f, 0.01f, 3));
// go back button
ButtonTextSounded returnButton = new ButtonTextSounded("Go Back");
returnButton.addListener(new ActorGestureListener() {
@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
super.tap(event, x, y, count, button);
SagittariusGame.setActiveScreen( new StartScreen() );
}
});
// Control buttons
VisTextButton shootArrow = new VisTextButton("mouse" + SagittariusGame.shootArrowButton);;
VisTextButton moveLeft = new VisTextButton(Keys.toString(SagittariusGame.moveLeftKey));
VisTextButton moveRight = new VisTextButton(Keys.toString(SagittariusGame.moveRightKey));
VisTextButton zoomIn = new VisTextButton(Keys.toString(SagittariusGame.zoomInKey));
VisTextButton zoomOut = new VisTextButton(Keys.toString(SagittariusGame.zoomOutKey));
VisTextButton[] other_buttons = {shootArrow, moveLeft, moveRight, zoomIn, zoomOut};
shootArrow.addListener( new ClickListener() {
private boolean capturing = false;
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (!shootArrow.isDisabled() && !capturing && button == Buttons.LEFT) {
capturing = true;
shootArrow.setText("capturing...");
moveLeft.setDisabled(true);
moveRight.setDisabled(true);
zoomIn.setDisabled(true);
zoomOut.setDisabled(true);
} else if (!shootArrow.isDisabled() && capturing) {
capturing = false;
shootArrow.setText("mouse" + button);
moveLeft.setDisabled(false);
moveRight.setDisabled(false);
zoomIn.setDisabled(false);
zoomOut.setDisabled(false);
}
return super.touchDown(event, x, y, pointer, button);
};
});
moveLeft.addListener(new ControllerListener(moveLeft, other_buttons, uiStage));
moveRight.addListener(new ControllerListener(moveRight, other_buttons, uiStage));
zoomIn.addListener(new ControllerListener(zoomIn, other_buttons, uiStage));
zoomOut.addListener(new ControllerListener(zoomOut, other_buttons, uiStage));
// save button
ButtonTextSounded saveButton = new ButtonTextSounded("Save");
saveButton.addListener(new ActorGestureListener() {
@ -66,13 +115,17 @@ public class SettingsScreen extends BaseScreen {
SagittariusGame.soundsVolume = Float.parseFloat(musicSounds.getTextField().getText());
SagittariusGame.debugMode = debugModeBox.isChecked();
SagittariusGame.moveLeftKey = Keys.valueOf(moveLeft.getText().toString());
SagittariusGame.moveRightKey = Keys.valueOf(moveRight.getText().toString());
SagittariusGame.zoomInKey = Keys.valueOf(zoomIn.getText().toString());
SagittariusGame.zoomOutKey = Keys.valueOf(zoomOut.getText().toString());
SagittariusGame.shootArrowButton = Keys.valueOf(shootArrow.getText().toString().substring(5));
// Deactivate music or not
if (disableMusic.isChecked() && SagittariusGame.music.isPlaying()){
SagittariusGame.music.stop();
SagittariusGame.disableMusic = true;
}
else if (!disableMusic.isChecked() && ! SagittariusGame.music.isPlaying()){
} else if (!disableMusic.isChecked() && ! SagittariusGame.music.isPlaying()){
SagittariusGame.music = Gdx.audio.newMusic(Gdx.files.internal("sounds/music/resumeMenu_music.mp3"));
SagittariusGame.music.setVolume(SagittariusGame.musicVolume);
SagittariusGame.music.play();
@ -82,40 +135,43 @@ public class SettingsScreen extends BaseScreen {
// Deactivate music or not
if (disableSounds.isChecked() ){
SagittariusGame.disableSounds = true;
}
else {
} else {
SagittariusGame.disableSounds= false;
}
}
});
// go back button
ButtonTextSounded returnButton = new ButtonTextSounded("Go Back");
returnButton.addListener(new ActorGestureListener() {
@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
super.tap(event, x, y, count, button);
SagittariusGame.setActiveScreen( new StartScreen() );
}
});
// Table structure
table.add(gConstField).width(250);
table.add(gConstField).width(250).colspan(2);
table.row();
table.add(disableMusic).align(Align.left);
table.add(disableMusic).align(Align.left).colspan(2);
table.row();
table.add(musicVolume).width(250);
table.add(musicVolume).width(250).colspan(2);
table.row();
table.add(disableSounds).align(Align.left);
table.add(disableSounds).align(Align.left).colspan(2);
table.row();
table.add(musicSounds).width(250);
table.add(musicSounds).width(250).colspan(2);
table.row();
table.add(debugModeBox).align(Align.left);
table.add(debugModeBox).align(Align.left).colspan(2);
table.row();
table.add(saveButton).width(250);
table.add(new VisLabel("shoot")).align(Align.left);
table.add(shootArrow).fill();
table.row();
table.add(returnButton).width(250);
table.add(new VisLabel("move left")).align(Align.left);
table.add(moveLeft).fill();
table.row();
table.add(new VisLabel("move right")).align(Align.left);
table.add(moveRight).fill();
table.row();
table.add(new VisLabel("zoom in")).align(Align.left);
table.add(zoomIn).fill();
table.row();
table.add(new VisLabel("zoom out")).align(Align.left);
table.add(zoomOut).fill();
table.row();
table.add(saveButton).width(250).colspan(2);
table.row();
table.add(returnButton).width(250).colspan(2);
}
@Override