Search in sources :

Example 1 with Point

use of com.codename1.ui.geom.Point in project codenameone-google-maps by codenameone.

the class MapContainer method addMarker.

/**
 * Adds a marker to the map with the given attributes
 * @param opts The marker options.
 * @return marker reference object that should be used when removing the marker
 */
public MapObject addMarker(MarkerOptions opts) {
    // public MapObject addMarker(EncodedImage icon, Coord location, String text, String longText, final ActionListener onClick) {
    EncodedImage icon = opts.getIcon();
    Coord location = opts.getLocation();
    String text = opts.getText();
    String longText = opts.getLongText();
    ActionListener onClick = opts.getOnClick();
    if (internalNative != null) {
        byte[] iconData = null;
        if (icon != null) {
            iconData = icon.getImageData();
            internalNative.setMarkerSize(icon.getWidth(), icon.getHeight());
        }
        long key = internalNative.addMarker(iconData, location.getLatitude(), location.getLongitude(), text, longText, onClick != null, opts.anchorU, opts.anchorV);
        MapObject o = new MapObject();
        o.mapKey = key;
        o.callback = onClick;
        markers.add(o);
        return o;
    } else {
        if (internalLightweightCmp != null) {
            PointLayer pl = new PointLayer(location, text, icon);
            if (points == null) {
                points = new PointsLayer();
                internalLightweightCmp.addLayer(points);
            }
            points.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    PointLayer point = (PointLayer) evt.getSource();
                    for (MapObject o : markers) {
                        if (o.point == point) {
                            if (o.callback != null) {
                                o.callback.actionPerformed(new ActionEvent(o));
                            }
                            evt.consume();
                            return;
                        }
                    }
                }
            });
            points.addPoint(pl);
            MapObject o = new MapObject();
            o.point = pl;
            o.callback = onClick;
            markers.add(o);
            internalLightweightCmp.revalidate();
            return o;
        } else {
            String uri = null;
            int iconWidth = 0;
            int iconHeight = 0;
            int anchorU = 0;
            int anchorV = 0;
            if (icon != null) {
                uri = WebBrowser.createDataURI(icon.getImageData(), "image/png");
                iconWidth = icon.getWidth();
                iconHeight = icon.getHeight();
                anchorU = (int) (icon.getWidth() * opts.anchorU);
                anchorV = (int) (icon.getHeight() * opts.anchorV);
            }
            // browserBridge.waitForReady();
            MapObject o = new MapObject();
            o.callback = onClick;
            o.pending = true;
            final String fUri = uri;
            final int fIconWidth = iconWidth;
            final int fIconHeight = iconHeight;
            final float fAnchorU = anchorU;
            final float fAnchorV = anchorV;
            browserBridge.ready(() -> {
                internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addMarker(${0}, ${1}, ${2}, ${3}, ${4}, ${5}, ${6}, ${7}, ${8}))", new Object[] { // long key = ((Double)browserBridge.bridge.call("addMarker", new Object[]{
                fUri, location.getLatitude(), location.getLongitude(), text, longText, fIconWidth, fIconHeight, fAnchorU, fAnchorV }, jsres -> {
                    o.mapKey = jsres.getInt();
                    o.pending = false;
                });
            });
            // MapObject o = new MapObject();
            // o.mapKey = res.getInt();
            // o.callback = onClick;
            markers.add(o);
            // System.out.println("MapKey added "+o.mapKey);
            return o;
        }
    }
}
Also used : Coord(com.codename1.maps.Coord) PointsLayer(com.codename1.maps.layers.PointsLayer) ActionListener(com.codename1.ui.events.ActionListener) PointLayer(com.codename1.maps.layers.PointLayer) ActionEvent(com.codename1.ui.events.ActionEvent) EncodedImage(com.codename1.ui.EncodedImage) Point(com.codename1.ui.geom.Point)

Example 2 with Point

use of com.codename1.ui.geom.Point in project codenameone-google-maps by codenameone.

the class MapLayout method layoutContainer.

@Override
public void layoutContainer(Container parent) {
    // if (true) {
    // return;
    // }
    int parentX = 0;
    int parentY = 0;
    for (Component current : parent) {
        Coord crd = (Coord) current.getClientProperty(COORD_KEY);
        Point p = (Point) current.getClientProperty(POINT_KEY);
        if (p == null) {
            p = map.getScreenCoordinate(crd);
            current.putClientProperty(POINT_KEY, p);
        }
        Float h = (Float) current.getClientProperty(HORIZONTAL_ALIGNMENT);
        if (h == null) {
            h = 0f;
        }
        Float v = (Float) current.getClientProperty(VERTICAL_ALIGNMENT);
        if (v == null) {
            v = 0f;
        }
        current.setSize(current.getPreferredSize());
        current.setX(convertX(p.getX() - parentX, current.getWidth(), h));
        current.setY(convertY(p.getY() - parentY, current.getHeight(), v));
    }
}
Also used : Coord(com.codename1.maps.Coord) Point(com.codename1.ui.geom.Point) Component(com.codename1.ui.Component) Point(com.codename1.ui.geom.Point)

Example 3 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawTextField.

/**
 * {@inheritDoc}
 */
public void drawTextField(Graphics g, TextArea ta) {
    setFG(g, ta);
    // display ******** if it is a password field
    String displayText = getTextFieldString(ta);
    Style style = ta.getStyle();
    int x = 0;
    // ta.getCursorX();
    int cursorCharPosition = ta.getCursorPosition();
    Font f = style.getFont();
    int cursorX = 0;
    int xPos = 0;
    int align = reverseAlignForBidi(ta);
    int displayX = 0;
    String inputMode = ta.getInputMode();
    int inputModeWidth = f.stringWidth(inputMode);
    // QWERTY devices don't quite have an input mode hide it also when we have a VK
    if (ta.isQwertyInput() || Display.getInstance().isVirtualKeyboardShowing()) {
        inputMode = "";
        inputModeWidth = 0;
    }
    if (ta.isSingleLineTextArea()) {
        // there is currently no support for CENTER aligned text fields
        if (align == Component.LEFT) {
            if (cursorCharPosition > 0) {
                cursorCharPosition = Math.min(displayText.length(), cursorCharPosition);
                xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
                cursorX = ta.getX() + style.getPaddingLeft(ta.isRTL()) + xPos;
                // no point in showing the input mode when there is only one input mode...
                if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length == 1) {
                    inputModeWidth = 0;
                }
                if (ta.isEnableInputScroll()) {
                    if (ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL())) {
                        if (x + xPos >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2) {
                            x = ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2 - xPos - 1;
                        }
                    }
                }
            }
            displayX = ta.getX() + x + style.getPaddingLeft(ta.isRTL());
        } else {
            x = 0;
            cursorX = getTextFieldCursorX(ta);
            int baseX = ta.getX() + style.getPaddingLeftNoRTL() + inputModeWidth;
            int endX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL();
            if (cursorX < baseX) {
                x = baseX - cursorX;
            } else {
                if (cursorX > endX) {
                    x = endX - cursorX;
                }
            }
            displayX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL() - f.stringWidth(displayText) + x;
        }
        int cx = g.getClipX();
        int cy = g.getClipY();
        int cw = g.getClipWidth();
        int ch = g.getClipHeight();
        int clipx = ta.getX() + style.getPaddingLeft(ta.isRTL());
        int clipw = ta.getWidth() - style.getPaddingLeft(ta.isRTL()) - style.getPaddingRight(ta.isRTL());
        // g.pushClip();
        g.clipRect(clipx, cy, clipw, ch);
        switch(ta.getVerticalAlignment()) {
            case Component.BOTTOM:
                g.drawString(displayText, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), style.getTextDecoration());
                break;
            case Component.CENTER:
                g.drawString(displayText, displayX, ta.getY() + ta.getHeight() / 2 - f.getHeight() / 2, style.getTextDecoration());
                break;
            default:
                g.drawString(displayText, displayX, ta.getY() + style.getPaddingTop(), style.getTextDecoration());
                break;
        }
        g.setClip(cx, cy, cw, ch);
    // g.popClip();
    } else {
        drawTextArea(g, ta);
    }
    // no point in showing the input mode when there is only one input mode...
    if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length > 1) {
        if (ta.handlesInput() && ta.getWidth() / 2 > inputModeWidth) {
            int drawXPos = ta.getX() + style.getPaddingLeft(ta.isRTL());
            if ((!ta.isRTL() && style.getAlignment() == Component.LEFT) || (ta.isRTL() && style.getAlignment() == Component.RIGHT)) {
                drawXPos = drawXPos + ta.getWidth() - inputModeWidth - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL();
            }
            g.setColor(style.getFgColor());
            int inputIndicatorY = ta.getY() + ta.getScrollY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight();
            g.fillRect(drawXPos, inputIndicatorY, inputModeWidth, f.getHeight(), (byte) 140);
            g.setColor(style.getBgColor());
            g.drawString(inputMode, drawXPos, inputIndicatorY);
        }
    }
}
Also used : Font(com.codename1.ui.Font)

Example 4 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class LazyValueC method postCreateComponents.

/**
 * Invoked after the components were created to allow properties that require the entire
 * tree to exist to update the component. This is useful for properties that point
 * at other components.
 */
private void postCreateComponents(DataInputStream in, Container parent, Resources res) throws Exception {
    // finds the component whose properties need to update
    String name = in.readUTF();
    Component lastComponent = null;
    while (name.length() > 0) {
        if (lastComponent == null || !lastComponent.getName().equals(name)) {
            lastComponent = findByName(name, parent);
        }
        Component c = lastComponent;
        int property = in.readInt();
        modifyingProperty(c, property);
        switch(property) {
            case PROPERTY_COMMAND_LEGACY:
                {
                    readCommand(in, c, parent, res, true);
                    break;
                }
            case PROPERTY_COMMAND:
                {
                    readCommand(in, c, parent, res, false);
                    break;
                }
            case PROPERTY_LABEL_FOR:
                c.setLabelForComponent((Label) findByName(in.readUTF(), parent));
                break;
            case PROPERTY_LEAD_COMPONENT:
                ((Container) c).setLeadComponent(findByName(in.readUTF(), parent));
                break;
            case PROPERTY_NEXT_FOCUS_UP:
                c.setNextFocusUp(findByName(in.readUTF(), parent));
                break;
            case PROPERTY_NEXT_FOCUS_DOWN:
                c.setNextFocusDown(findByName(in.readUTF(), parent));
                break;
            case PROPERTY_NEXT_FOCUS_LEFT:
                c.setNextFocusLeft(findByName(in.readUTF(), parent));
                break;
            case PROPERTY_NEXT_FOCUS_RIGHT:
                c.setNextFocusRight(findByName(in.readUTF(), parent));
                break;
        }
        name = in.readUTF();
    }
}
Also used : Container(com.codename1.ui.Container) Component(com.codename1.ui.Component)

Example 5 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class ArrowLinesLayer method paintSegment.

/**
 * Paints arrows on each segment. arrowSegmentLength decides how many
 * arrows will be on each segment.
 * @param g
 * @param segment
 * @param tile
 */
protected void paintSegment(Graphics g, Coord[] segment, Tile tile) {
    super.paintSegment(g, segment, tile);
    int pointsNo = segment.length;
    for (int i = 1; i < pointsNo; i++) {
        Coord start = segment[i - 1];
        Coord end = segment[i];
        Point s = tile.pointPosition(start);
        Point e = tile.pointPosition(end);
        int noOfSegments = calculateLength(s, e) / arrowSegmentLength;
        if (noOfSegments == 0 && calculateLength(s, e) > minArrowSementLength) {
            noOfSegments = 1;
        }
        for (int j = 1; j <= noOfSegments; j++) {
            if (j == 1) {
                double div = 1.0 / noOfSegments;
                drawArrow(g, new Point(s.getX(), s.getY()), new Point((int) (div * e.getX() + s.getX() * (1 - div)), (int) (div * e.getY() + s.getY() * (1 - div))));
            } else if (j == noOfSegments) {
                double div = (noOfSegments - 1) / (noOfSegments * 1.0);
                drawArrow(g, new Point((int) (div * e.getX() + s.getX() * (1 - div)), (int) (div * e.getY() + s.getY() * (1 - div))), new Point(e.getX(), e.getY()));
            } else {
                double div = ((j - 1) * 1.0) / noOfSegments;
                double div2 = (j * 1.0) / noOfSegments;
                drawArrow(g, new Point((int) (div * e.getX() + s.getX() * (1 - div)), (int) (div * e.getY() + s.getY() * (1 - div))), new Point((int) (div2 * e.getX() + s.getX() * (1 - div2)), (int) (div2 * e.getY() + s.getY() * (1 - div2))));
            }
        }
    }
}
Also used : Coord(com.codename1.maps.Coord) Point(com.codename1.ui.geom.Point) Point(com.codename1.ui.geom.Point)

Aggregations

Point (com.codename1.ui.geom.Point)11 Point (com.codename1.charts.models.Point)10 Coord (com.codename1.maps.Coord)9 Paint (com.codename1.charts.compat.Paint)7 Component (com.codename1.ui.Component)5 TextArea (com.codename1.ui.TextArea)3 Dimension (com.codename1.ui.geom.Dimension)3 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Rectangle (com.codename1.ui.geom.Rectangle)3 Border (com.codename1.ui.plaf.Border)3 ArrayList (java.util.ArrayList)3 SeriesSelection (com.codename1.charts.models.SeriesSelection)2 Container (com.codename1.ui.Container)2 EncodedImage (com.codename1.ui.EncodedImage)2 Form (com.codename1.ui.Form)2 Image (com.codename1.ui.Image)2 Label (com.codename1.ui.Label)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 Style (com.codename1.ui.plaf.Style)2