Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

71
LINES

< > BotCompany Repo | #1033785 // ImageSurfaceTool - a mouse handler for ImageSurface [dev.]

JavaX fragment (include)

asclass ImageSurfaceTool > MouseAdapter {
  ImageSurface is;
  Point startingPoint;
  bool enabled = true;
  static bool verbose = false;

  *(ImageSurface is) {
    register(is);
  }
  
  bool isMouseHandler() { true; }
  
  void register(ImageSurface is) {
    this.is = is;
    is.removeMouseHandlers();
    is.setMouseHandler(this);
  }
  
  void remove() {
    for (ImageSurfaceSelector s : collectInstances(is.tools, ImageSurfaceSelector)) {
        is.removeMouseListener(s);
        is.removeMouseMotionListener(s);
        is.tools.remove(s);
      }
  }
  


  public void mousePressed(MouseEvent evt) {
    if (verbose) print("mousePressed");
    if (evt.getButton() != MouseEvent.BUTTON1) return;
    if (enabled)
      startingPoint = getPoint(evt);
  }

  public void mouseDragged(MouseEvent e) {
    if (verbose) print("mouseDragged");
    if (startingPoint != null) {
      Point endPoint = getPoint(e);
      Rectangle r = new Rectangle(startingPoint, new Dimension(endPoint.x-startingPoint.x+1, endPoint.y-startingPoint.y+1));
      normalize(r);
      r.width = min(r.width, is.getImage().getWidth()-r.x);
      r.height = min(r.height, is.getImage().getHeight()-r.y);
      is.setSelection(r);
    }
    if (verbose) print("mouseDragged done");
  }

  public static void normalize(Rectangle r) {
    if (r.width < 0) {
      r.x += r.width;
      r.width = -r.width;
    }
    if (r.height < 0) {
      r.y += r.height;
      r.height = -r.height;
    }
  }

  public void mouseReleased(MouseEvent e) {
    if (verbose) print("mouseReleased");
    mouseDragged(e);
    if (getPoint(e).equals(startingPoint))
      is.setSelection((Rectangle) null);
    startingPoint = null;
  }
  
  Point getPoint(MouseEvent e) {
    return new Point((int) (e.getX()/is.getZoomX()), (int) (e.getY()/is.getZoomY()));
  }
}

Author comment

Began life as a copy of #1005517

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033785
Snippet name: ImageSurfaceTool - a mouse handler for ImageSurface [dev.]
Eternal ID of this version: #1033785/1
Text MD5: 8d3554b29126a6d4bbaad195a8d28d85
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-04 19:53:10
Source code size: 1896 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 123 / 135
Referenced in: [show references]