tweak picker; fix no-tiles-left case

Minor changes to strings and code for tile picker, and fix bug where
server.c was asking for 0 tiles (because none are left in pool.)
This commit is contained in:
Eric House 2017-03-15 07:12:40 -07:00
parent 364eec3c5c
commit 4c9c5b3418
4 changed files with 49 additions and 31 deletions

View file

@ -45,7 +45,7 @@ public class TilePickAlert extends XWDialogFragment
private TilePickState m_state; private TilePickState m_state;
private Action m_action; private Action m_action;
private AlertDialog m_dialog; private AlertDialog m_dialog;
private int[] m_selTiles; private int[] m_selTiles = new int[0];
public static class TilePickState implements Serializable { public static class TilePickState implements Serializable {
public int col; public int col;
@ -69,6 +69,8 @@ public class TilePickAlert extends XWDialogFragment
this.faces = faces; this.faces = faces;
this.counts = counts; this.counts = counts;
} }
public boolean forBlank() { return null == counts; }
} }
public static TilePickAlert newInstance( Action action, TilePickState state ) public static TilePickAlert newInstance( Action action, TilePickState state )
@ -106,11 +108,12 @@ public class TilePickAlert extends XWDialogFragment
m_view = (TilePickView)LocUtils.inflate( activity, R.layout.tile_picker ); m_view = (TilePickView)LocUtils.inflate( activity, R.layout.tile_picker );
m_view.init( this, m_state, sis ); m_view.init( this, m_state, sis );
int resId = m_state.forBlank()
? R.string.title_blank_picker : R.string.tile_tray_picker;
AlertDialog.Builder ab = LocUtils.makeAlertBuilder( activity ) AlertDialog.Builder ab = LocUtils.makeAlertBuilder( activity )
.setTitle( String.format( "Pick %d", m_state.nToPick ) ) .setTitle( resId )
.setView( m_view ); .setView( m_view );
if ( null != m_state.counts ) { if ( !m_state.forBlank() ) {
DialogInterface.OnClickListener lstnr = DialogInterface.OnClickListener lstnr =
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
@ -118,12 +121,26 @@ public class TilePickAlert extends XWDialogFragment
onDone(); onDone();
} }
}; };
ab.setPositiveButton( R.string.tilepick_all, lstnr ); ab.setPositiveButton( buttonTxt(), lstnr );
} }
m_dialog = ab.create(); m_dialog = ab.create();
return m_dialog; return m_dialog;
} }
// TilePickView.TilePickListener interface
@Override
public void onTilesChanged( int nToPick, int[] newTiles )
{
m_selTiles = newTiles;
boolean haveAll = nToPick == newTiles.length;
if ( haveAll && m_state.forBlank() ) {
onDone();
} else if ( null != m_dialog ) {
m_dialog.getButton( AlertDialog.BUTTON_POSITIVE )
.setText( buttonTxt() );
}
}
private void onDone() private void onDone()
{ {
Activity activity = getActivity(); Activity activity = getActivity();
@ -136,18 +153,13 @@ public class TilePickAlert extends XWDialogFragment
dismiss(); dismiss();
} }
// TilePickView.TilePickListener private String buttonTxt()
@Override
public void onTilesChanged( int nToPick, int[] newTiles )
{ {
m_selTiles = newTiles; Context context = getContext();
boolean done = nToPick == newTiles.length; int left = m_state.nToPick - m_selTiles.length;
if ( done && null == m_state.counts ) { String txt = 0 == left
onDone(); ? LocUtils.getString( context, android.R.string.ok )
} else if ( null != m_dialog ) { : LocUtils.getString( context, R.string.tilepick_all_fmt, left );
int msgID = done ? android.R.string.ok : R.string.tilepick_all; return txt;
Button button = m_dialog.getButton( AlertDialog.BUTTON_POSITIVE );
button.setText( LocUtils.getString( getContext(), msgID ) );
}
} }
} }

View file

@ -154,7 +154,7 @@ public class TilePickView extends LinearLayout {
private void showPending() private void showPending()
{ {
TextView desc = (TextView)findViewById( R.id.pending_desc ); TextView desc = (TextView)findViewById( R.id.pending_desc );
if ( null == m_state.counts ) { if ( m_state.forBlank() ) {
desc.setVisibility( View.GONE ); desc.setVisibility( View.GONE );
} else { } else {
List<String> faces = new ArrayList<String>(); List<String> faces = new ArrayList<String>();
@ -184,7 +184,7 @@ public class TilePickView extends LinearLayout {
Button button = m_buttons.get( index ); Button button = m_buttons.get( index );
Context context = getContext(); Context context = getContext();
String face = m_state.faces[index]; String face = m_state.faces[index];
if ( null != m_state.counts ) { if ( !m_state.forBlank() ) {
int count = m_state.counts[index] - pendingCount( index ); int count = m_state.counts[index] - pendingCount( index );
face = LocUtils.getString( context, R.string.tile_button_txt_fmt, face = LocUtils.getString( context, R.string.tile_button_txt_fmt,
face, count ); face, count );
@ -204,7 +204,7 @@ public class TilePickView extends LinearLayout {
private void updateDelButton() private void updateDelButton()
{ {
int vis = null == m_state.counts || m_pendingTiles.size() == 0 int vis = m_state.forBlank() || m_pendingTiles.size() == 0
? View.INVISIBLE : View.VISIBLE; ? View.INVISIBLE : View.VISIBLE;
findViewById( R.id.del ).setVisibility( vis ); findViewById( R.id.del ).setVisibility( vis );
} }

View file

@ -1535,7 +1535,8 @@
<!-- title of dialog allowing user to pick tiles "face up". (This <!-- title of dialog allowing user to pick tiles "face up". (This
feature is not yet supported on Android.) --> feature is not yet supported on Android.) -->
<string name="title_tile_picker">Letter for blank</string> <string name="title_blank_picker">Tile for blank</string>
<string name="tile_tray_picker">New tiles for tray</string>
<string name="tile_button_txt_fmt">%1$s (%2$d)</string> <string name="tile_button_txt_fmt">%1$s (%2$d)</string>
<string name="tile_pick_summary_fmt">Current pick: %1$s</string> <string name="tile_pick_summary_fmt">Current pick: %1$s</string>
@ -1819,7 +1820,7 @@
<!-- --> <!-- -->
<string name="tilepick_undo">Undo last\u200C</string> <string name="tilepick_undo">Undo last\u200C</string>
<!-- --> <!-- -->
<string name="tilepick_all">Pick for me</string> <string name="tilepick_all_fmt">Pick %1$d for me</string>
<!-- --> <!-- -->
<string name="cur_tiles_fmt">Tile picker\n(so far: %1$s)</string> <string name="cur_tiles_fmt">Tile picker\n(so far: %1$s)</string>
<!-- --> <!-- -->

View file

@ -1068,7 +1068,7 @@ server_tilesPicked( ServerCtxt* server, XP_U16 player,
util_requestTime( server->vol.util ); util_requestTime( server->vol.util );
} }
static void static XP_Bool
informNeedPickTiles( ServerCtxt* server, XP_Bool initial, XP_U16 turn, informNeedPickTiles( ServerCtxt* server, XP_Bool initial, XP_U16 turn,
XP_U16 nToPick ) XP_U16 nToPick )
{ {
@ -1082,13 +1082,17 @@ informNeedPickTiles( ServerCtxt* server, XP_Bool initial, XP_U16 turn,
if ( nLeft < nToPick ) { if ( nLeft < nToPick ) {
nToPick = nLeft; nToPick = nLeft;
} }
XP_Bool asking = nToPick > 0;
for ( Tile tile = 0; tile < nFaces; ++tile ) { if ( asking ) {
faces[tile] = dict_getTileString( dict, tile ); for ( Tile tile = 0; tile < nFaces; ++tile ) {
counts[tile] = pool_getNTilesLeftFor( server->pool, tile ); faces[tile] = dict_getTileString( dict, tile );
counts[tile] = pool_getNTilesLeftFor( server->pool, tile );
}
util_informNeedPickTiles( server->vol.util, initial, turn,
nToPick, nFaces, faces, counts );
} }
util_informNeedPickTiles( server->vol.util, initial, turn, return asking;
nToPick, nFaces, faces, counts );
} }
XP_Bool XP_Bool
@ -1812,7 +1816,7 @@ server_askPickTiles( ServerCtxt* server, XP_U16 turn, TrayTileSet* newTiles,
{ {
XP_Bool asked = newTiles == NULL && server->vol.gi->allowPickTiles; XP_Bool asked = newTiles == NULL && server->vol.gi->allowPickTiles;
if ( asked ) { if ( asked ) {
informNeedPickTiles( server, XP_FALSE, turn, nToPick ); asked = informNeedPickTiles( server, XP_FALSE, turn, nToPick );
} }
return asked; return asked;
} }
@ -1950,8 +1954,9 @@ assignTilesToAll( ServerCtxt* server )
&& gi->allowPickTiles; && gi->allowPickTiles;
for ( ii = 0; ii < nPlayers; ++ii ) { for ( ii = 0; ii < nPlayers; ++ii ) {
if ( 0 == model_getNumTilesInTray( model, ii ) ) { if ( 0 == model_getNumTilesInTray( model, ii ) ) {
if ( pickingTiles && !LP_IS_ROBOT(&gi->players[ii]) ) { if ( pickingTiles && !LP_IS_ROBOT(&gi->players[ii])
informNeedPickTiles( server, XP_TRUE, ii, MAX_TRAY_TILES ); && informNeedPickTiles( server, XP_TRUE, ii,
MAX_TRAY_TILES ) ) {
allDone = XP_FALSE; allDone = XP_FALSE;
break; break;
} }