Skip to content

Commit

Permalink
[#75] Selection got callbacks, Cells got marks, BaseField links them.
Browse files Browse the repository at this point in the history
  • Loading branch information
soulne4ny committed Dec 18, 2012
1 parent 8de695a commit 676be1e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
27 changes: 26 additions & 1 deletion Lib/src/com/yarcat/chemistrylines/field/Cell.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.yarcat.chemistrylines.field;

import java.util.EnumSet;

/** Represents a cell in a game field. */
public final class Cell {

// @formatter:off
public enum Mark {
SelectedAsSource,
SelectedAsDestination,
ReachableFromSource,
}
// @formatter:on

private Element mElement = null;
private EnumSet<Mark> x;

public boolean isEmpty() {
return getElement() == null;
Expand All @@ -16,4 +27,18 @@ public void setElement(Element element) {
public Element getElement() {
return mElement;
}
}

public void setMark(Mark m) {
x.add(m);
}

public void clearMark(Mark m) {
if (x.contains(m)) {
x.remove(m);
}
}

public boolean hasMark(Mark m) {
return x.contains(m);
}
}
47 changes: 46 additions & 1 deletion Lib/src/com/yarcat/chemistrylines/field/Field.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.yarcat.chemistrylines.field;

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

/** Game field (model) interface. */
public interface Field {
Expand Down Expand Up @@ -41,13 +44,55 @@ public interface SequenceVisitor {
public void removeCompound(int[] cells);

/* Implementation of functionality common for all fields */
public static abstract class BaseField implements Field {
public static abstract class BaseField implements Field, SelectionListener {

@Override
public void removeCompound(int[] cells) {
for (int n : cells) {
at(n).setElement(null);
}
}

@Override
public void onNewSource(int n) {
if (n > -1) {
at(n).setMark(Mark.SelectedAsSource);
markCellsReachableFrom(n);
}
}

@Override
public void onNewDestination(int n) {
if (n > -1) {
at(n).setMark(Mark.SelectedAsDestination);
}
}

@Override
public void onSelectionCleared(int s, int d) {
if (s > -1) {
at(s).clearMark(Mark.SelectedAsSource);
clearCellsReachableFrom(s);
}
if (d > -1) {
at(d).clearMark(Mark.SelectedAsSource);
}
}

private void markCellsReachableFrom(int n) {
Path p = new Path(this, n);
p.evaluate();
for (int i = 0; i < getLength(); ++i) {
if (p.isReachable(i)) {
at(i).setMark(Mark.ReachableFromSource);
}
}
}

private void clearCellsReachableFrom(int n) {
for (int i = 0; i < getLength(); ++i) {
at(i).clearMark(Mark.ReachableFromSource);
}
}
}
}
23 changes: 23 additions & 0 deletions Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package com.yarcat.chemistrylines.view;

public class SelectionInView {
public interface SelectionListener {

public void onNewSource(int n);

public void onNewDestination(int n);

public void onSelectionCleared(int src, int dst);
}

private SelectionListener mListener;
private int[] mSelection = new int[] { -1, -1 };

public void select(int n) {
if (mSelection[0] == -1) {
mSelection[0] = n;
if (mListener != null) {
mListener.onNewSource(n);
}
} else {
mSelection[1] = n;
if (mListener != null) {
mListener.onNewDestination(n);
}
}
}

public void clear() {
mSelection[0] = mSelection[1] = -1;
if (mListener != null) {
mListener.onSelectionCleared(mSelection[0], mSelection[1]);
}
}

public boolean hasSource() {
Expand All @@ -30,4 +49,8 @@ public boolean hasDestination() {
public int getDestination() {
return mSelection[1];
}

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

0 comments on commit 676be1e

Please sign in to comment.