Search in sources :

Example 1 with SVGPoint

use of org.w3c.dom.svg.SVGPoint in project scout.rt by eclipse.

the class SVGUtility method getElementsAt.

public static List<Element> getElementsAt(SVGDocument doc, SVGPoint point) {
    ArrayList<Element> list = new ArrayList<Element>();
    SVGOMRect svgOMRect = new SVGOMRect(point.getX(), point.getY(), 1, 1);
    NodeList intersectedElements = doc.getRootElement().getIntersectionList(svgOMRect, null);
    int n = intersectedElements.getLength();
    for (int i = 0; i < n; i++) {
        Node node = intersectedElements.item(i);
        if (node instanceof Element) {
            list.add((Element) node);
        }
    }
    return list;
}
Also used : SVGOMRect(org.apache.batik.dom.svg.SVGOMRect) SVGTSpanElement(org.w3c.dom.svg.SVGTSpanElement) SVGTextContentElement(org.w3c.dom.svg.SVGTextContentElement) SVGElement(org.w3c.dom.svg.SVGElement) Element(org.w3c.dom.Element) SVGAElement(org.w3c.dom.svg.SVGAElement) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) SVGPoint(org.w3c.dom.svg.SVGPoint)

Example 2 with SVGPoint

use of org.w3c.dom.svg.SVGPoint in project elki by elki-project.

the class SVGUtil method elementCoordinatesFromEvent.

/**
 * Convert the coordinates of an DOM Event from screen into element
 * coordinates.
 *
 * @param doc Document context
 * @param tag Element containing the coordinate system
 * @param evt Event to interpret
 * @return coordinates
 */
public static SVGPoint elementCoordinatesFromEvent(Document doc, Element tag, Event evt) {
    try {
        DOMMouseEvent gnme = (DOMMouseEvent) evt;
        SVGMatrix mat = ((SVGLocatable) tag).getScreenCTM();
        SVGMatrix imat = mat.inverse();
        SVGPoint cPt = ((SVGDocument) doc).getRootElement().createSVGPoint();
        cPt.setX(gnme.getClientX());
        cPt.setY(gnme.getClientY());
        return cPt.matrixTransform(imat);
    } catch (Exception e) {
        LoggingUtil.warning("Error getting coordinates from SVG event.", e);
        return null;
    }
}
Also used : SVGPoint(org.w3c.dom.svg.SVGPoint) SVGLocatable(org.w3c.dom.svg.SVGLocatable) SVGMatrix(org.w3c.dom.svg.SVGMatrix) DOMMouseEvent(org.apache.batik.dom.events.DOMMouseEvent)

Example 3 with SVGPoint

use of org.w3c.dom.svg.SVGPoint in project elki by elki-project.

the class BatikUtil method getRelativeCoordinates.

/**
 * Get the relative coordinates of a point within the coordinate system of a
 * particular SVG Element.
 *
 * @param evt Event, needs to be a DOMMouseEvent
 * @param reference SVG Element the coordinate system is used of
 * @return Array containing the X and Y values
 */
public static double[] getRelativeCoordinates(Event evt, Element reference) {
    if (evt instanceof DOMMouseEvent && reference instanceof SVGLocatable && reference instanceof SVGElement) {
        // Get the screen (pixel!) coordinates
        DOMMouseEvent gnme = (DOMMouseEvent) evt;
        SVGMatrix mat = ((SVGLocatable) reference).getScreenCTM();
        SVGMatrix imat = mat.inverse();
        SVGPoint cPt = ((SVGElement) reference).getOwnerSVGElement().createSVGPoint();
        cPt.setX(gnme.getClientX());
        cPt.setY(gnme.getClientY());
        // Have Batik transform the screen (pixel!) coordinates into SVG element
        // coordinates
        cPt = cPt.matrixTransform(imat);
        return new double[] { cPt.getX(), cPt.getY() };
    }
    return null;
}
Also used : SVGPoint(org.w3c.dom.svg.SVGPoint) SVGLocatable(org.w3c.dom.svg.SVGLocatable) SVGElement(org.w3c.dom.svg.SVGElement) SVGMatrix(org.w3c.dom.svg.SVGMatrix) DOMMouseEvent(org.apache.batik.dom.events.DOMMouseEvent)

Example 4 with SVGPoint

use of org.w3c.dom.svg.SVGPoint in project elki by elki-project.

the class DragableArea method handleEvent.

@Override
public void handleEvent(Event evt) {
    if (evt.getType().equals(SVGConstants.SVG_EVENT_MOUSEDOWN)) {
        SVGPoint dragPoint = getCoordinates(evt);
        if (startDrag(dragPoint, evt)) {
            // LoggingUtil.warning("Starting drag: "+dragPoint);
            startDragPoint = dragPoint;
            enableStop();
        }
    } else if (evt.getType().equals(SVGConstants.SVG_EVENT_MOUSEMOVE)) {
        if (startDragPoint != null) {
            SVGPoint dragPoint = getCoordinates(evt);
            if (!duringDrag(startDragPoint, dragPoint, evt, evt.getTarget() == element)) {
                // cancel the drag operation
                startDragPoint = null;
                disableStop();
            }
        }
    } else if (evt.getType().equals(SVGConstants.SVG_EVENT_MOUSEUP)) {
        if (startDragPoint != null) {
            SVGPoint dragPoint = getCoordinates(evt);
            if (endDrag(startDragPoint, dragPoint, evt, evt.getTarget() == element)) {
                // LoggingUtil.warning("Drag completed: "+dragPoint);
                startDragPoint = null;
                disableStop();
            }
        }
    } else if (evt.getType().equals(SVGConstants.SVG_EVENT_MOUSEOUT)) {
        // When leaving the document with the mouse!
        if (startDragPoint != null && evt.getTarget() == evt.getCurrentTarget()) {
            // LoggingUtil.warning("Mouseout: "+evt.getTarget().toString());
            SVGPoint dragPoint = getCoordinates(evt);
            if (endDrag(startDragPoint, dragPoint, evt, false)) {
                // LoggingUtil.warning("Drag completed: "+dragPoint);
                startDragPoint = null;
                disableStop();
            }
        }
    } else {
        LoggingUtil.warning("Unrecognized event: " + evt);
    }
}
Also used : SVGPoint(org.w3c.dom.svg.SVGPoint)

Aggregations

SVGPoint (org.w3c.dom.svg.SVGPoint)4 DOMMouseEvent (org.apache.batik.dom.events.DOMMouseEvent)2 SVGElement (org.w3c.dom.svg.SVGElement)2 SVGLocatable (org.w3c.dom.svg.SVGLocatable)2 SVGMatrix (org.w3c.dom.svg.SVGMatrix)2 ArrayList (java.util.ArrayList)1 SVGOMRect (org.apache.batik.dom.svg.SVGOMRect)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SVGAElement (org.w3c.dom.svg.SVGAElement)1 SVGTSpanElement (org.w3c.dom.svg.SVGTSpanElement)1 SVGTextContentElement (org.w3c.dom.svg.SVGTextContentElement)1