Skip to content

Instantly share code, notes, and snippets.

@Mr00Anderson
Created August 10, 2020 17:51
Show Gist options
  • Select an option

  • Save Mr00Anderson/732b6c5530e26b5cca01bb604a2c7964 to your computer and use it in GitHub Desktop.

Select an option

Save Mr00Anderson/732b6c5530e26b5cca01bb604a2c7964 to your computer and use it in GitHub Desktop.
Simple loading widget
package com.virtual_hex.gdx.engine;
public abstract class AbstractEngTask implements EngTask {
public String name;
public AbstractEngTask() {
}
public AbstractEngTask(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
package com.virtual_hex.gdx.engine;
public interface EngTask extends Runnable {
String getName();
}
package com.virtual_hex.gdx.engine;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.utils.Align;
import com.kotcrab.vis.ui.widget.VisLabel;
import com.kotcrab.vis.ui.widget.VisProgressBar;
import com.kotcrab.vis.ui.widget.VisTable;
import com.kotcrab.vis.ui.widget.VisWindow;
import com.virtual_hex.utils.StringFormatter;
import java.util.Collections;
import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
public class LoadingWindow extends VisWindow {
public static final int UPDATE_TIME_DEFAULT = -1;
private final String taskName;
public Deque<EngTask> runnableDeque;
public long updateTime;
public long totalTaskCount;
public long completeTaskCount;
public transient VisProgressBar progressBar;
public transient VisLabel labelField;
private int totalTaskTime;
/**
*
* @param title
* @param taskName
* @param tasks
*/
public LoadingWindow(String title, String taskName, EngTask... tasks) {
this(title, taskName, -1, tasks);
} // -1 Use this to indicate update one task per frame
public LoadingWindow(String title, String taskName, long updateTime, EngTask... tasks) {
super(title);
this.taskName = taskName;
this.updateTime = updateTime;
this.runnableDeque = new ConcurrentLinkedDeque<>();
Collections.addAll(runnableDeque, tasks);
totalTaskCount = tasks.length;
// Update the widgets internals here
centerWindow();
VisTable innerTable = new VisTable(true);
progressBar = new VisProgressBar(0, totalTaskCount, 1, false);
labelField = new VisLabel("Gathering power...");
innerTable.add(progressBar).align(Align.center).space(10);
innerTable.row();
innerTable.add(labelField).space(10);
add(innerTable).padTop(10);
pack();
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
progressBar.setValue(completeTaskCount);
// Use this time to do some loading
if(updateTime == UPDATE_TIME_DEFAULT){
long taskTime = updateOneTask();
} else {
long totalTicketTime = updateTime; // Create a temp var to count down from
do {
totalTicketTime -= updateOneTask();
} while (totalTicketTime >= 0);
}
}
private long updateOneTask() {
EngTask task = this.runnableDeque.poll();
if(task == null) {
remove();
return 0;
}
labelField.setText(StringFormatter.format("Loading... {}", task.getName()));
pack();
long taskTime;
long startTime = System.currentTimeMillis();
task.run();
long endTime = System.currentTimeMillis();
taskTime = endTime - startTime;
completeTaskCount++;
totalTaskTime += taskTime;
return taskTime;
}
}
package com.virtual_hex.gdx.engine;
import com.virtual_hex.utils.StringFormatter;
import java.util.SplittableRandom;
public class RandomDelayEngTask extends AbstractEngTask {
public int delay;
public RandomDelayEngTask (String rootTaskName, int delay){
this.delay = delay;
this.name = StringFormatter.format("{} random delay task. Thread.Sleep({})", rootTaskName, delay);
}
@Override
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment