DroidFish: Show material diff in edit board mode. From Leo Mayer.

This commit is contained in:
Peter Osterlund 2012-09-28 20:10:00 +00:00
parent 935fe8e4fe
commit fa0d1a20d9
4 changed files with 152 additions and 29 deletions

View File

@ -3,21 +3,65 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="fill_parent"
android:layout_gravity="center_vertical">
<view
class="org.petero.droidfish.activities.ChessBoardEdit"
android:id="@+id/eb_chessboard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
android:layout_height="fill_parent"
class="org.petero.droidfish.activities.ChessBoardEdit"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="2dp" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first_title_line"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/title">
</include>
android:layout_gravity="center_vertical"
android:background="@android:drawable/title_bar"
android:orientation="horizontal"
android:tag="title" >
<TextView
android:id="@+id/white_clock"
style="?android:attr/windowTitleStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="left"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="false"
android:singleLine="true"
android:tag="title" />
<TextView
android:id="@+id/title_text"
style="?android:attr/windowTitleStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:tag="title"
android:text="@string/app_name" />
<TextView
android:id="@+id/black_clock"
style="?android:attr/windowTitleStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="right"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:tag="title" />
</LinearLayout>
<TextView
android:text=""
android:id="@+id/eb_status"
@ -45,12 +89,69 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="@string/cancel"
android:id="@+id/eb_cancel"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
android:layout_weight="1"
android:text="@string/cancel" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/second_title_line"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/title_bar"
android:gravity="bottom"
android:orientation="vertical"
android:tag="title" >
<TextView
android:id="@+id/white_pieces"
style="?android:attr/windowTitleStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="left|center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:tag="title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/title_text_summary"
style="?android:attr/windowTitleStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_vertical"
android:tag="title"
android:visibility="visible" />
<TextView
android:id="@+id/black_pieces"
style="?android:attr/windowTitleStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="left|center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:tag="title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@ -8,6 +8,7 @@
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first_title_line"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"

View File

@ -58,7 +58,7 @@ public final class Util {
public static MaterialDiff getMaterialDiff(Position pos) {
StringBuilder whiteString = new StringBuilder();
StringBuilder blackString = new StringBuilder();
for (int p = Piece.WPAWN; p >= Piece.WQUEEN; p--) {
for (int p = Piece.WPAWN; p >= Piece.WKING; p--) {
int diff = pos.nPieces(p) - pos.nPieces(Piece.swapColor(p));
while (diff < 0) {
whiteString.append(Piece.toUniCode(Piece.swapColor(p)));

View File

@ -24,6 +24,7 @@ import org.petero.droidfish.ChessBoard;
import org.petero.droidfish.R;
import org.petero.droidfish.ChessBoard.SquareDecoration;
import org.petero.droidfish.Util;
import org.petero.droidfish.Util.MaterialDiff;
import org.petero.droidfish.gamelogic.ChessParseError;
import org.petero.droidfish.gamelogic.Move;
import org.petero.droidfish.gamelogic.Pair;
@ -39,6 +40,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
@ -63,15 +65,20 @@ public class EditBoard extends Activity {
private Button okButton;
private Button cancelButton;
private TextView whiteTitleText, blackTitleText, engineTitleText;
private View secondTitleLine;
boolean egtbHints;
private TextView whiteFigText;
private TextView blackFigText;
private TextView summaryTitleText;
private Typeface figNotation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
figNotation = Typeface.createFromAsset(getAssets(), "fonts/DroidFishChessNotationDark.otf");
initUI();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
@ -84,6 +91,7 @@ public class EditBoard extends Activity {
try {
pos = TextIO.readFEN(i.getAction());
cb.setPosition(pos);
checkValidAndUpdateMaterialDiff();
} catch (ChessParseError e) {
}
}
@ -101,6 +109,7 @@ public class EditBoard extends Activity {
setSelection(oldCB.selectedSquare);
cb.userSelectedSquare = oldCB.userSelectedSquare;
status.setText(statusStr);
checkValidAndUpdateMaterialDiff();
}
private final void initUI() {
@ -116,8 +125,16 @@ public class EditBoard extends Activity {
blackTitleText.setVisibility(View.GONE);
engineTitleText = (TextView)findViewById(R.id.title_text);
engineTitleText.setVisibility(View.GONE);
secondTitleLine = findViewById(R.id.second_title_line);
secondTitleLine.setVisibility(View.GONE);
whiteFigText = (TextView) findViewById(R.id.white_pieces);
whiteFigText.setTypeface(figNotation);
whiteFigText.setSelected(true);
whiteFigText.setTextColor(whiteTitleText.getTextColors());
blackFigText = (TextView) findViewById(R.id.black_pieces);
blackFigText.setTypeface(figNotation);
blackFigText.setSelected(true);
blackFigText.setTextColor(blackTitleText.getTextColors());
summaryTitleText = (TextView) findViewById(R.id.title_text_summary);
summaryTitleText.setVisibility(View.GONE);
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
@ -243,7 +260,7 @@ public class EditBoard extends Activity {
setSelection(-1);
else
setSelection(m.from);
checkValid();
checkValidAndUpdateMaterialDiff();
}
@Override
@ -256,7 +273,7 @@ public class EditBoard extends Activity {
}
private final void sendBackResult() {
if (checkValid()) {
if (checkValidAndUpdateMaterialDiff()) {
setPosFields();
String fen = TextIO.toFEN(cb.pos);
setResult(RESULT_OK, (new Intent()).setAction(fen));
@ -287,9 +304,13 @@ public class EditBoard extends Activity {
cb.pos.setEpSquare(epSquare);
}
/** Test if a position is valid. */
private final boolean checkValid() {
/** Test if a position is valid and update material diff display. */
private final boolean checkValidAndUpdateMaterialDiff() {
try {
MaterialDiff md = Util.getMaterialDiff(cb.pos);
whiteFigText.setText(md.white);
blackFigText.setText(md.black);
String fen = TextIO.toFEN(cb.pos);
TextIO.readFEN(fen);
status.setText("");
@ -332,13 +353,13 @@ public class EditBoard extends Activity {
case 0: // Edit side to move
showDialog(SIDE_DIALOG);
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
break;
case 1: { // Clear board
Position pos = new Position();
cb.setPosition(pos);
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
break;
}
case 2: { // Set initial position
@ -346,7 +367,7 @@ public class EditBoard extends Activity {
Position pos = TextIO.readFEN(TextIO.startPosFEN);
cb.setPosition(pos);
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
} catch (ChessParseError e) {
}
break;
@ -355,19 +376,19 @@ public class EditBoard extends Activity {
removeDialog(CASTLE_DIALOG);
showDialog(CASTLE_DIALOG);
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
break;
case 4: // Edit en passant file
removeDialog(EP_DIALOG);
showDialog(EP_DIALOG);
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
break;
case 5: // Edit move counters
removeDialog(MOVCNT_DIALOG);
showDialog(MOVCNT_DIALOG);
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
break;
case 6: { // Copy position
setPosFields();
@ -390,7 +411,7 @@ public class EditBoard extends Activity {
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
}
setSelection(-1);
checkValid();
checkValidAndUpdateMaterialDiff();
}
break;
}
@ -408,11 +429,11 @@ public class EditBoard extends Activity {
public void onClick(DialogInterface dialog, int id) {
if (id == 0) { // white to move
cb.pos.setWhiteMove(true);
checkValid();
checkValidAndUpdateMaterialDiff();
dialog.cancel();
} else {
cb.pos.setWhiteMove(false);
checkValid();
checkValidAndUpdateMaterialDiff();
dialog.cancel();
}
}
@ -452,7 +473,7 @@ public class EditBoard extends Activity {
if (h8Castle) castleMask |= 1 << Position.H8_CASTLE;
pos.setCastleMask(castleMask);
cb.setPosition(pos);
checkValid();
checkValidAndUpdateMaterialDiff();
}
});
AlertDialog alert = builder.create();