Initial temp support. (Thanks, Jack)
[desert.git] / src / net / sourceforge / desert / ui / Timer.java
blob018c2922b91e86c76b31ba74731bca57c2bf9292
1 /*
2 * Copyright (c) 2010 The Desert team
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 package net.sourceforge.desert.ui;
29 import java.awt.event.ActionEvent;
31 /**
33 * @author codistmonk (creation 2010-04-15)
35 @SuppressWarnings("serial")
36 public class Timer extends javax.swing.Timer {
38 private long time;
40 /**
42 * @param framerate
43 * <br>Should not be null
44 * <br>Range: <code>[1 .. Integer.MAX_VALUE]</code>
46 public Timer(final Integer framerate) {
47 super(1000 / framerate, null);
50 @Override
51 protected final void fireActionPerformed(final ActionEvent event) {
52 super.fireActionPerformed(new Event(event, this.computeDeltaTime()));
55 /**
57 * @return the time elapsed since the last call in seconds
58 * <br>A non-null value
59 * <br>Range: <code>[0.0 .. Long.MAX_VALUE / 1000.0]</code>
61 private final float computeDeltaTime() {
62 final long newTime = System.currentTimeMillis();
64 if (this.time == 0L) {
65 this.time = newTime - this.getDelay();
68 final float result = (newTime - this.time) / 1000F;
70 this.time = newTime;
72 return result;
75 /**
77 * @author codistmonk (creation 2010-04-15)
79 @SuppressWarnings("serial")
80 public static final class Event extends ActionEvent {
82 private final float deltaTime;
84 /**
86 * @param timerEvent the original swing timer event
87 * <br>Should not be null
88 * @param deltaTime
89 * <br>Should not be null
90 * <br>Range: <code>[0 .. float.POSITIVE_INFINITY[</code>
92 Event(final ActionEvent timerEvent, final float deltaTime) {
93 super(timerEvent.getSource(), timerEvent.getID(), timerEvent.getActionCommand(), timerEvent.getWhen(), timerEvent.getModifiers());
94 this.deltaTime = deltaTime;
97 /**
99 * @return
100 * <br>A non-null value
101 * <br>Range: <code>[0 .. float.POSITIVE_INFINITY[</code>
103 public float getDeltaTime() {
104 return this.deltaTime;