summaryrefslogtreecommitdiff
path: root/NET/worlds/console/AnimationButton.java
blob: 22b1aa809c661e5ffe8bb7f55483444b67665385 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package NET.worlds.console;

import NET.worlds.scape.FrameEvent;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;

public class AnimationButton extends ImageCanvas implements MainCallback, FramePart, DialogDisabled {
   private static final long serialVersionUID = 5605353426438467905L;
   private static final int frameInterval = 33;
   private String movieName;
   private int frameCount;
   private int curFrame;
   private int width;
   private int height;
   private boolean playVideoNext;
   private int videoHandle = -1;
   private boolean isDialogDisabled;

   public AnimationButton(String imageName, int frameCount, String movieName) {
      super(imageName);
      this.frameCount = frameCount;
      this.movieName = movieName;
   }

   @Override
   public void paint(Graphics g) {
      g.clipRect(0, 0, this.width, this.height);
      g.drawImage(this.image_, -this.width * this.curFrame, 0, null);
   }

   @Override
   public Dimension preferredSize() {
      Dimension d = super.preferredSize();
      return new Dimension(this.width = d.width / this.frameCount, this.height = d.height);
   }

   @Override
   public Dimension minimumSize() {
      return this.preferredSize();
   }

   @Override
   public void mainCallback() {
      if (this.videoHandle == -1) {
         this.videoHandle = Window.playVideoClip(this, this.movieName);
         if (this.videoHandle == -1) {
            this.videoHandle = Window.playVideoClip(this, "..\\" + this.movieName);
            if (this.videoHandle == -1) {
               Main.unregister(this);
            }
         }
      } else if (!Window.isVideoPlaying(this.videoHandle)) {
         Main.unregister(this);
         this.videoHandle = -1;
         this.repaint();
      }
   }

   @Override
   public boolean mouseDown(Event e, int x, int y) {
      if (this.isDialogDisabled) {
         return false;
      } else {
         if (this.playVideoNext) {
            Main.register(this);
            this.playVideoNext = false;
         } else if (this.videoHandle == -1) {
            Graphics g = this.getGraphics();
            g.clipRect(0, 0, this.width, this.height);
            int nextFrameIncr = this.curFrame == 0 ? 1 : -1;
            long startTime = System.currentTimeMillis();
            long frameTime = 0L;

            for (int i = 1; i < this.frameCount; i++) {
               this.curFrame += nextFrameIncr;
               g.drawImage(this.image_, -this.width * this.curFrame, 0, null);
               long elapsedTime = System.currentTimeMillis() - startTime;
               frameTime += 33L;
               long sleepTime = frameTime - elapsedTime;
               if (sleepTime > 0L) {
                  try {
                     Thread.sleep(sleepTime);
                  } catch (InterruptedException var16) {
                  }
               }
            }

            if (this.curFrame == this.frameCount - 1) {
               this.playVideoNext = true;
            }

            g.dispose();
         }

         return true;
      }
   }

   @Override
   public void activate(Console c, Container f, Console prev) {
   }

   @Override
   public void deactivate() {
   }

   @Override
   public boolean action(Event event, Object what) {
      return false;
   }

   @Override
   public boolean handle(FrameEvent f) {
      return false;
   }

   @Override
   public void dialogDisable(boolean disable) {
      this.isDialogDisabled = disable;
   }
}