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-base/bin
forge-m-desktop/bin
forge-m-desktop/res
forge-net/target
/forge.profile.properties
/nbactions.xml

View File

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

View File

@@ -13,9 +13,9 @@ public class FProgressBar extends FDisplayObject {
private static FSkinFont MSG_FONT;
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 tempMsg, message;
private String message;
private boolean showETA = true;
private boolean showCount = true;
@@ -37,50 +37,35 @@ public class FProgressBar extends FDisplayObject {
message = s0;
}
private final Runnable barIncrementor = new Runnable() {
@Override
public void run() {
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;
/** Increments bar. */
public void setValue(int value0) {
value = value0;
// String.format leads to StringBuilder anyway. Direct calls will be faster
StringBuilder sb = new StringBuilder(desc);
if (showCount) {
sb.append(" ");
if (percentMode) {
sb.append(100 * tempVal / maximum).append("%");
sb.append(100 * value / maximum).append("%");
}
else {
sb.append(tempVal).append(" of ").append(maximum);
sb.append(value).append(" of ").append(maximum);
}
}
if (showETA) {
calculateETA(tempVal);
calculateETA(value);
sb.append(", ETA").append(String.format("%02d:%02d:%02d", etaSecs / 3600, (etaSecs % 3600) / 60, etaSecs % 60 + 1));
}
tempMsg = sb.toString();
// When calculations finished; EDT can be used.
//SwingUtilities.invokeLater(barIncrementor);
barIncrementor.run();
message = sb.toString();
}
/** Resets the various values required for this class. Must be called from EDT. */
public void reset() {
//FThreads.assertExecutedByEdt(true);
value = 0;
startMillis = new Date().getTime();
setShowETA(true);
setShowCount(true);
value = 100;
maximum = 200;
}
/** @param b0   Boolean, show the ETA statistic or not */
@@ -130,7 +115,7 @@ public class FProgressBar extends FDisplayObject {
//draw message
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);
if (selWidth > 0 && !SEL_FORE_COLOR.equals(FORE_COLOR)) {

View File

@@ -17,12 +17,15 @@
*/
package forge.utils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Application.ApplicationType;
public final class Constants {
public static final String PROFILE_FILE = "forge.profile.properties";
public static final String PROFILE_TEMPLATE_FILE = PROFILE_FILE + ".example";
// 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/";
public static final String KEYWORD_LIST_FILE = _LIST_DIR + "NonStackingKWList.txt";
public static final String TYPE_LIST_FILE = _LIST_DIR + "TypeLists.txt";