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

com.bigcustard.scene2dplus.resource.ResourceSet Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.bigcustard.scene2dplus.resource;

import com.badlogic.gdx.utils.Disposable;
import com.bigcustard.scene2dplus.command.CommandHistory;
import com.bigcustard.util.WatchableList;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Stream;

public class ResourceSet implements Disposable {
    private final CommandHistory commandHistory;
    private WatchableList> resources;
    private static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

    public ResourceSet(List> resources, CommandHistory commandHistory) {
        this.commandHistory = commandHistory;
        this.resources = new WatchableList<>(resources);
        watchRemoveButtons();
        watchModelChanges();
    }

    public WatchableList> resources() {
        return resources;
    }

    public Stream> stream() {
        return resources.stream();
    }

    private void watchModelChanges() {
        resources.watchAdd(this::watchRemoveButton);
        resources.watchRemove(this::unwatchRemoveButton);
    }

    private void watchRemoveButtons() {
        resources.forEach(this::watchRemoveButton);
    }

    private void watchRemoveButton(Resource resource) {
        resource.controller().watchRemoveButton(() -> executeRemoveCommand(resource));
    }

    private void unwatchRemoveButton(Resource resource) {
        executor.submit(() -> resource.controller().unwatchRemoveButton());
    }

    private void executeRemoveCommand(Resource resource) {
        commandHistory.execute(() -> resources.remove(resource), () -> resources.add(resource));
    }

    @Override
    public void dispose() {
        resources.forEach(Resource::dispose);
        resources.dispose();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy