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

View file

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

View file

@ -1535,7 +1535,8 @@
<!-- title of dialog allowing user to pick tiles "face up". (This
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_pick_summary_fmt">Current pick: %1$s</string>
@ -1819,7 +1820,7 @@
<!-- -->
<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>
<!-- -->

View file

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