mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +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.app.gui.LocalServerSelector;
|
||||||
import org.toop.framework.asset.AssetLoader;
|
import org.toop.framework.asset.AssetLoader;
|
||||||
import org.toop.framework.asset.AssetManager;
|
import org.toop.framework.asset.AssetManager;
|
||||||
|
import org.toop.framework.asset.events.AssetEvents;
|
||||||
import org.toop.framework.asset.resources.TextAsset;
|
import org.toop.framework.asset.resources.TextAsset;
|
||||||
import org.toop.framework.audio.SoundManager;
|
import org.toop.framework.audio.SoundManager;
|
||||||
import org.toop.framework.audio.events.AudioEvents;
|
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.NetworkingClientManager;
|
||||||
import org.toop.framework.networking.NetworkingInitializationException;
|
import org.toop.framework.networking.NetworkingInitializationException;
|
||||||
|
|
||||||
import javax.sound.sampled.*;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.NotDirectoryException;
|
import java.nio.file.NotDirectoryException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static void main(String[] args) {
|
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"));
|
AssetManager.loadAssets(new AssetLoader("app/src/main/resources/assets"));
|
||||||
var text = AssetManager.getAllOfType(TextAsset.class).getFirst().getResource();
|
var text = AssetManager.getAllOfType(TextAsset.class).getFirst().getResource();
|
||||||
|
var jpg = AssetManager.getByName("background.jpg");
|
||||||
|
|
||||||
|
System.out.println(jpg.getResource().getFile());
|
||||||
|
|
||||||
text.load();
|
text.load();
|
||||||
|
|
||||||
IO.println(text.getContent());
|
IO.println(text.getContent());
|
||||||
|
|
||||||
var b = new NetworkingClientManager();
|
new Thread(NetworkingClientManager::new).start();
|
||||||
var c = new SoundManager();
|
new Thread(SoundManager::new).start();
|
||||||
|
|
||||||
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
||||||
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", false)).asyncPostEvent();
|
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", false)).asyncPostEvent();
|
||||||
@@ -35,8 +41,12 @@ public class Main {
|
|||||||
// Thread.sleep(200);
|
// Thread.sleep(200);
|
||||||
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", false)).asyncPostEvent();
|
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", false)).asyncPostEvent();
|
||||||
// new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.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 {
|
private static void initSystems() throws NetworkingInitializationException, NotDirectoryException {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.toop.framework.asset;
|
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 org.toop.framework.asset.resources.BaseResource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -11,32 +14,52 @@ import java.util.function.Function;
|
|||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.toop.framework.asset.resources.FileExtension;
|
import org.toop.framework.asset.resources.FileExtension;
|
||||||
import org.toop.framework.asset.resources.FontAsset;
|
import org.toop.framework.asset.resources.FontAsset;
|
||||||
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
|
||||||
public class AssetLoader {
|
public class AssetLoader {
|
||||||
|
private static final Logger logger = LogManager.getLogger(AssetLoader.class);
|
||||||
private final List<Asset<? extends BaseResource>> assets = new CopyOnWriteArrayList<>();
|
private final List<Asset<? extends BaseResource>> assets = new CopyOnWriteArrayList<>();
|
||||||
private final Map<String, Function<File, ? extends BaseResource>> registry = new ConcurrentHashMap<>();
|
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) {
|
public AssetLoader(File rootFolder) {
|
||||||
autoRegisterResources();
|
autoRegisterResources(); // make sure resources are registered!
|
||||||
fileSearcher(rootFolder);
|
|
||||||
|
List<File> foundFiles = new ArrayList<>();
|
||||||
|
fileSearcher(rootFolder, foundFiles);
|
||||||
|
this.totalCount = foundFiles.size();
|
||||||
|
loader(foundFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AssetLoader(String rootFolder) {
|
public AssetLoader(String rootFolder) {
|
||||||
this(new File(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() {
|
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) {
|
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) {
|
private <T extends BaseResource> T resourceMapper(File file, Class<T> type) {
|
||||||
String ext = getExtension(file.getName());
|
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;
|
if (factory == null) return null;
|
||||||
|
|
||||||
@@ -51,16 +74,34 @@ public class AssetLoader {
|
|||||||
return type.cast(resource);
|
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())) {
|
for (File fileEntry : Objects.requireNonNull(folder.listFiles())) {
|
||||||
if (fileEntry.isDirectory()) {
|
if (fileEntry.isDirectory()) {
|
||||||
fileSearcher(fileEntry);
|
fileSearcher(fileEntry, foundFiles);
|
||||||
} else {
|
} else {
|
||||||
BaseResource resource = resourceMapper(fileEntry, BaseResource.class);
|
foundFiles.add(fileEntry);
|
||||||
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.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,7 +114,7 @@ public class AssetLoader {
|
|||||||
if (!cls.isAnnotationPresent(FileExtension.class)) continue;
|
if (!cls.isAnnotationPresent(FileExtension.class)) continue;
|
||||||
FileExtension annotation = cls.getAnnotation(FileExtension.class);
|
FileExtension annotation = cls.getAnnotation(FileExtension.class);
|
||||||
for (String ext : annotation.value()) {
|
for (String ext : annotation.value()) {
|
||||||
registry.put(ext, file -> {
|
this.registry.put(ext, file -> {
|
||||||
try {
|
try {
|
||||||
return cls.getConstructor(File.class).newInstance(file);
|
return cls.getConstructor(File.class).newInstance(file);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class AssetManager {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Asset<? extends BaseResource> getById(String id) {
|
public static Asset<? extends BaseResource> getById(String id) {
|
||||||
for (Asset<? extends BaseResource> asset : assets.values()) {
|
for (Asset<? extends BaseResource> asset : assets.values()) {
|
||||||
if (asset.getId().toString().equals(id)) {
|
if (asset.getId().toString().equals(id)) {
|
||||||
return asset;
|
return asset;
|
||||||
@@ -42,15 +42,15 @@ public class AssetManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Asset<? extends BaseResource> getByName(String name) {
|
public static Asset<? extends BaseResource> getByName(String name) {
|
||||||
return assets.get(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));
|
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);
|
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