All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.multiplatform.loader.PixelMapLoader.kt Maven / Gradle / Ivy

The newest version!
package ru.casperix.multiplatform.loader

import ru.casperix.renderer.pixel_map.PixelMap
import ru.casperix.signals.concrete.EmptySignal
import ru.casperix.multiplatform.util.PixelMapLoaderInfo

object PixelMapLoader {
    private val loaders = mutableMapOf()

    private val loadingComplete = EmptySignal()

    fun thenComplete(callback: () -> Unit) {
        if (getLoadingAmount() == 0) {
            callback()
        } else {
            loadingComplete.then {
                callback()
            }
        }
    }

    fun getLoadingAmount(): Int {
        return loaders.values.filter { it.isLoading }.size
    }

    fun getLoaderList(fileList: List): List {
        return fileList.map {
            getLoader(it)
        }
    }

    fun getLoader(file: String): PixelMapLoaderInfo {
        return loaders.getOrPut(file) {
            PixelMapLoaderInfo(file, resourceLoader.loadImage(file)).apply {
                signal.then({
                    checkComplete()
                }, {
                    checkComplete()
                })
            }
        }
    }

    private fun checkComplete() {
        if (getLoadingAmount() != 0) return
        loadingComplete.set()
    }

    fun getOrNull(file: String): PixelMap? {
        val loader = loaders[file] ?: return null
        return loader.content
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy