Libraryless. Click here for Pure Java version (19927L/123K).
1 | sclass ImageSurfaceSelector extends ImageSurfaceMouseHandler {
|
2 | Point startingPoint; |
3 | bool enabled = true; |
4 | static bool verbose = false; |
5 | |
6 | *() {}
|
7 | *(ImageSurface is) {
|
8 | if (containsInstance(is.tools, ImageSurfaceSelector)) ret; |
9 | this.register(is); |
10 | } |
11 | |
12 | public void mousePressed(MouseEvent evt) {
|
13 | if (verbose) print("mousePressed");
|
14 | if (evt.getButton() != MouseEvent.BUTTON1) return; |
15 | if (enabled) |
16 | startingPoint = getPoint(evt); |
17 | } |
18 | |
19 | public void mouseDragged(MouseEvent e) {
|
20 | if (verbose) print("mouseDragged");
|
21 | if (startingPoint != null) {
|
22 | Point endPoint = getPoint(e); |
23 | Rectangle r = new Rectangle(startingPoint, new Dimension(endPoint.x-startingPoint.x+1, endPoint.y-startingPoint.y+1)); |
24 | normalize(r); |
25 | r.width = min(r.width, is.getImage().getWidth()-r.x); |
26 | r.height = min(r.height, is.getImage().getHeight()-r.y); |
27 | is.setSelection(r); |
28 | } |
29 | if (verbose) print("mouseDragged done");
|
30 | } |
31 | |
32 | public static void normalize(Rectangle r) {
|
33 | if (r.width < 0) {
|
34 | r.x += r.width; |
35 | r.width = -r.width; |
36 | } |
37 | if (r.height < 0) {
|
38 | r.y += r.height; |
39 | r.height = -r.height; |
40 | } |
41 | } |
42 | |
43 | public void mouseReleased(MouseEvent e) {
|
44 | if (verbose) print("mouseReleased");
|
45 | mouseDragged(e); |
46 | if (getPoint(e).equals(startingPoint)) |
47 | is.setSelection((Rectangle) null); |
48 | startingPoint = null; |
49 | } |
50 | } |
download show line numbers debug dex old transpilations
Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
| Snippet ID: | #1005517 |
| Snippet name: | ImageSurfaceSelector - provide rectangular selection by mouse to ImageSurface |
| Eternal ID of this version: | #1005517/9 |
| Text MD5: | 49351ddcebf4951997d6403b239f27c6 |
| Transpilation MD5: | 510bd6a86d5ef546af9039075cc9cb63 |
| Author: | stefan |
| Category: | javax / gui |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-02-15 22:29:01 |
| Source code size: | 1444 bytes / 50 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 911 / 6375 |
| Version history: | 8 change(s) |
| Referenced in: | [show references] |