<commit_before>package net.kiberion.swampmachine.gui.elements.toolbar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import com.badlogic.gdx.scenes.scene2d.ui.Table;

import net.kiberion.entities.common.api.ParametrizedRecalculable;
import net.kiberion.swampmachine.entityblocks.api.EntityProvider;
import net.kiberion.swampmachine.entityblocks.api.MetadataHolderBlock;
import net.kiberion.swampmachine.gui.view.AbstractStateSubView;
import net.kiberion.swampmachine.utils.MapUtils;

public class CommonToolbar<T extends MetadataHolderBlock, M extends EntityProvider<T>> extends AbstractStateSubView<M>
        implements ParametrizedRecalculable<T> {

    private final Map<String, CommonToolbarCell<T>> cellMap = new LinkedHashMap<>();
    private Table cellTable;

    @Override
    public void initGUIElements() {
        super.initGUIElements();
        cellTable = new Table();
        getMainStage().addActor(cellTable);
    }

    protected CommonToolbarCell<T> initCell(T entity) {
        CommonToolbarCell<T> cell = new CommonToolbarCell<>(entity.getId(), getGuiManager().getImageForEntity(entity.getId()), entity.getMetadata().getName());
        getMainStage().addActor(cell);
        return cell;
    }

    protected void addEntity(T entity) {
        CommonToolbarCell<T> cell = initCell(entity);
        cellMap.put(entity.getId(), cell);
        cellTable.addActor(cell);

        cell.setX(300 + (100 * cellMap.size()));
        cell.setY(10);
    }

    protected void initialFill() {
        for (T entity : getModel().getAllEntities()) {
            addEntity(entity);
        }
    }

    protected Collection<T> getAllEntities () {
        return getModel().getAllEntities();        
    }
    
    @Override
    public void update() {
        Collection<CommonToolbarCell<T>> removedCells = new ArrayList<>(cellMap.values());
        for (T entity : getAllEntities()) {
            CommonToolbarCell<T> cell = cellMap.get(entity.getId());
            if (internalUpdate(entity)) {
                removedCells.remove(cell);
            }
        }
        MapUtils.removeAll(cellMap, removedCells);
        super.update();
    }<commit_msg>Make toolbar position adjustable.<commit_after>package net.kiberion.swampmachine.gui.elements.toolbar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import com.badlogic.gdx.scenes.scene2d.ui.Table;

import net.kiberion.entities.common.api.ParametrizedRecalculable;
import net.kiberion.swampmachine.entities.spatial.api.Position;
import net.kiberion.swampmachine.entityblocks.api.EntityProvider;
import net.kiberion.swampmachine.entityblocks.api.MetadataHolderBlock;
import net.kiberion.swampmachine.gui.view.AbstractStateSubView;
import net.kiberion.swampmachine.utils.MapUtils;

public class CommonToolbar<T extends MetadataHolderBlock, M extends EntityProvider<T>> extends AbstractStateSubView<M>
        implements ParametrizedRecalculable<T> {

    private final Map<String, CommonToolbarCell<T>> cellMap = new LinkedHashMap<>();
    private Table cellTable;
    
    protected int distanceBetweenCells = 100;

    public void setPosition (float x, float y) {
        cellTable.setX(x);
        cellTable.setY(y);
        
    }
    
    public void setPosition (Position position) {
        setPosition (position.getX(), position.getY());
    }
    
    @Override
    public void initGUIElements() {
        super.initGUIElements();
        cellTable = new Table();
        getMainStage().addActor(cellTable);
    }

    protected CommonToolbarCell<T> initCell(T entity) {
        CommonToolbarCell<T> cell = new CommonToolbarCell<>(entity.getId(), getGuiManager().getImageForEntity(entity.getId()), entity.getMetadata().getName());
        getMainStage().addActor(cell);
        return cell;
    }

    protected void addEntity(T entity) {
        CommonToolbarCell<T> cell = initCell(entity);
        cellMap.put(entity.getId(), cell);
        cellTable.addActor(cell);

        cell.setX(0 + (distanceBetweenCells * cellMap.size()));
        cell.setY(0);
    }

    protected void initialFill() {
        for (T entity : getModel().getAllEntities()) {
            addEntity(entity);
        }
    }

    protected Collection<T> getAllEntities () {
        return getModel().getAllEntities();        
    }
    
    @Override
    public void update() {
        Collection<CommonToolbarCell<T>> removedCells = new ArrayList<>(cellMap.values());
        for (T entity : getAllEntities()) {
            CommonToolbarCell<T> cell = cellMap.get(entity.getId());
            if (internalUpdate(entity)) {
                removedCells.remove(cell);
            }
        }
        MapUtils.removeAll(cellMap, removedCells);
        super.update();
    }
{'fraction_non_alphanumeric': 0.11395013393777045, 'fraction_numerical': 0.0030908716257984753, 'mean_word_length': 3.217202432667246, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3756362', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1412, 'n_words': 254}