mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
Added ability to track loading amount through event.
This commit is contained in:
@@ -3,6 +3,7 @@ package org.toop;
|
||||
import org.toop.app.gui.LocalServerSelector;
|
||||
import org.toop.framework.asset.AssetLoader;
|
||||
import org.toop.framework.asset.AssetManager;
|
||||
import org.toop.framework.asset.events.AssetEvents;
|
||||
import org.toop.framework.asset.resources.TextAsset;
|
||||
import org.toop.framework.audio.SoundManager;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
@@ -10,22 +11,27 @@ import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.framework.networking.NetworkingClientManager;
|
||||
import org.toop.framework.networking.NetworkingInitializationException;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.NotDirectoryException;
|
||||
|
||||
public class Main {
|
||||
static void main(String[] args) {
|
||||
|
||||
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
|
||||
|
||||
new EventFlow().listen(Main::loadingHandler);
|
||||
|
||||
AssetManager.loadAssets(new AssetLoader("app/src/main/resources/assets"));
|
||||
var text = AssetManager.getAllOfType(TextAsset.class).getFirst().getResource();
|
||||
var jpg = AssetManager.getByName("background.jpg");
|
||||
|
||||
System.out.println(jpg.getResource().getFile());
|
||||
|
||||
text.load();
|
||||
|
||||
IO.println(text.getContent());
|
||||
|
||||
var b = new NetworkingClientManager();
|
||||
var c = new SoundManager();
|
||||
new Thread(NetworkingClientManager::new).start();
|
||||
new Thread(SoundManager::new).start();
|
||||
|
||||
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
||||
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", false)).asyncPostEvent();
|
||||
@@ -35,8 +41,12 @@ public class Main {
|
||||
// Thread.sleep(200);
|
||||
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", false)).asyncPostEvent();
|
||||
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", false)).asyncPostEvent();
|
||||
}
|
||||
|
||||
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
|
||||
private static void loadingHandler(AssetEvents.LoadingProgressUpdate update) {
|
||||
int loaded = update.hasLoadedAmount();
|
||||
int total = update.isLoadingAmount();
|
||||
double percent = (total == 0) ? 100.0 : (loaded * 100.0 / total);
|
||||
}
|
||||
|
||||
private static void initSystems() throws NetworkingInitializationException, NotDirectoryException {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package org.toop.framework.asset;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.framework.asset.events.AssetEvents;
|
||||
import org.toop.framework.asset.resources.BaseResource;
|
||||
|
||||
import java.io.File;
|
||||
@@ -11,32 +14,52 @@ import java.util.function.Function;
|
||||
import org.reflections.Reflections;
|
||||
import org.toop.framework.asset.resources.FileExtension;
|
||||
import org.toop.framework.asset.resources.FontAsset;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
|
||||
public class AssetLoader {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(AssetLoader.class);
|
||||
private final List<Asset<? extends BaseResource>> assets = new CopyOnWriteArrayList<>();
|
||||
private final Map<String, Function<File, ? extends BaseResource>> registry = new ConcurrentHashMap<>();
|
||||
|
||||
private volatile int loadedCount = 0;
|
||||
private volatile int totalCount = 0;
|
||||
|
||||
public AssetLoader(File rootFolder) {
|
||||
autoRegisterResources();
|
||||
fileSearcher(rootFolder);
|
||||
autoRegisterResources(); // make sure resources are registered!
|
||||
|
||||
List<File> foundFiles = new ArrayList<>();
|
||||
fileSearcher(rootFolder, foundFiles);
|
||||
this.totalCount = foundFiles.size();
|
||||
loader(foundFiles);
|
||||
}
|
||||
|
||||
public AssetLoader(String rootFolder) {
|
||||
this(new File(rootFolder));
|
||||
}
|
||||
|
||||
public double getProgress() {
|
||||
return (this.totalCount == 0) ? 1.0 : (this.loadedCount / (double) this.totalCount);
|
||||
}
|
||||
|
||||
public int getLoadedCount() {
|
||||
return this.loadedCount;
|
||||
}
|
||||
|
||||
public int getTotalCount() {
|
||||
return this.totalCount;
|
||||
}
|
||||
|
||||
public List<Asset<? extends BaseResource>> getAssets() {
|
||||
return new ArrayList<>(assets);
|
||||
return new ArrayList<>(this.assets);
|
||||
}
|
||||
|
||||
public <T extends BaseResource> void register(String extension, Function<File, T> factory) {
|
||||
registry.put(extension, factory);
|
||||
this.registry.put(extension, factory);
|
||||
}
|
||||
|
||||
private <T extends BaseResource> T resourceMapper(File file, Class<T> type) {
|
||||
String ext = getExtension(file.getName());
|
||||
Function<File, ? extends BaseResource> factory = registry.get(ext);
|
||||
Function<File, ? extends BaseResource> factory = this.registry.get(ext);
|
||||
|
||||
if (factory == null) return null;
|
||||
|
||||
@@ -51,16 +74,34 @@ public class AssetLoader {
|
||||
return type.cast(resource);
|
||||
}
|
||||
|
||||
private void fileSearcher(final File folder) {
|
||||
private void loader(List<File> files) {
|
||||
for (File file : files) {
|
||||
BaseResource resource = resourceMapper(file, BaseResource.class);
|
||||
if (resource != null) {
|
||||
Asset<? extends BaseResource> asset = new Asset<>(file.getName(), resource);
|
||||
this.assets.add(asset);
|
||||
|
||||
if (resource instanceof FontAsset fontAsset) {
|
||||
fontAsset.load();
|
||||
}
|
||||
|
||||
logger.info("Loaded {} from {}", resource.getClass().getSimpleName(), file.getAbsolutePath());
|
||||
|
||||
this.loadedCount++; // TODO: Fix non atmomic operation
|
||||
new EventFlow()
|
||||
.addPostEvent(new AssetEvents.LoadingProgressUpdate(this.loadedCount, this.totalCount))
|
||||
.postEvent();
|
||||
}
|
||||
}
|
||||
logger.info("Loaded {} assets", files.size());
|
||||
}
|
||||
|
||||
private void fileSearcher(final File folder, List<File> foundFiles) {
|
||||
for (File fileEntry : Objects.requireNonNull(folder.listFiles())) {
|
||||
if (fileEntry.isDirectory()) {
|
||||
fileSearcher(fileEntry);
|
||||
fileSearcher(fileEntry, foundFiles);
|
||||
} else {
|
||||
BaseResource resource = resourceMapper(fileEntry, BaseResource.class);
|
||||
if (resource != null) {
|
||||
assets.add(new Asset<>(fileEntry.getName(), resource));
|
||||
if (resource instanceof FontAsset fontAsset) fontAsset.load(); // Autoload if is a FONT for easy css support.
|
||||
}
|
||||
foundFiles.add(fileEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +114,7 @@ public class AssetLoader {
|
||||
if (!cls.isAnnotationPresent(FileExtension.class)) continue;
|
||||
FileExtension annotation = cls.getAnnotation(FileExtension.class);
|
||||
for (String ext : annotation.value()) {
|
||||
registry.put(ext, file -> {
|
||||
this.registry.put(ext, file -> {
|
||||
try {
|
||||
return cls.getConstructor(File.class).newInstance(file);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -33,7 +33,7 @@ public class AssetManager {
|
||||
return list;
|
||||
}
|
||||
|
||||
public Asset<? extends BaseResource> getById(String id) {
|
||||
public static Asset<? extends BaseResource> getById(String id) {
|
||||
for (Asset<? extends BaseResource> asset : assets.values()) {
|
||||
if (asset.getId().toString().equals(id)) {
|
||||
return asset;
|
||||
@@ -42,15 +42,15 @@ public class AssetManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asset<? extends BaseResource> getByName(String name) {
|
||||
public static Asset<? extends BaseResource> getByName(String name) {
|
||||
return assets.get(name);
|
||||
}
|
||||
|
||||
public Optional<Asset<? extends BaseResource>> findByName(String name) {
|
||||
public static Optional<Asset<? extends BaseResource>> findByName(String name) {
|
||||
return Optional.ofNullable(assets.get(name));
|
||||
}
|
||||
|
||||
public void addAsset(Asset<? extends BaseResource> asset) {
|
||||
public static void addAsset(Asset<? extends BaseResource> asset) {
|
||||
assets.put(asset.getName(), asset);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.toop.framework.asset.events;
|
||||
|
||||
import org.toop.framework.eventbus.events.EventWithoutSnowflake;
|
||||
|
||||
public class AssetEvents {
|
||||
public record LoadingProgressUpdate(int hasLoadedAmount, int isLoadingAmount) implements EventWithoutSnowflake {}
|
||||
}
|
||||
Reference in New Issue
Block a user