Commit cddc6da2 authored by Nico Mack's avatar Nico Mack

Added check to avoid adding same listener multiple times to Variable

Added Reset capabilities to SelectorWidget
parent a72cdff7
......@@ -224,8 +224,10 @@ public abstract class Variable<V> implements Cloneable {
* The {@link InputChangeListener} to add.
*/
public void addListener(InputChangeListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
/**
* Sets the suspendNotification flag for InputChangeListeners. When suspend,
......
......@@ -25,13 +25,15 @@ public class SelectorWidget extends ValueWidget {
/** The currently selected position */
protected int currentPosition = NONE;
protected int presetPosition = NONE;
protected float presetAngleOffset = 0;
protected boolean isPersistent = false;
// ***************************************************************************
// * Constants *
// ***************************************************************************
public static int NONE = -1;
private static double TWO_PI = 2 * Math.PI;
public static final int NONE = -1;
private static final double TWO_PI = 2 * Math.PI;
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -62,7 +64,7 @@ public class SelectorWidget extends ValueWidget {
// ***************************************************************************
// ---------------------------------------------------------------------------
private int getNumberOfPositions() {
protected int getNumberOfPositions() {
List<IndexedCorona> selectableCoronas = getCoronas(IndexedCorona.class);
HashSet<Integer> alreadySeen = new HashSet<>();
......@@ -77,6 +79,16 @@ public class SelectorWidget extends ValueWidget {
// ---------------------------------------------------------------------------
protected float moduloTwoPi(float rawAngle) {
double angle = rawAngle % TWO_PI;
if (angle < 0)
angle += TWO_PI;
return (float) angle;
}
// ---------------------------------------------------------------------------
@Override
protected void updateFromRotation(double angle) {
......@@ -84,10 +96,12 @@ public class SelectorWidget extends ValueWidget {
// ---------------------------------------------------------------------------
private int getCurrentPosition(float angle) {
protected int getCurrentPosition(float currentAngle) {
double segment = TWO_PI / numberOfPositions;
int current = 0;
float angle = this.moduloTwoPi(currentAngle);
// detect in which segment the current rotation is
for (int position = 0; position < numberOfPositions; position++) {
double minrot = (position - 0.5) * segment;
......@@ -104,7 +118,7 @@ public class SelectorWidget extends ValueWidget {
// ---------------------------------------------------------------------------
private float getCurrentAngle(int position) {
protected float getCurrentAngle(int position) {
double segment = TWO_PI / numberOfPositions;
float angle = 0;
......@@ -119,7 +133,7 @@ public class SelectorWidget extends ValueWidget {
// ---------------------------------------------------------------------------
private void selectPosition(int position) {
protected void selectPosition(int position) {
if (position != currentPosition) {
this.getCoronas(IndexedCorona.class).forEach(indexed -> indexed.setSelected(indexed.getIndex() == position));
......@@ -138,6 +152,18 @@ public class SelectorWidget extends ValueWidget {
// ***************************************************************************
// ---------------------------------------------------------------------------
public void reset() {
if (isPersistent) {
this.selectPosition(presetPosition);
Point centre = this.getPosition();
if (centre != null) {
presetAngleOffset = this.moduloTwoPi(this.getCurrentAngle(currentPosition) - (centre.getAngle() - presetAngleOffset));
}
}
}
// ---------------------------------------------------------------------------
public void setSelectedPosition(int position) {
currentPosition = position;
}
......@@ -160,9 +186,13 @@ public class SelectorWidget extends ValueWidget {
@Override
public void actionMove(TangibleObject tuioObject) {
super.actionMove(tuioObject);
int position = getCurrentPosition(tuioObject.getAngle());
double angle = this.moduloTwoPi(tuioObject.getAngle() + presetAngleOffset);
TangibleObject clone = tuioObject.constrainedClone(new Point(tuioObject.getX(), tuioObject.getY(), (float) angle));
super.actionMove(clone);
int position = getCurrentPosition(clone.getAngle());
int widgetId = tuioObject.getObjectId();
StateManager manager = states.get(widgetId);
......@@ -193,10 +223,12 @@ public class SelectorWidget extends ValueWidget {
@Override
public void actionDrop(TangibleObject tuioObject) {
int position = (this.isPersistent(tuioObject.getObjectId())) ? currentPosition : presetPosition;
float angle = this.getCurrentAngle(position);
isPersistent = this.isPersistent(tuioObject.getObjectId());
int position = (isPersistent) ? currentPosition : presetPosition;
presetAngleOffset = (isPersistent) ? this.moduloTwoPi(this.getCurrentAngle(position) - tuioObject.getAngle()) : 0;
TangibleObject clone = tuioObject.constrainedClone(new Point(tuioObject.getX(), tuioObject.getY(), angle));
TangibleObject clone = tuioObject.constrainedClone(new Point(tuioObject.getX(), tuioObject.getY(), presetAngleOffset));
super.actionDrop(clone);
this.selectPosition(position);
......@@ -216,7 +248,7 @@ public class SelectorWidget extends ValueWidget {
public void actionLift(TangibleObject tuioObject) {
super.actionLift(tuioObject);
if (!this.isPersistent(tuioObject.getObjectId())) {
if (!isPersistent) {
this.selectPosition(NONE);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment