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 Dec 19, 2012
1 parent c54c4e5 commit 0b4b9d1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 23 deletions.
8 changes: 8 additions & 0 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 Down Expand Up @@ -76,4 +78,10 @@ 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]);
}
}
77 changes: 77 additions & 0 deletions Lib/tests/com/yarcat/tests/chemistrylines/SelectionInViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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() {
assertFalse(s.hasSource());
assertFalse(s.hasTarget());
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 targetEqualsSource() {
s.select(1);
s.select(1);
assertTrue(s.hasSource());
assertFalse(s.hasTarget());
s.select(2);
assertTrue(s.hasTarget());
s.select(1);
assertTrue(s.hasTarget());
assertEquals(1, s.getSource());
assertEquals(2, s.getTarget());
}

@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 0b4b9d1

Please sign in to comment.