]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Implement ImageDescriptor.getImageData(int zoom) 33/2533/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 7 Dec 2018 12:52:57 +0000 (14:52 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 7 Dec 2018 12:52:57 +0000 (14:52 +0200)
gitlab #228

Change-Id: I0b0294a8a48d6d633c452fee780be502f4d58406

bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ColorImageDescriptor.java
bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/TextImageDescriptor.java

index eca35f1fce9bd634689a8562344c1556488ab4f1..6c0f26d4e809bc3611898a4e9559e64a94e17707 100644 (file)
@@ -36,28 +36,44 @@ public class ColorImageDescriptor extends ImageDescriptor {
                this.selected = selected;
        }
 
+       @Override
+       public ImageData getImageData(int zoom) {
+               int w = width;
+               int h = height;
+               if (zoom > 100) {
+                       float s = zoom / 100.0f;
+                       w = Math.round(width * s);
+                       h = Math.round(height * s);
+               }
+               return getImageData(w, h);
+       }
+
        @Override
        public ImageData getImageData() {
-        ImageData id = new ImageData(width, height, 24, PALETTEDATA);
-        int cx = width / 2;
-        int cy = height / 2;
-        int dst = height * width / 23;
-        for (int x=0; x<width; x++) {
-            for (int y=0; y<height; y++) {
-                int color = c;
-                boolean border = x==0||x==width-1||y==0||y==height-1;
-                if ( border ) color = 0;
-                if (selected) {
-                       int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);
-                       if ( dist < dst ) color = 0xcccccc; 
-                }
-                id.setPixel(x, y, color);
-            }
-        }
-        
+               return getImageData(100);
+       }
+
+       private ImageData getImageData(int width, int height) {
+               ImageData id = new ImageData(width, height, 24, PALETTEDATA);
+               int cx = width / 2;
+               int cy = height / 2;
+               int dst = height * width / 23;
+               for (int x=0; x<width; x++) {
+                       for (int y=0; y<height; y++) {
+                               int color = c;
+                               boolean border = x==0||x==width-1||y==0||y==height-1;
+                               if ( border ) color = 0;
+                               if (selected) {
+                                       int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);
+                                       if ( dist < dst ) color = 0xcccccc; 
+                               }
+                               id.setPixel(x, y, color);
+                       }
+               }
+
                return id;
        }
-       
+
        @Override
        public int hashCode() {
                return c + 7*width + 13*height + (selected?234234:4235);
index 71aff7113931ce96c3bb504e194bdb719a9c671e..6676ae2bc2b6f503eda05d8fed20b59e1f9e5c92 100644 (file)
@@ -28,16 +28,16 @@ import org.simantics.utils.ObjectUtils;
 public class TextImageDescriptor extends ImageDescriptor {
 
        static final PaletteData _RGB = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
-       
+
        public String text;
        public int width, height;
        public String font;
        public int fontSize;
        public int style;
        public int rgb;
-       
+
        private transient int hash;
-       
+
        public TextImageDescriptor(String text, int width, int height, 
                        String font, int fontSize, 
                        int style, int rgb) {
@@ -53,38 +53,52 @@ public class TextImageDescriptor extends ImageDescriptor {
        }
 
        @Override
-       public ImageData getImageData() {
-        ImageData id = new ImageData(width, height, 24, _RGB);         
-        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
-        Graphics2D g = (Graphics2D) bi.getGraphics();
-        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
-        Font f = new Font(font, style, fontSize);
-        g.setFont( f );
-        Color c = new Color(rgb);
-        g.setColor( c );
-        FontMetrics fm = g.getFontMetrics(f);
-        Rectangle2D rect = fm.getStringBounds(text, g);
-        g.drawString(text, (float) ((width-rect.getWidth())/2), (float) ( (height/2) + (height-rect.getHeight())/2) );
-        g.dispose();        
-        
-        Raster alpha = bi.getAlphaRaster();        
-        for (int x=0; x<width; x++) {
-               for (int y=0; y<height; y++) {
-                       int a = alpha.getSample(x, y, 0);
-                       int rgb = bi.getRGB(x, y);
-                       id.setAlpha(x, y, a);
-                       id.setPixel(x, y, rgb);
-               }
-        }
-        
+       public ImageData getImageData(int zoom) {
+               int w = width;
+               int h = height;
+               int fs = fontSize;
+               if (zoom > 100) {
+                       float s = zoom / 100.0f;
+                       w = Math.round(width * s);
+                       h = Math.round(height * s);
+                       fs = Math.round(fontSize * s);
+               }
+               return getImageData(w, h, fs);
+       }
+
+       private ImageData getImageData(int width, int height, int fontSize) {
+               ImageData id = new ImageData(width, height, 24, _RGB);
+
+               BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+               Graphics2D g = (Graphics2D) bi.getGraphics();
+               g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+               Font f = new Font(font, style, fontSize);
+               g.setFont( f );
+               Color c = new Color(rgb);
+               g.setColor( c );
+               FontMetrics fm = g.getFontMetrics(f);
+               Rectangle2D rect = fm.getStringBounds(text, g);
+               g.drawString(text, (float) ((width-rect.getWidth())/2), (float) ( (height/2) + (height-rect.getHeight())/2) );
+               g.dispose();
+
+               Raster alpha = bi.getAlphaRaster();
+               for (int x=0; x<width; x++) {
+                       for (int y=0; y<height; y++) {
+                               int a = alpha.getSample(x, y, 0);
+                               int rgb = bi.getRGB(x, y);
+                               id.setAlpha(x, y, a);
+                               id.setPixel(x, y, rgb);
+                       }
+               }
+
                return id;
        }
-       
+
        @Override
        public int hashCode() {
                return hash;
        }
-       
+
        @Override
        public boolean equals(Object obj) {
                if (obj instanceof TextImageDescriptor==false) return false;