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)

1  
asclass ImageSurfaceTool > MouseAdapter {
2  
  ImageSurface is;
3  
  Point startingPoint;
4  
  bool enabled = true;
5  
  static bool verbose = false;
6  
7  
  *(ImageSurface is) {
8  
    register(is);
9  
  }
10  
  
11  
  bool isMouseHandler() { true; }
12  
  
13  
  void register(ImageSurface is) {
14  
    this.is = is;
15  
    is.removeMouseHandlers();
16  
    is.setMouseHandler(this);
17  
  }
18  
  
19  
  void remove() {
20  
    for (ImageSurfaceSelector s : collectInstances(is.tools, ImageSurfaceSelector)) {
21  
        is.removeMouseListener(s);
22  
        is.removeMouseMotionListener(s);
23  
        is.tools.remove(s);
24  
      }
25  
  }
26  
  
27  
28  
29  
  public void mousePressed(MouseEvent evt) {
30  
    if (verbose) print("mousePressed");
31  
    if (evt.getButton() != MouseEvent.BUTTON1) return;
32  
    if (enabled)
33  
      startingPoint = getPoint(evt);
34  
  }
35  
36  
  public void mouseDragged(MouseEvent e) {
37  
    if (verbose) print("mouseDragged");
38  
    if (startingPoint != null) {
39  
      Point endPoint = getPoint(e);
40  
      Rectangle r = new Rectangle(startingPoint, new Dimension(endPoint.x-startingPoint.x+1, endPoint.y-startingPoint.y+1));
41  
      normalize(r);
42  
      r.width = min(r.width, is.getImage().getWidth()-r.x);
43  
      r.height = min(r.height, is.getImage().getHeight()-r.y);
44  
      is.setSelection(r);
45  
    }
46  
    if (verbose) print("mouseDragged done");
47  
  }
48  
49  
  public static void normalize(Rectangle r) {
50  
    if (r.width < 0) {
51  
      r.x += r.width;
52  
      r.width = -r.width;
53  
    }
54  
    if (r.height < 0) {
55  
      r.y += r.height;
56  
      r.height = -r.height;
57  
    }
58  
  }
59  
60  
  public void mouseReleased(MouseEvent e) {
61  
    if (verbose) print("mouseReleased");
62  
    mouseDragged(e);
63  
    if (getPoint(e).equals(startingPoint))
64  
      is.setSelection((Rectangle) null);
65  
    startingPoint = null;
66  
  }
67  
  
68  
  Point getPoint(MouseEvent e) {
69  
    return new Point((int) (e.getX()/is.getZoomX()), (int) (e.getY()/is.getZoomY()));
70  
  }
71  
}

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: 126 / 138
Referenced in: [show references]