Skip to content

Commit

Permalink
[#75] Field selection logic simplified.
Browse files Browse the repository at this point in the history
  • Loading branch information
soulne4ny committed Jan 31, 2013
1 parent d983d8b commit 120712b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 30 deletions.
39 changes: 32 additions & 7 deletions Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.yarcat.chemistrylines.view;

import android.annotation.SuppressLint;

public class SelectionInView {
public interface SelectionListener {

Expand All @@ -17,7 +19,9 @@ public interface SelectionListener {

public void select(int n) {
if (hasSource()) {
if (getSource() != n) {
if (getSource() == n) {
clear();
} else {
selectTarget(n);
}
} else {
Expand All @@ -26,25 +30,39 @@ public void select(int n) {
}

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

private void selectTarget(int n) {
if (mListener != null && hasTarget()) {
mListener.onTargetCleared(mSelection[1]);
}
clearTarget();
mSelection[1] = n;
if (mListener != null && hasTarget()) {
mListener.onNewTarget(n);
}
}

private void clearSource() {
if (hasSource()) {
if (mListener != null) {
mListener.onSourceCleared(mSelection[0]);
}
mSelection[0] = -1;
}
}

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

public void clear() {
if (mListener != null) {
if (hasSource()) {
Expand Down Expand Up @@ -76,4 +94,11 @@ public int getTarget() {
public void setListener(SelectionListener l) {
mListener = l;
}

@SuppressLint("DefaultLocale")
@Override
public String toString() {
return String
.format("Selection(%d, %d)", mSelection[0], mSelection[1]);
}
}
78 changes: 78 additions & 0 deletions Lib/tests/com/yarcat/tests/chemistrylines/SelectionInViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.yarcat.tests.chemistrylines;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import com.yarcat.chemistrylines.view.SelectionInView;

public class SelectionInViewTest {

SelectionInView s;

@Before
public void setUp() {
s = new SelectionInView();
}

@Test
public void empty() {
assertFalse(s.hasSource());
assertFalse(s.hasTarget());
}

@Test
public void oneSelect() {
s.select(1);
assertTrue(s.hasSource());
assertFalse(s.hasTarget());
}

@Test
public void differentCells() {
s.select(1);
s.select(2);
assertTrue(s.hasSource());
assertTrue(s.hasTarget());
assertEquals(1, s.getSource());
assertEquals(2, s.getTarget());
s.select(3);
assertTrue(s.hasSource());
assertTrue(s.hasTarget());
assertEquals(1, s.getSource());
assertEquals(3, s.getTarget());
}

@Test
public void reselectSource() {
s.select(1);
assertTrue(s.hasSource());
assertFalse(s.hasTarget());
s.select(1);
assertFalse(s.hasSource());
assertFalse(s.hasTarget());
s.select(1);
s.select(2);
assertTrue(s.hasSource());
assertTrue(s.hasTarget());
s.select(1);
assertFalse(s.hasSource());
assertFalse(s.hasTarget());
}

@Test
public void clear() {
s.select(1);
s.clear();
assertFalse(s.hasSource());
assertFalse(s.hasTarget());
s.select(1);
s.select(2);
s.clear();
assertFalse(s.hasSource());
assertFalse(s.hasTarget());
}
}
48 changes: 25 additions & 23 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingChemistryLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,42 @@ public void mouseExited(MouseEvent e) {

@Override
public void mousePressed(MouseEvent e) {
FieldButton b = (FieldButton) e.getSource();
if (selection().hasSource() && selection().hasTarget()
&& selection().getSource() == selection().getTarget()
&& selection().getSource() == b.n) {
selection().clear();
final FieldButton b = (FieldButton) e.getSource();
if (selection().hasSource()) {
selection().select(b.n);
tryMakeMove();
} else {
tryMakeMove(b.n);
selection().select(b.n);
}
// TODO: refresh field via selection listener
refreshField();
}

private void tryMakeMove(int id) {
// We need this because for the drag case mouseReleased is called for
// the source button, and we don't wanna overwrite the value.
if (!selection().hasTarget()) {
selection().select(id);
@Override
public void mouseReleased(MouseEvent e) {
if (selection().hasSource()) {
// mouseReleased() gets the same button that was pressed.
tryMakeMove();
}
if (selection().hasTarget()
&& selection().getSource() != selection().getTarget()) {
}

private void tryMakeMove() {
if (selection().hasTarget()) {
int s, t;
if (mGame.getField().at(selection().getSource()).isEmpty()) {
s = selection().getTarget();
t = selection().getSource();
} else {
s = selection().getSource();
t = selection().getTarget();
}
try {
mGame.makeMove(selection().getSource(), selection()
.getTarget());
mGame.makeMove(s, t);
refresh();
} catch (InvalidMove e1) {
}
selection().clear();
}
}

@Override
public void mouseReleased(MouseEvent e) {
if (selection().hasSource()) {
FieldButton b = (FieldButton) e.getSource();
tryMakeMove(b.n);
refreshField();
}
}

Expand Down

0 comments on commit 120712b

Please sign in to comment.