Skip to content

Commit

Permalink
Merge pull request #4361 from kwvanderlinde/bugfix/4335-apply-frame-r…
Browse files Browse the repository at this point in the history
…ate-preference-to-all-maps

Propagate frame rate cap changes  to each ZoneRenderer
  • Loading branch information
cwisniew authored Nov 3, 2023
2 parents e2553ac + 93ca78b commit 42803ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/main/java/net/rptools/maptool/client/DebounceExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class DebounceExecutor {
Executors.newSingleThreadScheduledExecutor(threadFactory);

/** The time, in milliseconds, during which to throttle subsequent requests to run the task. */
private final long delay;
private final AtomicLong delay;

/** A {@link Runnable} representing the task to be managed. */
private final Runnable task;
Expand All @@ -71,14 +71,20 @@ public class DebounceExecutor {
* @param task The task to be executed after the <i>delay</i> elapses.
*/
public DebounceExecutor(long delay, @Nonnull Runnable task) {
this.delay = delay;
this.delay = new AtomicLong(delay);
this.task = task;
}

public void setDelay(long delay) {
this.delay.set(delay);
}

/** Dispatches a task to be executed by this {@link DebounceExecutor} instance. */
public void dispatch() {
if (this.delay < 1) {
this.task.run();
final var delay = this.delay.get();
if (delay < 1) {
// Have to run via the executor otherwise it's possible the task runs in parallel with itself.
this.executor.schedule(this.task, 0, TimeUnit.MILLISECONDS);
return;
}

Expand All @@ -93,7 +99,7 @@ public void dispatch() {
final var now = System.currentTimeMillis();
if (now >= taskScheduledTime) {
// This request is not redundant, so we need to schedule it.
final var nextTargetTime = Math.max(now, taskScheduledTime + this.delay);
final var nextTargetTime = Math.max(now, taskScheduledTime + delay);
// If this check fails, that means someone beat us to the punch and our task is now redundant.
if (this.taskScheduledTime.compareAndSet(taskScheduledTime, nextTargetTime)) {
this.executor.schedule(this.task, nextTargetTime - now, TimeUnit.MILLISECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ public void focusLost(FocusEvent e) {
@Override
protected void storeNumericValue(Integer value) {
AppPreferences.setFrameRateCap(value);

for (final var renderer : MapTool.getFrame().getZoneRenderers()) {
renderer.setFrameRateCap(value);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ public ZoneRenderer(Zone zone) {
}
this.zone = zone;

// The interval, in milliseconds, during which calls to repaint() will be debounced.
int repaintDebounceInterval = 1000 / AppPreferences.getFrameRateCap();
repaintDebouncer = new DebounceExecutor(repaintDebounceInterval, this::repaint);
repaintDebouncer = new DebounceExecutor(1000 / AppPreferences.getFrameRateCap(), this::repaint);

setFocusable(true);
setZoneScale(new Scale());
Expand Down Expand Up @@ -223,6 +221,10 @@ public void mouseMoved(MouseEvent e) {
new MapToolEventBus().getMainEventBus().register(this);
}

public void setFrameRateCap(int cap) {
this.repaintDebouncer.setDelay(1000 / cap);
}

public void setAutoResizeStamp(boolean value) {
this.autoResizeStamp = value;
}
Expand Down

0 comments on commit 42803ca

Please sign in to comment.