Browse Source

Add opacity to textpane backgrounds

Also fix an invalid default option for the "background option"
pull/433/head
Greg Holmes 8 years ago
parent
commit
f40c0c89ad

+ 2
- 1
ui_swing/plugin.config View File

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

+ 25
- 9
ui_swing/src/com/dmdirc/addons/ui_swing/UIUtilities.java View File

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

@@ -60,6 +60,12 @@ public class BackgroundPainter extends LayerUI<JComponent> {
60 60
      */
61 61
     @Nonnull
62 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 69
     /** The URL builder to use to find icons. */
64 70
     private final URLBuilder urlBuilder;
65 71
     /**
@@ -74,6 +80,10 @@ public class BackgroundPainter extends LayerUI<JComponent> {
74 80
      * Background option type.
75 81
      */
76 82
     private BackgroundOption backgroundOption;
83
+    /**
84
+     * Background opacity.
85
+     */
86
+    private float opacity;
77 87
 
78 88
     /**
79 89
      * Creates a new background painter.
@@ -83,17 +93,19 @@ public class BackgroundPainter extends LayerUI<JComponent> {
83 93
      * @param domain        Domain to retrieve settings from
84 94
      * @param imageKey      Key for background image
85 95
      * @param optionKey     Key for background type
96
+     * @param opacityKey    Key for the opacity
86 97
      */
87 98
     public BackgroundPainter(
88 99
             final AggregateConfigProvider configManager,
89 100
             final URLBuilder urlBuilder,
90 101
             @Nonnull final String domain, @Nonnull final String imageKey,
91
-            @Nonnull final String optionKey) {
102
+            @Nonnull final String optionKey, @Nonnull final String opacityKey) {
92 103
         this.configManager = configManager;
93 104
         this.urlBuilder = urlBuilder;
94 105
         this.domain = domain;
95 106
         this.imageKey = imageKey;
96 107
         this.optionKey = optionKey;
108
+        this.opacityKey = opacityKey;
97 109
         configManager.getBinder().bind(this, BackgroundPainter.class);
98 110
     }
99 111
 
@@ -112,6 +124,11 @@ public class BackgroundPainter extends LayerUI<JComponent> {
112 124
         return optionKey;
113 125
     }
114 126
 
127
+    @Nonnull
128
+    protected String getOpacityKey() {
129
+        return opacityKey;
130
+    }
131
+
115 132
     protected void setBackgroundImage(final Image backgroundImage) {
116 133
         this.backgroundImage = backgroundImage;
117 134
     }
@@ -144,13 +161,25 @@ public class BackgroundPainter extends LayerUI<JComponent> {
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 176
     @Override
148 177
     public void paint(final Graphics graphics, final JComponent component) {
149 178
         final Graphics2D g2 = (Graphics2D) graphics;
150 179
         g2.setColor(component.getBackground());
151 180
         g2.fill(g2.getClipBounds());
152 181
         UIUtilities.paintBackground(g2, component.getBounds(),
153
-                backgroundImage, backgroundOption);
182
+                backgroundImage, backgroundOption, opacity);
154 183
         super.paint(graphics, component);
155 184
     }
156 185
 

+ 1
- 1
ui_swing/src/com/dmdirc/addons/ui_swing/textpane/TextPane.java View File

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

Loading…
Cancel
Save