Commit abeae232 authored by Nico Mack's avatar Nico Mack

Changes to Tetherable interface

Fix bug in tethering mechanism when tetherable objects were lifted
Implemented desaturation feature in ColorFactory helper class
parent 8f511e9b
......@@ -143,6 +143,7 @@ SHADE_BRIGHTER_VALUE=brighter
SHADE_EXTRA_BRIGHT_VALUE=extra bright
SHADE_EXTRA_DARK_VALUE=extra dark
SHADE_DARKER_VALUE=darker
SHADE_DESATURATED_VALUE=desaturated
SHAPE_NODE=shape
SHORT_RANGE_NODE=shortRange
SOUND_BASE64_NODE=soundBase64
......
......@@ -17,6 +17,7 @@
package lu.list.itis.dkd.tui.bootstrapping;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.ColorFactory;
import lu.list.itis.dkd.tui.utility.Externalization;
import lu.list.itis.dkd.tui.utility.StringUtils;
......@@ -607,6 +608,8 @@ public class BootstrappingUtils {
colour = colour.brighter();
} else if (Externalization.SHADE_EXTRA_BRIGHT_VALUE.equals(shade)) {
colour = colour.brighter().brighter();
} else if (Externalization.SHADE_DESATURATED_VALUE.equals(shade)) {
colour = ColorFactory.desaturateColour(colour);
}
} else {
String colourString = getContent(colourNode, Externalization.EMPTY_STRING, null);
......
......@@ -188,15 +188,15 @@ public abstract class TetherableMarker extends Marker implements Tetherable {
// ---------------------------------------------------------------------------
@Override
public void tetherWith(Tetherable otherTetherable) {
this.tetherManager.tetherWith(otherTetherable);
public void tetherWith(Tetherable otherTetherable, boolean propagate) {
this.tetherManager.tetherWith(otherTetherable, propagate);
}
// ---------------------------------------------------------------------------
@Override
public void separateFrom(Tetherable tethered) {
this.tetherManager.separateFrom(tethered);
public void separateFrom(Tetherable tethered, boolean propagate) {
this.tetherManager.separateFrom(tethered, propagate);
}
// ---------------------------------------------------------------------------
......
......@@ -29,6 +29,10 @@ public class ColorFactory {
private static final double TWO_PI_THIRD = (2 * Math.PI) / 3;
private static final double FOUR_PI_THIRD = (4 * Math.PI) / 3;
private static final int HUE = 0;
private static final int SATURATION = 1;
private static final int BRIGHTNESS = 2;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......@@ -114,6 +118,27 @@ public class ColorFactory {
return makeColorGradient(number, frequency, frequency, frequency, 0d, TWO_PI_THIRD, FOUR_PI_THIRD, 128, 127);
}
// ---------------------------------------------------------------------------
/**
* @param colour
* @return
*/
// ---------------------------------------------------------------------------
public static Color desaturateColour(Color colour) {
float[] hsb = new float[3];
hsb = Color.RGBtoHSB(colour.getRed(), colour.getGreen(), colour.getBlue(), hsb);
int alpha = colour.getAlpha();
hsb[SATURATION] *= 0.3;
int rgb = Color.HSBtoRGB(hsb[HUE], hsb[SATURATION], hsb[BRIGHTNESS]);
rgb |= ((alpha << 24) & 0xFF000000);
return new Color(rgb);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
......
......@@ -178,6 +178,7 @@ public class Externalization extends NLS {
public static String SHADE_EXTRA_BRIGHT_VALUE;
public static String SHADE_EXTRA_DARK_VALUE;
public static String SHADE_DARKER_VALUE;
public static String SHADE_DESATURATED_VALUE;
public static String SHAPE_NODE;
public static String SHORT_RANGE_NODE;
public static String SOUND_BASE64_NODE;
......
......@@ -267,8 +267,8 @@ public abstract class TetherableWidget extends PointingWidget implements Tethera
// ---------------------------------------------------------------------------
@Override
public void tetherWith(Tetherable otherTetherable) {
this.tetherManager.tetherWith(otherTetherable);
public void tetherWith(Tetherable otherTetherable, boolean propagate) {
this.tetherManager.tetherWith(otherTetherable, propagate);
}
// ---------------------------------------------------------------------------
......@@ -278,8 +278,8 @@ public abstract class TetherableWidget extends PointingWidget implements Tethera
// ---------------------------------------------------------------------------
@Override
public void separateFrom(Tetherable tethered) {
this.tetherManager.separateFrom(tethered);
public void separateFrom(Tetherable tethered, boolean propagate) {
this.tetherManager.separateFrom(tethered, propagate);
}
// ---------------------------------------------------------------------------
......
......@@ -76,6 +76,9 @@ public class TetherManager {
private static final int HYSTERESIS = 5;
public static final boolean DO_PROPAGATE = true;
public static final boolean DONT_PROPAGATE = false;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......@@ -179,10 +182,12 @@ public class TetherManager {
Point tetherOrigin = this.tetherable.getTetherOrigin().toScreenCoordinates();
double shortest = Double.MAX_VALUE;
synchronized (currentlyTethered) {
for (Tetherable tethered : currentlyTethered) {
Point tetherEnding = tethered.getTetherOrigin().toScreenCoordinates();
shortest = Math.min(this.distance(tetherOrigin, tetherEnding), shortest);
if (!currentlyTethered.isEmpty()) {
synchronized (currentlyTethered) {
for (Tetherable tethered : currentlyTethered) {
Point tetherEnding = tethered.getTetherOrigin().toScreenCoordinates();
shortest = Math.min(this.distance(tetherOrigin, tetherEnding), shortest);
}
}
}
return shortest;
......@@ -201,7 +206,7 @@ public class TetherManager {
List<Tetherable> exTethers = new ArrayList<>(this.currentlyTethered);
for (Tetherable exTether : exTethers) {
this.separateFrom(exTether);
this.separateFrom(exTether, DO_PROPAGATE);
}
this.currentlyTethered.clear();
}
......@@ -502,7 +507,7 @@ public class TetherManager {
if (!this.potentialTethers.containsKey(potential)) {
this.potentialTethers.put(potential, tether);
if (propagate)
potential.addPotentialTether(this.tetherable, tether, false);
potential.addPotentialTether(this.tetherable, tether, DONT_PROPAGATE);
} else {
LOGGER.warn("Attempt to add an already known tetherable as potential tether!"); //$NON-NLS-1$
}
......@@ -518,10 +523,10 @@ public class TetherManager {
public void removePotentialTether(Tetherable potential, boolean propagate) {
Preconditions.checkNotNull(potential, "Potential Tetherable MUST not be null!"); //$NON-NLS-1$
if (this.potentialTethers.containsKey(potential)) {
this.separateFrom(potential);
this.separateFrom(potential, DO_PROPAGATE);
this.potentialTethers.remove(potential);
if (propagate)
potential.removePotentialTether(this.tetherable, false);
potential.removePotentialTether(this.tetherable, DONT_PROPAGATE);
} else {
LOGGER.warn("Attempt to remove an unknown tetherable!"); //$NON-NLS-1$
}
......@@ -535,10 +540,11 @@ public class TetherManager {
*
* @param candidate
* specifies the already known potential tether to tether with.
* @param propagate
*/
// ---------------------------------------------------------------------------
public void tetherWith(Tetherable candidate) {
public void tetherWith(Tetherable candidate, boolean propagate) {
Preconditions.checkNotNull(candidate, "Other Tetherable MUST not be null!"); //$NON-NLS-1$
if (!this.tetheringEnabled) {
......@@ -556,8 +562,8 @@ public class TetherManager {
this.currentlyTethered.add(candidate);
this.tetherListeners.notifyTetherListener(new TetherEvent(this.tetherable, candidate, TetherEvent.TetherState.ESTABLISHED));
if (!candidate.isTetheredWith(this.tetherable)) {
candidate.tetherWith(this.tetherable);
if (propagate && !candidate.isTetheredWith(this.tetherable)) {
candidate.tetherWith(this.tetherable, DONT_PROPAGATE);
}
}
} else {
......@@ -571,18 +577,19 @@ public class TetherManager {
*
* @param tethered
* specifies the object to separate from.
* @param propagate
*/
// ---------------------------------------------------------------------------
public void separateFrom(Tetherable tethered) {
public void separateFrom(Tetherable tethered, boolean propagate) {
Preconditions.checkNotNull(tethered, "Tethered object MUST not be null!"); //$NON-NLS-1$
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);
if (propagate && tethered.isTetheredWith(this.tetherable)) {
tethered.separateFrom(this.tetherable, DONT_PROPAGATE);
}
} else {
LOGGER.warn("Not tethered to specified tetherable!"); //$NON-NLS-1$
......@@ -696,6 +703,8 @@ public class TetherManager {
continue;
boolean alreadyTetheredWith = this.isTetheredWith(potential);
if (potential.getTetherOrigin() == null)
continue;
Point tetherEnding = potential.getTetherOrigin().toScreenCoordinates();
double distance = this.distance(tetherOrigin, tetherEnding);
......@@ -704,12 +713,12 @@ public class TetherManager {
if (!alreadyTetheredWith) {
if ((distance <= tetheringDistance - HYSTERESIS) && (distance < shortestTether)) {
this.tetherable.tetherWith(potential);
this.tetherable.tetherWith(potential, DO_PROPAGATE);
isTethered = true;
}
} else if (distance >= tetheringDistance + HYSTERESIS) {
if (!potential.isDraggable()) {
this.tetherable.separateFrom(potential);
this.tetherable.separateFrom(potential, DO_PROPAGATE);
} else {
// TODO implement dragging
}
......@@ -764,6 +773,7 @@ public class TetherManager {
// }
this.separateFromAll();
this.tetherable.setTetherOrigin(null);
}
// ---------------------------------------------------------------------------
......
......@@ -46,9 +46,9 @@ public interface Tetherable {
public abstract void setTetherOrigin(Point Origin);
public abstract void tetherWith(Tetherable otherTetherable);
public abstract void tetherWith(Tetherable otherTetherable, boolean propagate);
public abstract void separateFrom(Tetherable tethered);
public abstract void separateFrom(Tetherable tethered, boolean propagate);
public abstract boolean isActive();
......
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