Skip to content

Commit

Permalink
[#75] Code review 2:
Browse files Browse the repository at this point in the history
  * Fix after review f48d02b.
  * Refresh field on highlight change via listener
  • Loading branch information
soulne4ny committed Dec 28, 2012
1 parent 48c56c9 commit 64cd56a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 58 deletions.
45 changes: 32 additions & 13 deletions Lib/src/com/yarcat/chemistrylines/view/FieldHightlights.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@

import com.yarcat.chemistrylines.algorithms.Path;
import com.yarcat.chemistrylines.field.Field;
import com.yarcat.chemistrylines.view.SelectionInView.SelectionListener;
import com.yarcat.chemistrylines.view.SelectionInView;

public class FieldHightlights implements SelectionListener {
public class FieldHightlights implements SelectionInView.Listener {
// @formatter:off
public enum Mark {
SelectedAsSource,
SelectedAsDestination,
ReachableFromSource,
SOURCE,
TARGET,
REACHABLE,
}
// @formatter:on

public interface Listener {
public void onHighlightChange();
}

final ArrayList<EnumSet<Mark>> mMarks;
final Field mField;
private Listener mListener;

private FieldHightlights(Field f, ArrayList<EnumSet<Mark>> m) {
mField = f;
Expand All @@ -33,11 +38,11 @@ public static FieldHightlights create(Field f) {
return new FieldHightlights(f, m);
}

public void setMark(int n, Mark m) {
private void setMark(int n, Mark m) {
mMarks.get(n).add(m);
}

public void clearMark(int n, Mark m) {
private void clearMark(int n, Mark m) {
mMarks.get(n).remove(m);
}

Expand All @@ -47,40 +52,54 @@ public boolean hasMark(int n, Mark m) {

@Override
public void onNewSource(int n) {
setMark(n, Mark.SelectedAsSource);
setMark(n, Mark.SOURCE);
markCellsReachableFrom(n);
onChange();
}

@Override
public void onNewTarget(int n) {
setMark(n, Mark.SelectedAsDestination);
setMark(n, Mark.TARGET);
onChange();
}

@Override
public void onSourceCleared(int n) {
clearMark(n, Mark.SelectedAsSource);
clearMark(n, Mark.SOURCE);
clearCellsReachableFrom(n);
onChange();
}

@Override
public void onTargetCleared(int n) {
clearMark(n, Mark.SelectedAsDestination);
clearMark(n, Mark.TARGET);
onChange();
}

private void markCellsReachableFrom(int n) {
Path p = Path.prepare(mField, n);
boolean markEmpties = p.reachableCount < mField.getLength() * 2 / 3;
for (int i = 0; i < mField.getLength(); ++i) {
if (p.isReachable(i) && (!mField.at(i).isEmpty() || markEmpties)) {
setMark(i, Mark.ReachableFromSource);
setMark(i, Mark.REACHABLE);
}
}

}

private void clearCellsReachableFrom(int n) {
for (int i = 0; i < mField.getLength(); ++i) {
clearMark(i, Mark.ReachableFromSource);
clearMark(i, Mark.REACHABLE);
}
}

private void onChange() {
if (mListener != null) {
mListener.onHighlightChange();
}
}

public void setListener(Listener l) {
mListener = l;
}
}
46 changes: 20 additions & 26 deletions Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import android.annotation.SuppressLint;

public class SelectionInView {
public interface SelectionListener {
public interface Listener {

public void onNewSource(int n);

public void onNewTarget(int n);

public void onSourceCleared(int i);
public void onSourceCleared(int n);

public void onTargetCleared(int i);
public void onTargetCleared(int n);
}

private SelectionListener mListener;
private final int[] mSelection = new int[] { -1, -1 };
private Listener mListener;
private int mSource = -1;
private int mTarget = -1;

public void select(int n) {
if (hasSource()) {
Expand All @@ -31,15 +32,15 @@ public void select(int n) {

private void selectSource(int n) {
clearSource();
mSelection[0] = n;
mSource = n;
if (mListener != null && hasSource()) {
mListener.onNewSource(n);
}
}

private void selectTarget(int n) {
clearTarget();
mSelection[1] = n;
mTarget = n;
if (mListener != null && hasTarget()) {
mListener.onNewTarget(n);
}
Expand All @@ -48,57 +49,50 @@ private void selectTarget(int n) {
public void clearSource() {
if (hasSource()) {
if (mListener != null) {
mListener.onSourceCleared(mSelection[0]);
mListener.onSourceCleared(mSource);
}
mSelection[0] = -1;
mSource = -1;
}
}

public void clearTarget() {
if (hasTarget()) {
if (mListener != null) {
mListener.onTargetCleared(mSelection[1]);
mListener.onTargetCleared(mTarget);
}
mSelection[1] = -1;
mTarget = -1;
}
}

public void clear() {
if (mListener != null) {
if (hasSource()) {
mListener.onSourceCleared(mSelection[0]);
}
if (hasTarget()) {
mListener.onTargetCleared(mSelection[1]);
}
}
mSelection[0] = mSelection[1] = -1;
clearSource();
clearTarget();
}

public boolean hasSource() {
return mSelection[0] != -1;
return mSource != -1;
}

public int getSource() {
return mSelection[0];
return mSource;
}

public boolean hasTarget() {
return mSelection[1] != -1;
return mTarget != -1;
}

public int getTarget() {
return mSelection[1];
return mTarget;
}

public void setListener(SelectionListener l) {
public void setListener(Listener l) {
mListener = l;
}

@SuppressLint("DefaultLocale")
@Override
public String toString() {
return String
.format("Selection(%d, %d)", mSelection[0], mSelection[1]);
.format("Selection(%d, %d)", mSource, mTarget);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SwingChemistryLines(GameLogic game, SwingField fieldUI,
}

void refresh() {
refreshField();
mFieldUI.refresh();
mPreviewUI.refresh();
if (mCleanerUI != null) {
mCleanerUI.refresh();
Expand All @@ -42,10 +42,6 @@ private void refreshScore() {
mScoreUI.append(mGame.getScorer().get());
}

private void refreshField() {
mFieldUI.refresh();
}

@Override
public void mouseClicked(MouseEvent e) {
}
Expand All @@ -59,7 +55,6 @@ public void mouseEntered(MouseEvent e) {
} else {
selection().select(b.n);
}
refreshField();
}
}

Expand All @@ -76,8 +71,6 @@ public void mousePressed(MouseEvent e) {
} else {
selection().select(b.n);
}
// TODO: refresh field via selection listener
refreshField();
}

@Override
Expand All @@ -104,7 +97,6 @@ private void tryMakeMove() {
} catch (InvalidMove e1) {
}
selection().clear();
refreshField();
}
}

Expand Down
24 changes: 15 additions & 9 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingField.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import com.yarcat.chemistrylines.field.Field;
import com.yarcat.chemistrylines.game.GameLogic;
import com.yarcat.chemistrylines.view.FieldHightlights;
import com.yarcat.chemistrylines.view.SelectionInView;
import com.yarcat.chemistrylines.view.FieldHightlights.Mark;
import com.yarcat.chemistrylines.view.SelectionInView;

class SwingField {
class SwingField implements FieldHightlights.Listener {
@SuppressWarnings("serial")
class FieldButton extends ElementButton {
public final int n;
Expand All @@ -35,7 +35,7 @@ boolean isEmpty() {
return cell().isEmpty();
}

private boolean marked(Mark m) {
private boolean markedAs(Mark m) {
return mFieldMarks.hasMark(n, m);
}

Expand All @@ -48,11 +48,11 @@ void updateStyle() {
@Override
Color getBgColor() {
Color bg;
if (marked(Mark.SelectedAsSource)) {
if (markedAs(Mark.SOURCE)) {
bg = Color.DARK_GRAY;
} else if (marked(Mark.SelectedAsDestination)) {
} else if (markedAs(Mark.TARGET)) {
bg = Color.GRAY;
} else if (isEmpty() && marked(Mark.ReachableFromSource)) {
} else if (isEmpty() && markedAs(Mark.REACHABLE)) {
bg = style.REACHABLE_BG;
} else {
bg = super.getBgColor();
Expand All @@ -61,7 +61,7 @@ Color getBgColor() {
}

private Border getEdge() {
return !isEmpty() && marked(Mark.ReachableFromSource)
return !isEmpty() && markedAs(Mark.REACHABLE)
? style.REACHABLE_BORDER : style.DEFAULT_BORDER;
}
}
Expand All @@ -76,6 +76,7 @@ public SwingField(GameLogic game, FieldButton[] buttons) {
mButtons = buttons;
mSel = new SelectionInView();
mFieldMarks = FieldHightlights.create(mGame.getField());
mFieldMarks.setListener(this);
mSel.setListener(mFieldMarks);
}

Expand All @@ -89,8 +90,8 @@ void refresh() {
}
}

Cell at(int i) {
return getField().at(i);
Cell at(int n) {
return getField().at(n);
}

Field getField() {
Expand All @@ -108,4 +109,9 @@ public void clear(int n) {
FieldButton newButton(int n) {
return new FieldButton(n);
}

@Override
public void onHighlightChange() {
refresh();
}
}
2 changes: 1 addition & 1 deletion Swing/src/com/yarcat/chemistrylines/swing/style.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class style {
final static Border DEFAULT_BORDER =
BorderFactory.createLineBorder(new Color(0x55, 0x44, 0x33));
final static Border REACHABLE_BORDER =
BorderFactory.createLineBorder(new Color(0x20, 0x00, 0x30), 2);
BorderFactory.createLineBorder(new Color(0x20, 0x00, 0x30), 2);
// @formatter:on

static Component defaultColor(Component c) {
Expand Down

0 comments on commit 64cd56a

Please sign in to comment.