I am trying to make a wordsearch app using libgdx. Created a AbstractScreen class which implements Screen I extend this AbstractScreen class to create different screens for the game.
AbstractScreen.java
public abstract class AbstractScreen implements Screen
{
// the fixed viewport dimensions (ratio: 1.6)
public static final int GAME_VIEWPORT_WIDTH = 1080, GAME_VIEWPORT_HEIGHT = 1920;
public static final int MENU_VIEWPORT_WIDTH = 1080, MENU_VIEWPORT_HEIGHT = 1920;
protected final WordSearch1 game;
protected final Stage stage;
private BitmapFont font;
private SpriteBatch batch;
private Skin skin;
private TextureAtlas atlas;
private Table table;
public AbstractScreen(WordSearch1 game )
{
this.game = game;
int width = ( isGameScreen() ? GAME_VIEWPORT_WIDTH : MENU_VIEWPORT_WIDTH );
int height = ( isGameScreen() ? GAME_VIEWPORT_HEIGHT : MENU_VIEWPORT_HEIGHT );
this.stage = new Stage();
}
//**** Removed some method for this post ****
protected Table getTable()
{
if( table == null ) {
table = new Table( getSkin() );
table.setFillParent( true );
if( WordSearch1.DEV_MODE ) {
table.debug();
}
stage.addActor( table );
}
return table;
}
// Screen implementation
@Override
public void show()
{
Gdx.app.log( WordSearch1.LOG, "Showing screen: " + getName() );
// set the stage as the input processor
Gdx.input.setInputProcessor( stage );
}
@Override
public void resize(
int width,
int height )
{
Gdx.app.log( WordSearch1.LOG, "Resizing screen: " + getName() + " to: " + width + " x " + height );
}
@Override
public void render(
float delta )
{
stage.act( delta );
// clear the screen with the given RGB color (black)
Gdx.gl.glClearColor( 1f, 1f, 1f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
// draw the actors
stage.draw();
// draw the table debug lines
Table.drawDebug( stage );
}
}
GameScreen.java
public class GameScreen extends AbstractScreen
{
public GameScreen( WordSearch1 game )
{
super( game );
}
@Override
public void show()
{
super.show();
TextButton startGameButton = new TextButton( "Start game", getSkin() );
String[][] data = gets a 2D array of letters for grid
// retrieve the default table actor
Table tableR = super.getTable();
Table table = new Table( super.getSkin() );
table.row();
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
TextButton letter = new TextButton( data[i][j], getSkin());
letter.getLabel().setTouchable(Touchable.disabled);
Gdx.app.log( WordSearch1.LOG, " -- Grid Letter: " + letter.getText() );
letter.addListener( new DefaultActorListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button )
{
//Gdx.app.log( WordSearch1.LOG, " -- Touch X: " + x + " Y: " + y );
//super.touchUp( event, x, y, pointer, button );
//TextButton b = (TextButton) event.getListenerActor();
//Gdx.app.log( WordSearch1.LOG, " -- Touch Name: " + b.getText() );
//game.getSoundManager().play( TyrianSound.CLICK );
//game.setScreen( new StartGameScreen( game ) );
}
} );
table.add( letter ).size( 30, 30 ).spaceBottom( 0 );
}
table.row();
}
table.row();
table.setColor(0, 0, 0, 0);
table.addAction(sequence( delay(1.0f), fadeIn( 1.50f )));
//table.debug();
tableR.row();
tableR.add(startGameButton);
tableR.row();
tableR.row();
tableR.add(table); //****Display nicely but input does not work.
tableR.addActor(table); //**** Displays only 1/4th of table
tableR.row();
table.addListener(new InputListener() {
ArrayList<Integer> hashList= new ArrayList<Integer>();
float scX , scY ; //Start CenterX, CenterY
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("----------------------------------------");
System.out.println("2- DOWN - Pressed - X : " + x + " Y: " + y);
System.out.println("2- DOWN - HIT : " + stage.hit(x, y, true) );
hashList.clear();
float sX = x; //Start X
float sY = y; //Start Y
if (stage.hit(sX, sY, true) != null) {
scX = stage.hit(sX, sY, true).getCenterX();
scY = stage.hit(sX, sY, true).getCenterY();
System.out.println("2- DOWN---- DRAG START Center - X : " + scX + " Y: " + scY);
System.out.println("2- DOWN---- DRAG START Center - HIT : " + stage.hit(scX, scY, true) );
}
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("2- UP - Released - X : " + x + " Y: " + y);
float upX = x;
float upY = y;
float tX = scX; //temp
float tY = scY; //temp
String test = "";
// *** implemented only for Left to Right drag ****
while (tX <= upX) {
//System.out.println("2- While : " + test);
if (stage.hit(tX, tY, true) != null) {
System.out.println("2- IF : " + test);
TextButton t = (TextButton) stage.hit(tX, tY, true);
test = test + t.getText();
}
tX = tX + 30;
}
System.out.println("2- UP - WORD : " + test);
}
// *** implemented only for Left to Right drag ****
public void touchDragged (InputEvent event, float x, float y, int pointer) {
//System.out.println("2- Dragged - X : " + x + " Y: " + y);
//System.out.println("------- Dragged ------");
if (stage.hit(x, y, true) != null) {
int hash = stage.hit(x, y, true).hashCode();
if (hashList.contains(hash) == false) {
hashList.add(hash);
float cX = stage.hit(x, y, true).getCenterX();
float cY = stage.hit(x, y, true).getCenterY();
TextButton t = (TextButton) stage.hit(cX, cY, true);
System.out.println("2- Dragged - hit : " + stage.hit(x, y, true));
System.out.println("2- Dragged - hit C : " + stage.hit(cX, cY, true));
System.out.println("2- Dragged - Text : " + t.getText());
}
else {
//System.out.println("2- Dragged - Hash MATCHED : ");
}
}
else {
System.out.println("2- Dragged - Cancelled : ");
event.cancel();
}
}
});
}
}
I have included most of the code above. Now the problem is If I use only one table - tableR and get rid of the table then the touchdragged works as expected. Gives me the proper behavior by selecteing letters between start and end points in left-to-right direction. No I wanted to use tableR as root table and do the rest of UI. So I created a different table - "table" (The above code is when I use both these tables)
Now the problem is
If I use tableR.add(table); table with letters is displayed correctly but the input functions does not behave as expected. It hits Label although touchable is disabled for labels of the textbutton.
If I use tableR.addActor(table) then the table with letters is not displayed properly. Only the 1/4th -top right part of table is visible and it is aligned to bottom left. Rest of the table is just not visible. Input seems to work perfectly for the 1/4th that is visible.
I can share the full code if that would be helpful.
Please help!! On a side note please tell me if the inputListener (table.addListener(new InputListener()) is the right way to do it. My goal is to display a grid of letters and have the touch behavior like in the wordsearch games on market.
0 comments:
Post a Comment