2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.ui;
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.util.Disposer;
20 import org.jetbrains.annotations.NotNull;
25 public class IconAnimator extends Animator implements Icon {
26 private final Icon myBase;
27 @NotNull private final PaintCallback myCallback;
28 private final Color[] myAlphas;
30 private boolean myActive;
32 public IconAnimator(@NotNull Disposable parent, @NotNull Icon base, @NotNull PaintCallback callback) {
33 super("IconAnimator{" + System.currentTimeMillis()+"}", base.getIconWidth(), 2000, true);
34 Disposer.register(parent, this);
36 myCallback = callback;
37 myAlphas = initAlphas();
40 private Color[] initAlphas() {
41 Color[] colors = new Color[myBase.getIconWidth()];
42 for (int i = 0; i < colors.length; i++) {
43 double a = 2 * Math.PI * i / colors.length;
44 float v = (1 - (float)Math.sin(a)) / 2;
45 //noinspection UseJBColor
46 colors[i] = new Color(v, v, v, v * v * v / 16 + .05F);
51 public void setActive(boolean active) {
52 if (myActive == active) return;
56 if (isRunning() ^ myActive) {
62 myCallback.paintNow(this);
67 public void paintNow(int frame, int totalFrames, int cycle) {
69 myCallback.paintNow(this);
73 public void paintIcon(Component component, Graphics graphics, int x, int y) {
74 myBase.paintIcon(component, graphics, x, y);
75 if (!myActive) return;
77 for (int i = 0; i < myAlphas.length; i++) {
78 Color alpha = myAlphas[(i + myFrame) % myAlphas.length];
79 graphics.setColor(alpha);
80 graphics.drawRect(x + i, y, 1, getIconHeight());
85 public int getIconWidth() {
86 return myBase.getIconWidth();
90 public int getIconHeight() {
91 return myBase.getIconHeight();
94 public interface PaintCallback {
95 void paintNow(Icon icon);