Skip to content

Commit

Permalink
[#75] Refresh field on highlight change via listener
Browse files Browse the repository at this point in the history
  • Loading branch information
soulne4ny committed Jan 31, 2013
1 parent eac3224 commit 1ff7ed0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
23 changes: 21 additions & 2 deletions Lib/src/com/yarcat/chemistrylines/view/FieldHightlights.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ public enum Mark {
}
// @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 @@ -49,22 +54,26 @@ public boolean hasMark(int n, Mark m) {
public void onNewSource(int n) {
setMark(n, Mark.SOURCE);
markCellsReachableFrom(n);
onChange();
}

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

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

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

private void markCellsReachableFrom(int n) {
Expand All @@ -83,4 +92,14 @@ private void clearCellsReachableFrom(int n) {
clearMark(i, Mark.REACHABLE);
}
}

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

public void setListener(Listener l) {
mListener = l;
}
}
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
10 changes: 8 additions & 2 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 Down Expand Up @@ -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 Down Expand Up @@ -108,4 +109,9 @@ public void clear(int n) {
FieldButton newButton(int n) {
return new FieldButton(n);
}

@Override
public void onHighlightChange() {
refresh();
}
}

0 comments on commit 1ff7ed0

Please sign in to comment.