Commit 9516a636 authored by Nico Mack's avatar Nico Mack

Stability improvements in TetherManager

parent 56a7dc5a
......@@ -35,6 +35,7 @@ import lu.list.itis.dkd.tui.widget.tether.listener.TetherListener;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import TUIO.TuioObject;
......@@ -113,6 +114,10 @@ public abstract class TetherableWidget extends PointingWidget implements Tethera
// ***************************************************************************
// ---------------------------------------------------------------------------
protected Map<Tetherable, Tether> getPotentialTethers() {
return this.tetherManager.getPotentialTethers();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
......@@ -237,7 +242,9 @@ public abstract class TetherableWidget extends PointingWidget implements Tethera
@Override
public void addPotentialTether(Tetherable potential, Tether tether, boolean propagate) {
this.tetherManager.addPotentialTether(potential, tether, propagate);
if (!this.tetherManager.isPotentialTether(potential)) {
this.tetherManager.addPotentialTether(potential, tether, propagate);
}
}
// ---------------------------------------------------------------------------
......@@ -248,7 +255,9 @@ public abstract class TetherableWidget extends PointingWidget implements Tethera
@Override
public void removePotentialTether(Tetherable potential, boolean propagate) {
this.tetherManager.removePotentialTether(potential, propagate);
if (this.tetherManager.isPotentialTether(potential)) {
this.tetherManager.removePotentialTether(potential, propagate);
}
}
// ---------------------------------------------------------------------------
......
......@@ -39,6 +39,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
......@@ -369,6 +370,13 @@ public class TetherManager {
return potentialTether;
}
// ---------------------------------------------------------------------------
public boolean isPotentialTether(Tetherable potential) {
return this.potentialTethers.containsKey(potential);
}
// ---------------------------------------------------------------------------
/**
* Checks whether this manager is a provider for the specified receiver
......@@ -453,7 +461,7 @@ public class TetherManager {
*/
// ---------------------------------------------------------------------------
public void setPotentialTethers(HashMap<Tetherable, Tether> potentials) {
public void setPotentialTethers(Map<Tetherable, Tether> potentials) {
this.potentialTethers.clear();
this.potentialTethers.putAll(potentials);
}
......@@ -466,7 +474,7 @@ public class TetherManager {
*/
// ---------------------------------------------------------------------------
public HashMap<Tetherable, Tether> getPotentialTethers() {
public Map<Tetherable, Tether> getPotentialTethers() {
return new HashMap<>(this.potentialTethers);
}
......@@ -569,14 +577,16 @@ public class TetherManager {
public void separateFrom(Tetherable tethered) {
Preconditions.checkNotNull(tethered, "Tethered object MUST not be null!"); //$NON-NLS-1$
if (this.currentlyTethered.contains(tethered)) {
this.currentlyTethered.remove(tethered);
this.tetherListeners.notifyTetherListener(new TetherEvent(this.tetherable, tethered, TetherEvent.TetherState.TORNDOWN));
if (tethered.isTetheredWith(this.tetherable)) {
tethered.separateFrom(this.tetherable);
synchronized (this.currentlyTethered) {
if (this.currentlyTethered.contains(tethered)) {
this.currentlyTethered.remove(tethered);
this.tetherListeners.notifyTetherListener(new TetherEvent(this.tetherable, tethered, TetherEvent.TetherState.TORNDOWN));
if (tethered.isTetheredWith(this.tetherable)) {
tethered.separateFrom(this.tetherable);
}
} else {
LOGGER.warn("Not tethered to specified tetherable!"); //$NON-NLS-1$
}
} else {
LOGGER.warn("Not tethered to specified tetherable!"); //$NON-NLS-1$
}
}
......@@ -676,49 +686,51 @@ public class TetherManager {
int tetherCount = 0;
boolean isTethered;
for (Entry<Tetherable, Tether> entry : potentialTethers.entrySet()) {
synchronized (potentialTethers) {
for (Entry<Tetherable, Tether> entry : potentialTethers.entrySet()) {
isTethered = false;
Tetherable potential = entry.getKey();
isTethered = false;
Tetherable potential = entry.getKey();
if (!potential.isActive())
continue;
if (!potential.isActive())
continue;
boolean alreadyTetheredWith = this.isTetheredWith(potential);
boolean alreadyTetheredWith = this.isTetheredWith(potential);
Point tetherEnding = potential.getTetherOrigin().toScreenCoordinates();
double distance = this.distance(tetherOrigin, tetherEnding);
Point tetherEnding = potential.getTetherOrigin().toScreenCoordinates();
double distance = this.distance(tetherOrigin, tetherEnding);
// Case 1: Figure out whether potential tether is closer than current tether (if any).
// Case 1: Figure out whether potential tether is closer than current tether (if any).
if (!alreadyTetheredWith) {
if ((distance <= tetheringDistance - HYSTERESIS) && (distance < shortestTether)) {
this.tetherable.tetherWith(potential);
if (!alreadyTetheredWith) {
if ((distance <= tetheringDistance - HYSTERESIS) && (distance < shortestTether)) {
this.tetherable.tetherWith(potential);
isTethered = true;
}
} else if (distance >= tetheringDistance + HYSTERESIS) {
if (!potential.isDraggable()) {
this.tetherable.separateFrom(potential);
} else {
// TODO implement dragging
}
} else {
isTethered = true;
}
} else if (distance >= tetheringDistance + HYSTERESIS) {
if (!potential.isDraggable()) {
this.tetherable.separateFrom(potential);
} else {
// TODO implement dragging
if (isTethered && this.tetherable.rotatesWithTether()) {
Point lineOfSight = PolarCoordinateHelper.carthesianToPolar(tetherOrigin, tetherEnding);
angle += lineOfSight.getAngle();
tetherCount++;
}
} else {
isTethered = true;
}
if (isTethered && this.tetherable.rotatesWithTether()) {
Point lineOfSight = PolarCoordinateHelper.carthesianToPolar(tetherOrigin, tetherEnding);
angle += lineOfSight.getAngle();
tetherCount++;
}
if (alreadyTetheredWith) {
Tether tether = entry.getValue();
tether.setOrigin(tetherOrigin);
tether.setEnding(tetherEnding);
}
if (alreadyTetheredWith) {
Tether tether = entry.getValue();
tether.setOrigin(tetherOrigin);
tether.setEnding(tetherEnding);
}
}
if (this.tetherable.rotatesWithTether() && tetherCount > 0) {
......
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