Skip to content

Commit

Permalink
[#75] Fix after review f48d02b.
Browse files Browse the repository at this point in the history
  • Loading branch information
soulne4ny committed Dec 26, 2012
1 parent 2dc0f0d commit 5bae935
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 45 deletions.
22 changes: 11 additions & 11 deletions Lib/src/com/yarcat/chemistrylines/view/FieldHightlights.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

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

Expand Down Expand Up @@ -47,40 +47,40 @@ public boolean hasMark(int n, Mark m) {

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

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

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

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

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);
}
}
}
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);
}
}
14 changes: 7 additions & 7 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingField.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -89,8 +89,8 @@ void refresh() {
}
}

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

Field getField() {
Expand Down
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 5bae935

Please sign in to comment.