Change for Timer to allow millis instead of seconds

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Change for Timer to allow millis instead of seconds

Postby Guich » Thu Feb 01, 2018 11:48 am

Hi,

I'm using Timer to implement key repeat for special keys (like backspace, arrows, etc). I do NOT have continuous rendering enabled, so i cannot use the standard approach. The Timer class works fine if i had the hability to specify the time in millis instead of seconds. I had to copy the whole class to my project just to change these two methods below. It would be wonderful if this can be integrated.

Code: Select all
   /** Schedules a task to occur once after the specified delay and then a number of additional times at the specified
    * interval. */
   public Task scheduleTask (Task task, float delay, float interval, int repeatCount) {
      return scheduleTaskInMillis(task, (long)(delay * 1000L), (long)(interval * 1000L), repeatCount);
   }

   /** Schedules a task to occur once after the specified delay IN MILLIS and then a number of additional times at the specified
    * interval. */
   public Task scheduleTaskInMillis (Task task, long delay, long interval, int repeatCount) {
      synchronized (task) {
         if (task.repeatCount != CANCELLED) throw new IllegalArgumentException("The same task may not be scheduled twice.");
         task.executeTimeMillis = System.nanoTime() / 1000000 + delay;
         task.intervalMillis = interval;
         task.repeatCount = repeatCount;
      }
      synchronized (this) {
         tasks.add(task);
      }
      synchronized (threadLock) {
         threadLock.notifyAll();
      }
      return task;
   }
Guich
 
Posts: 58
Joined: Sun Dec 11, 2016 9:36 am

Re: Change for Timer to allow millis instead of seconds

Postby evilentity » Thu Feb 01, 2018 2:43 pm

Why would you do all that? Something like that would be more then enough if you like millis
Code: Select all
public class TimerUtils {
    public Task scheduleTask (Timer timer, Task task, long delayMillis, float intervalSeconds) {
   return timer.scheduleTask(task, delayMillis / 1000f, intervalSeconds, FOREVER);
    }
}


Tho submiting a PR probably woundnt hurt.
Looking for a freelancer? PM me!
evilentity
 
Posts: 4461
Joined: Wed Aug 24, 2011 11:37 am

Re: Change for Timer to allow millis instead of seconds

Postby CoDi » Thu Feb 01, 2018 2:59 pm

what's wrong with

Code: Select all
long delayMillis = ...;
timer.scheduleTask(task, 0.001f * delayMillis, intervalSeconds, repeatCount);

?
Robotality - steamworks4j - @code_disaster - codi^r @ #libgdx
CoDi
 
Posts: 203
Joined: Sun Nov 24, 2013 11:55 am
Location: Hamelin, Germany


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest