~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to laf-widget/src/main/java/org/pushingpixels/lafwidget/animation/effects/GhostPaintingUtils.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2007 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 
11
 *    this list of conditions and the following disclaimer in the documentation 
 
12
 *    and/or other materials provided with the distribution. 
 
13
 *     
 
14
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of 
 
15
 *    its contributors may be used to endorse or promote products derived 
 
16
 *    from this software without specific prior written permission. 
 
17
 *     
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
29
 */
 
30
package org.pushingpixels.lafwidget.animation.effects;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.image.BufferedImage;
 
34
import java.util.LinkedHashMap;
 
35
import java.util.Map;
 
36
 
 
37
import javax.swing.*;
 
38
 
 
39
import org.pushingpixels.lafwidget.LafWidgetUtilities;
 
40
import org.pushingpixels.lafwidget.animation.AnimationConfigurationManager;
 
41
import org.pushingpixels.lafwidget.animation.AnimationFacet;
 
42
import org.pushingpixels.trident.Timeline;
 
43
import org.pushingpixels.trident.Timeline.TimelineState;
 
44
 
 
45
/**
 
46
 * Utility class that implements the ghost effects.
 
47
 * 
 
48
 * @author Kirill Grouchnikov
 
49
 */
 
50
public class GhostPaintingUtils {
 
51
        /**
 
52
         * Minimal starting opacity for icon ghosting. Change to a higher value for
 
53
         * debugging / demoing purposes.
 
54
         */
 
55
        public static float MIN_ICON_GHOSTING_ALPHA = 0.15f;
 
56
 
 
57
        /**
 
58
         * Maximal starting opacity for icon ghosting. Change to a higher value for
 
59
         * debugging / demoing purposes.
 
60
         */
 
61
        public static float MAX_ICON_GHOSTING_ALPHA = 0.5f;
 
62
 
 
63
        /**
 
64
         * Minimal starting opacity for press ghosting. Change to a higher value for
 
65
         * debugging / demoing purposes.
 
66
         */
 
67
        public static float MIN_PRESS_GHOSTING_ALPHA = 0.15f;
 
68
 
 
69
        /**
 
70
         * Maximal starting opacity for press ghosting. Change to a higher value for
 
71
         * debugging / demoing purposes.
 
72
         */
 
73
        public static float MAX_PRESS_GHOSTING_ALPHA = 0.3f;
 
74
 
 
75
        /**
 
76
         * Global decay factor.
 
77
         */
 
78
        public static float DECAY_FACTOR = 1.0f;
 
79
 
 
80
        /**
 
81
         * Cache of component ghost images. Used to speed up the rendering of the
 
82
         * ghost effects.
 
83
         */
 
84
        private static LinkedHashMap<String, BufferedImage> componentGhostCache = new LinkedHashMap<String, BufferedImage>() {
 
85
                @Override
 
86
                protected boolean removeEldestEntry(
 
87
                                java.util.Map.Entry<String, BufferedImage> eldest) {
 
88
                        return this.size() > 50;
 
89
                }
 
90
        };
 
91
 
 
92
        /**
 
93
         * Cache of icon ghost images. Used to speed up the rendering of the ghost
 
94
         * effects.
 
95
         */
 
96
        private static LinkedHashMap<String, BufferedImage> iconGhostCache = new LinkedHashMap<String, BufferedImage>() {
 
97
                @Override
 
98
                protected boolean removeEldestEntry(
 
99
                                java.util.Map.Entry<String, BufferedImage> eldest) {
 
100
                        return this.size() > 50;
 
101
                }
 
102
        };
 
103
 
 
104
        /**
 
105
         * Returns a scaled ghost image of the specified component.
 
106
         * 
 
107
         * @param comp
 
108
         *            Component.
 
109
         * @param scaleFactor
 
110
         *            Scale factor.
 
111
         * @return A scaled ghost image of the specified component.
 
112
         */
 
113
        protected static synchronized BufferedImage getComponentGhostImage(
 
114
                        JComponent comp, Timeline ghostPressTimeline, double scaleFactor) {
 
115
                String key = ghostPressTimeline.getTimelinePosition() + ":"
 
116
                                + comp.hashCode() + ":" + scaleFactor;
 
117
 
 
118
                BufferedImage result = componentGhostCache.get(key);
 
119
                if (result == null) {
 
120
                        Rectangle bounds = comp.getBounds();
 
121
 
 
122
                        double iWidth = bounds.width * scaleFactor;
 
123
                        double iHeight = bounds.height * scaleFactor;
 
124
                        result = LafWidgetUtilities.getBlankImage((int) iWidth,
 
125
                                        (int) iHeight);
 
126
                        Graphics2D iGraphics = result.createGraphics();
 
127
                        iGraphics.scale(scaleFactor, scaleFactor);
 
128
                        comp.paint(iGraphics);
 
129
                        iGraphics.dispose();
 
130
 
 
131
                        componentGhostCache.put(key, result);
 
132
                }
 
133
                return result;
 
134
        }
 
135
 
 
136
        /**
 
137
         * Returns a scaled ghost image of the specified icon.
 
138
         * 
 
139
         * @param comp
 
140
         *            Component.
 
141
         * @param icon
 
142
         *            Icon.
 
143
         * @param scaleFactor
 
144
         *            Scale factor.
 
145
         * @return A scaled ghost image of the specified icon.
 
146
         */
 
147
        protected static synchronized BufferedImage getIconGhostImage(
 
148
                        JComponent comp, Timeline ghostRolloverTimeline, Icon icon,
 
149
                        double scaleFactor) {
 
150
                String key = ghostRolloverTimeline.getTimelinePosition() + ":"
 
151
                                + comp.hashCode() + ":" + icon.hashCode() + ":" + scaleFactor;
 
152
 
 
153
                BufferedImage result = iconGhostCache.get(key);
 
154
                if (result == null) {
 
155
                        int oWidth = icon.getIconWidth();
 
156
                        int oHeight = icon.getIconHeight();
 
157
                        double iWidth = oWidth * scaleFactor;
 
158
                        double iHeight = oHeight * scaleFactor;
 
159
                        result = LafWidgetUtilities.getBlankImage((int) iWidth,
 
160
                                        (int) iHeight);
 
161
                        Graphics2D iGraphics = result.createGraphics();
 
162
                        iGraphics.scale(scaleFactor, scaleFactor);
 
163
                        icon.paintIcon(comp, iGraphics, 0, 0);
 
164
                        iGraphics.dispose();
 
165
 
 
166
                        iconGhostCache.put(key, result);
 
167
                }
 
168
                return result;
 
169
        }
 
170
 
 
171
        /**
 
172
         * Paints ghost images on the specified component.
 
173
         * 
 
174
         * @param mainComponent
 
175
         *            Component.
 
176
         * @param g
 
177
         *            Graphics context.
 
178
         */
 
179
        public static void paintGhostImages(Component mainComponent, Graphics g) {
 
180
                if (!mainComponent.isShowing())
 
181
                        return;
 
182
                if (!mainComponent.isVisible())
 
183
                        return;
 
184
                // The following check is for offscreen rendering. The component
 
185
                // may be showing and visible, but have no peer (non displayable).
 
186
                if (!mainComponent.isDisplayable())
 
187
                        return;
 
188
                if (SwingUtilities.getWindowAncestor(mainComponent) == null)
 
189
                        return;
 
190
 
 
191
                Graphics2D graphics = (Graphics2D) g.create();
 
192
 
 
193
                Rectangle mainRect = mainComponent.getBounds();
 
194
                mainRect.setLocation(mainComponent.getLocationOnScreen());
 
195
                if (AnimationConfigurationManager.getInstance().isAnimationAllowed(
 
196
                                AnimationFacet.GHOSTING_BUTTON_PRESS, mainComponent)) {
 
197
                        Map<JComponent, Timeline> runningGhostPressTimelines = GhostingListener
 
198
                                        .getRunningGhostPressTimelines();
 
199
                        for (Map.Entry<JComponent, Timeline> entry : runningGhostPressTimelines
 
200
                                        .entrySet()) {
 
201
                                JComponent comp = entry.getKey();
 
202
                                Timeline timeline = entry.getValue();
 
203
 
 
204
                                if (comp == mainComponent)
 
205
                                        continue;
 
206
 
 
207
                                if (!comp.isShowing())
 
208
                                        continue;
 
209
                                if (!comp.isVisible())
 
210
                                        continue;
 
211
                                // The following check is for offscreen rendering. The component
 
212
                                // may be showing and visible, but have no peer (non
 
213
                                // displayable).
 
214
                                if (!comp.isDisplayable())
 
215
                                        return;
 
216
 
 
217
                                Rectangle compRect = comp.getBounds();
 
218
                                compRect.setLocation(comp.getLocationOnScreen());
 
219
 
 
220
                                int dx = compRect.x - mainRect.x;
 
221
                                int dy = compRect.y - mainRect.y;
 
222
 
 
223
                                compRect.x -= compRect.width / 2;
 
224
                                compRect.y -= compRect.height / 2;
 
225
                                compRect.width *= 2;
 
226
                                compRect.height *= 2;
 
227
 
 
228
                                if (mainRect.intersects(compRect)) {
 
229
                                        float fade = timeline.getTimelinePosition();
 
230
                                        // 0.0 --> 0.3
 
231
                                        // 1.0 --> 0.0
 
232
                                        double start = MAX_PRESS_GHOSTING_ALPHA - 0.0015
 
233
                                                        * compRect.getWidth();
 
234
                                        float coef = Math.max((float) start,
 
235
                                                        MIN_PRESS_GHOSTING_ALPHA);
 
236
                                        float opFactor = coef * (1.0f - DECAY_FACTOR * fade);
 
237
                                        double iFactor = 1.0 + fade;
 
238
 
 
239
                                        graphics.setComposite(LafWidgetUtilities.getAlphaComposite(
 
240
                                                        mainComponent, opFactor));
 
241
 
 
242
                                        Rectangle bounds = comp.getBounds();
 
243
 
 
244
                                        BufferedImage ghost = getComponentGhostImage(comp,
 
245
                                                        timeline, iFactor);
 
246
                                        dx -= ((ghost.getWidth() - bounds.width) / 2);
 
247
                                        dy -= ((ghost.getHeight() - bounds.height) / 2);
 
248
                                        graphics.drawImage(ghost, dx, dy, null);
 
249
                                }
 
250
                        }
 
251
                }
 
252
 
 
253
                if (AnimationConfigurationManager.getInstance().isAnimationAllowed(
 
254
                                AnimationFacet.GHOSTING_ICON_ROLLOVER, mainComponent)) {
 
255
                        Map<JComponent, Timeline> runningGhostRolloverTimelines = GhostingListener
 
256
                                        .getRunningGhostRolloverTimelines();
 
257
                        for (Map.Entry<JComponent, Timeline> entry : runningGhostRolloverTimelines
 
258
                                        .entrySet()) {
 
259
                                JComponent comp = entry.getKey();
 
260
                                Timeline timeline = entry.getValue();
 
261
                                if (comp == mainComponent)
 
262
                                        continue;
 
263
 
 
264
                                if (!(comp instanceof JComponent))
 
265
                                        continue;
 
266
 
 
267
                                JComponent jc = (JComponent) comp;
 
268
 
 
269
                                if (!jc.isShowing())
 
270
                                        continue;
 
271
                                if (!jc.isVisible())
 
272
                                        continue;
 
273
 
 
274
                                Rectangle compRect = jc.getBounds();
 
275
                                compRect.setLocation(jc.getLocationOnScreen());
 
276
 
 
277
                                int dx = compRect.x - mainRect.x;
 
278
                                int dy = compRect.y - mainRect.y;
 
279
 
 
280
                                compRect.x -= compRect.width / 2;
 
281
                                compRect.y -= compRect.height / 2;
 
282
                                compRect.width *= 2;
 
283
                                compRect.height *= 2;
 
284
 
 
285
                                if (mainRect.intersects(compRect)) {
 
286
                                        float fade = timeline.getTimelinePosition();
 
287
                                        // Rectangle bounds = comp.getBounds();
 
288
                                        Icon icon = null;
 
289
                                        Rectangle iconRect = (Rectangle) jc
 
290
                                                        .getClientProperty("icon.bounds");
 
291
                                        if (iconRect != null) {
 
292
                                                if (jc instanceof AbstractButton) {
 
293
                                                        icon = LafWidgetUtilities
 
294
                                                                        .getIcon((AbstractButton) jc);
 
295
                                                } else {
 
296
                                                        icon = (Icon) jc.getClientProperty("icon");
 
297
                                                }
 
298
                                        }
 
299
 
 
300
                                        if ((icon != null) && (iconRect != null)) {
 
301
                                                double iFactor = 1.0 + fade;
 
302
                                                // double iWidth = icon.getIconWidth() * iFactor;
 
303
                                                // double iHeight = icon.getIconHeight() * iFactor;
 
304
                                                // BufferedImage iImage = LafWidgetUtilities
 
305
                                                // .getBlankImage((int) iWidth, (int) iHeight);
 
306
                                                // Graphics2D iGraphics = (Graphics2D) iImage
 
307
                                                // .createGraphics();
 
308
                                                // iGraphics.scale(iFactor, iFactor);
 
309
                                                // icon.paintIcon(comp, iGraphics, 0, 0);
 
310
                                                // iGraphics.dispose();
 
311
 
 
312
                                                BufferedImage iImage = getIconGhostImage(comp,
 
313
                                                                timeline, icon, iFactor);
 
314
 
 
315
                                                // System.out.println(iconRect);
 
316
 
 
317
                                                // BufferedImage bImage = SubstanceCoreUtilities.blur(
 
318
                                                // iImage, 2);
 
319
 
 
320
                                                int iWidth = iImage.getWidth();
 
321
                                                int iHeight = iImage.getHeight();
 
322
                                                dx -= ((iWidth - icon.getIconWidth()) / 2);
 
323
                                                dy -= ((iHeight - icon.getIconHeight()) / 2);
 
324
 
 
325
                                                double start = MAX_ICON_GHOSTING_ALPHA
 
326
                                                                - (MAX_ICON_GHOSTING_ALPHA - MIN_ICON_GHOSTING_ALPHA)
 
327
                                                                * (iWidth - 16) / 48;
 
328
                                                float coef = Math.max((float) start,
 
329
                                                                MIN_ICON_GHOSTING_ALPHA);
 
330
                                                float opFactor = coef * (1.0f - DECAY_FACTOR * fade);
 
331
                                                graphics.setComposite(LafWidgetUtilities
 
332
                                                                .getAlphaComposite(mainComponent, opFactor));
 
333
 
 
334
                                                graphics.drawImage(iImage, dx + iconRect.x, dy
 
335
                                                                + iconRect.y, null);
 
336
                                        }
 
337
                                }
 
338
                        }
 
339
                }
 
340
                graphics.dispose();
 
341
        }
 
342
 
 
343
        /**
 
344
         * Paints the ghost icon inside the bounds of the specified button.
 
345
         * 
 
346
         * @param graphics
 
347
         *            Graphics context.
 
348
         * @param b
 
349
         *            Button.
 
350
         * @param icon
 
351
         *            Icon to paint.
 
352
         */
 
353
        public static void paintGhostIcon(Graphics2D graphics, AbstractButton b,
 
354
                        Icon icon) {
 
355
                paintGhostIcon(graphics, b, icon, (Rectangle) b
 
356
                                .getClientProperty("icon.bounds"));
 
357
        }
 
358
 
 
359
        /**
 
360
         * Paints the ghost icon inside the bounds of the specified button.
 
361
         * 
 
362
         * @param graphics
 
363
         *            Graphics context.
 
364
         * @param b
 
365
         *            Button.
 
366
         * @param iconRectangle
 
367
         *            Rectangle of the button icon.
 
368
         */
 
369
        public static void paintGhostIcon(Graphics2D graphics, AbstractButton b,
 
370
                        Rectangle iconRectangle) {
 
371
                paintGhostIcon(graphics, b, LafWidgetUtilities.getIcon(b),
 
372
                                iconRectangle);
 
373
        }
 
374
 
 
375
        /**
 
376
         * Paints the ghost icon inside the bounds of the specified button.
 
377
         * 
 
378
         * @param graphics
 
379
         *            Graphics context.
 
380
         * @param b
 
381
         *            Button.
 
382
         * @param icon
 
383
         *            Icon to paint.
 
384
         * @param iconRectangle
 
385
         *            Rectangle of the button icon.
 
386
         */
 
387
        public static void paintGhostIcon(Graphics2D graphics, Component b,
 
388
                        Icon icon, Rectangle iconRectangle) {
 
389
                // System.out.println(b.getText() + ":" + icon + ":" + iconRectangle);
 
390
                if (!AnimationConfigurationManager.getInstance().isAnimationAllowed(
 
391
                                AnimationFacet.GHOSTING_ICON_ROLLOVER, b)) {
 
392
                        return;
 
393
                }
 
394
 
 
395
                if (!(b instanceof JComponent))
 
396
                        return;
 
397
 
 
398
                GhostingListener gl = (GhostingListener) ((JComponent) b)
 
399
                                .getClientProperty(GhostingListener.GHOST_LISTENER_KEY);
 
400
                if (gl == null)
 
401
                        return;
 
402
 
 
403
                Timeline ghostRolloverTimeline = gl.getGhostIconRolloverTimeline();
 
404
 
 
405
                if (ghostRolloverTimeline.getState() != TimelineState.IDLE) {
 
406
                        float fade = ghostRolloverTimeline.getTimelinePosition();
 
407
                        if ((icon != null) && (iconRectangle != null)) {
 
408
                                double iFactor = 1.0 + fade;
 
409
                                BufferedImage iImage = getIconGhostImage((JComponent) b,
 
410
                                                ghostRolloverTimeline, icon, iFactor);
 
411
 
 
412
                                int iWidth = iImage.getWidth();
 
413
                                int iHeight = iImage.getHeight();
 
414
                                int dx = ((iWidth - icon.getIconWidth()) / 2);
 
415
                                int dy = ((iHeight - icon.getIconHeight()) / 2);
 
416
 
 
417
                                double start = MAX_ICON_GHOSTING_ALPHA
 
418
                                                - (MAX_ICON_GHOSTING_ALPHA - MIN_ICON_GHOSTING_ALPHA)
 
419
                                                * (iWidth - 16) / 48;
 
420
                                float coef = Math.max((float) start, MIN_ICON_GHOSTING_ALPHA);
 
421
                                float opFactor = coef * (1.0f - DECAY_FACTOR * fade);
 
422
                                graphics.setComposite(LafWidgetUtilities.getAlphaComposite(b,
 
423
                                                opFactor));
 
424
 
 
425
                                graphics.drawImage(iImage, iconRectangle.x - dx,
 
426
                                                iconRectangle.y - dy, null);
 
427
                        }
 
428
                }
 
429
        }
 
430
}