Skip to content

Commit

Permalink
[#75] Selection highligh moved to SwingField.FieldSelection.
Browse files Browse the repository at this point in the history
  • Loading branch information
soulne4ny committed Dec 18, 2012
1 parent 8de695a commit f48d02b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 16 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 {

This comment has been minimized.

Copy link
@yarcat

yarcat Dec 19, 2012

Owner

Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants.

public enum Mark {SOURCE, DESTINATION, REACHABLE_FROM_SOURCE};
SelectedAsSource,
SelectedAsDestination,
ReachableFromSource,
}
// @formatter:on

private Element mElement = null;
private EnumSet<Mark> x = EnumSet.noneOf(Mark.class);

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);
}
}
52 changes: 49 additions & 3 deletions Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
package com.yarcat.chemistrylines.view;

public class SelectionInView {
public interface SelectionListener {

This comment has been minimized.

Copy link
@yarcat

yarcat Dec 19, 2012

Owner

I think Listener is enough, as in the code it will look like SelectionInView.Listener


public void onNewSource(int n);

This comment has been minimized.

Copy link
@yarcat

yarcat Dec 19, 2012

Owner

Why some methods accept n and others i? Maybe cell or idx. I don't really care, but it has to be consistent.


public void onNewTarget(int n);

public void onSourceCleared(int i);

public void onTargetCleared(int i);
}

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

This comment has been minimized.

Copy link
@yarcat

yarcat Dec 19, 2012

Owner

I'm wondering, maybe private int mSelectionStart, mSelectionEnd would look better? Wdyt?


public void select(int n) {
if (mSelection[0] == -1) {
mSelection[0] = n;
if (hasSource()) {
if (getSource() != n) {
selectTarget(n);
}
} else {
mSelection[1] = n;
selectSource(n);
}
}

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

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

public void clear() {
if (mListener != null) {
if (hasSource()) {
mListener.onSourceCleared(mSelection[0]);
}
if (hasDestination()) {
mListener.onTargetCleared(mSelection[1]);
}
}
mSelection[0] = mSelection[1] = -1;
}

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

public void setListener(SelectionListener l) {
mListener = l;
}
}
9 changes: 4 additions & 5 deletions Swing/src/com/yarcat/chemistrylines/swing/ElementButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ abstract class ElementButton extends JLabel {

public abstract Element getElement();

public void refresh() {
updateContent();
updateStyle();
void init() {
style.button(this);
refresh();
}

void init() {
public void refresh() {
updateContent();
style.button(this);
updateStyle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public void mousePressed(MouseEvent e) {
&& selection().getSource() == selection().getDestination()
&& selection().getSource() == b.n) {
selection().clear();
refreshField();
} else {
tryMakeMove(b.n);
}
refreshField();
}

private void tryMakeMove(int id) {
Expand All @@ -92,7 +92,6 @@ && selection().getSource() != selection().getDestination()) {
}
selection().clear();
}
refreshField();
}

@Override
Expand Down
48 changes: 43 additions & 5 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import java.awt.Color;

import com.yarcat.chemistrylines.field.Cell;
import com.yarcat.chemistrylines.field.Cell.Mark;
import com.yarcat.chemistrylines.field.Element;
import com.yarcat.chemistrylines.field.Field;
import com.yarcat.chemistrylines.game.GameLogic;
import com.yarcat.chemistrylines.view.SelectionInView;
import com.yarcat.chemistrylines.view.SelectionInView.SelectionListener;

class SwingField {

Expand All @@ -17,29 +20,59 @@ public FieldButton(int index) {
n = index;
}

private Cell cell() {
return at(n);
}

@Override
public Element getElement() {
return at(n).getElement();
return cell().getElement();
}

@Override
boolean isEmpty() {
return at(n).isEmpty();
return cell().isEmpty();
}

private boolean marked(Mark m) {

This comment has been minimized.

Copy link
@yarcat

yarcat Dec 19, 2012

Owner

What do you think this name – isMarkedWith or markedWith?

return cell().hasMark(m);
}

@Override
Color getBgColor() {
Color bg;
if (mSel.hasSource() && mSel.getSource() == n) {
if (marked(Mark.SelectedAsSource)) {
bg = Color.DARK_GRAY;
} else if (mSel.hasDestination() && mSel.getDestination() == n) {
} else if (marked(Mark.SelectedAsDestination)) {
bg = Color.GRAY;
} else {
bg = super.getBgColor();
}
return bg;
}
}

class FieldSelection implements SelectionListener {

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

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

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

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

private final GameLogic mGame;
Expand All @@ -50,6 +83,7 @@ public SwingField(GameLogic game, FieldButton[] buttons) {
mGame = game;
mButtons = buttons;
mSel = new SelectionInView();
mSel.setListener(new FieldSelection());
}

SelectionInView selection() {
Expand All @@ -63,7 +97,11 @@ void refresh() {
}

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

private Field getField() {
return mGame.getField();
}

public FieldButton getButton(int n) {
Expand Down

0 comments on commit f48d02b

Please sign in to comment.