Libraryless. Click here for Pure Java version (18213L/115K).
1 | abstract sclass DynImageSurface extends DynModule { |
2 | transient ImageSurface imageSurface; |
3 | transient BufferedImage image; |
4 | transient S imageID; |
5 | transient bool useUpdateHack, zoomToWindowOnSetImage; |
6 | transient O onNewImage; |
7 | switchable bool autoSaveToDisk = autoSaveToDiskDefault(); // dev. |
8 | switchable bool pixelate; |
9 | |
10 | visualize { ret _visualize(); } |
11 | |
12 | final private JComponent _visualize() { |
13 | imageSurface = imageSurface(image); |
14 | imageSurface.onNewImage = r newImageFromSurface; |
15 | imageSurface_pixelated(imageSurface, pixelate); |
16 | ret jscroll_center(imageSurface); |
17 | } |
18 | |
19 | // call after visualize |
20 | void enableDoubleBuffering { |
21 | if (imageSurface != null) imageSurface.setDoubleBuffered(true); |
22 | } |
23 | |
24 | JComponent visualizeWithDoubleBuffering() { |
25 | JComponent c = _visualize(); |
26 | enableDoubleBuffering(); |
27 | ret c; |
28 | } |
29 | |
30 | // hold image during reload (not during reboot yet) |
31 | |
32 | O _getReloadData() { ret image; } |
33 | void _setReloadData(BufferedImage image) { this.image = image; } |
34 | |
35 | start { |
36 | dm_watchField pixelate(-> imageSurface_pixelated(imageSurface, pixelate)); |
37 | |
38 | if (autoSaveToDisk) |
39 | autoLoadNow(); |
40 | } |
41 | |
42 | void autoLoadNow enter { |
43 | pcall { |
44 | File f = autoSaveImageFile(); |
45 | if (fileExists(f)) { |
46 | print("Auto-loading " + f); |
47 | setImage(loadImage2(f), true); |
48 | } |
49 | } |
50 | } |
51 | |
52 | void autoSaveNow enter { |
53 | pcall { |
54 | File f = autoSaveImageFile(); |
55 | savePNG(f, image); |
56 | infoBox("Auto-saved " + renderFileInfo(f)); |
57 | } |
58 | } |
59 | |
60 | bool autoSaveToDiskDefault() { false; } |
61 | |
62 | enhanceFrame { |
63 | internalFramePopupMenuItem(f, "Auto-save now", rThread autoSaveNow); |
64 | } |
65 | |
66 | // API |
67 | |
68 | void setImage(MakesBufferedImage image) { imageID = null; setImage(image == null ? null : image.getBufferedImage()); } |
69 | |
70 | void newImageFromSurface { |
71 | setImage(imageSurface.getImage()); |
72 | } |
73 | |
74 | void setImage(BufferedImage image, bool loadingFromAutoSave default false) { |
75 | this.image = image; imageID = null; |
76 | if (imageSurface != null) { |
77 | temp tempSetField(imageSurface, onNewImage := null); // avoid recursion |
78 | imageSurface.setImage(image); |
79 | if (zoomToWindowOnSetImage) imageSurface.zoomToWindow(); |
80 | if (autoSaveToDisk && !loadingFromAutoSave) |
81 | autoSaveNow(); |
82 | } |
83 | vmBus_send('newImage, this); |
84 | pcallF(onNewImage, image); |
85 | } |
86 | |
87 | void setImage(S imageID) { |
88 | if (sameSnippetID(this.imageID, imageID)) ret; |
89 | setImage(loadImage2(imageID)); |
90 | this.imageID = imageID; |
91 | } |
92 | |
93 | File autoSaveImageFile() { |
94 | S id = dm_moduleID(me()); |
95 | ret javaxDataDir("Image Autosaves/" + id + "-autosave.png"); |
96 | } |
97 | |
98 | BufferedImage getImage() { |
99 | if (useUpdateHack) { |
100 | // HACKY: to get changes in ImageSurface (e.g. images drag&dropped in there directly) |
101 | ImageSurface is = imageSurface; |
102 | if (is != null && is.getImage() != image) setImage(is.getImage()); |
103 | } |
104 | |
105 | ret image; |
106 | } |
107 | bool hasImage() { ret image != null; } |
108 | |
109 | void setZoom(double zoom) { if (imageSurface != null) imageSurface.setZoom(zoom); } |
110 | |
111 | Rect getSelection() { ret imageSurface == null ? null : toRect(imageSurface.getSelection()); } |
112 | |
113 | void setSelection(Rect r) { if (imageSurface != null) imageSurface.setSelection(toRectangle(r)); } |
114 | |
115 | int imageWidth() { ret imageSurface.getImage().getWidth(); } |
116 | int imageHeight() { ret imageSurface.getImage().getHeight(); } |
117 | |
118 | void pixelate() { |
119 | setField(pixelate := true); |
120 | } |
121 | } |
Began life as a copy of #1016205
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1018873 |
Snippet name: | DynImageSurface - module showing an image |
Eternal ID of this version: | #1018873/32 |
Text MD5: | 0dc93b3fa8b5eb4a9ab4ecd07f0b4940 |
Transpilation MD5: | 167b95c936a83af5ff68510232830595 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-12-26 18:51:09 |
Source code size: | 3567 bytes / 121 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 562 / 3406 |
Version history: | 31 change(s) |
Referenced in: | [show references] |