Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate frame rate cap changes to each ZoneRenderer #4361

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading