xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Age Bosma <agebo...@gmail.com>
Subject Re: Unable to remove event listener
Date Tue, 10 Mar 2009 11:30:22 GMT
2009/3/9  <thomas.deweese@kodak.com>:
>
> Hi Age,
>
> Age Bosma <agebosma@gmail.com> wrote on 03/09/2009 06:28:45 AM:
>
>> This works fine. The event gets triggered as it should. The event is
>> used to drag an element. When it gets triggered I'm adding two new
>> events to the same element. These get triggered nicely as well but I
>> can't get them removed anyone.
>
>    You need to pass the same object as the second argument to
> removeEventListener as you passed to addEventListener.  I
> suspect you might be best off passing 'this' for all of the objects
> (and setting startX/Y on this on mousedown).
>

Thanks Thomas, that did the trick! Some additional alterations where
required to make it all work but using 'this' for all the event
listeners was the important bit.

There's still something weird going on with the dragging part though.
At first I added the move and up event listeners to the element that
had to be dragged. This did work fine up to the point where I moved
the mouse to fast. It lost focus when doing so. Because of this I
moved the additional event listeners to the background element. This
does work...but only for the first time. After I dragged it ones and
let it go, I can't drag it for a second time. No mouse down event gets
triggered on the element any more.

This is what I've got now:

--------------------------------------------------------
public class DeviceHandler implements EventListener {
  DeviceHandler() {
  }

  @Override
  public void handleEvent(Event evt) {
      String eventType = evt.getType();
      MouseEvent event = (MouseEvent) evt;

      SVGElement eventTarget = (SVGElement) evt.getTarget();
      SVGElement backgroundElement = (SVGElement)
eventTarget.getOwnerSVGElement().getElementById("background");

      // Determine our mouse position on the SVG canvas
      SVGMatrix mat =
((SVGLocatable)eventTarget.getOwnerSVGElement()).getScreenCTM();
      SVGMatrix imat = mat.inverse();
      mousePosition = eventTarget.getOwnerSVGElement().createSVGPoint();
      mousePosition.setX((float) event.getClientX());
      mousePosition.setY((float) event.getClientY());
      mousePosition = mousePosition.matrixTransform(imat);

      if (eventType.equals("mousedown")) {
          deviceElement = (SVGElement) evt.getTarget();

          startX = mousePosition.getX();
          startY = mousePosition.getY();

          ((EventTarget)backgroundElement).addEventListener("mousemove",
this, false);
          ((EventTarget)backgroundElement).addEventListener("mouseup",
this, false);
          deviceElement.setAttributeNS(null, "pointer-events", "none");
          backgroundElement.setAttributeNS(null, "pointer-events", "all");
      }
      else if (eventType.equals("mousemove")) {
          // Determine the new target position and move it
          newX = (mousePosition.getX() - startX) + offsetX;
          newY = (mousePosition.getY() - startY) + offsetY;
          deviceElement.setAttributeNS(null, "transform", "translate("
+ newX + "," + newY + ")");

          // Get the target's way point line
          SVGOMPathElement pathElement = (SVGOMPathElement)
deviceElement.getOwnerSVGElement().getElementById("device_path_" +
deviceElement.getId().substring(14));
          SVGPathSegMovetoAbs pathSeg = (SVGPathSegMovetoAbs)
pathElement.getPathSegList().getItem(0);
                    // Set the starting position of the way point line
to the center of the target's new location
          SVGRect targetBBox = ((SVGLocatable)deviceElement).getBBox();
          pathSeg.setX(targetBBox.getX() + (targetBBox.getWidth() / 2) + newX);
          pathSeg.setY(targetBBox.getY() + (targetBBox.getHeight() / 2) + newY);
      }
      else if (eventType.equals("mouseup")) {
          offsetX = newX;
          offsetY = newY;

          ((EventTarget)backgroundElement).removeEventListener("mousemove",
this, false);
          ((EventTarget)backgroundElement).removeEventListener("mouseup",
this, false);
          deviceElement.setAttributeNS(null, "pointer-events", "all");
          backgroundElement.setAttributeNS(null, "pointer-events", "none");
      }
  }

  private SVGElement deviceElement = null;
  private float startX;
  private float startY;
  private float offsetX = 0;
  private float offsetY = 0;
  private float newX;
  private float newY;
  private SVGPoint mousePosition;
}
--------------------------------------------------------

It gets called the same way as before. When I add the element to the
canvas, I also add the mousedown event listener. At the same moment as
I added the element which has to be dragged (circle) I'm adding a path
(straight line) from that element centre to the centre of the canvas
as well.
If I leave out the 'way point' movement bit (i.e. I move the line path
begin point in accordance to the circle drag movement) in the code
above I get the problem I mentioned above. I can not move the circle
for a second time.
However, if I do move the line along with the circle, I can drag the
circle multiple times but the mouse down event only gets triggered
again if the mouse is positioned (clicked) within the overlapping bit
of the circle and the bbox of the path element :-S Thus not in any
other part of the circle element. How can this be?

Yours,

Age

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message