Support loading card database using background thread

This commit is contained in:
drdev
2014-02-27 06:39:35 +00:00
parent 9a22f8d8c3
commit 72fd1819e3
4 changed files with 55 additions and 50 deletions

1
.gitignore vendored
View File

@@ -92,6 +92,7 @@ forge-gui/tools/oracleScript.log
forge-m-android/bin forge-m-android/bin
forge-m-base/bin forge-m-base/bin
forge-m-desktop/bin forge-m-desktop/bin
forge-m-desktop/res
forge-net/target forge-net/target
/forge.profile.properties /forge.profile.properties
/nbactions.xml /nbactions.xml

View File

@@ -3,8 +3,6 @@ package forge;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Stack; import java.util.Stack;
import javax.swing.GroupLayout.Alignment;
import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
@@ -55,40 +53,58 @@ public class Forge implements ApplicationListener {
public void create() { public void create() {
batch = new SpriteBatch(); batch = new SpriteBatch();
shapeRenderer = new ShapeRenderer(); shapeRenderer = new ShapeRenderer();
Gdx.graphics.setContinuousRendering(false); //save power consumption by disabling continuous rendering
Gdx.input.setInputProcessor(new FGestureDetector());
splashScreen = new SplashScreen(); splashScreen = new SplashScreen();
FSkin.loadLight("journeyman", splashScreen); FSkin.loadLight("journeyman", splashScreen);
CardStorageReader.ProgressObserver progressBarBridge = new CardStorageReader.ProgressObserver() { // Loads card database on background thread (using progress bar to report progress)
final FProgressBar bar = splashScreen.getProgressBar(); new Thread(new Runnable() {
@Override @Override
public void setOperationName(final String name, final boolean usePercents) { public void run() {
final CardStorageReader.ProgressObserver progressBarBridge = new CardStorageReader.ProgressObserver() {
final FProgressBar bar = splashScreen.getProgressBar();
@Override
public void setOperationName(final String name, final boolean usePercents) {
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
bar.setDescription(name);
bar.setPercentMode(usePercents);
}
});
}
@Override
public void report(final int current, final int total) {
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
bar.setMaximum(total);
bar.setValue(current);
}
});
}
};
final CardStorageReader reader = new CardStorageReader(Constants.CARD_DATA_DIR, progressBarBridge, null);
magicDb = new StaticData(reader, "res/editions", "res/blockdata");
Gdx.app.postRunnable(new Runnable() { Gdx.app.postRunnable(new Runnable() {
@Override @Override
public void run() { public void run() {
bar.setDescription(name); afterDbLoaded();
bar.setPercentMode(usePercents);
} }
}); });
} }
}).start();
}
@Override private void afterDbLoaded() {
public void report(int current, int total) { FSkin.loadFull(splashScreen);
if (total != bar.getMaximum()) {
bar.setMaximum(total);
}
bar.setValueThreadSafe(current);
}
};
// Loads all cards (using progress bar). Gdx.graphics.setContinuousRendering(false); //save power consumption by disabling continuous rendering once assets loaded
/*final CardStorageReader reader = new CardStorageReader(Constants.CARD_DATA_DIR, progressBarBridge, null); Gdx.input.setInputProcessor(new FGestureDetector());
magicDb = new StaticData(reader, "res/editions", "res/blockdata");*/ openScreen(new HomeScreen());
splashScreen = null;
//FSkin.loadFull(true);
//openScreen(new HomeScreen());
} }
public static void showMenu() { public static void showMenu() {

View File

@@ -13,9 +13,9 @@ public class FProgressBar extends FDisplayObject {
private static FSkinFont MSG_FONT; private static FSkinFont MSG_FONT;
private long startMillis = 0; private long startMillis = 0;
private int tempVal = 0, etaSecs = 0, maximum = 0, value = 0; private int etaSecs = 0, maximum = 0, value = 0;
private String desc = ""; private String desc = "";
private String tempMsg, message; private String message;
private boolean showETA = true; private boolean showETA = true;
private boolean showCount = true; private boolean showCount = true;
@@ -37,50 +37,35 @@ public class FProgressBar extends FDisplayObject {
message = s0; message = s0;
} }
private final Runnable barIncrementor = new Runnable() { /** Increments bar. */
@Override public void setValue(int value0) {
public void run() { value = value0;
value = tempVal;
message = tempMsg;
}
};
/** Increments bar, thread safe. Calculations executed on separate thread. */
public void setValueThreadSafe(int value) {
//GuiUtils.checkEDT("FProgressBar$increment", false);
tempVal = value;
// String.format leads to StringBuilder anyway. Direct calls will be faster // String.format leads to StringBuilder anyway. Direct calls will be faster
StringBuilder sb = new StringBuilder(desc); StringBuilder sb = new StringBuilder(desc);
if (showCount) { if (showCount) {
sb.append(" "); sb.append(" ");
if (percentMode) { if (percentMode) {
sb.append(100 * tempVal / maximum).append("%"); sb.append(100 * value / maximum).append("%");
} }
else { else {
sb.append(tempVal).append(" of ").append(maximum); sb.append(value).append(" of ").append(maximum);
} }
} }
if (showETA) { if (showETA) {
calculateETA(tempVal); calculateETA(value);
sb.append(", ETA").append(String.format("%02d:%02d:%02d", etaSecs / 3600, (etaSecs % 3600) / 60, etaSecs % 60 + 1)); sb.append(", ETA").append(String.format("%02d:%02d:%02d", etaSecs / 3600, (etaSecs % 3600) / 60, etaSecs % 60 + 1));
} }
tempMsg = sb.toString(); message = sb.toString();
// When calculations finished; EDT can be used.
//SwingUtilities.invokeLater(barIncrementor);
barIncrementor.run();
} }
/** Resets the various values required for this class. Must be called from EDT. */ /** Resets the various values required for this class. Must be called from EDT. */
public void reset() { public void reset() {
//FThreads.assertExecutedByEdt(true); value = 0;
startMillis = new Date().getTime(); startMillis = new Date().getTime();
setShowETA(true); setShowETA(true);
setShowCount(true); setShowCount(true);
value = 100;
maximum = 200;
} }
/** @param b0   Boolean, show the ETA statistic or not */ /** @param b0   Boolean, show the ETA statistic or not */
@@ -130,7 +115,7 @@ public class FProgressBar extends FDisplayObject {
//draw message //draw message
if (MSG_FONT == null) { //must wait to initialize until after FSkin initialized if (MSG_FONT == null) { //must wait to initialize until after FSkin initialized
MSG_FONT = FSkinFont.get(12); MSG_FONT = FSkinFont.get(11);
} }
g.drawText(message, MSG_FONT, FORE_COLOR, 0, 0, w, h, false, HAlignment.CENTER, true); g.drawText(message, MSG_FONT, FORE_COLOR, 0, 0, w, h, false, HAlignment.CENTER, true);
if (selWidth > 0 && !SEL_FORE_COLOR.equals(FORE_COLOR)) { if (selWidth > 0 && !SEL_FORE_COLOR.equals(FORE_COLOR)) {

View File

@@ -17,12 +17,15 @@
*/ */
package forge.utils; package forge.utils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Application.ApplicationType;
public final class Constants { public final class Constants {
public static final String PROFILE_FILE = "forge.profile.properties"; public static final String PROFILE_FILE = "forge.profile.properties";
public static final String PROFILE_TEMPLATE_FILE = PROFILE_FILE + ".example"; public static final String PROFILE_TEMPLATE_FILE = PROFILE_FILE + ".example";
// data that is only in the program dir // data that is only in the program dir
private static final String _ASSETS_ROOT = "assets/"; private static final String _ASSETS_ROOT = Gdx.app.getType() == ApplicationType.Desktop ? "bin/" : "assets/";
private static final String _LIST_DIR = _ASSETS_ROOT + "lists/"; private static final String _LIST_DIR = _ASSETS_ROOT + "lists/";
public static final String KEYWORD_LIST_FILE = _LIST_DIR + "NonStackingKWList.txt"; public static final String KEYWORD_LIST_FILE = _LIST_DIR + "NonStackingKWList.txt";
public static final String TYPE_LIST_FILE = _LIST_DIR + "TypeLists.txt"; public static final String TYPE_LIST_FILE = _LIST_DIR + "TypeLists.txt";