Android Open Source - Airplanes Hotseat Players Screen






From Project

Back to project page Airplanes.

License

The source code is released under:

GNU General Public License

If you think the Android project Airplanes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.axnsan.airplanes.screens;
/* w w w  .  j  a  v a2  s.c o m*/
import java.util.ArrayList;

import com.axnsan.airplanes.Airplanes;
import com.axnsan.airplanes.GameConfiguration;
import com.axnsan.airplanes.util.FontManager;
import com.axnsan.airplanes.util.StringManager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class HotseatPlayersScreen implements Screen {
  private Stage stage;
  private Airplanes game;
  private Table table;
  private Button add;
  private TextField dummy;

  public HotseatPlayersScreen()
  {
    game = Airplanes.game;
  }
  
  @Override
  public void render(float delta) {
    Gdx.gl.glClearColor(1,1,1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    
    stage.act(delta);
    stage.draw();

  }

  @Override
  public void resize(int width, int height) {
    if (stage != null) {
      game.input.removeProcessor(stage);
      stage.dispose();
    }
    stage = new Stage();
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    
    table = new Table();
    table.setFillParent(true);
    stage.addActor(table);
    table.align(Align.top | Align.center);
    table.padTop(10);
    
    add = new TextButton("+", game.skin);
    add.addListener(new ClickListener() {
      @Override
      public void clicked(InputEvent event, float x, float y) 
      {
        addPlayer();
      }
    });
    dummy = new TextField("", game.skin);
    dummy.setVisible(false);
    
    game.config.numPlayers = Math.max(2, game.config.numPlayers);
    for (int i = 0;i < GameConfiguration.maxPlayers;++i)
    {
      TextButton but = new TextButton("-", Airplanes.game.skin);
      but.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) 
        {
          removePlayer();
        }
      });
      but.setVisible(false);
      TextField tf = new TextField(game.config.playerNames[i], game.skin);
      tf.setMaxLength(GameConfiguration.maxNameLength);
      tf.setBlinkTime(0.5f);
      tf.setTextFieldFilter(new TextFieldFilter() {

        @Override
        public boolean acceptChar(TextField textField, char key) {
          return Character.isDigit(key) || Character.isLetter(key) || key == ' ';
        }
      });

      players.add(new Row(tf, but));
    }

    rebuildTable();
    
    game.setTextButtonFont(FontManager.getFontForHeight(h/20));
    TextButton nextButton = new TextButton(StringManager.getString("start"), game.skin);
    nextButton.addListener(new ClickListener() {
      @Override
        public void clicked(InputEvent event, float x, float y)
        {
        game.back(); //Pop the settings screen off the stack
        game.setScreen(new HotseatScreen());
        }
    });
    stage.addActor(nextButton);

    TextButton backButton = new TextButton(StringManager.getString("back"), game.skin);
    backButton.addListener(new ClickListener() {
      @Override
        public void clicked(InputEvent event, float x, float y)
        {
        game.back();
        }
    });
    stage.addActor(backButton);
    
    backButton.setBounds(10, 10, w/2 - 15, h/10);
    nextButton.setBounds(Gdx.graphics.getWidth() + 5 - w/2, 10, w/2 - 15, h/10);
    
    game.input.addProcessor(stage);

  }
  
  private static class Row
  {
    public TextField tf;
    public Button but;
    
    public Row(TextField tf, Button but) { this.tf = tf; this.but = but; }
  }
  private ArrayList<Row> players = new ArrayList<Row>();
  
  private void addPlayer()
  {
    if (game.config.numPlayers >= GameConfiguration.maxPlayers)
      throw new RuntimeException("What sorcery is this?!");
    
    game.config.numPlayers += 1;
    rebuildTable();
  }
  private void removePlayer()
  {
    if (players.size() <= 2)
      throw new RuntimeException("What sorcery is this?");
    
    game.config.numPlayers -= 1;
    rebuildTable();
  }
  
  private void rebuildTable()
  {
    if (game.config.numPlayers > GameConfiguration.maxPlayers)
      throw new RuntimeException("What sorcery is this?!");
    
    table.clearChildren();
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    game.setTextButtonFont(FontManager.getFontForHeight(h/10));
    game.setTextFieldFont(FontManager.getFontForHeight(h/20));
    TextButtonStyle tbs = game.skin.get(TextButtonStyle.class);
    for (int i = 0;i < game.config.numPlayers;++i)
    {
      Row r = players.get(i);
      r.but.setVisible(false);
      r.but.setStyle(tbs);
      table.add(r.but).size(h/14).padRight(10);
      table.add(r.tf).pad(2).size(w/2, h/12);
      table.row();
    }
    table.add(add).size(h/14).padRight(10);
    add.setStyle(tbs);
    table.add(dummy).pad(2);
    add.setVisible(game.config.numPlayers < GameConfiguration.maxPlayers);
    if (game.config.numPlayers > 2)
      players.get(game.config.numPlayers-1).but.setVisible(true);
  }
  
  @Override
  public void show() {
    
  }

  @Override
  public void hide() {
    for (int i = 0;i < GameConfiguration.maxPlayers;++i)
    {
      String n = players.get(i).tf.getText().replace(",", "").replace("[", "").replace("]","");
      if (n.length() > 0)
        game.config.playerNames[i] = n;
    }
    game.config.dumpConfig();
    dispose();
  }

  @Override
  public void pause() {
    // TODO Auto-generated method stub

  }

  @Override
  public void resume() {
    // TODO Auto-generated method stub

  }

  @Override
  public void dispose() {
    if (stage != null)
    {
      game.input.removeProcessor(stage);
      stage.dispose();
      stage = null;
    }
  }

}




Java Source Code List

com.axnsan.airplanes.Airplanes.java
com.axnsan.airplanes.BaseGrid.java
com.axnsan.airplanes.GameConfiguration.java
com.axnsan.airplanes.GameState.java
com.axnsan.airplanes.GridRandomizer.java
com.axnsan.airplanes.Grid.java
com.axnsan.airplanes.GuardedScreen.java
com.axnsan.airplanes.HotseatMatchHandler.java
com.axnsan.airplanes.MainActivity.java
com.axnsan.airplanes.Main.java
com.axnsan.airplanes.MatchHandler.java
com.axnsan.airplanes.PlacementGrid.java
com.axnsan.airplanes.Plane.java
com.axnsan.airplanes.Player.java
com.axnsan.airplanes.PlayingGrid.java
com.axnsan.airplanes.online.ClientSocket.java
com.axnsan.airplanes.online.EventHandler.java
com.axnsan.airplanes.online.Game.java
com.axnsan.airplanes.online.LobbyScreen.java
com.axnsan.airplanes.online.LoginScreen.java
com.axnsan.airplanes.online.Message.java
com.axnsan.airplanes.online.Messages.java
com.axnsan.airplanes.online.OnlineGameScreen.java
com.axnsan.airplanes.online.OnlineMatchHandler.java
com.axnsan.airplanes.online.OnlineMenuScreen.java
com.axnsan.airplanes.online.OnlineSettingsScreen.java
com.axnsan.airplanes.online.SessionData.java
com.axnsan.airplanes.screens.BeginTurnScreen.java
com.axnsan.airplanes.screens.HotseatPlayersScreen.java
com.axnsan.airplanes.screens.HotseatScreen.java
com.axnsan.airplanes.screens.HotseatSettingsScreen.java
com.axnsan.airplanes.screens.HotseatStartScreen.java
com.axnsan.airplanes.screens.MainMenuScreen.java
com.axnsan.airplanes.screens.PlacementScreen.java
com.axnsan.airplanes.screens.PlayMenuScreen.java
com.axnsan.airplanes.screens.PracticeScreen.java
com.axnsan.airplanes.screens.PracticeSettingsScreen.java
com.axnsan.airplanes.screens.PracticeStartScreen.java
com.axnsan.airplanes.util.ActionManager.java
com.axnsan.airplanes.util.ActionResolver.java
com.axnsan.airplanes.util.BasicFontManager.java
com.axnsan.airplanes.util.Detector.java
com.axnsan.airplanes.util.DoubleTapDetector.java
com.axnsan.airplanes.util.DoubleTapListener.java
com.axnsan.airplanes.util.FontManagerInterface.java
com.axnsan.airplanes.util.FontManager.java
com.axnsan.airplanes.util.JavaXmlParser.java
com.axnsan.airplanes.util.Point2D.java
com.axnsan.airplanes.util.RandomizedQueue.java
com.axnsan.airplanes.util.StringManager.java
com.axnsan.airplanes.util.StringXmlParser.java
com.axnsan.airplanes.util.TTFFontManager.java
com.axnsan.airplanes.util.TapDetector.java
com.axnsan.airplanes.util.TapListener.java