Libraryless. Click here for Pure Java version (18213L/115K).
abstract sclass DynImageSurface extends DynModule { transient ImageSurface imageSurface; transient BufferedImage image; transient S imageID; transient bool useUpdateHack, zoomToWindowOnSetImage; transient O onNewImage; switchable bool autoSaveToDisk = autoSaveToDiskDefault(); // dev. switchable bool pixelate; visualize { ret _visualize(); } final private JComponent _visualize() { imageSurface = imageSurface(image); imageSurface.onNewImage = r newImageFromSurface; imageSurface_pixelated(imageSurface, pixelate); ret jscroll_center(imageSurface); } // call after visualize void enableDoubleBuffering { if (imageSurface != null) imageSurface.setDoubleBuffered(true); } JComponent visualizeWithDoubleBuffering() { JComponent c = _visualize(); enableDoubleBuffering(); ret c; } // hold image during reload (not during reboot yet) O _getReloadData() { ret image; } void _setReloadData(BufferedImage image) { this.image = image; } start { dm_watchField pixelate(-> imageSurface_pixelated(imageSurface, pixelate)); if (autoSaveToDisk) autoLoadNow(); } void autoLoadNow enter { pcall { File f = autoSaveImageFile(); if (fileExists(f)) { print("Auto-loading " + f); setImage(loadImage2(f), true); } } } void autoSaveNow enter { pcall { File f = autoSaveImageFile(); savePNG(f, image); infoBox("Auto-saved " + renderFileInfo(f)); } } bool autoSaveToDiskDefault() { false; } enhanceFrame { internalFramePopupMenuItem(f, "Auto-save now", rThread autoSaveNow); } // API void setImage(MakesBufferedImage image) { imageID = null; setImage(image == null ? null : image.getBufferedImage()); } void newImageFromSurface { setImage(imageSurface.getImage()); } void setImage(BufferedImage image, bool loadingFromAutoSave default false) { this.image = image; imageID = null; if (imageSurface != null) { temp tempSetField(imageSurface, onNewImage := null); // avoid recursion imageSurface.setImage(image); if (zoomToWindowOnSetImage) imageSurface.zoomToWindow(); if (autoSaveToDisk && !loadingFromAutoSave) autoSaveNow(); } vmBus_send('newImage, this); pcallF(onNewImage, image); } void setImage(S imageID) { if (sameSnippetID(this.imageID, imageID)) ret; setImage(loadImage2(imageID)); this.imageID = imageID; } File autoSaveImageFile() { S id = dm_moduleID(me()); ret javaxDataDir("Image Autosaves/" + id + "-autosave.png"); } BufferedImage getImage() { if (useUpdateHack) { // HACKY: to get changes in ImageSurface (e.g. images drag&dropped in there directly) ImageSurface is = imageSurface; if (is != null && is.getImage() != image) setImage(is.getImage()); } ret image; } bool hasImage() { ret image != null; } void setZoom(double zoom) { if (imageSurface != null) imageSurface.setZoom(zoom); } Rect getSelection() { ret imageSurface == null ? null : toRect(imageSurface.getSelection()); } void setSelection(Rect r) { if (imageSurface != null) imageSurface.setSelection(toRectangle(r)); } int imageWidth() { ret imageSurface.getImage().getWidth(); } int imageHeight() { ret imageSurface.getImage().getHeight(); } void pixelate() { setField(pixelate := true); } }
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