Search in sources :

Example 36 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class GameCanvasImplementation method captureVideo.

public void captureVideo(ActionListener response) {
    captureResponse = response;
    try {
        final Form current = Display.getInstance().getCurrent();
        final MMAPIPlayer player = MMAPIPlayer.createPlayer("capture://video", null);
        RecordControl record = (RecordControl) player.nativePlayer.getControl("RecordControl");
        if (record == null) {
            player.cleanup();
            throw new RuntimeException("Capture Video is not supported on this device");
        }
        final Form cam = new Form();
        cam.setTransitionInAnimator(CommonTransitions.createEmpty());
        cam.setTransitionOutAnimator(CommonTransitions.createEmpty());
        cam.setLayout(new BorderLayout());
        cam.show();
        MIDPVideoComponent video = new MIDPVideoComponent(player, canvas);
        video.play();
        video.setVisible(true);
        cam.addComponent(BorderLayout.CENTER, video);
        final Label time = new Label("0:00");
        cam.addComponent(BorderLayout.SOUTH, time);
        cam.revalidate();
        ActionListener l = new ActionListener() {

            boolean recording = false;

            OutputStream out = null;

            String videoPath = null;

            RecordControl record;

            public void actionPerformed(ActionEvent evt) {
                if (!recording) {
                    record = (RecordControl) player.nativePlayer.getControl("RecordControl");
                    recording = true;
                    String type = record.getContentType();
                    String prefix = "";
                    if (type.endsWith("mpeg")) {
                        prefix = ".mpeg";
                    } else if (type.endsWith("4")) {
                        prefix = ".mp4";
                    } else if (type.endsWith("3gpp")) {
                        prefix = ".3gp";
                    } else if (type.endsWith("avi")) {
                        prefix = ".avi";
                    }
                    videoPath = getOutputMediaFile() + prefix;
                    try {
                        out = FileSystemStorage.getInstance().openOutputStream(videoPath);
                        record.setRecordStream(out);
                        record.startRecord();
                        cam.registerAnimated(new Animation() {

                            long current = System.currentTimeMillis();

                            long zero = current;

                            int sec = 0;

                            public boolean animate() {
                                long now = System.currentTimeMillis();
                                if (now - current > 1000) {
                                    current = now;
                                    sec++;
                                    return true;
                                }
                                return false;
                            }

                            public void paint(Graphics g) {
                                String txt = sec / 60 + ":" + sec % 60;
                                time.setText(txt);
                            }
                        });
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        System.out.println("failed to store video to " + videoPath);
                    } finally {
                    }
                } else {
                    if (out != null) {
                        try {
                            record.stopRecord();
                            record.commit();
                            out.close();
                            player.cleanup();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    captureResponse.actionPerformed(new ActionEvent(videoPath));
                    current.showBack();
                }
            }
        };
        cam.addGameKeyListener(Display.GAME_FIRE, l);
        cam.addPointerReleasedListener(l);
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException("failed to start camera");
    }
}
Also used : Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Graphics(com.codename1.ui.Graphics) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Animation(com.codename1.ui.animations.Animation) RecordControl(javax.microedition.media.control.RecordControl)

Example 37 with Animation

use of com.codename1.ui.animations.Animation in project CodenameOne by codenameone.

the class BlackBerryImplementation method nativeEdit.

public void nativeEdit(final Component cmp, final int maxSize, final int constraint, String text, int keyCode) {
    if (nativeEdit != null) {
        finishEdit(true);
    }
    lightweightEdit = (TextArea) cmp;
    if (keyCode > 0 && getKeyboardType() == Display.KEYBOARD_TYPE_QWERTY) {
        // if this is a number
        if ((constraint & TextArea.DECIMAL) == TextArea.DECIMAL || (constraint & TextArea.NUMERIC) == TextArea.NUMERIC || (constraint & TextArea.PHONENUMBER) == TextArea.PHONENUMBER) {
            if (keyCode == 119) {
                text += "1";
            } else if (keyCode == 101) {
                text += "2";
            } else if (keyCode == 114) {
                text += "3";
            } else if (keyCode == 115) {
                text += "4";
            } else if (keyCode == 100) {
                text += "5";
            } else if (keyCode == 102) {
                text += "6";
            } else if (keyCode == 122) {
                text += "7";
            } else if (keyCode == 120) {
                text += "8";
            } else if (keyCode == 99) {
                text += "9";
            }
        } else {
            text += ((char) keyCode);
        }
        lightweightEdit.setText(text);
    }
    class LightweightEdit implements Runnable, Animation {

        public void run() {
            long type = 0;
            TextArea lightweightEditTmp = lightweightEdit;
            if (lightweightEditTmp == null) {
                return;
            }
            int constraint = lightweightEditTmp.getConstraint();
            if ((constraint & TextArea.DECIMAL) == TextArea.DECIMAL) {
                type = BasicEditField.FILTER_REAL_NUMERIC;
            } else if ((constraint & TextArea.EMAILADDR) == TextArea.EMAILADDR) {
                type = BasicEditField.FILTER_EMAIL;
            } else if ((constraint & TextArea.NUMERIC) == TextArea.NUMERIC) {
                type = BasicEditField.FILTER_NUMERIC;
            }
            if ((constraint & TextArea.PHONENUMBER) == TextArea.PHONENUMBER) {
                type = BasicEditField.FILTER_PHONE;
            }
            if ((constraint & TextArea.NON_PREDICTIVE) == TextArea.NON_PREDICTIVE) {
                type |= BasicEditField.NO_COMPLEX_INPUT;
            }
            if (lightweightEditTmp.isSingleLineTextArea()) {
                type |= BasicEditField.NO_NEWLINE;
            }
            if ((constraint & TextArea.PASSWORD) == TextArea.PASSWORD) {
                nativeEdit = new BBPasswordEditField(lightweightEditTmp, type, maxSize);
            } else {
                nativeEdit = new BBEditField(lightweightEditTmp, type, maxSize);
            }
            nativeEdit.setEditable(true);
            Font f = nativeEdit.getFont();
            if (f.getHeight() > lightweightEditTmp.getStyle().getFont().getHeight()) {
                nativeEdit.setFont(f.derive(f.getStyle(), lightweightEditTmp.getStyle().getFont().getHeight()));
            }
            canvas.add(nativeEdit);
            nativeEdit.setCursorPosition(lightweightEditTmp.getText().length());
            try {
                nativeEdit.setFocus();
            } catch (Throwable t) {
            // no idea why this throws an exception sometimes
            // t.printStackTrace();
            }
        }

        public boolean animate() {
            BasicEditField ef = nativeEdit;
            Component lw = lightweightEdit;
            if (lw == null || lw.getComponentForm() != Display.getInstance().getCurrent()) {
                Display.getInstance().getCurrent().deregisterAnimated(this);
                finishEdit(false);
            } else {
                if (ef != null) {
                    if (ef.isDirty()) {
                        lw.repaint();
                    }
                }
            }
            return false;
        }

        public void paint(com.codename1.ui.Graphics g) {
        }
    }
    LightweightEdit lw = new LightweightEdit();
    Display.getInstance().getCurrent().registerAnimated(lw);
    Application.getApplication().invokeLater(lw);
}
Also used : TextArea(com.codename1.ui.TextArea) BasicEditField(net.rim.device.api.ui.component.BasicEditField) Font(net.rim.device.api.ui.Font) Graphics(net.rim.device.api.ui.Graphics) Animation(com.codename1.ui.animations.Animation) Component(com.codename1.ui.Component) PeerComponent(com.codename1.ui.PeerComponent)

Aggregations

Animation (com.codename1.ui.animations.Animation)13 Motion (com.codename1.ui.animations.Motion)8 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)7 Dimension (com.codename1.ui.geom.Dimension)7 Style (com.codename1.ui.plaf.Style)7 Form (com.codename1.ui.Form)6 Component (com.codename1.ui.Component)5 Graphics (com.codename1.ui.Graphics)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 ArrayList (java.util.ArrayList)5 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 FontImage (com.codename1.ui.FontImage)3 Transition (com.codename1.ui.animations.Transition)3 IOException (java.io.IOException)3 BufferedOutputStream (com.codename1.io.BufferedOutputStream)2 Dialog (com.codename1.ui.Dialog)2 Image (com.codename1.ui.Image)2 PeerComponent (com.codename1.ui.PeerComponent)2 AnimationObject (com.codename1.ui.animations.AnimationObject)2