Fix #290 Compartment Crash on Opening

This commit is contained in:
mezz committed 2017-08-25 23:12:39 -07:00
1 parent 37c3231624
commit adef8dc0e4
15 files changed
+65 -124

No files matched your search

+11 -7
View File
@@ -25,7 +25,7 @@ import binnie.core.gui.geometry.Area;
public class Widget implements IWidget {
@Nullable
private final IWidget parent;
private IWidget parent;
@Nullable
private IArea cropArea;
@Nullable
@@ -34,8 +34,8 @@ public class Widget implements IWidget {
private int color;
private List<IWidget> children;
private List<IWidgetAttribute> attributes;
private IPoint position;
private IPoint size;
protected IPoint position;
protected IPoint size;
private IPoint offset;
private Collection<EventHandler<? extends Event>> eventHandlers;
private boolean visible;
@@ -50,10 +50,7 @@ public class Widget implements IWidget {
this.color = 0xffffff;
this.eventHandlers = new ArrayList<>();
this.visible = true;
this.parent = parent;
if (parent != null) {
parent.addChild(this);
}
setParent(parent);
}
/* ATTRIBUTES */
@@ -66,6 +63,13 @@ public class Widget implements IWidget {
return this.attributes.add(attribute);
}
protected final void setParent(@Nullable IWidget parent) {
this.parent = parent;
if (parent != null) {
parent.addChild(this);
}
}
/* CHILDREN */
@Override
public void deleteChild(IWidget child) {
@@ -27,21 +27,14 @@ public class Control extends Widget implements ITooltipHelp, ITooltip {
this.helpStrings = new ArrayList<>();
this.tooltipStrings = new ArrayList<>();
this.extraLevel = 0;
this.setPosition(new Point(xPos, yPos));
this.setSize(new Point(width, height));
this.initialise();
this.position = new Point(xPos, yPos);
this.size = new Point(width, height);
}
public Control(IWidget parent, IArea area) {
this(parent, area.xPos(), area.yPos(), area.width(), area.height());
}
/**
* Called at the end of the constructor of control class.
*/
protected void initialise() {
}
public void addHelp(final String string) {
this.helpStrings.add(string);
}
@@ -11,10 +11,7 @@ import binnie.core.util.IValidator;
public class ControlListBox<T> extends ControlScrollableContent<ControlList<T>> implements IControlValue<T> {
public ControlListBox(final IWidget parent, final int x, final int y, final int w, final int h, final int scrollBarSize) {
super(parent, x, y, w, h, scrollBarSize);
}
@Override
public void initialise() {
ControlList<T> content = this.getContent();
T defaultValue = content == null ? null : content.getDefaultValue();
ControlList<T> child = new ControlList<>(this, 1, 1, this.getWidth() - 2 - this.scrollBarSize, this.getHeight() - 2, defaultValue);
@@ -26,16 +26,20 @@ public class ControlTab<T> extends Control implements ITooltip, IControlValue<T>
protected T value;
private ControlTabBar<T> tabBar;
public ControlTab(final ControlTabBar<T> parent, final int x, final int y, final int w, final int h, final T value) {
super(parent, x, y, w, h);
public ControlTab(final int x, final int y, final int w, final int h, final T value) {
super(null, x, y, w, h);
this.value = value;
this.tabBar = parent;
this.addAttribute(Attribute.MOUSE_OVER);
this.addSelfEventHandler(EventMouse.Down.class, event -> {
ControlTab.this.callEvent(new EventValueChanged<Object>(ControlTab.this.getWidget(), ControlTab.this.getValue()));
});
}
public void setTabBar(ControlTabBar<T> tabBar) {
super.setParent(tabBar);
this.tabBar = tabBar;
}
@Override
@SideOnly(Side.CLIENT)
public void getTooltip(final Tooltip tooltip, ITooltipFlag tooltipFlag) {
@@ -12,12 +12,21 @@ import binnie.core.gui.events.EventValueChanged;
import binnie.core.api.gui.Alignment;
public class ControlTabBar<T> extends Control implements IControlValue<T> {
public interface ITabCreator<T> {
ControlTab<T> createTab(final int x, final int y, final int w, final int h, final T value);
}
private T value;
private Alignment alignment;
public ControlTabBar(final IWidget parent, final int x, final int y, final int width, final int height, final Alignment alignment, final Collection<T> values) {
public ControlTabBar(IWidget parent, int x, int y, int width, int height, Alignment alignment, Collection<T> values) {
this(parent, x, y, width, height, alignment, Iterables.get(values, 0));
setValues(values);
setValues(values, ControlTab::new);
}
public ControlTabBar(IWidget parent, int x, int y, int width, int height, Alignment alignment, Collection<T> values, ITabCreator<T> tabCreator) {
this(parent, x, y, width, height, alignment, Iterables.get(values, 0));
setValues(values, tabCreator);
}
public ControlTabBar(final IWidget parent, final int x, final int y, final int width, final int height, final Alignment alignment, final T value) {
@@ -29,7 +38,7 @@ public class ControlTabBar<T> extends Control implements IControlValue<T> {
});
}
private void setValues(final Collection<T> values) {
private void setValues(Collection<T> values, ITabCreator<T> tabCreator) {
deleteAllChildren();
final float length = values.size();
int tabDimension = (int) (this.getSize().yPos() / length);
@@ -39,18 +48,16 @@ public class ControlTabBar<T> extends Control implements IControlValue<T> {
int j = 0;
for (final T value : values) {
if (this.alignment == Alignment.TOP || this.alignment == Alignment.BOTTOM) {
this.createTab(j * tabDimension, 0, tabDimension, this.getSize().yPos(), value);
ControlTab<T> tab = tabCreator.createTab(j * tabDimension, 0, tabDimension, this.getSize().yPos(), value);
tab.setTabBar(this);
} else {
this.createTab(0, j * tabDimension, this.getSize().xPos(), tabDimension, value);
ControlTab<T> tab = tabCreator.createTab(0, j * tabDimension, this.getSize().xPos(), tabDimension, value);
tab.setTabBar(this);
}
++j;
}
}
public ControlTab<T> createTab(final int x, final int y, final int w, final int h, final T value) {
return new ControlTab<>(this, x, y, w, h, value);
}
@Override
public T getValue() {
return this.value;
@@ -255,17 +255,12 @@ public abstract class WindowAbstractDatabase extends Window {
private static class DatabaseTabBar extends ControlTabBar<IDatabaseMode> {
public DatabaseTabBar(WindowAbstractDatabase windowAbstractDatabase) {
super(windowAbstractDatabase, 176 + windowAbstractDatabase.selectionBoxWidth, 24, 22, 176, Alignment.RIGHT, windowAbstractDatabase.modePages.getValues());
}
@Override
public ControlTab<IDatabaseMode> createTab(final int x, final int y, final int w, final int h, final IDatabaseMode value) {
return new DatabaseTab(this, x, y, w, h, value);
super(windowAbstractDatabase, 176 + windowAbstractDatabase.selectionBoxWidth, 24, 22, 176, Alignment.RIGHT, windowAbstractDatabase.modePages.getValues(), DatabaseTab::new);
}
private static class DatabaseTab extends ControlTab<IDatabaseMode> {
public DatabaseTab(DatabaseTabBar databaseTabBar, int x, int y, int w, int h, IDatabaseMode value) {
super(databaseTabBar, x, y, w, h, value);
public DatabaseTab(int x, int y, int w, int h, IDatabaseMode value) {
super(x, y, w, h, value);
}
@Override
@@ -29,7 +29,6 @@ import binnie.core.genetics.Gene;
import binnie.core.api.gui.IWidget;
import binnie.core.gui.controls.listbox.ControlListBox;
import binnie.core.gui.controls.listbox.ControlTextOption;
import binnie.core.gui.controls.tab.ControlTab;
import binnie.core.gui.controls.tab.ControlTabBar;
import binnie.core.gui.events.EventMouse;
import binnie.core.gui.events.EventValueChanged;
@@ -193,17 +192,12 @@ public class WindowGenesis extends Window {
private static class GenesisTabSystems extends ControlTabBar<IBreedingSystem> {
public GenesisTabSystems(WindowGenesis windowGenesis) {
super(windowGenesis, 8, 28, 23, 100, Alignment.LEFT, Binnie.GENETICS.getActiveSystems());
}
@Override
public ControlTab<IBreedingSystem> createTab(final int x, final int y, final int w, final int h, final IBreedingSystem value) {
return new GenesisTabIcon(this, x, y, w, h, value);
super(windowGenesis, 8, 28, 23, 100, Alignment.LEFT, Binnie.GENETICS.getActiveSystems(), GenesisTabIcon::new);
}
private static class GenesisTabIcon extends ControlTabIcon<IBreedingSystem> {
public GenesisTabIcon(GenesisTabSystems genesisTabSystems, int x, int y, int w, int h, IBreedingSystem value) {
super(genesisTabSystems, x, y, w, h, value);
public GenesisTabIcon(int x, int y, int w, int h, IBreedingSystem value) {
super(x, y, w, h, value);
}
@Override
@@ -20,7 +20,6 @@ public abstract class Dialog extends Control {
this.addAttribute(Attribute.MOUSE_OVER);
this.addAttribute(Attribute.ALWAYS_ON_TOP);
this.addAttribute(Attribute.BLOCK_TOOLTIP);
this.initialise();
this.addEventHandler(EventMouse.Down.class, EventHandlerOrigin.ANY, this, event -> {
if (!Dialog.this.getArea().contains(Dialog.this.getRelativeMousePosition())) {
Dialog.this.onClose();
@@ -29,9 +28,6 @@ public abstract class Dialog extends Control {
});
}
@Override
public abstract void initialise();
public abstract void onClose();
@Override
@@ -13,8 +13,8 @@ import binnie.core.gui.geometry.Point;
public class ControlTabIcon<T> extends ControlTab<T> {
private ControlItemDisplay item;
public ControlTabIcon(final ControlTabBar<T> parent, final int x, final int y, final int w, final int h, final T value) {
super(parent, x, y, w, h, value);
public ControlTabIcon(final int x, final int y, final int w, final int h, final T value) {
super(x, y, w, h, value);
this.item = new ControlItemDisplay(this, -8 + w / 2, -8 + h / 2);
this.item.hastooltip = false;
}
@@ -38,7 +38,6 @@ import binnie.core.gui.controls.core.Control;
import binnie.core.gui.controls.page.ControlPage;
import binnie.core.gui.controls.page.ControlPages;
import binnie.core.gui.controls.scroll.ControlScrollableContent;
import binnie.core.gui.controls.tab.ControlTab;
import binnie.core.gui.controls.tab.ControlTabBar;
import binnie.core.gui.events.EventMouse;
import binnie.core.gui.events.EventTextEdit;
@@ -232,9 +231,10 @@ public class WindowCompartment extends WindowMachine implements IWindowAffectsSh
@SideOnly(Side.CLIENT)
public void updateTabs() {
this.tabName.setValue(this.getCurrentTab().getName());
this.tabIcon.setItemStack(this.getCurrentTab().getIcon());
this.tabColour.setValue(this.getCurrentTab().getColor());
CompartmentTab currentTab = this.getCurrentTab();
this.tabName.setValue(currentTab.getName());
this.tabIcon.setItemStack(currentTab.getIcon());
this.tabColour.setValue(currentTab.getColor());
}
@Override
@@ -287,14 +287,7 @@ public class WindowCompartment extends WindowMachine implements IWindowAffectsSh
public SearchDialog(WindowCompartment windowCompartment) {
super(windowCompartment, 252, 192);
this.windowCompartment = windowCompartment;
}
@Override
public void onClose() {
}
@Override
public void initialise() {
final ControlScrollableContent<IWidget> scroll = new SearchScrollContent(this);
scroll.setScrollableContent(this.slotGrid = new Control(scroll, 1, 1, 108, 18));
new ControlPlayerInventory(this, true);
@@ -310,6 +303,10 @@ public class WindowCompartment extends WindowMachine implements IWindowAffectsSh
this.updateSearch();
}
@Override
public void onClose() {
}
private void updateSearch() {
Map<Integer, String> slotIds = new HashMap<>();
final IInventory inv = windowCompartment.getInventory();
@@ -423,44 +420,29 @@ public class WindowCompartment extends WindowMachine implements IWindowAffectsSh
}
private static class CompartmentTabBar1 extends ControlTabBar<Integer> {
private WindowCompartment windowCompartment;
public CompartmentTabBar1(final WindowCompartment windowCompartment, Control controlCompartment, int compartmentPageHeight, Integer[] tabs1) {
super(controlCompartment, 0, 0, 24, compartmentPageHeight, Alignment.LEFT, Arrays.asList(tabs1));
this.windowCompartment = windowCompartment;
}
@Override
public ControlTab<Integer> createTab(final int x, final int y, final int w, final int h, final Integer value) {
return new CompartmentTabIcon(this, this.windowCompartment, x, y, w, h, value);
super(controlCompartment, 0, 0, 24, compartmentPageHeight, Alignment.LEFT, Arrays.asList(tabs1), (x, y, w, h, value) -> new CompartmentTabIcon(windowCompartment, x, y, w, h, value));
}
}
private static class CompartmentTabBar2 extends ControlTabBar<Integer> {
private WindowCompartment windowCompartment;
public CompartmentTabBar2(WindowCompartment windowCompartment, Control controlCompartment, int compartmentPageWidth, int compartmentPageHeight, Integer[] tabs2) {
super(controlCompartment, 24 + compartmentPageWidth, 0, 24, compartmentPageHeight, Alignment.RIGHT, Arrays.asList(tabs2));
this.windowCompartment = windowCompartment;
}
@Override
public ControlTab<Integer> createTab(final int x, final int y, final int w, final int h, final Integer value) {
return new CompartmentTabIcon(this, this.windowCompartment, x, y, w, h, value);
super(controlCompartment, 24 + compartmentPageWidth, 0, 24, compartmentPageHeight, Alignment.RIGHT, Arrays.asList(tabs2), (x, y, w, h, value) -> new CompartmentTabIcon(windowCompartment, x, y, w, h, value));
}
}
private static class CompartmentTabIcon extends ControlTabIcon<Integer> {
private WindowCompartment windowCompartment;
public CompartmentTabIcon(ControlTabBar<Integer> compartmentTabBar, WindowCompartment windowCompartment, int x, int y, int w, int h, Integer value) {
super(compartmentTabBar, x, y, w, h, value);
public CompartmentTabIcon(WindowCompartment windowCompartment, int x, int y, int w, int h, Integer value) {
super(x, y, w, h, value);
this.windowCompartment = windowCompartment;
}
@Override
public ItemStack getItemStack() {
return windowCompartment.getTab(this.value).getIcon();
CompartmentTab tab = windowCompartment.getTab(this.value);
return tab.getIcon();
}
@Override
@@ -96,16 +96,8 @@ public class AnalystPageDatabase extends Control implements ITitledWidget {
}
private static class Scroll extends ControlListBox<IAlleleSpecies> {
private final Collection<IAlleleSpecies> options;
public Scroll(AnalystPageDatabase analystPageDatabase, int y, Collection<IAlleleSpecies> options) {
super(analystPageDatabase, 4, y, analystPageDatabase.getWidth() - 8, analystPageDatabase.getHeight() - y - 8 - 20, 0);
this.options = options;
}
@Override
public void initialise() {
super.initialise();
setOptions(options);
}
@@ -195,10 +187,7 @@ public class AnalystPageDatabase extends Control implements ITitledWidget {
this.options = options;
this.system = system;
this.analystPageDatabase = analystPageDatabase;
}
@Override
public void initialise() {
int maxBiomePerLine = (getWidth() - 4 + 2) / 18;
int biomeListX = -6 + (getWidth() - (maxBiomePerLine * 18 - 2)) / 2;
int dx = 0;
@@ -225,10 +214,7 @@ public class AnalystPageDatabase extends Control implements ITitledWidget {
this.ind = ind;
this.species = species;
this.itemScrollList = itemScrollList;
}
@Override
public void initialise() {
addSelfEventHandler(EventMouse.Down.class, event -> {
WindowAnalyst window = (WindowAnalyst) itemScrollList.analystPageDatabase.getTopParent();
window.setIndividual(ind);
@@ -33,10 +33,6 @@ public class ControlResultantMutation extends ControlMutation {
}
}
@Override
public void initialise() {
}
@Override
@SideOnly(Side.CLIENT)
public void onRenderBackground(int guiWidth, int guiHeight) {
@@ -14,6 +14,7 @@ import binnie.core.gui.geometry.Point;
import binnie.core.gui.renderer.RenderUtil;
import binnie.core.gui.resource.textures.CraftGUITexture;
@SideOnly(Side.CLIENT)
public class ControlSystemButton extends Control {
IBreedingSystem system;
WindowAnalyst window;
@@ -22,10 +23,7 @@ public class ControlSystemButton extends Control {
super(window, xPos, yPos, 20, 20);
this.system = system;
this.window = window;
}
@Override
public void initialise() {
addAttribute(Attribute.MOUSE_OVER);
addSelfEventHandler(EventMouse.Down.class, event -> {
window.setSystem(system);
@@ -355,10 +355,7 @@ public class WindowAnalyst extends Window {
private static class AnalystNoneControl extends Control {
public AnalystNoneControl(WindowAnalyst windowAnalyst) {
super(windowAnalyst.analystPanel, 0, 0, windowAnalyst.analystPanel.getWidth(), windowAnalyst.analystPanel.getHeight());
}
@Override
public void initialise() {
new ControlTextCentered(this, 20, I18N.localise("genetics.gui.analyst.desc")).setColor(4473924);
new ControlPlayerInventory(this);
}
@@ -28,7 +28,6 @@ import binnie.core.gui.Tooltip;
import binnie.core.gui.controls.ControlText;
import binnie.core.gui.controls.ControlTextEdit;
import binnie.core.gui.controls.scroll.ControlScrollableContent;
import binnie.core.gui.controls.tab.ControlTab;
import binnie.core.gui.controls.tab.ControlTabBar;
import binnie.core.gui.events.EventTextEdit;
import binnie.core.gui.events.EventValueChanged;
@@ -165,24 +164,17 @@ public class WindowGeneBank extends WindowMachine {
}
private static class GeneBankTabBar extends ControlTabBar<IBreedingSystem> {
private WindowGeneBank windowGeneBank;
public GeneBankTabBar(final WindowGeneBank windowGeneBank, int boxX) {
super(windowGeneBank, boxX, 32, 24, 120, Alignment.LEFT, Binnie.GENETICS.getActiveSystems());
this.windowGeneBank = windowGeneBank;
}
@Override
public ControlTab<IBreedingSystem> createTab(final int x, final int y, final int w, final int h, final IBreedingSystem value) {
return new GeneBankTab(this, x, y, w, h, value);
super(windowGeneBank, boxX, 32, 24, 120, Alignment.LEFT, Binnie.GENETICS.getActiveSystems(), (x, y, w, h, value) -> new GeneBankTab(windowGeneBank, x, y, w, h, value));
}
private static class GeneBankTab extends ControlTabIcon<IBreedingSystem> {
private GeneBankTabBar geneBankTabBar;
private WindowGeneBank windowGeneBank;
public GeneBankTab(GeneBankTabBar geneBankTabBar, int x, int y, int w, int h, IBreedingSystem value) {
super(geneBankTabBar, x, y, w, h, value);
this.geneBankTabBar = geneBankTabBar;
public GeneBankTab(WindowGeneBank windowGeneBank, int x, int y, int w, int h, IBreedingSystem value) {
super(x, y, w, h, value);
this.windowGeneBank = windowGeneBank;
}
@Override
@@ -190,7 +182,7 @@ public class WindowGeneBank extends WindowMachine {
tooltip.add(this.getValue().toString());
int totalGenes = 0;
int seqGenes = 0;
final GeneTracker tracker = GeneTracker.getTracker(geneBankTabBar.windowGeneBank.getWorld(), geneBankTabBar.windowGeneBank.getUsername());
final GeneTracker tracker = GeneTracker.getTracker(windowGeneBank.getWorld(), windowGeneBank.getUsername());
final Map<IChromosomeType, List<IAllele>> genes = Binnie.GENETICS.getChromosomeMap(this.getValue().getSpeciesRoot());
for (final Map.Entry<IChromosomeType, List<IAllele>> entry : genes.entrySet()) {
totalGenes += entry.getValue().size();