use of com.codename1.ui.animations.Transition in project CodenameOne by codenameone.
the class CommonTransitions method initTransition.
/**
* {@inheritDoc}
*/
public void initTransition() {
firstFinished = false;
if (transitionType == TYPE_EMPTY) {
return;
}
startTime = System.currentTimeMillis();
Component source = getSource();
Component destination = getDestination();
position = 0;
int w = source.getWidth();
int h = source.getHeight();
// improper replace() calls, this may still be valid and shouldn't fail
if (w <= 0 || h <= 0) {
return;
}
// nothing to prepare in advance for a shift fade transition
if (transitionType == TYPE_SLIDE_AND_FADE) {
if (getSource() instanceof Form && getDestination() instanceof Form) {
motion = createMotion(100, 200, speed);
motion2 = createMotion(0, getDestination().getWidth(), speed);
motion.start();
motion2.start();
return;
}
transitionType = TYPE_SLIDE;
}
if (transitionType == TYPE_PULSATE_DIALOG) {
if (getDestination() instanceof Dialog) {
motion = createMotion(600, 1100, 150);
motion.start();
motion2 = createMotion(100, 255, 225);
motion2.start();
pulseState = 0;
Component c = getDialogParent(getDestination());
originalX = c.getX();
originalY = c.getY();
originalWidth = c.getWidth();
originalHeight = c.getHeight();
Display d = Display.getInstance();
Dialog dlg = (Dialog) destination;
// transparent image!
buffer = Image.createImage(Math.min(d.getDisplayWidth(), getDialogParent(dlg).getWidth()), Math.min(d.getDisplayHeight(), dlg.getContentPane().getParent().getHeight() + getDialogTitleHeight(dlg)), 0);
Graphics g = buffer.getGraphics();
Style stl = dlg.getDialogComponent().getStyle();
byte bgt = stl.getBgTransparency();
stl.setBgTransparency(0xff);
drawDialogCmp(buffer.getGraphics(), dlg);
stl.setBgTransparency(bgt & 0xff, true);
return;
}
transitionType = TYPE_EMPTY;
motion = createMotion(0, 0, 0);
pulseState = (byte) 3;
return;
}
if (Display.getInstance().areMutableImagesFast() || transitionType == TYPE_TIMELINE) {
if (buffer == null) {
buffer = createMutableImage(w, h);
} else {
// this might happen when screen orientation changes
if (buffer.getWidth() != w || buffer.getHeight() != h) {
buffer = createMutableImage(w, h);
rgbBuffer = null;
// slide motion might need resetting since screen size is different
motion = null;
}
}
}
if (transitionType == TYPE_FADE) {
motion = createMotion(0, 256, speed);
motion.start();
if (Display.getInstance().areMutableImagesFast()) {
Graphics g = buffer.getGraphics();
g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
if (getSource().getParent() != null) {
getSource().getComponentForm().paintComponent(g);
}
getSource().paintBackgrounds(g);
g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
paint(g, getDestination(), 0, 0);
rgbBuffer = new RGBImage(buffer.getRGBCached(), buffer.getWidth(), buffer.getHeight());
paint(g, getSource(), 0, 0, true);
g.translate(source.getAbsoluteX(), source.getAbsoluteY());
}
return;
}
if (transitionType == TYPE_TIMELINE) {
Graphics g = buffer.getGraphics();
g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
if (timeline.getWidth() != buffer.getWidth() || timeline.getHeight() != buffer.getHeight()) {
timeline = timeline.scaled(buffer.getWidth(), buffer.getHeight());
}
if (timeline instanceof Timeline) {
((Timeline) timeline).setTime(0);
((Timeline) timeline).setLoop(false);
((Timeline) timeline).setAnimationDelay(0);
}
paint(g, getDestination(), 0, 0);
g.translate(source.getAbsoluteX(), source.getAbsoluteY());
return;
}
if (transitionType == TYPE_SLIDE || transitionType == TYPE_FAST_SLIDE || transitionType == TYPE_COVER || transitionType == TYPE_UNCOVER) {
int dest;
int startOffset = 0;
boolean direction = forward;
// flip the direction only for horizontal slides
if ((source.getUIManager().getLookAndFeel().isRTL()) && slideType == SLIDE_HORIZONTAL) {
direction = !direction;
}
if (slideType == SLIDE_HORIZONTAL) {
dest = w;
if (destination instanceof Dialog) {
startOffset = w - getDialogParent(destination).getWidth();
if (direction) {
startOffset -= getDialogParent(destination).getStyle().getMarginLeft(destination.isRTL());
} else {
startOffset -= getDialogParent(destination).getStyle().getMarginRight(destination.isRTL());
}
} else {
if (source instanceof Dialog) {
dest = getDialogParent(source).getWidth();
if (direction) {
dest += getDialogParent(source).getStyle().getMarginLeft(source.isRTL());
} else {
dest += getDialogParent(source).getStyle().getMarginRight(source.isRTL());
}
}
}
} else {
dest = h;
if (destination instanceof Dialog) {
startOffset = h - getDialogParent(destination).getHeight() - getDialogTitleHeight((Dialog) destination);
if (direction) {
startOffset -= getDialogParent(destination).getStyle().getMarginBottom();
} else {
startOffset -= getDialogParent(destination).getStyle().getMarginTop();
startOffset -= ((Dialog) destination).getTitleStyle().getMarginTop();
if (!drawDialogMenu && ((Dialog) destination).getCommandCount() > 0) {
Container p = ((Dialog) destination).getSoftButton(0).getParent();
if (p != null) {
startOffset -= p.getHeight();
}
}
}
} else {
if (source instanceof Dialog) {
dest = getDialogParent(source).getHeight() + getDialogTitleHeight((Dialog) source);
if (direction) {
dest += getDialogParent(source).getStyle().getMarginBottom();
} else {
dest += getDialogParent(source).getStyle().getMarginTop();
dest += ((Dialog) source).getTitleStyle().getMarginTop();
if (((Dialog) source).getCommandCount() > 0) {
Container p = ((Dialog) source).getSoftButton(0).getParent();
if (p != null) {
dest += p.getHeight();
}
}
}
}
}
}
motion = createMotion(startOffset, dest, speed);
if (!Display.getInstance().areMutableImagesFast()) {
motion.start();
buffer = null;
return;
}
// make sure the destination is painted fully at least once
// we must use a full buffer otherwise the clipping will take effect
Graphics g = buffer.getGraphics();
// tinting is expensive
if (getSource() instanceof Dialog) {
paint(g, getDestination(), 0, 0);
if (transitionType == TYPE_FAST_SLIDE && !(destination instanceof Dialog)) {
Dialog d = (Dialog) source;
secondaryBuffer = createMutableImage(getDialogParent(d).getWidth(), getDialogParent(d).getHeight() + getDialogTitleHeight(d));
drawDialogCmp(secondaryBuffer.getGraphics(), d);
}
} else {
if (getDestination() instanceof Dialog) {
paint(g, getSource(), 0, 0);
if (transitionType == TYPE_FAST_SLIDE && !(source instanceof Dialog)) {
Dialog d = (Dialog) destination;
secondaryBuffer = createMutableImage(getDialogParent(d).getWidth(), d.getContentPane().getParent().getHeight() + getDialogTitleHeight(d));
drawDialogCmp(secondaryBuffer.getGraphics(), d);
}
} else {
paint(g, source, -source.getAbsoluteX(), -source.getAbsoluteY(), true);
if (transitionType == TYPE_FAST_SLIDE) {
secondaryBuffer = createMutableImage(destination.getWidth(), destination.getHeight());
paint(secondaryBuffer.getGraphics(), destination, -destination.getAbsoluteX(), -destination.getAbsoluteY());
}
}
}
motion.start();
}
}
use of com.codename1.ui.animations.Transition in project CodenameOne by codenameone.
the class CommonTransitions method paintSlideAtPosition.
private void paintSlideAtPosition(Graphics g, int slideX, int slideY) {
Component source = getSource();
// if this is the first form we can't do a slide transition since we have no source form
if (source == null) {
return;
}
Component dest = getDestination();
int w = source.getWidth();
int h = source.getHeight();
if (slideType == SLIDE_HORIZONTAL) {
h = 0;
} else {
w = 0;
}
boolean dir = forward;
if (dest != null && dest.getUIManager().getLookAndFeel().isRTL() && slideType == SLIDE_HORIZONTAL) {
dir = !dir;
}
if (dir) {
w = -w;
h = -h;
} else {
slideX = -slideX;
slideY = -slideY;
}
g.setClip(source.getAbsoluteX() + source.getScrollX(), source.getAbsoluteY() + source.getScrollY(), source.getWidth(), source.getHeight());
// dialog animation is slightly different...
if (source instanceof Dialog) {
if (buffer != null) {
g.drawImage(buffer, 0, 0);
} else {
paint(g, dest, 0, 0);
}
paint(g, source, -slideX, -slideY);
return;
}
if (dest instanceof Dialog) {
if (buffer != null) {
g.drawImage(buffer, 0, 0);
} else {
paint(g, source, 0, 0);
}
paint(g, dest, -slideX - w, -slideY - h);
return;
}
if (source.getParent() != null || buffer == null) {
source.paintBackgrounds(g);
paint(g, source, slideX, slideY);
} else {
g.drawImage(buffer, slideX, slideY);
}
paint(g, dest, slideX + w, slideY + h);
}
use of com.codename1.ui.animations.Transition in project CodenameOne by codenameone.
the class FlipTransition method paint.
@Override
public void paint(Graphics g) {
// this can happen if a transition is cut short
if (destBuffer == null) {
return;
}
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();
int x = getSource().getAbsoluteX();
int y = getSource().getAbsoluteY();
int w = getSource().getWidth();
int h = getSource().getHeight();
g.setClip(x, y, w, h);
if (getBgColor() >= 0) {
int c = g.getColor();
g.setColor(getBgColor());
g.fillRect(x, y, w, h);
g.setColor(c);
} else {
getSource().paintBackgrounds(g);
}
if (g.isPerspectiveTransformSupported()) {
float displayH = Display.getInstance().getDisplayHeight();
float displayW = Display.getInstance().getDisplayWidth();
double midX = (float) x + (float) w / 2.0;
if (perspectiveT == null) {
perspectiveT = Transform.makeIdentity();
}
makePerspectiveTransform(perspectiveT);
float[] bottomRight = perspectiveT.transformPoint(new float[] { displayW, displayH, zNear });
if (currTransform == null) {
currTransform = Transform.makeTranslation(0, 0, 0);
} else {
currTransform.setIdentity();
}
float xfactor = -displayW / bottomRight[0];
float yfactor = -displayH / bottomRight[1];
currTransform.scale(xfactor, yfactor, 0f);
currTransform.translate((x + w / 2) / xfactor, (y + h / 2) / yfactor, 0);
currTransform.concatenate(perspectiveT);
float cameraZ = -zNear - w / 2 * zState;
float cameraX = -x - w / 2;
float cameraY = -y - h / 2;
currTransform.translate(cameraX, cameraY, cameraZ);
if (transitionState == STATE_FLIP) {
currTransform.translate((float) midX, y, 0);
}
Image img = null;
if (flipState < 0.5) {
img = sourceBuffer;
if (transitionState == STATE_FLIP) {
// We are showing the front image
// We will rotate it up to 90 degrees
// 0 -> 0 degrees
// 0.5 -> 90 degress
double sin = flipState * 2.0;
double angle = MathUtil.asin(sin);
// rotate about y axis
currTransform.rotate((float) angle, 0, 1, 0);
}
} else {
img = destBuffer;
if (transitionState == STATE_FLIP) {
// We are showing the back image
// We are showing the back of the image
// We will rotate it from 90 degrees back to 0
// 0.5 -> 90 degrees
// 1.0 -> 0 degrees
double sin = (1.0 - flipState) * 2.0;
double angle = Math.PI - MathUtil.asin(sin);
// rotate about y axis
currTransform.rotate((float) angle, 0, 1, 0);
}
}
if (transitionState == STATE_FLIP) {
currTransform.translate(-(float) midX, -y, 0);
if (flipState >= 0.5f) {
// The rotation will leave the destination image flipped
// backwards, so we need to transform it to be the
// mirror image
currTransform.scale(-1, 1, 1);
currTransform.translate(-2 * x - w, 0, 0);
}
}
if (tmpTransform == null) {
tmpTransform = Transform.makeIdentity();
}
g.getTransform(tmpTransform);
g.setTransform(currTransform);
g.drawImage(img, x, y, w, h);
g.setTransform(tmpTransform);
} else {
perspectiveSupported = false;
if (flipState < 0.5) {
int frontX = x + (int) (flipState * (float) w);
int frontWidth = (int) ((float) w * (1.0 - flipState * 2.0));
g.drawImage(sourceBuffer, frontX, y, frontWidth, h);
} else {
double backState = 1.0 - flipState;
int backX = x + (int) (backState * (float) w);
int backWidth = (int) ((float) w * (1.0 - backState * 2.0));
g.drawImage(destBuffer, backX, y, backWidth, h);
}
}
g.setClip(cx, cy, cw, ch);
}
use of com.codename1.ui.animations.Transition in project CodenameOne by codenameone.
the class SwipeBackSupport method bind.
/**
* Binds support for swiping to the given forms
*
* @param currentForm the current form
* @param destination the destination form which can be created lazily
*/
protected void bind(final Form currentForm, final LazyValue<Form> destination) {
pointerDragged = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (sideSwipePotential) {
final int x = evt.getX();
final int y = evt.getY();
if (Math.abs(y - initialDragY) > x - initialDragX) {
sideSwipePotential = false;
return;
}
evt.consume();
if (dragActivated) {
currentX = x;
Display.getInstance().getCurrent().repaint();
} else {
if (x - initialDragX > Display.getInstance().convertToPixels(currentForm.getUIManager().getThemeConstant("backGestureThresholdInt", 5), true)) {
dragActivated = true;
destinationForm = destination.get();
startBackTransition(currentForm, destinationForm);
}
}
}
}
};
pointerReleased = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (dragActivated) {
int destNumberX = Display.getInstance().getDisplayWidth();
int incrementsX = Display.getInstance().convertToPixels(3, true);
if (currentX < destNumberX / 2) {
destinationForm = currentForm;
destNumberX = 0;
incrementsX *= -1;
}
final int destNumber = destNumberX;
final int increments = incrementsX;
Display.getInstance().getCurrent().registerAnimated(new Animation() {
public boolean animate() {
currentX += increments;
if (currentX > 0 && currentX >= destNumber || currentX < 0 && currentX <= destNumber) {
currentX = destNumber;
Transition t = destinationForm.getTransitionInAnimator();
destinationForm.setTransitionInAnimator(CommonTransitions.createEmpty());
destinationForm.show();
destinationForm.setTransitionInAnimator(t);
destinationForm = null;
dragActivated = false;
return false;
}
return true;
}
public void paint(Graphics g) {
}
});
}
}
};
pointerPressed = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sideSwipePotential = false;
int displayWidth = Display.getInstance().getDisplayWidth();
sideSwipePotential = !transitionRunning && evt.getX() < displayWidth / currentForm.getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
initialDragX = evt.getX();
initialDragY = evt.getY();
/*if (sideSwipePotential) {
Component c = Display.getInstance().getCurrent().getComponentAt(initialDragX, initialDragY);
if (c != null && c.shouldBlockSideSwipe()) {
sideSwipePotential = false;
}
}*/
}
};
currentForm.addPointerDraggedListener(pointerDragged);
currentForm.addPointerReleasedListener(pointerReleased);
currentForm.addPointerPressedListener(pointerPressed);
}
use of com.codename1.ui.animations.Transition in project CodenameOne by codenameone.
the class IOSImplementation method foldKeyboard.
public static void foldKeyboard() {
if (instance.isAsyncEditMode()) {
final Component cmp = Display.getInstance().getCurrent().getFocused();
instance.callHideTextEditor();
nativeInstance.foldVKB();
// after folding the keyboard the screen layout might shift
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (cmp != null) {
Form f = Display.getInstance().getCurrent();
if (f == cmp.getComponentForm()) {
cmp.requestFocus();
}
if (nativeInstance.isAsyncEditMode() && f.isFormBottomPaddingEditingMode() && getRootPane(f).getUnselectedStyle().getPaddingBottom() > 0) {
getRootPane(f).getUnselectedStyle().setPadding(Component.BOTTOM, 0);
f.forceRevalidate();
return;
}
// revalidate even if we transition to a different form since the
// spacing might have remained during the transition
f.revalidate();
}
}
});
}
}
Aggregations