Search in sources :

Example 41 with Rectangle

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

the class Container method binarySearchFirstIntersectionY.

/**
 * Performs a binary search within the children of the container to find components
 * that intersect the given range on the y-axis.  <b>This should only be used
 * if it is known that the child components are sorted by their y coordinates
 * in ascending order.  Otherwise you'll get undefined results.</b>
 * @param y1 The lower y-bound of the region to search.  (0,0) is top-left corner of container.
 * @param y2 The upper y-bound of the region to search.  (0,0) is top-left corner of container.
 * @param start The lower "index" to search.
 * @param end The upper "index" to search.
 * @return The index within the components array of the first child component
 * that intersects the given region.  Or -1 if none is found.
 */
private int binarySearchFirstIntersectionY(int y1, int y2, int start, int end) {
    if (start >= end) {
        return -1;
    }
    int pos = (start + end) / 2;
    Component c = components.get(pos);
    Rectangle bounds = c.getBounds();
    int cy1 = bounds.getY();
    int cy2 = bounds.getY() + bounds.getHeight();
    if ((cy1 >= y1 && cy1 <= y2) || (cy2 >= y1 && cy2 <= y2) || (cy1 <= y1 && cy2 >= y2)) {
        // We have a hit let's roll backward until we find the first visible
        while (pos > start && cy1 > y1) {
            c = components.get(--pos);
            cy1 = c.getBounds().getY();
        }
        return pos;
    } else if (cy1 > y2) {
        return binarySearchFirstIntersectionY(y1, y2, start, pos);
    } else {
        return binarySearchFirstIntersectionY(y1, y2, pos + 1, end);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 42 with Rectangle

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

the class Dialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param c the context component which is used to position the dialog and can also be pointed at
 * @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
 */
public Command showPopupDialog(Component c) {
    Rectangle componentPos = c.getSelectedRect();
    componentPos.setX(componentPos.getX() - c.getScrollX());
    componentPos.setY(componentPos.getY() - c.getScrollY());
    return showPopupDialog(componentPos);
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 43 with Rectangle

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

the class HTMLComponent method handleImageMapArea.

/**
 * Handles an area definition for an image map
 *
 * @param areaTag The AREA tag
 */
private void handleImageMapArea(HTMLElement areaTag) {
    if (curImageMap != null) {
        String shape = areaTag.getAttributeById(HTMLElement.ATTR_SHAPE);
        boolean supportedShape = false;
        if (shape != null) {
            String hrefStr = areaTag.getAttributeById(HTMLElement.ATTR_HREF);
            if (shape.equalsIgnoreCase("default")) {
                supportedShape = true;
                curImageMap.setDefaultLink(hrefStr);
            } else if ((shape.equalsIgnoreCase("rect")) || (shape.equalsIgnoreCase("circle"))) {
                supportedShape = true;
                String coordsStr = areaTag.getAttributeById(HTMLElement.ATTR_COORDS);
                if ((coordsStr != null) && (hrefStr != null)) {
                    String curValStr = "";
                    int[] coords = new int[4];
                    int curCoord = 0;
                    boolean error = true;
                    try {
                        for (int c = 0; c < coordsStr.length(); c++) {
                            char ch = coordsStr.charAt(c);
                            if (ch != ',') {
                                curValStr += ch;
                            } else {
                                coords[curCoord] = Integer.parseInt(curValStr);
                                curCoord++;
                                curValStr = "";
                            }
                        }
                        if (curValStr.length() > 0) {
                            coords[curCoord] = Integer.parseInt(curValStr);
                            curCoord++;
                        }
                        if (shape.equalsIgnoreCase("rect")) {
                            if (curCoord == 4) {
                                curImageMap.addRectArea(new Rectangle(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]), hrefStr);
                                error = false;
                            }
                        } else if (curCoord == 3) {
                            // circle
                            // coords[2] is the radius, and 0,1 are the center x,y
                            curImageMap.addRectArea(new Rectangle(coords[0] - coords[2], coords[1] - coords[2], coords[2] * 2 + 1, coords[2] * 2 + 1), hrefStr);
                            // Note: The 'circle' SHAPE in AREA tag is implemented as a rectangle to avoid complication of circle pixel collision
                            error = false;
                        }
                    } catch (Exception e) {
                    // Can be number format exception or index out of bounds
                    // do nothing - error will stay true
                    }
                    if (error) {
                        notifyImageMapError("AREA tag 'coords' property value is invalid (should be exactly 3 comma seperated numbers for circle, 4 for rectangle): " + coordsStr, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_COORDS, coordsStr);
                    }
                }
            }
        }
        if (!supportedShape) {
            notifyImageMapError("Unsupported or missing AREA tag 'shape' property: " + shape, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_SHAPE, shape);
        }
    } else {
        notifyImageMapError("AREA tag is defined without a parent MAP tag - ignoring", HTMLCallback.ERROR_INVALID_TAG_HIERARCHY, -1, null);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 44 with Rectangle

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

the class List method paintFocus.

private void paintFocus(Graphics g, int width, Rectangle pos, Dimension rendererSize) {
    if (ignoreFocusComponentWhenUnfocused && !hasFocus()) {
        return;
    }
    if (!shouldRenderSelection()) {
        return;
    }
    calculateComponentPosition(getCurrentSelected(), width, pos, rendererSize, getElementSize(true, true), true);
    Dimension size = pos.getSize();
    Component cmp = renderer.getListFocusComponent(this);
    if (cmp != null) {
        cmp.setCellRenderer(true);
        int x = pos.getX();
        int y = pos.getY();
        // prevent focus animation from working during a drag operation
        if (orientation != HORIZONTAL) {
            y -= (animationPosition + fixedDraggedAnimationPosition);
        } else {
            x -= (animationPosition + fixedDraggedAnimationPosition);
        }
        renderComponentBackground(g, cmp, x, y, size.getWidth(), size.getHeight());
        renderComponent(g, cmp, x, y, size.getWidth(), size.getHeight());
    }
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 45 with Rectangle

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

the class List method calculateComponentPosition.

/**
 * Calculates the desired bounds for the component and returns them within the
 * given rectangle.
 */
private void calculateComponentPosition(int index, int defaultWidth, Rectangle rect, Dimension rendererSize, Dimension selectedSize, boolean beforeSelected) {
    Style style = getStyle();
    int initialY = style.getPaddingTop();
    int initialX = style.getPaddingLeftNoRTL();
    boolean rtl = isRTL();
    if (rtl) {
        initialX += getSideGap();
    }
    int selection = getCurrentSelected();
    Dimension d = rect.getSize();
    int selectedDiff;
    // which will cause the bottom elements to "return" from the top.
    if (orientation != HORIZONTAL) {
        int height = rendererSize.getHeight();
        selectedDiff = selectedSize.getHeight() - height;
        rect.setX(initialX);
        d.setHeight(height);
        d.setWidth(defaultWidth);
        int y = 0;
        int listHeight = getHeight() - style.getVerticalPadding();
        int totalHeight = (height + itemGap) * getModel().getSize() + selectedDiff;
        switch(fixedSelection) {
            case FIXED_CENTER:
                y = listHeight / 2 - (height + itemGap + selectedDiff) / 2 + (index - selection) * (height + itemGap);
                if (!beforeSelected) {
                    y += selectedDiff;
                }
                y = recalcOffset(y, totalHeight, listHeight, height + itemGap);
                break;
            case FIXED_TRAIL:
                y = listHeight - (height + itemGap + selectedDiff);
            case FIXED_LEAD:
                y += (index - selection) * (height + itemGap);
                if (index - selection > 0) {
                    y += selectedDiff;
                }
                y = recalcOffset(y, totalHeight, listHeight, height + itemGap);
                break;
            default:
                y = index * (height + itemGap);
                if (!beforeSelected) {
                    y += selectedDiff;
                }
                break;
        }
        rect.setY(y + initialY);
        if (index == selection) {
            d.setHeight(d.getHeight() + selectedDiff);
        }
    } else {
        int width = rendererSize.getWidth();
        selectedDiff = selectedSize.getWidth() - width;
        rect.setY(initialY);
        d.setHeight(getHeight() - style.getVerticalPadding());
        d.setWidth(width);
        int x = 0;
        int listWidth = getWidth() - style.getHorizontalPadding();
        int totalWidth = (width + itemGap) * getModel().getSize() + selectedDiff;
        switch(fixedSelection) {
            case FIXED_CENTER:
                x = listWidth / 2 - (width + itemGap + selectedDiff) / 2 + (index - selection) * (width + itemGap);
                if (!beforeSelected) {
                    x += selectedDiff;
                }
                if (rtl) {
                    x = listWidth - x - width;
                }
                x = recalcOffset(x, totalWidth, listWidth, width + itemGap);
                break;
            case FIXED_TRAIL:
                x = listWidth - (width + itemGap + selectedDiff);
            case FIXED_LEAD:
                x += (index - selection) * (width + itemGap);
                if (index - selection > 0) {
                    x += selectedDiff;
                }
                if (rtl) {
                    x = listWidth - x - width;
                }
                x = recalcOffset(x, totalWidth, listWidth, width + itemGap);
                break;
            default:
                x = index * (width + itemGap);
                if (!beforeSelected) {
                    x += selectedDiff;
                }
                break;
        }
        int rectX = initialX + x;
        if ((rtl) && (fixedSelection < FIXED_NONE_BOUNDRY)) {
            rectX = initialX + totalWidth - (x - initialX) - (width + itemGap);
            if (index == getCurrentSelected()) {
                rectX -= selectedDiff;
            }
            if (totalWidth < listWidth) {
                rectX += (listWidth - totalWidth);
            }
        }
        rect.setX(rectX);
        if (index == selection) {
            d.setWidth(d.getWidth() + selectedDiff);
        }
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Rectangle (com.codename1.ui.geom.Rectangle)41 Dimension (com.codename1.ui.geom.Dimension)16 Style (com.codename1.ui.plaf.Style)13 Image (com.codename1.ui.Image)10 ActionEvent (com.codename1.ui.events.ActionEvent)5 Border (com.codename1.ui.plaf.Border)5 Component (com.codename1.ui.Component)4 Graphics (com.codename1.ui.Graphics)4 UIManager (com.codename1.ui.plaf.UIManager)4 Form (com.codename1.ui.Form)3 Label (com.codename1.ui.Label)3 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Point (com.codename1.ui.geom.Point)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Paint (com.codename1.charts.compat.Paint)2 Button (com.codename1.ui.Button)2 EncodedImage (com.codename1.ui.EncodedImage)2 Painter (com.codename1.ui.Painter)2 ActionListener (com.codename1.ui.events.ActionListener)2 RoundBorder (com.codename1.ui.plaf.RoundBorder)2