Created
August 10, 2020 17:51
-
-
Save Mr00Anderson/732b6c5530e26b5cca01bb604a2c7964 to your computer and use it in GitHub Desktop.
Simple loading widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.virtual_hex.gdx.engine; | |
| public interface EngTask extends Runnable { | |
| String getName(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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