while (isControlModeActive() || isTransientPanModeActive()) {
overlay.popMode();
controlMode = null;
}
// See if that did the trick, to avoid duplicate pushes.
Mode oldMode = overlay.peekMode();
if (oldMode == newMode) {
return;
}
// If the current Mode is one of ours, pop it before pushing the next:
if (oldMode == regionMode ||
oldMode == cropMode ||
oldMode == rotateMode) {
// If we are in region mode make sure we exit edit mode
if (oldMode == regionMode && regionMode instanceof RegionOverlay)
((RegionOverlay) regionMode).finishEditingCurve();
overlay.popMode();
// The CropModes need setup and teardown:
if ((oldMode == cropMode) || (oldMode == rotateMode)) {
CropMode crop = (CropMode) oldMode;
crop.doCrop();
}
oldMode.exit();
}
if (newMode != null) {
overlay.pushMode(newMode);
newMode.enter();
}