Search in sources :

Example 46 with Graphics

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

the class SideMenuBar method createMenu.

private Form createMenu(final String placement) {
    final Form m = new Form() {

        private boolean pressedInRightPanel;

        private boolean manualMotionLock;

        protected boolean shouldSendPointerReleaseToOtherForm() {
            return true;
        }

        void actionCommandImpl(Command cmd, ActionEvent ev) {
            if (cmd instanceof SideMenuBar.CommandWrapper) {
                cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
                ev = new ActionEvent(cmd, ActionEvent.Type.Command);
            }
            final Command c = cmd;
            final ActionEvent e = ev;
            Display.getInstance().scheduleBackgroundTask(new Runnable() {

                public void run() {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (Display.getInstance().getCurrent() != parent) {
                                try {
                                    Thread.sleep(40);
                                } catch (Exception ex) {
                                }
                            }
                        }
                    });
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            parent.actionCommandImpl(c, e);
                        }
                    });
                }
            });
        }

        protected void sizeChanged(int w, int h) {
            Style formStyle = getStyle();
            int width = w - (formStyle.getHorizontalMargins());
            parent.sizeChangedInternal(w, h);
            // close the menu
            if (getWidth() != width) {
                closeMenu();
            }
            super.sizeChanged(w, h);
        }

        public void pointerPressed(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerPressed(x, y);
            if (rightPanel.contains(x, y)) {
                pressedInRightPanel = true;
            }
        }

        public void pointerDragged(int[] x, int[] y) {
            if (manualMotionLock) {
                return;
            }
            if (!transitionRunning && pressedInRightPanel) {
                dragActivated = true;
                pressedInRightPanel = false;
            }
            if (dragActivated) {
                setMenuGlassPane(menu, placement);
                draggedX = x[0];
                repaint();
                return;
            }
            super.pointerDragged(x, y);
        }

        public void pointerReleased(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerReleased(x, y);
            boolean isRTLValue = isRTL();
            if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                isRTLValue = !isRTLValue;
            }
            int displayWidth = Display.getInstance().getDisplayWidth();
            if (isRTLValue) {
                if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            } else {
                if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            }
            if (dragActivated || rightPanel.contains(x, y)) {
                setMenuGlassPane(menu, placement);
                draggedX = x;
                int start = x;
                int end = 0;
                if (isRTLValue) {
                    end = getWidth();
                }
                final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
                motion.start();
                manualMotionLock = true;
                sideSwipePotential = false;
                rightSideSwipePotential = false;
                topSwipePotential = false;
                registerAnimated(new Animation() {

                    public boolean animate() {
                        draggedX = motion.getValue();
                        if (motion.isFinished()) {
                            dragActivated = false;
                        }
                        return true;
                    }

                    public void paint(Graphics g) {
                        repaint();
                        if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
                            parent.setTransitionInAnimator(CommonTransitions.createEmpty());
                            parent.show();
                            deregisterAnimated(this);
                            Display.getInstance().callSerially(new Runnable() {

                                public void run() {
                                    clean();
                                }
                            });
                        }
                    }
                });
            }
        }

        public void keyReleased(int keyCode) {
            if (keyCode == leftSK) {
                if (transitionRunning) {
                    return;
                }
                closeMenu();
                return;
            }
            super.keyReleased(keyCode);
        }
    };
    m.setScrollable(false);
    m.removeComponentFromForm(m.getTitleArea());
    m.putClientProperty("Menu", "true");
    m.setTransitionInAnimator(CommonTransitions.createEmpty());
    m.setTransitionOutAnimator(CommonTransitions.createEmpty());
    m.setBackCommand(new Command("") {

        public void actionPerformed(ActionEvent evt) {
            if (transitionRunning) {
                return;
            }
            closeMenu();
        }
    });
    m.setLayout(new BorderLayout());
    if (Display.getInstance().areMutableImagesFast()) {
        rightPanel = new Container(new BorderLayout());
    } else {
        rightPanel = new Container(new BorderLayout()) {

            public void paintBackground(Graphics g) {
            }

            public void paintBackgrounds(Graphics g) {
            }

            public void paint(Graphics g) {
                Component c = (Component) rightPanel.getClientProperty("$parent");
                // not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
                if (c != null) {
                    boolean b = c.isVisible();
                    c.setVisible(true);
                    int x = getAbsoluteX();
                    g.translate(x, 0);
                    Container.sidemenuBarTranslation = x;
                    c.paintComponent(g, true);
                    Container.sidemenuBarTranslation = 0;
                    g.translate(-x, 0);
                    c.setVisible(b);
                }
            }
        };
    }
    if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
        int v = 0;
        if (Display.getInstance().isPortrait()) {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 2 / 3;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
                if (v < 0) {
                    v = openButton.getHeight();
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        } else {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 4 / 10;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        }
        rightPanel.setPreferredH(v);
    } else {
        if (Display.getInstance().isPortrait()) {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 2 / 3;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
                if (v < 0) {
                    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                        if (isRTL()) {
                            v = openButton.getWidth();
                        } else {
                            v = rightSideButton.getWidth();
                        }
                    } else {
                        v = openButton.getWidth();
                    }
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        } else {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 4 / 10;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        }
    }
    if (sidePanel != null) {
        sidePanel.removeAll();
        sidePanel = null;
    }
    sidePanel = createSideNavigationComponent(getCommands(), placement);
    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
        m.addComponent(BorderLayout.WEST, rightPanel);
        m.addComponent(BorderLayout.CENTER, sidePanel);
    } else {
        if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
            m.addComponent(BorderLayout.NORTH, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
            Button button = new Button(" ");
            button.setUIID("Container");
            button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
            m.addComponent(BorderLayout.SOUTH, button);
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    closeMenu();
                }
            });
        } else {
            m.addComponent(BorderLayout.EAST, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
        }
    }
    m.putClientProperty("cn1$sideMenuParent", this);
    return m;
}
Also used : Motion(com.codename1.ui.animations.Motion) ActionEvent(com.codename1.ui.events.ActionEvent) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Style(com.codename1.ui.plaf.Style) Animation(com.codename1.ui.animations.Animation)

Example 47 with Graphics

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

Example 48 with Graphics

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

the class CodenameOneImplementation method paintDirty.

/**
 * Invoked by the EDT to paint the dirty regions
 */
public void paintDirty() {
    int size = 0;
    synchronized (displayLock) {
        size = paintQueueFill;
        Animation[] array = paintQueue;
        paintQueue = paintQueueTemp;
        paintQueueTemp = array;
        paintQueueFill = 0;
    }
    if (size > 0) {
        Graphics wrapper = getCodenameOneGraphics();
        int dwidth = getDisplayWidth();
        int dheight = getDisplayHeight();
        int topX = dwidth;
        int topY = dheight;
        int bottomX = 0;
        int bottomY = 0;
        for (int iter = 0; iter < size; iter++) {
            Animation ani = paintQueueTemp[iter];
            // might happen due to paint queue removal
            if (ani == null) {
                continue;
            }
            paintQueueTemp[iter] = null;
            wrapper.translate(-wrapper.getTranslateX(), -wrapper.getTranslateY());
            wrapper.setClip(0, 0, dwidth, dheight);
            if (ani instanceof Component) {
                Component cmp = (Component) ani;
                Rectangle dirty = cmp.getDirtyRegion();
                if (dirty != null) {
                    Dimension d = dirty.getSize();
                    wrapper.setClip(dirty.getX(), dirty.getY(), d.getWidth(), d.getHeight());
                    cmp.setDirtyRegion(null);
                }
                cmp.paintComponent(wrapper);
                getPaintableBounds(cmp, paintDirtyTmpRect);
                int cmpAbsX = paintDirtyTmpRect.getX();
                topX = Math.min(cmpAbsX, topX);
                bottomX = Math.max(cmpAbsX + paintDirtyTmpRect.getWidth(), bottomX);
                int cmpAbsY = paintDirtyTmpRect.getY();
                topY = Math.min(cmpAbsY, topY);
                bottomY = Math.max(cmpAbsY + paintDirtyTmpRect.getHeight(), bottomY);
            } else {
                bottomX = dwidth;
                bottomY = dheight;
                topX = 0;
                topY = 0;
                ani.paint(wrapper);
            }
        }
        paintOverlay(wrapper);
        // Log.p("Flushing graphics : "+topX+","+topY+","+bottomX+","+bottomY);
        flushGraphics(topX, topY, bottomX - topX, bottomY - topY);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Animation(com.codename1.ui.animations.Animation) Dimension(com.codename1.ui.geom.Dimension)

Example 49 with Graphics

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

the class CodenameOneImplementation method clipRect.

/**
 * Changes the current clipping rectangle to subset the current clipping with
 * the given clipping.
 *
 * @param graphics the graphics context
 * @param rect rectangle representing the new clipping area
 */
public void clipRect(Object graphics, Rectangle rect) {
    Dimension d = rect.getSize();
    clipRect(graphics, rect.getX(), rect.getY(), d.getWidth(), d.getHeight());
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 50 with Graphics

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

the class OnOffSwitch method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    if (iosMode) {
        int switchButtonPadInt = UIManager.getInstance().getThemeConstant("switchButtonPadInt", 16);
        if (Display.getInstance().getDisplayWidth() > 480) {
            // is retina
            switchButtonPadInt *= 2;
        }
        Style s = getStyle();
        int x = getX() + s.getPaddingLeftNoRTL();
        int y = getY() + s.getPaddingTop();
        if (!value) {
            if (deltaX > 0) {
                dragged = false;
            } else {
                if (deltaX < -switchOnImage.getWidth()) {
                    deltaX = -switchOnImage.getWidth();
                }
            }
        } else {
            if (deltaX < 0) {
                dragged = false;
            } else {
                if (deltaX > switchOnImage.getWidth()) {
                    deltaX = switchOnImage.getWidth();
                }
            }
        }
        if (dragged) {
            int onX;
            int offX;
            if (value) {
                onX = x - deltaX;
                offX = x - deltaX + switchOnImage.getWidth() - 2 * switchButtonPadInt;
            } else {
                onX = x - deltaX - switchOnImage.getWidth() + 2 * switchButtonPadInt;
                offX = x - deltaX;
            }
            switchButtonPadInt /= 2;
            int oldClipX = g.getClipX();
            int oldClipY = g.getClipY();
            int oldClipW = g.getClipWidth();
            int oldClipH = g.getClipHeight();
            g.clipRect(getX(), getY(), switchMaskImage.getWidth(), switchMaskImage.getHeight());
            g.drawImage(switchOnImage, onX, y);
            g.drawImage(switchOffImage, offX, y);
            int strWidth = s.getFont().stringWidth(on);
            int sX = onX + switchMaskImage.getWidth() / 2 - strWidth / 2 - switchButtonPadInt;
            int sY = y + switchMaskImage.getHeight() / 2 - s.getFont().getHeight() / 2;
            g.setFont(s.getFont());
            g.setColor(0xffffff);
            if (!noTextMode) {
                g.drawString(on, sX, sY, Style.TEXT_DECORATION_3D);
            }
            strWidth = s.getFont().stringWidth(off);
            g.setColor(0x333333);
            sX = offX + switchMaskImage.getWidth() / 2 - strWidth / 2 + switchButtonPadInt;
            if (!noTextMode) {
                g.drawString(off, sX, sY);
            }
            g.setClip(oldClipX, oldClipY, oldClipW, oldClipH);
        } else {
            String str;
            switchButtonPadInt /= 2;
            if (value) {
                g.drawImage(switchOnImage, x, y);
                str = on;
                g.setColor(0xffffff);
                switchButtonPadInt *= -1;
            } else {
                g.drawImage(switchOffImage, x, y);
                str = off;
                g.setColor(0x333333);
            }
            int strWidth = s.getFont().stringWidth(str);
            int sX = x + switchMaskImage.getWidth() / 2 - strWidth / 2 + switchButtonPadInt;
            int sY = y + switchMaskImage.getHeight() / 2 - s.getFont().getHeight() / 2;
            g.setFont(s.getFont());
            if (!noTextMode) {
                g.drawString(str, sX, sY);
            }
        }
        g.drawImage(switchMaskImage, x, y);
    } else {
        super.paint(g);
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Image (com.codename1.ui.Image)18 Component (com.codename1.ui.Component)17 Point (com.codename1.ui.geom.Point)16 Style (com.codename1.ui.plaf.Style)15 Graphics (com.codename1.ui.Graphics)14 Form (com.codename1.ui.Form)12 Font (com.codename1.ui.Font)11 GeneralPath (com.codename1.ui.geom.GeneralPath)9 Rectangle (com.codename1.ui.geom.Rectangle)9 Animation (com.codename1.ui.animations.Animation)8 Dialog (com.codename1.ui.Dialog)7 Dimension (com.codename1.ui.geom.Dimension)6 Painter (com.codename1.ui.Painter)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 FontImage (com.codename1.ui.FontImage)4 RGBImage (com.codename1.ui.RGBImage)4 Motion (com.codename1.ui.animations.Motion)4 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 IOException (java.io.IOException)4