Legger til validering av spillernavn

This commit is contained in:
Kristian Knarvik 2020-04-07 15:12:45 +02:00
parent 485d04eb5f
commit 9fa8f04ef7

View File

@ -13,41 +13,55 @@ import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport; import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.game_wrapper.RoboRallyWrapper; import inf112.fiasko.roborally.game_wrapper.RoboRallyWrapper;
/**
* This screen allows a user to choose their player name
*/
public class UsernameScreen extends AbstractScreen { public class UsernameScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper; private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera; private final OrthographicCamera camera;
private final Viewport viewport; private final Viewport viewport;
private final Stage stage; private final Stage stage;
private final int applicationWidth = 600; private TextField textInput;
private final int applicationHeight = 800;
private TextField txtinput;
private TextButton loginbutton;
public UsernameScreen(final RoboRallyWrapper roboRallyWrapper) {
/**
* Instantiates a new username screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public UsernameScreen(final RoboRallyWrapper roboRallyWrapper) {
stage = new Stage(); stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("uiskin.json")); Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
loginbutton = new TextButton("click",skin); TextButton confirm = new TextButton("Confirm", skin);
loginbutton.setSize(300,60); confirm.setSize(300,60);
loginbutton.setPosition(300,300); confirm.setPosition(300,300);
loginbutton.addListener(new ClickListener(){ confirm.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
return true;
}
@Override @Override
public void touchUp(InputEvent e, float x, float y, int point, int button) { public void touchUp(InputEvent e, float x, float y, int point, int button) {
roboRallyWrapper.client.sendElement(txtinput.getText()); String userName = textInput.getText();
System.out.println(userName);
System.out.println(validateName(userName));
if (!validateName(userName)) {
return;
}
roboRallyWrapper.client.sendElement(userName);
if (roboRallyWrapper.server == null) { if (roboRallyWrapper.server == null) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper)); roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
} } else{
else{
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLobbyScreen(roboRallyWrapper)); roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLobbyScreen(roboRallyWrapper));
} }
} }
}); });
txtinput = new TextField("",skin); textInput = new TextField("",skin);
txtinput.setPosition(300,250); textInput.setPosition(300,250);
txtinput.setSize(150,40); textInput.setSize(150,40);
stage.addActor(txtinput); stage.addActor(textInput);
stage.addActor(loginbutton); stage.addActor(confirm);
camera = new OrthographicCamera(); camera = new OrthographicCamera();
viewport = new FitViewport(applicationWidth, applicationHeight, camera); viewport = new FitViewport(applicationWidth, applicationHeight, camera);
@ -57,6 +71,15 @@ public class UsernameScreen extends AbstractScreen {
} }
/**
* Validates the username set by the user
* @param userName The username the user wants
* @return True if the username can be used
*/
private boolean validateName(String userName) {
//TODO: Find a way to ask the server if the name is taken
return !userName.equals("");
}
@Override @Override
public void render(float delta) { public void render(float delta) {
@ -67,7 +90,8 @@ public class UsernameScreen extends AbstractScreen {
roboRallyWrapper.batch.begin(); roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Click the button to enter username", roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Click the button to enter username",
applicationWidth/2f-380/2f,applicationHeight/2f + 100,380, 1, true); applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 100,380, 1,
true);
roboRallyWrapper.batch.end(); roboRallyWrapper.batch.end();
stage.draw(); stage.draw();