Search in sources :

Example 76 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class Toolbar method bindScrollListener.

private void bindScrollListener(boolean bind) {
    final Form f = getComponentForm();
    if (f != null) {
        final Container actualPane = f.getActualPane();
        final Container contentPane = f.getContentPane();
        if (bind) {
            initVars(actualPane);
            scrollListener = new ScrollListener() {

                public void scrollChanged(int scrollX, int scrollY, int oldscrollX, int oldscrollY) {
                    int diff = scrollY - oldscrollY;
                    int toolbarNewY = getY() - diff;
                    if (scrollY < 0 || Math.abs(toolbarNewY) < 2) {
                        return;
                    }
                    toolbarNewY = Math.max(toolbarNewY, -getHeight());
                    toolbarNewY = Math.min(toolbarNewY, initialY);
                    if (toolbarNewY != getY()) {
                        setY(toolbarNewY);
                        if (!layered) {
                            actualPane.setY(actualPaneInitialY + toolbarNewY);
                            actualPane.setHeight(actualPaneInitialH + getHeight() - toolbarNewY);
                            actualPane.doLayout();
                        }
                        f.repaint();
                    }
                }
            };
            contentPane.addScrollListener(scrollListener);
            releasedListener = new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    if (getY() + getHeight() / 2 > 0) {
                        showToolbar();
                    } else {
                        hideToolbar();
                    }
                    f.repaint();
                }
            };
            contentPane.addPointerReleasedListener(releasedListener);
        } else {
            if (scrollListener != null) {
                contentPane.removeScrollListener(scrollListener);
                contentPane.removePointerReleasedListener(releasedListener);
            }
        }
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) ScrollListener(com.codename1.ui.events.ScrollListener)

Example 77 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class CommonTransitions method paintAlpha.

private void paintAlpha(Graphics graphics) {
    Component src = getSource();
    int w = src.getWidth();
    int h = src.getHeight();
    int position = this.position;
    if (position > 255) {
        position = 255;
    } else {
        if (position < 0) {
            position = 0;
        }
    }
    // for slow mutable images
    if (buffer == null) {
        Component dest = getDestination();
        Component srcCmp = src;
        Component destCmp = dest;
        int alpha = position;
        if (src instanceof Dialog && dest instanceof Form) {
            srcCmp = dest;
            destCmp = src;
            alpha = 255 - position;
        }
        paint(graphics, srcCmp, 0, 0);
        destCmp.setX(srcCmp.getX());
        destCmp.setY(srcCmp.getY());
        destCmp.setWidth(srcCmp.getWidth());
        destCmp.setHeight(srcCmp.getHeight());
        graphics.setAlpha(alpha);
        paint(graphics, destCmp, 0, 0);
        graphics.setAlpha(255);
        return;
    }
    // this will always be invoked on the EDT so there is no race condition risk
    if (rgbBuffer != null || secondaryBuffer != null) {
        if (secondaryBuffer != null) {
            Component dest = getDestination();
            int x = dest.getAbsoluteX();
            int y = dest.getAbsoluteY();
            graphics.drawImage(buffer, x, y);
            graphics.setAlpha(position);
            graphics.drawImage(secondaryBuffer, x, y);
            graphics.setAlpha(0xff);
        } else {
            int alpha = position << 24;
            int[] bufferArray = rgbBuffer.getRGB();
            int size = bufferArray.length;
            for (int iter = 0; iter < size; iter++) {
                bufferArray[iter] = ((bufferArray[iter] & 0xFFFFFF) | alpha);
            }
            Component dest = getDestination();
            int x = dest.getAbsoluteX();
            int y = dest.getAbsoluteY();
            graphics.drawImage(buffer, x, y);
            graphics.drawImage(rgbBuffer, x, y);
        }
    }
}
Also used : Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Example 78 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class CommonTransitions method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    try {
        switch(transitionType) {
            case TYPE_FAST_SLIDE:
            case TYPE_SLIDE:
                // if this is an up or down slide
                if (slideType == SLIDE_HORIZONTAL) {
                    paintSlideAtPosition(g, position, 0);
                } else {
                    paintSlideAtPosition(g, 0, position);
                }
                return;
            case TYPE_UNCOVER:
                int p = motion.getDestinationValue() - position;
                if (slideType == SLIDE_HORIZONTAL) {
                    paintCoverAtPosition(g, p, 0);
                } else {
                    paintCoverAtPosition(g, 0, p);
                }
                return;
            case TYPE_COVER:
                if (slideType == SLIDE_HORIZONTAL) {
                    paintCoverAtPosition(g, position, 0);
                } else {
                    paintCoverAtPosition(g, 0, position);
                }
                return;
            case TYPE_FADE:
                paintAlpha(g);
                return;
            case TYPE_TIMELINE:
                Object mask = timeline.createMask();
                paint(g, getSource(), 0, 0);
                g.drawImage(buffer.applyMask(mask), 0, 0);
                return;
            case TYPE_SLIDE_AND_FADE:
                {
                    Form sourceForm = (Form) getSource();
                    Form destForm = (Form) getDestination();
                    int alpha = position;
                    int slidePos = motion2.getValue();
                    int clipX = g.getClipX();
                    int clipY = g.getClipY();
                    int clipW = g.getClipWidth();
                    int clipH = g.getClipHeight();
                    if (clipW <= 0 || clipH <= 0) {
                        return;
                    }
                    g.translate(0, sourceForm.getTitleArea().getHeight());
                    Container sourcePane = ((Form) getSource()).getContentPane();
                    Container destPane = ((Form) getDestination()).getContentPane();
                    boolean dir = forward;
                    if (sourceForm != null && sourceForm.getUIManager().getLookAndFeel().isRTL()) {
                        dir = !dir;
                    }
                    if (dir) {
                        g.translate(slidePos, 0);
                        paint(g, sourcePane, -sourcePane.getAbsoluteX() - sourcePane.getScrollX(), -sourcePane.getAbsoluteY() - sourcePane.getScrollY(), true);
                        g.translate(-destPane.getWidth(), 0);
                        paint(g, destPane, -destPane.getAbsoluteX() - destPane.getScrollX(), -destPane.getAbsoluteY() - destPane.getScrollY(), true);
                        g.translate(destPane.getWidth() - slidePos, 0);
                    } else {
                        g.translate(-slidePos, 0);
                        paint(g, sourcePane, -sourcePane.getAbsoluteX() - sourcePane.getScrollX(), -sourcePane.getAbsoluteY() - sourcePane.getScrollY(), true);
                        g.translate(destPane.getWidth(), 0);
                        paint(g, destPane, -destPane.getAbsoluteX() - destPane.getScrollX(), -destPane.getAbsoluteY() - destPane.getScrollY(), true);
                        g.translate(slidePos - destPane.getWidth(), 0);
                    }
                    g.translate(0, -sourceForm.getTitleArea().getHeight());
                    g.setClip(clipX, clipY, clipW, clipH);
                    sourceForm.getTitleArea().paintComponentBackground(g);
                    paintShiftFadeHierarchy(sourceForm.getTitleArea(), 255 - alpha, g, false);
                    paintShiftFadeHierarchy(destForm.getTitleArea(), alpha, g, true);
                    return;
                }
            case TYPE_PULSATE_DIALOG:
                paint(g, getSource(), 0, 0);
                int alpha = g.getAlpha();
                if (motion2 != null) {
                    g.setAlpha(motion2.getValue());
                }
                Component c = getDialogParent(getDestination());
                float ratio = ((float) position) / 1000.0f;
                if (g.isAffineSupported()) {
                    g.scale(ratio, ratio);
                    int w = (int) (originalWidth * ratio);
                    int h = (int) (originalHeight * ratio);
                    c.setX(originalX + ((originalWidth - w) / 2));
                    c.setY(originalY + ((originalHeight - h) / 2));
                    int currentDlgX = getDialogParent(getDestination()).getX();
                    int currentDlgY = getDialogParent(getDestination()).getY();
                    g.drawImage(buffer, currentDlgX, currentDlgY);
                    // paint(g, c, 0, 0);
                    g.resetAffine();
                } else {
                    c.setWidth((int) (originalWidth * ratio));
                    c.setHeight((int) (originalHeight * ratio));
                    c.setX(originalX + ((originalWidth - c.getWidth()) / 2));
                    c.setY(originalY + ((originalHeight - c.getHeight()) / 2));
                    paint(g, c, 0, 0);
                }
                g.setAlpha(alpha);
                return;
        }
    } catch (Throwable t) {
        Log.p("An exception occurred during transition paint this might be valid in case of a resize in the middle of a transition");
        Log.e(t);
    }
}
Also used : Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) Component(com.codename1.ui.Component)

Example 79 with Form

use of com.codename1.ui.Form 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();
    }
}
Also used : Graphics(com.codename1.ui.Graphics) Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component) RGBImage(com.codename1.ui.RGBImage) Display(com.codename1.ui.Display)

Example 80 with Form

use of com.codename1.ui.Form 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);
}
Also used : Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

Aggregations

Form (com.codename1.ui.Form)90 ActionEvent (com.codename1.ui.events.ActionEvent)41 Component (com.codename1.ui.Component)38 BorderLayout (com.codename1.ui.layouts.BorderLayout)38 Container (com.codename1.ui.Container)26 ActionListener (com.codename1.ui.events.ActionListener)25 Dialog (com.codename1.ui.Dialog)21 Command (com.codename1.ui.Command)19 Hashtable (java.util.Hashtable)17 Label (com.codename1.ui.Label)14 Style (com.codename1.ui.plaf.Style)14 IOException (java.io.IOException)14 TextArea (com.codename1.ui.TextArea)13 Vector (java.util.Vector)12 Button (com.codename1.ui.Button)11 Graphics (com.codename1.ui.Graphics)9 RadioButton (com.codename1.ui.RadioButton)9 Animation (com.codename1.ui.animations.Animation)9 Image (com.codename1.ui.Image)8 UIManager (com.codename1.ui.plaf.UIManager)8