소스 검색

Add opacity to textpane backgrounds

Also fix an invalid default option for the "background option"
pull/433/head
Greg Holmes 8 년 전
부모
커밋
f40c0c89ad

+ 2
- 1
ui_swing/plugin.config 파일 보기

33
     showfulltopic=false
33
     showfulltopic=false
34
     hideEmptyTopicBar=false
34
     hideEmptyTopicBar=false
35
     textpanebackground=
35
     textpanebackground=
36
-    textpanebackgroundoption=STRETCH
36
+    textpanebackgroundoption=SCALE
37
+    textpanebackgroundopacity=1
37
     desktopbackground=dmdirc://com/dmdirc/res/background.png
38
     desktopbackground=dmdirc://com/dmdirc/res/background.png
38
     desktopbackgroundoption=CENTER
39
     desktopbackgroundoption=CENTER
39
     showjrewarning=true
40
     showjrewarning=true

+ 25
- 9
ui_swing/src/com/dmdirc/addons/ui_swing/UIUtilities.java 파일 보기

28
 import com.dmdirc.addons.ui_swing.components.SupplierLoggingSwingWorker;
28
 import com.dmdirc.addons.ui_swing.components.SupplierLoggingSwingWorker;
29
 import com.dmdirc.util.colours.Colour;
29
 import com.dmdirc.util.colours.Colour;
30
 
30
 
31
+import java.awt.AlphaComposite;
31
 import java.awt.Color;
32
 import java.awt.Color;
33
+import java.awt.Composite;
32
 import java.awt.Font;
34
 import java.awt.Font;
33
 import java.awt.FontMetrics;
35
 import java.awt.FontMetrics;
34
 import java.awt.Graphics2D;
36
 import java.awt.Graphics2D;
457
      * @param bounds           Bounds to paint within
459
      * @param bounds           Bounds to paint within
458
      * @param backgroundImage  background image
460
      * @param backgroundImage  background image
459
      * @param backgroundOption How to draw the background
461
      * @param backgroundOption How to draw the background
462
+     * @param opacity          Opacity of the image
460
      */
463
      */
461
     public static void paintBackground(final Graphics2D g,
464
     public static void paintBackground(final Graphics2D g,
462
             final Rectangle bounds,
465
             final Rectangle bounds,
463
             final Image backgroundImage,
466
             final Image backgroundImage,
464
-            final BackgroundOption backgroundOption) {
467
+            final BackgroundOption backgroundOption,
468
+            final float opacity) {
465
         if (backgroundImage == null) {
469
         if (backgroundImage == null) {
466
             paintNoBackground(g, bounds);
470
             paintNoBackground(g, bounds);
467
         } else {
471
         } else {
468
             switch (backgroundOption) {
472
             switch (backgroundOption) {
469
                 case TILED:
473
                 case TILED:
470
-                    paintTiledBackground(g, bounds, backgroundImage);
474
+                    paintTiledBackground(g, bounds, backgroundImage, opacity);
471
                     break;
475
                     break;
472
                 case SCALE:
476
                 case SCALE:
473
-                    paintStretchedBackground(g, bounds, backgroundImage);
477
+                    paintStretchedBackground(g, bounds, backgroundImage, opacity);
474
                     break;
478
                     break;
475
                 case SCALE_ASPECT_RATIO:
479
                 case SCALE_ASPECT_RATIO:
476
-                    paintStretchedAspectRatioBackground(g, bounds, backgroundImage);
480
+                    paintStretchedAspectRatioBackground(g, bounds, backgroundImage, opacity);
477
                     break;
481
                     break;
478
                 case CENTER:
482
                 case CENTER:
479
-                    paintCenterBackground(g, bounds, backgroundImage);
483
+                    paintCenterBackground(g, bounds, backgroundImage, opacity);
480
                     break;
484
                     break;
481
                 default:
485
                 default:
482
                     break;
486
                     break;
489
     }
493
     }
490
 
494
 
491
     private static void paintStretchedBackground(final Graphics2D g,
495
     private static void paintStretchedBackground(final Graphics2D g,
492
-            final Rectangle bounds, final Image backgroundImage) {
496
+            final Rectangle bounds, final Image backgroundImage, final float opacity) {
497
+        final Composite originalComposite = g.getComposite();
498
+        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
493
         g.drawImage(backgroundImage, 0, 0, bounds.width, bounds.height, null);
499
         g.drawImage(backgroundImage, 0, 0, bounds.width, bounds.height, null);
500
+        g.setComposite(originalComposite);
494
     }
501
     }
495
 
502
 
496
     private static void paintCenterBackground(final Graphics2D g,
503
     private static void paintCenterBackground(final Graphics2D g,
497
-            final Rectangle bounds, final Image backgroundImage) {
504
+            final Rectangle bounds, final Image backgroundImage, final float opacity) {
498
         final int x = bounds.width / 2 - backgroundImage.getWidth(null) / 2;
505
         final int x = bounds.width / 2 - backgroundImage.getWidth(null) / 2;
499
         final int y = bounds.height / 2 - backgroundImage.getHeight(null) / 2;
506
         final int y = bounds.height / 2 - backgroundImage.getHeight(null) / 2;
507
+        final Composite originalComposite = g.getComposite();
508
+        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
500
         g.drawImage(backgroundImage, x, y, backgroundImage.getWidth(null),
509
         g.drawImage(backgroundImage, x, y, backgroundImage.getWidth(null),
501
                 backgroundImage.getHeight(null), null);
510
                 backgroundImage.getHeight(null), null);
511
+        g.setComposite(originalComposite);
502
     }
512
     }
503
 
513
 
504
     private static void paintStretchedAspectRatioBackground(final Graphics2D g,
514
     private static void paintStretchedAspectRatioBackground(final Graphics2D g,
505
-            final Rectangle bounds, final Image backgroundImage) {
515
+            final Rectangle bounds, final Image backgroundImage, final float opacity) {
506
         final double widthratio = bounds.width
516
         final double widthratio = bounds.width
507
                 / (double) backgroundImage.getWidth(null);
517
                 / (double) backgroundImage.getWidth(null);
508
         final double heightratio = bounds.height
518
         final double heightratio = bounds.height
513
 
523
 
514
         final int x = bounds.width / 2 - width / 2;
524
         final int x = bounds.width / 2 - width / 2;
515
         final int y = bounds.height / 2 - height / 2;
525
         final int y = bounds.height / 2 - height / 2;
526
+        final Composite originalComposite = g.getComposite();
527
+        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
516
         g.drawImage(backgroundImage, x, y, width, height, null);
528
         g.drawImage(backgroundImage, x, y, width, height, null);
529
+        g.setComposite(originalComposite);
517
     }
530
     }
518
 
531
 
519
     private static void paintTiledBackground(final Graphics2D g,
532
     private static void paintTiledBackground(final Graphics2D g,
520
-            final Rectangle bounds, final Image backgroundImage) {
533
+            final Rectangle bounds, final Image backgroundImage, final float opacity) {
521
         final int width = backgroundImage.getWidth(null);
534
         final int width = backgroundImage.getWidth(null);
522
         final int height = backgroundImage.getHeight(null);
535
         final int height = backgroundImage.getHeight(null);
523
 
536
 
526
             return;
539
             return;
527
         }
540
         }
528
 
541
 
542
+        final Composite originalComposite = g.getComposite();
543
+        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
529
         for (int x = 0; x < bounds.width; x += width) {
544
         for (int x = 0; x < bounds.width; x += width) {
530
             for (int y = 0; y < bounds.height; y += height) {
545
             for (int y = 0; y < bounds.height; y += height) {
531
                 g.drawImage(backgroundImage, x, y, width, height, null);
546
                 g.drawImage(backgroundImage, x, y, width, height, null);
532
             }
547
             }
533
         }
548
         }
549
+        g.setComposite(originalComposite);
534
     }
550
     }
535
 
551
 
536
     /**
552
     /**

+ 31
- 2
ui_swing/src/com/dmdirc/addons/ui_swing/textpane/BackgroundPainter.java 파일 보기

60
      */
60
      */
61
     @Nonnull
61
     @Nonnull
62
     private final String optionKey;
62
     private final String optionKey;
63
+    /**
64
+     * Key in the domain to get image opacity level.
65
+     */
66
+    @Nonnull
67
+    private final String opacityKey;
68
+
63
     /** The URL builder to use to find icons. */
69
     /** The URL builder to use to find icons. */
64
     private final URLBuilder urlBuilder;
70
     private final URLBuilder urlBuilder;
65
     /**
71
     /**
74
      * Background option type.
80
      * Background option type.
75
      */
81
      */
76
     private BackgroundOption backgroundOption;
82
     private BackgroundOption backgroundOption;
83
+    /**
84
+     * Background opacity.
85
+     */
86
+    private float opacity;
77
 
87
 
78
     /**
88
     /**
79
      * Creates a new background painter.
89
      * Creates a new background painter.
83
      * @param domain        Domain to retrieve settings from
93
      * @param domain        Domain to retrieve settings from
84
      * @param imageKey      Key for background image
94
      * @param imageKey      Key for background image
85
      * @param optionKey     Key for background type
95
      * @param optionKey     Key for background type
96
+     * @param opacityKey    Key for the opacity
86
      */
97
      */
87
     public BackgroundPainter(
98
     public BackgroundPainter(
88
             final AggregateConfigProvider configManager,
99
             final AggregateConfigProvider configManager,
89
             final URLBuilder urlBuilder,
100
             final URLBuilder urlBuilder,
90
             @Nonnull final String domain, @Nonnull final String imageKey,
101
             @Nonnull final String domain, @Nonnull final String imageKey,
91
-            @Nonnull final String optionKey) {
102
+            @Nonnull final String optionKey, @Nonnull final String opacityKey) {
92
         this.configManager = configManager;
103
         this.configManager = configManager;
93
         this.urlBuilder = urlBuilder;
104
         this.urlBuilder = urlBuilder;
94
         this.domain = domain;
105
         this.domain = domain;
95
         this.imageKey = imageKey;
106
         this.imageKey = imageKey;
96
         this.optionKey = optionKey;
107
         this.optionKey = optionKey;
108
+        this.opacityKey = opacityKey;
97
         configManager.getBinder().bind(this, BackgroundPainter.class);
109
         configManager.getBinder().bind(this, BackgroundPainter.class);
98
     }
110
     }
99
 
111
 
112
         return optionKey;
124
         return optionKey;
113
     }
125
     }
114
 
126
 
127
+    @Nonnull
128
+    protected String getOpacityKey() {
129
+        return opacityKey;
130
+    }
131
+
115
     protected void setBackgroundImage(final Image backgroundImage) {
132
     protected void setBackgroundImage(final Image backgroundImage) {
116
         this.backgroundImage = backgroundImage;
133
         this.backgroundImage = backgroundImage;
117
     }
134
     }
144
         }
161
         }
145
     }
162
     }
146
 
163
 
164
+    @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackgroundopacity")
165
+    public void updateOpacity(final String opacity) {
166
+        try {
167
+            this.opacity = Float.valueOf(opacity);
168
+            if (this.opacity < 0 || this.opacity > 1) {
169
+                this.opacity = 1;
170
+            }
171
+        } catch (IllegalArgumentException ex) {
172
+            this.opacity = 1;
173
+        }
174
+    }
175
+
147
     @Override
176
     @Override
148
     public void paint(final Graphics graphics, final JComponent component) {
177
     public void paint(final Graphics graphics, final JComponent component) {
149
         final Graphics2D g2 = (Graphics2D) graphics;
178
         final Graphics2D g2 = (Graphics2D) graphics;
150
         g2.setColor(component.getBackground());
179
         g2.setColor(component.getBackground());
151
         g2.fill(g2.getClipBounds());
180
         g2.fill(g2.getClipBounds());
152
         UIUtilities.paintBackground(g2, component.getBounds(),
181
         UIUtilities.paintBackground(g2, component.getBounds(),
153
-                backgroundImage, backgroundOption);
182
+                backgroundImage, backgroundOption, opacity);
154
         super.paint(graphics, component);
183
         super.paint(graphics, component);
155
     }
184
     }
156
 
185
 

+ 1
- 1
ui_swing/src/com/dmdirc/addons/ui_swing/textpane/TextPane.java 파일 보기

115
         setLayout(new MigLayout("fill, hidemode 3"));
115
         setLayout(new MigLayout("fill, hidemode 3"));
116
         backgroundPainter = new BackgroundPainter(frame.getContainer().getConfigManager(),
116
         backgroundPainter = new BackgroundPainter(frame.getContainer().getConfigManager(),
117
                 urlBuilder, configDomain, "textpanebackground",
117
                 urlBuilder, configDomain, "textpanebackground",
118
-                "textpanebackgroundoption");
118
+                "textpanebackgroundoption", "textpanebackgroundopacity");
119
         canvas = new TextPaneCanvas(this,
119
         canvas = new TextPaneCanvas(this,
120
                 new CachingDocument<>(document, new AttributedStringMessageMaker()));
120
                 new CachingDocument<>(document, new AttributedStringMessageMaker()));
121
         final JLayer<JComponent> layer = new JLayer<>(canvas);
121
         final JLayer<JComponent> layer = new JLayer<>(canvas);

Loading…
취소
저장