<commit_before>package net.machinemuse.general.gui.frame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.machinemuse.api.IPowerModule;
import net.machinemuse.api.MuseItemUtils;
import net.machinemuse.general.MuseRenderer;
import net.machinemuse.general.geometry.Colour;
import net.machinemuse.general.geometry.MusePoint2D;
import net.machinemuse.general.geometry.MuseRect;
import net.machinemuse.general.gui.clickable.ClickableItem;
import net.machinemuse.general.gui.clickable.ClickableModule;

public class ModuleSelectionFrame extends ScrollableFrame {
	protected ItemSelectionFrame target;
	protected Map<String, ModuleSelectionSubFrame> categories;
	protected List<ClickableModule> moduleButtons;
	protected int selectedModule = -1;
	protected IPowerModule prevSelection;

	public ModuleSelectionFrame(MusePoint2D topleft, MusePoint2D bottomright,
			Colour borderColour, Colour insideColour, ItemSelectionFrame target) {
		super(topleft, bottomright, borderColour, insideColour);
		this.target = target;

		moduleButtons = new ArrayList();
		categories = new HashMap();
	}

	@Override
	public void update(double mousex, double mousey) {

	}

	@Override
	public void draw() {
		if (target.getSelectedItem() != null) {
			loadModules();
			drawBackground();
			drawItems();
			drawSelection();
		}
	}

	private void drawBackground() {
		super.draw();
	}

	private void drawItems() {
		for (ModuleSelectionSubFrame frame : categories.values()) {
			frame.draw();
		}
	}

	private void drawSelection() {
		ClickableModule module = getSelectedModule();
		if (module != null) {
			MuseRenderer.drawCircleAround(
					moduleButtons.get(selectedModule).getPosition().x(),
					moduleButtons.get(selectedModule).getPosition().y(),
					10);

		}

	}

	public ClickableModule getSelectedModule() {
		if (moduleButtons.size() > selectedModule && selectedModule != -1) {
			return moduleButtons.get(selectedModule);
		} else {
			return null;
		}
	}

	public void loadModules() {
		ClickableItem selectedItem = target.getSelectedItem();
		if (selectedItem != null) {
			double centerx = (border.left() + border.right()) / 2;
			double centery = (border.top() + border.bottom()) / 2;

			moduleButtons = new ArrayList();
			categories = new HashMap();

			List<IPowerModule> workingModules = MuseItemUtils.getValidModulesForItem(null, selectedItem.getItem());
			if (workingModules.size() > 0) {
				List<MusePoint2D> points = MuseRenderer.pointsInLine(
						workingModules.size(),
						new MusePoint2D(centerx, border.top()),
						new MusePoint2D(centerx, border.bottom()));
				this.selectedModule = -1;
				Iterator<MusePoint2D> pointiter = points.iterator();
				for (IPowerModule module : workingModules) {
					ModuleSelectionSubFrame frame = getOrCreateCategory(module.getCategory());
					ClickableModule moduleClickable = frame.addModule(module);
					if (moduleClickable.getModule().equals(this.prevSelection)) {
						this.selectedModule = moduleButtons.size();
					}
					moduleButtons.add(moduleClickable);
				}<commit_msg>When an item is selected in the power armor GUI, it will now hide modules from view that are disallowed in the config.  Disallowed modules that are still installed will still be shown so they can be salvaged.<commit_after>package net.machinemuse.general.gui.frame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.machinemuse.api.IPowerModule;
import net.machinemuse.api.MuseItemUtils;
import net.machinemuse.general.MuseRenderer;
import net.machinemuse.general.geometry.Colour;
import net.machinemuse.general.geometry.MusePoint2D;
import net.machinemuse.general.geometry.MuseRect;
import net.machinemuse.general.gui.clickable.ClickableItem;
import net.machinemuse.general.gui.clickable.ClickableModule;

public class ModuleSelectionFrame extends ScrollableFrame {
	protected ItemSelectionFrame target;
	protected Map<String, ModuleSelectionSubFrame> categories;
	protected List<ClickableModule> moduleButtons;
	protected int selectedModule = -1;
	protected IPowerModule prevSelection;

	public ModuleSelectionFrame(MusePoint2D topleft, MusePoint2D bottomright,
			Colour borderColour, Colour insideColour, ItemSelectionFrame target) {
		super(topleft, bottomright, borderColour, insideColour);
		this.target = target;

		moduleButtons = new ArrayList();
		categories = new HashMap();
	}

	@Override
	public void update(double mousex, double mousey) {

	}

	@Override
	public void draw() {
		if (target.getSelectedItem() != null) {
			loadModules();
			drawBackground();
			drawItems();
			drawSelection();
		}
	}

	private void drawBackground() {
		super.draw();
	}

	private void drawItems() {
		for (ModuleSelectionSubFrame frame : categories.values()) {
			frame.draw();
		}
	}

	private void drawSelection() {
		ClickableModule module = getSelectedModule();
		if (module != null) {
			MuseRenderer.drawCircleAround(
					moduleButtons.get(selectedModule).getPosition().x(),
					moduleButtons.get(selectedModule).getPosition().y(),
					10);

		}

	}

	public ClickableModule getSelectedModule() {
		if (moduleButtons.size() > selectedModule && selectedModule != -1) {
			return moduleButtons.get(selectedModule);
		} else {
			return null;
		}
	}

	public void loadModules() {
		ClickableItem selectedItem = target.getSelectedItem();
		if (selectedItem != null) {
			double centerx = (border.left() + border.right()) / 2;
			double centery = (border.top() + border.bottom()) / 2;

			moduleButtons = new ArrayList();
			categories = new HashMap();

			List<IPowerModule> workingModules = MuseItemUtils.getValidModulesForItem(null, selectedItem.getItem());

			// Prune the list of disallowed modules that are not installed.
			// That way we don't show modules that can't be installed anyway.
			// A disallowed module that's installed will still be shown so it can be salvaged.
			for (Iterator<IPowerModule> it = workingModules.iterator(); it.hasNext();) {
				IPowerModule module = it.next();
				if (module.isAllowed() == false &&
						MuseItemUtils.itemHasModule(selectedItem.getItem(), module.getName()) == false) {
					it.remove();
				}
			}

			if (workingModules.size() > 0) {
				List<MusePoint2D> points = MuseRenderer.pointsInLine(
						workingModules.size(),
						new MusePoint2D(centerx, border.top()),
						new MusePoint2D(centerx, border.bottom()));
				this.selectedModule = -1;
				Iterator<MusePoint2D> pointiter = points.iterator();
				for (IPowerModule module : workingModules) {
					ModuleSelectionSubFrame frame = getOrCreateCategory(module.getCategory());
					ClickableModule moduleClickable = frame.addModule(module);
					if (moduleClickable.getModule().equals(this.prevSelection)) {
						this.selectedModule = moduleButtons.size();
					}
					moduleButtons.add(moduleClickable);
				}
{'fraction_non_alphanumeric': 0.11088709677419355, 'fraction_numerical': 0.004320276497695853, 'mean_word_length': 4.600806451612903, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, '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': '5014172', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2078, 'n_words': 469}