From 30010d2554f0dab6426b665589ef2d0b94edee6c Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Thu, 31 May 2018 18:10:05 +0200 Subject: [PATCH] Improve and Clean Up Gui Elements --- .../forestry/api/apiculture/BeeManager.java | 16 + .../api/arboriculture/TreeManager.java | 10 + .../forestry/api/genetics/AlleleManager.java | 1 + .../forestry/api/genetics/DatabaseMode.java | 7 + .../forestry/api/genetics/IAlyzerPlugin.java | 40 +- .../api/genetics/IDatabasePlugin.java | 10 +- .../forestry/api/genetics/IDatabaseTab.java | 8 +- .../forestry/api/genetics/IGeneticPlugin.java | 5 - .../api/genetics/ISpeciesDisplayHelper.java | 14 +- .../java/forestry/api/gui/GuiConstants.java | 14 + .../forestry/api/gui/GuiElementAlignment.java | 2 +- .../forestry/api/gui/IDatabaseElement.java | 63 ++++ .../forestry/api/gui/IElementGenetic.java | 37 -- .../java/forestry/api/gui/IElementGroup.java | 60 ++- .../java/forestry/api/gui/IGuiElement.java | 275 ++++++++++++-- .../forestry/api/gui/IGuiElementFactory.java | 9 +- .../java/forestry/api/gui/IItemElement.java | 15 + .../java/forestry/api/gui/ILabelElement.java | 27 ++ .../java/forestry/api/gui/ITextElement.java | 24 ++ .../{IGuiState.java => IWindowElement.java} | 28 +- .../forestry/api/gui/events/ElementEvent.java | 59 +++ .../api/gui/events/GuiElementEvent.java | 19 + .../forestry/api/gui/events/GuiEvent.java | 104 ++++++ .../api/gui/events/GuiEventDestination.java | 69 ++++ .../api/gui/events/GuiEventHandler.java | 40 ++ .../api/gui/events/GuiEventOrigin.java | 40 ++ .../forestry/api/gui/events/package-info.java | 12 + .../forestry/api/gui/style/ITextStyle.java | 34 ++ .../api/gui/style/ImmutableTextStyle.java | 65 ++++ .../forestry/api/gui/style/TextStyle.java | 115 ++++++ .../api/gui/style/TextStyleBuilder.java | 86 +++++ .../forestry/api/gui/style/package-info.java | 12 + .../api/lepidopterology/ButterflyManager.java | 11 + .../apiculture/genetics/BeeDatabaseTab.java | 85 +++-- .../apiculture/genetics/BeePlugin.java | 5 +- .../genetics/TreeDatabaseTab.java | 61 +-- .../arboriculture/genetics/TreePlugin.java | 5 +- .../book/data/content/ImageContent.java | 2 +- .../forestry/book/gui/GuiForesterBook.java | 76 +--- .../book/gui/GuiForestryBookEntries.java | 9 +- .../book/gui/GuiForestryBookPages.java | 6 +- .../book/gui/elements/IndexElement.java | 35 +- .../book/gui/elements/MultiblockElement.java | 22 +- .../book/gui/elements/MutationElement.java | 3 +- .../book/gui/elements/SelectionElement.java | 9 +- .../book/gui/elements/TextDataElement.java | 2 +- .../core/genetics/SpeciesDisplayHelper.java | 38 +- .../core/genetics/analyzer/AnalyzerTab.java | 4 +- .../core/genetics/analyzer/MutationsTab.java | 10 +- .../core/genetics/analyzer/ProductsTab.java | 8 +- .../core/gui/ContainerAnalyzerProvider.java | 1 + .../core/gui/GuiAnalyzerProvider.java | 5 +- .../java/forestry/core/gui/GuiForestry.java | 54 ++- .../java/forestry/core/gui/GuiWindow.java | 141 +++++++ .../gui/elements/AbstractItemElement.java | 11 +- .../core/gui/elements/ButtonElement.java | 32 +- .../core/gui/elements/DatabaseElement.java | 248 +++++++++++++ .../core/gui/elements/ElementList.java | 125 +++++++ .../core/gui/elements/ElementManager.java | 21 +- .../core/gui/elements/GeneticAnalyzer.java | 70 ++-- .../gui/elements/GeneticAnalyzerTabs.java | 24 +- .../core/gui/elements/GuiElement.java | 343 ++++++++++++----- .../core/gui/elements/GuiElementFactory.java | 39 +- .../forestry/core/gui/elements/GuiState.java | 98 ----- .../core/gui/elements/IngredientElement.java | 3 +- .../core/gui/elements/ItemElement.java | 23 +- .../core/gui/elements/LabelElement.java | 104 ++++++ .../core/gui/elements/ScrollBarElement.java | 12 +- .../core/gui/elements/ScrollableElement.java | 61 +++ .../core/gui/elements/SplitTextElement.java | 73 ++++ .../core/gui/elements/TankElement.java | 5 + .../core/gui/elements/TextEditElement.java | 10 + .../core/gui/elements/TextElement.java | 46 --- .../forestry/core/gui/elements/Window.java | 346 ++++++++++++++++++ .../layouts/AbstractElementLayout.java | 4 + .../gui/elements/layouts/ElementGroup.java | 127 +++---- .../elements/layouts/HorizontalLayout.java | 10 +- .../elements/layouts/ScrollableElement.java | 210 ----------- .../gui/elements/layouts/VerticalLayout.java | 16 +- .../core/gui/event/EventValueChanged.java | 21 ++ .../forestry/core/gui/event/package-info.java | 10 + .../forestry/core/utils/GuiElementUtil.java | 42 +-- .../genetics/ButterflyDatabaseTab.java | 94 ++--- .../genetics/ButterflyPlugin.java | 5 +- .../genetics/ButterflyProductsTab.java | 10 +- .../forestry/textures/gui/research_unused.png | Bin 0 -> 1334 bytes 86 files changed, 3026 insertions(+), 1129 deletions(-) create mode 100644 src/main/java/forestry/api/genetics/DatabaseMode.java delete mode 100644 src/main/java/forestry/api/genetics/IGeneticPlugin.java create mode 100644 src/main/java/forestry/api/gui/GuiConstants.java create mode 100644 src/main/java/forestry/api/gui/IDatabaseElement.java delete mode 100644 src/main/java/forestry/api/gui/IElementGenetic.java create mode 100644 src/main/java/forestry/api/gui/IItemElement.java create mode 100644 src/main/java/forestry/api/gui/ILabelElement.java create mode 100644 src/main/java/forestry/api/gui/ITextElement.java rename src/main/java/forestry/api/gui/{IGuiState.java => IWindowElement.java} (60%) create mode 100644 src/main/java/forestry/api/gui/events/ElementEvent.java create mode 100644 src/main/java/forestry/api/gui/events/GuiElementEvent.java create mode 100644 src/main/java/forestry/api/gui/events/GuiEvent.java create mode 100644 src/main/java/forestry/api/gui/events/GuiEventDestination.java create mode 100644 src/main/java/forestry/api/gui/events/GuiEventHandler.java create mode 100644 src/main/java/forestry/api/gui/events/GuiEventOrigin.java create mode 100644 src/main/java/forestry/api/gui/events/package-info.java create mode 100644 src/main/java/forestry/api/gui/style/ITextStyle.java create mode 100644 src/main/java/forestry/api/gui/style/ImmutableTextStyle.java create mode 100644 src/main/java/forestry/api/gui/style/TextStyle.java create mode 100644 src/main/java/forestry/api/gui/style/TextStyleBuilder.java create mode 100644 src/main/java/forestry/api/gui/style/package-info.java create mode 100644 src/main/java/forestry/core/gui/GuiWindow.java create mode 100644 src/main/java/forestry/core/gui/elements/DatabaseElement.java create mode 100644 src/main/java/forestry/core/gui/elements/ElementList.java delete mode 100644 src/main/java/forestry/core/gui/elements/GuiState.java create mode 100644 src/main/java/forestry/core/gui/elements/LabelElement.java create mode 100644 src/main/java/forestry/core/gui/elements/ScrollableElement.java create mode 100644 src/main/java/forestry/core/gui/elements/SplitTextElement.java create mode 100644 src/main/java/forestry/core/gui/elements/TextEditElement.java delete mode 100644 src/main/java/forestry/core/gui/elements/TextElement.java create mode 100644 src/main/java/forestry/core/gui/elements/Window.java delete mode 100644 src/main/java/forestry/core/gui/elements/layouts/ScrollableElement.java create mode 100644 src/main/java/forestry/core/gui/event/EventValueChanged.java create mode 100644 src/main/java/forestry/core/gui/event/package-info.java create mode 100644 src/main/resources/assets/forestry/textures/gui/research_unused.png diff --git a/src/main/java/forestry/api/apiculture/BeeManager.java b/src/main/java/forestry/api/apiculture/BeeManager.java index 07ad74f32..3d3984c64 100644 --- a/src/main/java/forestry/api/apiculture/BeeManager.java +++ b/src/main/java/forestry/api/apiculture/BeeManager.java @@ -5,6 +5,7 @@ ******************************************************************************/ package forestry.api.apiculture; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -34,26 +35,41 @@ public class BeeManager { /** * Convenient access to AlleleManager.alleleRegistry.getSpeciesRoot("rootBees") + * + * @implNote Only null if the "apiculture" module is not enabled. */ + @Nullable public static IBeeRoot beeRoot; /** * Used to create new bees. + * + * @implNote Only null if the "apiculture" module is not enabled. */ + @Nullable public static IBeeFactory beeFactory; /** * Used to create new bee mutations. + * + * @implNote Only null if the "apiculture" module is not enabled. */ + @Nullable public static IBeeMutationFactory beeMutationFactory; /** * Used to get Forestry's jubilance implementations. + * + * @implNote Only null if the "apiculture" module is not enabled. */ + @Nullable public static IJubilanceFactory jubilanceFactory; /** * Used to check whether a player is wearing Apiarist Armor. + * + * @implNote Only null if the "apiculture" module is not enabled. */ + @Nullable public static IArmorApiaristHelper armorApiaristHelper; } diff --git a/src/main/java/forestry/api/arboriculture/TreeManager.java b/src/main/java/forestry/api/arboriculture/TreeManager.java index 4071ade56..8d69b26b2 100644 --- a/src/main/java/forestry/api/arboriculture/TreeManager.java +++ b/src/main/java/forestry/api/arboriculture/TreeManager.java @@ -5,6 +5,7 @@ ******************************************************************************/ package forestry.api.arboriculture; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; @@ -12,7 +13,10 @@ public class TreeManager { /** * Convenient access to AlleleManager.alleleRegistry.getSpeciesRoot("rootTrees") + * + * @implNote Only null if the "arboriculture" module is not enabled. */ + @Nullable public static ITreeRoot treeRoot; /** @@ -22,12 +26,18 @@ public class TreeManager { /** * Used to create new trees. + * + * @implNote Only null if the "arboriculture" module is not enabled. */ + @Nullable public static ITreeFactory treeFactory; /** * Used to create new tree mutations. + * + * @implNote Only null if the "arboriculture" module is not enabled. */ + @Nullable public static ITreeMutationFactory treeMutationFactory; public static List pileWalls = new ArrayList<>(); diff --git a/src/main/java/forestry/api/genetics/AlleleManager.java b/src/main/java/forestry/api/genetics/AlleleManager.java index 9cd03ab4b..d2620e941 100644 --- a/src/main/java/forestry/api/genetics/AlleleManager.java +++ b/src/main/java/forestry/api/genetics/AlleleManager.java @@ -28,4 +28,5 @@ public class AlleleManager { * @since 5.8 */ public static IFilterRegistry filterRegistry; + } diff --git a/src/main/java/forestry/api/genetics/DatabaseMode.java b/src/main/java/forestry/api/genetics/DatabaseMode.java new file mode 100644 index 000000000..b6d5073e6 --- /dev/null +++ b/src/main/java/forestry/api/genetics/DatabaseMode.java @@ -0,0 +1,7 @@ +package forestry.api.genetics; + +public enum DatabaseMode { + ACTIVE, + INACTIVE, + BOTH +} diff --git a/src/main/java/forestry/api/genetics/IAlyzerPlugin.java b/src/main/java/forestry/api/genetics/IAlyzerPlugin.java index 1c8f6857b..c1572f04d 100644 --- a/src/main/java/forestry/api/genetics/IAlyzerPlugin.java +++ b/src/main/java/forestry/api/genetics/IAlyzerPlugin.java @@ -14,53 +14,19 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import forestry.api.gui.IElementGenetic; - +@SideOnly(Side.CLIENT) public interface IAlyzerPlugin { - @SideOnly(Side.CLIENT) void drawAnalyticsPage1(GuiScreen gui, ItemStack itemStack); - @SideOnly(Side.CLIENT) void drawAnalyticsPage2(GuiScreen gui, ItemStack itemStack); - @SideOnly(Side.CLIENT) void drawAnalyticsPage3(GuiScreen gui, ItemStack itemStack); - /** - * Creates the first page of the alyzer. - * - * @param gui A instance of the alyzer gui. - * @param container A helper to create the gui elements. - * @param individual The individual that is currently in the alyzer slot. - */ - default void createFirstPage(GuiScreen gui, IElementGenetic container, IIndividual individual){ - } - - /** - * Creates the second page of the alyzer. - * - * @param gui A instance of the alyzer gui. - * @param container A helper to create the gui elements. - * @param individual The individual that is currently in the alyzer slot. - */ - default void createSecondPage(GuiScreen gui, IElementGenetic container, IIndividual individual){ - } - - /** - * Creates the third page of the alyzer. This page is usually used to display the products of the individual. - * - * @param gui A instance of the alyzer gui. - * @param container A helper to create the gui elements. - * @param individual The individual that is currently in the alyzer slot. - */ - default void createThirdPage(GuiScreen gui, IElementGenetic container, IIndividual individual){ - } - - Map getIconStacks(); - /** * The hints that will be shown in the alyzer gui. */ List getHints(); + + Map getIconStacks(); } diff --git a/src/main/java/forestry/api/genetics/IDatabasePlugin.java b/src/main/java/forestry/api/genetics/IDatabasePlugin.java index e679650c1..eab2e4e7e 100644 --- a/src/main/java/forestry/api/genetics/IDatabasePlugin.java +++ b/src/main/java/forestry/api/genetics/IDatabasePlugin.java @@ -20,16 +20,10 @@ import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public interface IDatabasePlugin { - /** - * A map that contains a {@link ItemStack} for every possible {@link IAlleleSpecies}. - * This item stack represents the species in the database and alyzer gui. - * - * The key of the map is the uid of the species. - */ - Map getIndividualStacks(); - /* ALYZER */ List getHints(); IDatabaseTab[] getTabs(); + + Map getIndividualStacks(); } diff --git a/src/main/java/forestry/api/genetics/IDatabaseTab.java b/src/main/java/forestry/api/genetics/IDatabaseTab.java index 0c920c702..02b2ba9e9 100644 --- a/src/main/java/forestry/api/genetics/IDatabaseTab.java +++ b/src/main/java/forestry/api/genetics/IDatabaseTab.java @@ -7,7 +7,7 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; /** * A tab of the database screen that shows some information about a {@link IIndividual}. @@ -20,7 +20,7 @@ public interface IDatabaseTab { * @param individual The individual that is currently in the database selected. * @param container A helper to create the gui elements. */ - void createElements(IElementGenetic container, I individual, ItemStack itemStack); + void createElements(IDatabaseElement container, I individual, ItemStack itemStack); ItemStack getIconStack(); @@ -31,4 +31,8 @@ public interface IDatabaseTab { default String getTooltip(I individual){ return null; } + + default DatabaseMode getMode(){ + return DatabaseMode.ACTIVE; + } } diff --git a/src/main/java/forestry/api/genetics/IGeneticPlugin.java b/src/main/java/forestry/api/genetics/IGeneticPlugin.java deleted file mode 100644 index cb1d12bae..000000000 --- a/src/main/java/forestry/api/genetics/IGeneticPlugin.java +++ /dev/null @@ -1,5 +0,0 @@ -package forestry.api.genetics; - -public class IGeneticPlugin { - -} diff --git a/src/main/java/forestry/api/genetics/ISpeciesDisplayHelper.java b/src/main/java/forestry/api/genetics/ISpeciesDisplayHelper.java index 6d157ffa4..37a567f7f 100644 --- a/src/main/java/forestry/api/genetics/ISpeciesDisplayHelper.java +++ b/src/main/java/forestry/api/genetics/ISpeciesDisplayHelper.java @@ -1,24 +1,12 @@ package forestry.api.genetics; -import java.util.Optional; - import net.minecraft.item.ItemStack; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) -public interface ISpeciesDisplayHelper { - /** - * Plugin to add information for the genetic database. - */ - Optional getDatabasePlugin(); - - /** - * Plugin to add information for the handheld genetic analyzer. - */ - Optional getAlyzerPlugin(); - +public interface ISpeciesDisplayHelper { /** * Retrieves a stack that can and should only be used on the client side in a gui. * diff --git a/src/main/java/forestry/api/gui/GuiConstants.java b/src/main/java/forestry/api/gui/GuiConstants.java new file mode 100644 index 000000000..f60c4cf90 --- /dev/null +++ b/src/main/java/forestry/api/gui/GuiConstants.java @@ -0,0 +1,14 @@ +package forestry.api.gui; + +import forestry.api.gui.style.ITextStyle; +import forestry.api.gui.style.ImmutableTextStyle; +import forestry.api.gui.style.TextStyleBuilder; + +public class GuiConstants { + public static final ITextStyle DEFAULT_STYLE = new ImmutableTextStyle(); + public static final ITextStyle BLACK_STYLE = new TextStyleBuilder().color(0x000000).build(); + public static final ITextStyle UNICODE_STYLE = new TextStyleBuilder().unicode(true).build(); + public static final ITextStyle UNDERLINED_STYLE = new TextStyleBuilder().underlined(true).build(); + public static final ITextStyle BOLD_BLACK_STYLE = new TextStyleBuilder(BLACK_STYLE).bold(true).build(); + public static final ITextStyle ITALIC_BLACK_STYLE = new TextStyleBuilder(BLACK_STYLE).italic(true).build(); +} diff --git a/src/main/java/forestry/api/gui/GuiElementAlignment.java b/src/main/java/forestry/api/gui/GuiElementAlignment.java index 4163991fe..4671a7075 100644 --- a/src/main/java/forestry/api/gui/GuiElementAlignment.java +++ b/src/main/java/forestry/api/gui/GuiElementAlignment.java @@ -6,7 +6,7 @@ package forestry.api.gui; /** - * The alignment of a gui element. + * The alignment of the {@link IGuiElement} defines the position of the element relative to the position of its parent. */ public enum GuiElementAlignment { TOP_LEFT(0.0f, 0.0f), diff --git a/src/main/java/forestry/api/gui/IDatabaseElement.java b/src/main/java/forestry/api/gui/IDatabaseElement.java new file mode 100644 index 000000000..97656a3b7 --- /dev/null +++ b/src/main/java/forestry/api/gui/IDatabaseElement.java @@ -0,0 +1,63 @@ +package forestry.api.gui; + +import javax.annotation.Nullable; +import java.util.function.BiFunction; +import java.util.function.Function; + +import forestry.api.genetics.DatabaseMode; +import forestry.api.genetics.IAllele; +import forestry.api.genetics.IBreedingTracker; +import forestry.api.genetics.IChromosomeType; +import forestry.api.genetics.IGenome; +import forestry.api.genetics.IIndividual; +import forestry.api.genetics.IMutation; +import forestry.api.gui.style.ITextStyle; + +public interface IDatabaseElement extends IElementLayout { + + /** + * Adds the chromosomeName and the name of the active/not active allele, of the chromosome, with {@link #label}. + */ + void addLine(String chromosomeName, IChromosomeType chromosome); + + /** + * Adds the chromosomeName and the result of toString with {@link #label}. + */ + void addLine(String chromosomeName, BiFunction toText, IChromosomeType chromosome); + + void addLine(String chromosomeName, BiFunction toText, IChromosomeType chromosome, boolean dominant); + + void addLine(String leftText, Function toText, IChromosomeType chromosome); + + void addLine(String firstText, String secondText, ITextStyle firstStyle, ITextStyle secondStyle); + + void addLine(String leftText, String rightText, boolean dominant); + + void addLine(String leftText, Function toText, boolean dominant); + + void addFertilityLine(String chromosomeName, IChromosomeType chromosome, int texOffset); + + void addToleranceLine(IChromosomeType chromosome); + + void addMutation(int x, int y, int width, int height, IMutation mutation, IAllele species, IBreedingTracker breedingTracker); + + void addMutationResultant(int x, int y, int width, int height, IMutation mutation, IBreedingTracker breedingTracker); + + //void addRow(String firstText, String secondText, String thirdText, ITextStyle firstStyle, ITextStyle secondStyle, ITextStyle thirdStyle); + + + //void addRow(String firstText, String secondText, String thirdText, boolean secondDominant, boolean thirdDominant); + + //void addRow(String firstText, String secondText, String thirdText, IIndividual individual, IChromosomeType chromosome); + + void addSpeciesLine(String firstText, @Nullable String secondText, IChromosomeType chromosome); + + void init(DatabaseMode mode, IIndividual individual, int secondColumn, int thirdColumn); + + @Nullable + IIndividual getIndividual(); + + IGenome getGenome(); + + //void addSpeciesLine(String firstText, @Nullable String secondText, @Nullable String thirdText, IIndividual individual, IChromosomeType chromosome); +} diff --git a/src/main/java/forestry/api/gui/IElementGenetic.java b/src/main/java/forestry/api/gui/IElementGenetic.java deleted file mode 100644 index 0da598268..000000000 --- a/src/main/java/forestry/api/gui/IElementGenetic.java +++ /dev/null @@ -1,37 +0,0 @@ -package forestry.api.gui; - -import java.util.function.Function; - -import forestry.api.genetics.IAllele; -import forestry.api.genetics.IAlleleInteger; -import forestry.api.genetics.IAlleleSpecies; -import forestry.api.genetics.IAlleleTolerance; -import forestry.api.genetics.IBreedingTracker; -import forestry.api.genetics.IChromosomeType; -import forestry.api.genetics.IIndividual; -import forestry.api.genetics.IMutation; - -public interface IElementGenetic extends IElementGroup { - - /** - * Adds the chromosomeName and the name of the active/not active allele, of the chromosome, with {@link #text}. - */ - void addAlleleRow(String chromosomeName, IIndividual individual, IChromosomeType chromosome, boolean active); - - /** - * Adds the chromosomeName and the result of toString with {@link #text}. - */ - void addAlleleRow(String chromosomeName, Function toString, IIndividual individual, IChromosomeType chromosome, boolean active); - - void addFertilityInfo(String chromosomeName, IAlleleInteger fertilityAllele, int texOffset); - - void addToleranceInfo(String chromosomeName, IAlleleTolerance toleranceAllele, IAlleleSpecies species, String text); - - void addMutation(int x, int y, int width, int height, IMutation mutation, IAllele species, IBreedingTracker breedingTracker); - - void addMutationResultant(int x, int y, int width, int height, IMutation mutation, IBreedingTracker breedingTracker); - - void addRow(String firstText, String secondText, int firstColor, int secondColor); - - void addRow(String leftText, String rightText, boolean dominant); -} diff --git a/src/main/java/forestry/api/gui/IElementGroup.java b/src/main/java/forestry/api/gui/IElementGroup.java index 8e452c9f2..a9692c84b 100644 --- a/src/main/java/forestry/api/gui/IElementGroup.java +++ b/src/main/java/forestry/api/gui/IElementGroup.java @@ -6,6 +6,12 @@ import java.util.List; import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import forestry.api.gui.style.ITextStyle; + +@SideOnly(Side.CLIENT) public interface IElementGroup extends IGuiElement { /** * Adds a element to this layout. @@ -51,47 +57,37 @@ public interface IElementGroup extends IGuiElement { */ List getElements(); - IGuiElement item(int xPos, int yPos, ItemStack itemStack); + IItemElement item(int xPos, int yPos, ItemStack itemStack); - default IGuiElement item(ItemStack itemStack) { + default IItemElement item(ItemStack itemStack) { return item(0, 0, itemStack); } /** - * Adds a text element with the default color,the align {@link GuiElementAlignment#TOP_LEFT} and the height 12. + * Adds a single line of text. */ - IGuiElement text(String text); + ILabelElement label(String text); + + ILabelElement label(String text, ITextStyle style); + + ILabelElement label(String text, GuiElementAlignment align); + + ILabelElement label(String text, GuiElementAlignment align, ITextStyle textStyle); + + ILabelElement label(String text, int width, int height, GuiElementAlignment align, ITextStyle textStyle); + + ILabelElement label(String text, int x, int y, int width, int height, GuiElementAlignment align, ITextStyle textStyle); /** - * Adds a text element with the align {@link GuiElementAlignment#TOP_LEFT} and the height 12. + * Adds a text element that splits the text with wordwrap. */ - IGuiElement text(String text, int color); + ITextElement splitText(String text, int width); - /** - * Adds a text element with the default color and the height 12. - */ - IGuiElement text(String text, GuiElementAlignment align); + ITextElement splitText(String text, int width, ITextStyle textStyle); - IGuiElement text(String text, GuiElementAlignment align, int color, boolean unicode); + ITextElement splitText(String text, int width, GuiElementAlignment align, ITextStyle textStyle); - /** - * Adds a text element with the height 12. - */ - IGuiElement text(String text, GuiElementAlignment align, int color); - - IGuiElement text(int x, String text, GuiElementAlignment align, int color); - - /** - * Adds a text element. - */ - IGuiElement text(int x, int height, String text, GuiElementAlignment align, int color); - - IGuiElement text(int x, int y, int width, int height, String text); - - /** - * Adds a text element. - */ - IGuiElement text(int x, int height, String text, GuiElementAlignment align, int color, boolean unicode); + ITextElement splitText(String text, int x, int y, int width, GuiElementAlignment align, ITextStyle textStyle); default IElementLayout vertical(int width) { return vertical(0, 0, width); @@ -105,10 +101,10 @@ public interface IElementGroup extends IGuiElement { return horizontal(0, 0, height); } - IElementGroup panel(int xPos, int yPos, int width, int height); + IElementGroup pane(int xPos, int yPos, int width, int height); - default IElementGroup panel(int width, int height) { - return panel(0, 0, width, height); + default IElementGroup pane(int width, int height) { + return pane(0, 0, width, height); } IElementLayoutHelper layoutHelper(IElementLayoutHelper.LayoutFactory layoutFactory, int width, int height); diff --git a/src/main/java/forestry/api/gui/IGuiElement.java b/src/main/java/forestry/api/gui/IGuiElement.java index dbb76deea..aa82f4883 100644 --- a/src/main/java/forestry/api/gui/IGuiElement.java +++ b/src/main/java/forestry/api/gui/IGuiElement.java @@ -8,29 +8,62 @@ package forestry.api.gui; import javax.annotation.Nullable; import java.util.Collection; import java.util.List; +import java.util.function.Consumer; + +import net.minecraft.client.gui.GuiScreen; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import forestry.api.gui.events.GuiElementEvent; +import forestry.api.gui.events.GuiEventDestination; +import forestry.api.gui.events.GuiEventHandler; +import forestry.api.gui.events.GuiEventOrigin; + +/** + * A gui element is an object having a graphical representation that can be displayed on the screen and that + * can interact with the user. + * + * The interface the most gui elements of forestry implement. You must place the element a containment hierarchy whose + * root is a top-level {@link IWindowElement}. + */ @SideOnly(Side.CLIENT) public interface IGuiElement { /* Position and Size*/ /** - * @return the x position of this element relative to the position of the top widget. + * @return the x position of this element relative to the position of its parent. */ int getX(); /** - * @return the y position of this element relative to the position of the top widget. + * @return the y position of this element relative to the position of its parent. */ int getY(); - void setAlign(GuiElementAlignment align); + /** + * Sets the alignment of this element. + */ + IGuiElement setAlign(GuiElementAlignment align); + /** + * The alignment of the {@link IGuiElement} defines the position of the element relative to the position of its + * parent. + */ GuiElementAlignment getAlign(); + /** + * The root of the containment hierarchy that this element is part of. + */ + IWindowElement getWindow(); + + /** + * @return the x position of this element relative to the gui. + */ int getAbsoluteX(); + /** + * @return the y position of this element relative to the gui. + */ int getAbsoluteY(); /** @@ -38,35 +71,124 @@ public interface IGuiElement { */ int getWidth(); + void setWidth(int width); + /** * @return the size of the element on the y-axis. */ int getHeight(); - void setSize(int width, int height); - - void setLocation(int xPos, int yPos); - - void setBounds(int xPos, int yPos, int width, int height); + void setHeight(int height); /** - * Sets the x-axis offset to the original x-axis position. + * Sets the dimensions of this element. + * + * @param width the size of the element on the x-axis. + * @param height the size of the element on the y-axis. + */ + IGuiElement setSize(int width, int height); + + /** + * Sets the position of this element. + * + * @param xPos the x position of this element relative to the position of its parent. + * @param yPos the y position of this element relative to the position of its parent. + */ + IGuiElement setLocation(int xPos, int yPos); + + IGuiElement setOffset(int xOffset, int yOffset); + + /** + * Sets the dimensions and position of this element. + * + * @param xPos the x position of this element relative to the position of its parent. + * @param yPos the y position of this element relative to the position of its parent. + * @param width the size of the element on the x-axis. + * @param height the size of the element on the y-axis. + */ + IGuiElement setBounds(int xPos, int yPos, int width, int height); + + /** + * Sets the x position of this element relative to the position of its parent. */ void setXPosition(int xPos); /** - * Sets the y-axis offset to the original y-axis position. + * Sets the y position of this element relative to the position of its parent. */ void setYPosition(int yPos); + /* CROPPED */ + + /** + * Sets the cropped zone of this element and the element the zone is relative to. + * + * @param cropElement The element the zone is relative to. + * @param cropX The x start coordinate of the zone. + * @param cropY The y start coordinate of the zone. + * @param cropWidth The width of the zone. + * @param cropHeight The height of the zone. + */ + void setCroppedZone(@Nullable IGuiElement cropElement, int cropX, int cropY, int cropWidth, int cropHeight); + + /** + * @return The element the cropped zone is relative to. + */ + @Nullable + IGuiElement getCropElement(); + + /** + * @return The x start coordinate of the cropped zone. + */ + int getCropX(); + + /** + * @return The y start coordinate of the cropped zone. + */ + int getCropY(); + + /** + * @return The width of the cropped zone. + */ + int getCropWidth(); + + /** + * @return The height of the cropped zone. + */ + int getCropHeight(); + + /** + * @return True if {@link #setCroppedZone(IGuiElement, int, int, int, int)} was ever called on this element. + */ + boolean isCropped(); + /* Parent */ /** - * @return the element that contains this element and handles all events. + * The position of this element is relative to the position of its parent. + * + * @return the parent element of this element. */ @Nullable IGuiElement getParent(); - void setParent(@Nullable IGuiElement parent); + /** + * Sets the parent of this element. + */ + IGuiElement setParent(@Nullable IGuiElement parent); + + /* Creation & Deletion */ + /** + * Called at {@link IElementGroup#add(IGuiElement)} after the element was added to the group and + * {@link #setParent(IGuiElement)} was called at the element. + * + * Can be used to add other element to the element if the element is an {@link IElementGroup}. + */ + void onCreation(); + + /** + * Called at {@link IElementGroup#remove(IGuiElement...)} after the element was removed from the group. + */ + void onDeletion(); /* Rendering */ /** @@ -79,28 +201,13 @@ public interface IGuiElement { /** * Draws the element itself at the current position. + * + * @param mouseX The x position of the mouse relative to the parent of the element. + * @param mouseY The y position of the mouse relative to the parent of the element. */ void drawElement(int mouseX, int mouseY); - /* Events */ - /** - * @param mouseX The x position of the mouse relative to the parent of the element. - * @param mouseY The y position of the mouse relative to the parent of the element. - * @param mouseButton The pressed mouse button. - */ - void mouseClicked(int mouseX, int mouseY, int mouseButton); - - /** - * @param mouseX The x position of the mouse relative to the parent of the element. - * @param mouseY The y position of the mouse relative to the parent of the element. - * @param mouseButton The pressed mouse button. - */ - void mouseReleased(int mouseX, int mouseY, int mouseButton); - - void mouseClickMove(int mouseX, int mouseY, int mouseButton); - - boolean keyTyped(char typedChar, int keyCode); - + /* Mouse Over */ /** * @param mouseX The x position of the mouse relative to the parent of the element. * @param mouseY The y position of the mouse relative to the parent of the element. @@ -113,11 +220,70 @@ public interface IGuiElement { */ boolean isMouseOver(); + default boolean canMouseOver(){ + return false; + } + + /* Updates */ + /** + * Updates the element. Called at {@link GuiScreen#updateScreen()}. + */ + @SideOnly(Side.CLIENT) + void updateClient(); + + /* State */ + /** + * @return True if this element can be focused and processes keys. + */ + default boolean canFocus(){ + return false; + } + + /** + * The element can be hided with {@link #hide()} and be made visible again with {@link #show()}. + * + * @return True of this element is currently visible. + */ + boolean isVisible(); + + /** + * Makes this element visible again. + */ + void show(); + + /** + * Hides this element. + */ + void hide(); + + /** + * The most elements are enabled by default. Only a few elements are disabled at a certain time like buttons. + * + * @return True if this element is enabled. + */ + boolean isEnabled(); + /* Tooltip */ + /** + * Adds an additional tooltip to the current tooltip of the element. + */ IGuiElement addTooltip(String line); + /** + * Adds an additional tooltip to the current tooltip of the element. + */ IGuiElement addTooltip(Collection lines); + /** + * @return True if this element currently has a tooltip. + */ + boolean hasTooltip(); + + /** + * Clears the tooltips that were added with {@link #addTooltip(String)} and {@link #addTooltip(Collection)}. + * It does not remove default tooltips of an element like the fluid information of a tank or the item information + * of an slot element. + */ void clearTooltip(); /** @@ -128,8 +294,51 @@ public interface IGuiElement { */ List getTooltip(int mouseX, int mouseY); + /** + * @return Returns the tooltips that were added with {@link #addTooltip(String)} and {@link #addTooltip(Collection)}. + */ List getTooltip(); - @Nullable - IGuiState getGuiState(); + /* Events */ + /** + * Adds an event handler that handles events that this element receives with {@link #receiveEvent(GuiElementEvent)}. + */ + void addEventHandler(Consumer eventHandler); + + /** + * Adds an event handler that handles events that this element receives with {@link #receiveEvent(GuiElementEvent)}. + */ + default void addEventHandler(Class eventClass, Consumer eventHandler){ + addEventHandler(new GuiEventHandler<>(eventClass, eventHandler)); + } + + /** + * Adds an event handler that handles events that this element receives with {@link #receiveEvent(GuiElementEvent)}. + */ + default void addEventHandler(Class eventClass, GuiEventOrigin origin, IGuiElement relative, Consumer eventHandler){ + addEventHandler(new GuiEventHandler<>(eventClass, origin, relative, eventHandler)); + } + + /** + * Adds an event handler that handles events that this element receives with {@link #receiveEvent(GuiElementEvent)}. + */ + default void addSelfEventHandler(Class eventClass, Consumer eventHandler){ + addEventHandler(new GuiEventHandler<>(eventClass, GuiEventOrigin.SELF, this, eventHandler)); + } + + /** + * Distributes the event to the elements that are defined by the {@link GuiEventDestination}. + */ + default void postEvent(GuiElementEvent event, GuiEventDestination destination){ + destination.sendEvent(this, event); + } + + default void postEvent(GuiElementEvent event){ + postEvent(event, GuiEventDestination.SINGLE); + } + + /** + * Receives an event and distributes them to the event handlers of this element. + */ + void receiveEvent(GuiElementEvent event); } diff --git a/src/main/java/forestry/api/gui/IGuiElementFactory.java b/src/main/java/forestry/api/gui/IGuiElementFactory.java index e14b94d58..782001a1b 100644 --- a/src/main/java/forestry/api/gui/IGuiElementFactory.java +++ b/src/main/java/forestry/api/gui/IGuiElementFactory.java @@ -18,6 +18,7 @@ import forestry.api.genetics.IBreedingTracker; import forestry.api.genetics.IGeneticAnalyzer; import forestry.api.genetics.IGeneticAnalyzerProvider; import forestry.api.genetics.IMutation; +import forestry.api.gui.style.ITextStyle; /** * A helper interface to create gui elements. @@ -25,7 +26,7 @@ import forestry.api.genetics.IMutation; @SideOnly(Side.CLIENT) public interface IGuiElementFactory { - IGeneticAnalyzer createAnalyzer(int xPos, int yPos, boolean rightBoarder, IGeneticAnalyzerProvider provider); + IGeneticAnalyzer createAnalyzer(IWindowElement window, int xPos, int yPos, boolean rightBoarder, IGeneticAnalyzerProvider provider); /* GENETIC*/ @@ -35,6 +36,10 @@ public interface IGuiElementFactory { */ int getColorCoding(boolean dominant); + ITextStyle getStateStyle(boolean dominant); + + ITextStyle getGuiStyle(); + /** * @return Null if the mutation is secret and undiscovered. {@link IMutation#isSecret()} */ @@ -58,5 +63,5 @@ public interface IGuiElementFactory { IElementLayout createVertical(int xPos, int yPos, int width); - IElementGroup createPanel(int xPos, int yPos, int width, int height); + IElementGroup createPane(int xPos, int yPos, int width, int height); } diff --git a/src/main/java/forestry/api/gui/IItemElement.java b/src/main/java/forestry/api/gui/IItemElement.java new file mode 100644 index 000000000..ef9fddad5 --- /dev/null +++ b/src/main/java/forestry/api/gui/IItemElement.java @@ -0,0 +1,15 @@ +package forestry.api.gui; + +import net.minecraft.item.ItemStack; + +/** + * A element that contains and displays a {@link ItemStack}. + */ +public interface IItemElement extends IGuiElement { + /** + * @return The contained {@link ItemStack}. + */ + ItemStack getStack(); + + IItemElement setStack(ItemStack stack); +} diff --git a/src/main/java/forestry/api/gui/ILabelElement.java b/src/main/java/forestry/api/gui/ILabelElement.java new file mode 100644 index 000000000..7fe20194d --- /dev/null +++ b/src/main/java/forestry/api/gui/ILabelElement.java @@ -0,0 +1,27 @@ +package forestry.api.gui; + +import forestry.api.gui.style.ITextStyle; + +/** + * A label element that displays and contains one single line of text + */ +public interface ILabelElement extends ITextElement { + /** + * @return The style of the text. + */ + ITextStyle getStyle(); + + ILabelElement setStyle(ITextStyle style); + + /** + * @return The current text of this element with its {@link net.minecraft.util.text.TextFormatting}s. + */ + String getText(); + + ILabelElement setText(String text); + + /** + * The current text of this element without its {@link net.minecraft.util.text.TextFormatting}s. + */ + String getRawText(); +} diff --git a/src/main/java/forestry/api/gui/ITextElement.java b/src/main/java/forestry/api/gui/ITextElement.java new file mode 100644 index 000000000..1b11d0359 --- /dev/null +++ b/src/main/java/forestry/api/gui/ITextElement.java @@ -0,0 +1,24 @@ +package forestry.api.gui; + +import java.util.Collection; +import java.util.Map; + +import forestry.api.gui.style.ITextStyle; + +/** + * A element that displays or contains one ore more lines of text. + */ +public interface ITextElement extends IGuiElement { + + /** + * @return The text this element displays. + */ + Collection getLines(); + + ITextElement setText(String text); + + /** + * @return The raw text this element displays without any formations and their style. + */ + Map getRawLines(); +} diff --git a/src/main/java/forestry/api/gui/IGuiState.java b/src/main/java/forestry/api/gui/IWindowElement.java similarity index 60% rename from src/main/java/forestry/api/gui/IGuiState.java rename to src/main/java/forestry/api/gui/IWindowElement.java index 242bee5ca..b65b394f1 100644 --- a/src/main/java/forestry/api/gui/IGuiState.java +++ b/src/main/java/forestry/api/gui/IWindowElement.java @@ -1,14 +1,19 @@ package forestry.api.gui; +import javax.annotation.Nullable; + import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +/** + * The window element is the root element of the containment hierarchy. Its the only element the gui interacts with + * directly. + */ @SideOnly(Side.CLIENT) -public interface IGuiState { - +public interface IWindowElement extends IElementGroup { /** * Returns the mouse position. */ @@ -22,12 +27,12 @@ public interface IGuiState { /** * Returns the mouse position relative to the given element. */ - int getRelativeMouseX(IGuiElement element); + int getRelativeMouseX(@Nullable IGuiElement element); /** * Returns the mouse position relative to the given element. */ - int getRelativeMouseY(IGuiElement element); + int getRelativeMouseY(@Nullable IGuiElement element); /** * Returns the current screen width. @@ -56,4 +61,19 @@ public interface IGuiState { TextureManager getTextureManager(); FontRenderer getFontRenderer(); + + @Nullable + IGuiElement getMousedOverElement(); + + @Nullable + IGuiElement getDraggedElement(); + + @Nullable + IGuiElement getFocusedElement(); + + boolean isMouseOver(IGuiElement element); + + boolean isDragged(IGuiElement element); + + boolean isFocused(IGuiElement element); } diff --git a/src/main/java/forestry/api/gui/events/ElementEvent.java b/src/main/java/forestry/api/gui/events/ElementEvent.java new file mode 100644 index 000000000..fd91da5c8 --- /dev/null +++ b/src/main/java/forestry/api/gui/events/ElementEvent.java @@ -0,0 +1,59 @@ +package forestry.api.gui.events; + +import forestry.api.gui.IGuiElement; + +public class ElementEvent extends GuiElementEvent { + + private ElementEvent(IGuiElement origin) { + super(origin); + } + + public static class Deletion extends ElementEvent { + public Deletion(IGuiElement origin) { + super(origin); + } + } + + public static class StartMouseOver extends ElementEvent { + public StartMouseOver(IGuiElement origin) { + super(origin); + } + } + + public static class EndMouseOver extends ElementEvent { + public EndMouseOver(IGuiElement origin) { + super(origin); + } + } + + public static class StartDrag extends ElementEvent { + private final int button; + + public StartDrag(IGuiElement origin, int button) { + super(origin); + this.button = button; + } + + public int getButton() { + return this.button; + } + } + + public static class EndDrag extends ElementEvent { + public EndDrag(IGuiElement origin) { + super(origin); + } + } + + public static class GainFocus extends ElementEvent { + public GainFocus(IGuiElement origin) { + super(origin); + } + } + + public static class LoseFocus extends ElementEvent { + public LoseFocus(IGuiElement origin) { + super(origin); + } + } +} diff --git a/src/main/java/forestry/api/gui/events/GuiElementEvent.java b/src/main/java/forestry/api/gui/events/GuiElementEvent.java new file mode 100644 index 000000000..9b2ec1a22 --- /dev/null +++ b/src/main/java/forestry/api/gui/events/GuiElementEvent.java @@ -0,0 +1,19 @@ +package forestry.api.gui.events; + +import forestry.api.gui.IGuiElement; + +public abstract class GuiElementEvent { + private final IGuiElement origin; + + public GuiElementEvent(IGuiElement origin) { + this.origin = origin; + } + + public final IGuiElement getOrigin() { + return origin; + } + + public final boolean isOrigin(IGuiElement element) { + return this.origin == element; + } +} diff --git a/src/main/java/forestry/api/gui/events/GuiEvent.java b/src/main/java/forestry/api/gui/events/GuiEvent.java new file mode 100644 index 000000000..13d10dea5 --- /dev/null +++ b/src/main/java/forestry/api/gui/events/GuiEvent.java @@ -0,0 +1,104 @@ +package forestry.api.gui.events; + +import forestry.api.gui.IGuiElement; + +public class GuiEvent extends GuiElementEvent { + private GuiEvent(IGuiElement origin) { + super(origin); + } + + public static class KeyEvent extends GuiEvent { + private final char character; + private final int key; + + public KeyEvent(IGuiElement origin, char character, int key) { + super(origin); + this.character = character; + this.key = key; + } + + public char getCharacter() { + return this.character; + } + + public int getKey() { + return this.key; + } + } + + public static class ButtonEvent extends GuiEvent { + private final int x; + private final int y; + private final int button; + + public ButtonEvent(IGuiElement origin, int x, int y, int button) { + super(origin); + this.x = x; + this.y = y; + this.button = button; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public int getButton() { + return this.button; + } + } + + public static class DownEvent extends ButtonEvent { + public DownEvent(IGuiElement origin, int x, int y, int button) { + super(origin, x, y, button); + } + } + + public static class UpEvent extends ButtonEvent { + public UpEvent(IGuiElement origin, int x, int y, int button) { + super(origin, x, y, button); + } + } + + public static class MoveEvent extends GuiEvent { + private final float dx; + private final float dy; + + public MoveEvent(IGuiElement origin, float dx, float dy) { + super(origin); + this.dx = dx; + this.dy = dy; + } + + public float getDx() { + return this.dx; + } + + public float getDy() { + return this.dy; + } + } + + public static class DragEvent extends MoveEvent { + public DragEvent(IGuiElement draggedWidget, float dx, float dy) { + super(draggedWidget, dx, dy); + } + } + + public static class WheelEvent extends GuiEvent { + private int dWheel; + + public WheelEvent(IGuiElement origin, int dWheel) { + super(origin); + this.dWheel = 0; + this.dWheel = dWheel / 28; + } + + public int getDWheel() { + return this.dWheel; + } + } +} diff --git a/src/main/java/forestry/api/gui/events/GuiEventDestination.java b/src/main/java/forestry/api/gui/events/GuiEventDestination.java new file mode 100644 index 000000000..9798e1445 --- /dev/null +++ b/src/main/java/forestry/api/gui/events/GuiEventDestination.java @@ -0,0 +1,69 @@ +package forestry.api.gui.events; + +import forestry.api.gui.IElementGroup; +import forestry.api.gui.IGuiElement; + +public enum GuiEventDestination { + //Only the current element + SINGLE{ + @Override + public void sendEvent(IGuiElement element, GuiElementEvent event) { + element.receiveEvent(event); + } + }, + //Only the origin element + ORIGIN{ + @Override + public void sendEvent(IGuiElement element, GuiElementEvent event) { + event.getOrigin().receiveEvent(event); + } + }, + //All children elements of the element + CHILDREN{ + @Override + public void sendEvent(IGuiElement element, GuiElementEvent event) { + if(!(element instanceof IElementGroup)){ + return; + } + for(IGuiElement child : ((IElementGroup) element).getElements()){ + child.receiveEvent(event); + } + } + }, + //The parent element of the element + PARENT{ + @Override + public void sendEvent(IGuiElement element, GuiElementEvent event) { + IGuiElement parent = element.getParent(); + if(parent == null){ + return; + } + parent.receiveEvent(event); + } + }, + //The other children of the parent of the element + SIBLINGS{ + @Override + public void sendEvent(IGuiElement element, GuiElementEvent event) { + IGuiElement parent = element.getParent(); + if(parent == null){ + return; + } + parent.postEvent(event, CHILDREN); + } + }, + ALL{ + @Override + public void sendEvent(IGuiElement element, GuiElementEvent event) { + element.receiveEvent(event); + if(!(element instanceof IElementGroup)){ + return; + } + for(IGuiElement child : ((IElementGroup) element).getElements()){ + child.postEvent(event, ALL); + } + } + }; + + public abstract void sendEvent(IGuiElement element, GuiElementEvent event); +} diff --git a/src/main/java/forestry/api/gui/events/GuiEventHandler.java b/src/main/java/forestry/api/gui/events/GuiEventHandler.java new file mode 100644 index 000000000..1739ebefe --- /dev/null +++ b/src/main/java/forestry/api/gui/events/GuiEventHandler.java @@ -0,0 +1,40 @@ +package forestry.api.gui.events; + +import javax.annotation.Nullable; +import java.util.function.Consumer; + +import forestry.api.gui.IGuiElement; + +public final class GuiEventHandler implements Consumer { + private final Consumer handlerAction; + private final Class eventClass; + private final GuiEventOrigin origin; + @Nullable + private final IGuiElement relative; + + public GuiEventHandler(Class eventClass, Consumer handlerAction) { + this.origin = GuiEventOrigin.ANY; + this.relative = null; + this.eventClass = eventClass; + this.handlerAction = handlerAction; + } + + public GuiEventHandler(Class eventClass, GuiEventOrigin origin, IGuiElement relative, Consumer handlerAction) { + this.origin = origin; + this.relative = relative; + this.eventClass = eventClass; + this.handlerAction = handlerAction; + } + + private boolean canHandle(GuiElementEvent event) { + boolean instance = this.eventClass.isInstance(event); + return instance && this.origin.isOrigin(event.getOrigin(), this.relative); + } + + @Override + public final void accept(E e) { + if(canHandle(e)) { + handlerAction.accept(e); + } + } +} diff --git a/src/main/java/forestry/api/gui/events/GuiEventOrigin.java b/src/main/java/forestry/api/gui/events/GuiEventOrigin.java new file mode 100644 index 000000000..eb00805b0 --- /dev/null +++ b/src/main/java/forestry/api/gui/events/GuiEventOrigin.java @@ -0,0 +1,40 @@ +package forestry.api.gui.events; + +import javax.annotation.Nullable; +import java.util.Collection; + +import forestry.api.gui.IElementGroup; +import forestry.api.gui.IGuiElement; + +public enum GuiEventOrigin { + ANY{ + @Override + public boolean isOrigin(IGuiElement origin, @Nullable IGuiElement element) { + return true; + } + }, + SELF{ + @Override + public boolean isOrigin(IGuiElement origin, @Nullable IGuiElement element) { + return element == origin; + } + }, + PARENT{ + @Override + public boolean isOrigin(IGuiElement origin, @Nullable IGuiElement element) { + return element != null && element.getParent() == origin; + } + }, + DIRECT_CHILD{ + @Override + public boolean isOrigin(IGuiElement origin, @Nullable IGuiElement element) { + if(element == null || !(element instanceof IElementGroup)){ + return false; + } + Collection elements = ((IElementGroup) element).getElements(); + return elements.contains(origin); + } + }; + + public abstract boolean isOrigin(IGuiElement origin, @Nullable IGuiElement element); +} diff --git a/src/main/java/forestry/api/gui/events/package-info.java b/src/main/java/forestry/api/gui/events/package-info.java new file mode 100644 index 000000000..32c782f83 --- /dev/null +++ b/src/main/java/forestry/api/gui/events/package-info.java @@ -0,0 +1,12 @@ +/******************************************************************************* + * Copyright 2011-2014 SirSengir + * + * This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details. + ******************************************************************************/ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package forestry.api.gui.events; + +import javax.annotation.ParametersAreNonnullByDefault; + +import mcp.MethodsReturnNonnullByDefault; diff --git a/src/main/java/forestry/api/gui/style/ITextStyle.java b/src/main/java/forestry/api/gui/style/ITextStyle.java new file mode 100644 index 000000000..0c9c89c23 --- /dev/null +++ b/src/main/java/forestry/api/gui/style/ITextStyle.java @@ -0,0 +1,34 @@ +package forestry.api.gui.style; + +public interface ITextStyle { + + int getColor(); + + default boolean isBold(){ + return false; + } + + default boolean isObfuscated(){ + return false; + } + + default boolean isStrikethrough(){ + return false; + } + + default boolean isUnderlined(){ + return false; + } + + default boolean isItalic(){ + return false; + } + + default boolean isShadow(){ + return false; + } + + default boolean isUnicode(){ + return false; + } +} diff --git a/src/main/java/forestry/api/gui/style/ImmutableTextStyle.java b/src/main/java/forestry/api/gui/style/ImmutableTextStyle.java new file mode 100644 index 000000000..9652b83f7 --- /dev/null +++ b/src/main/java/forestry/api/gui/style/ImmutableTextStyle.java @@ -0,0 +1,65 @@ +package forestry.api.gui.style; + +import java.util.function.IntSupplier; + +public final class ImmutableTextStyle implements ITextStyle { + private final IntSupplier color; + private final boolean bold; + private final boolean italic; + private final boolean underlined; + private final boolean strikethrough; + private final boolean obfuscated; + private final boolean shadow; + private final boolean unicode; + + public ImmutableTextStyle() { + this(() -> 0xFFFFFF, false, false, false, false, false, false, false); + } + + public ImmutableTextStyle(int color, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean obfuscated, boolean shadow, boolean unicode) { + this(() -> color, bold, italic, underlined, strikethrough, obfuscated, shadow, unicode); + } + + public ImmutableTextStyle(IntSupplier color, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean obfuscated, boolean shadow, boolean unicode) { + this.color = color; + this.bold = bold; + this.italic = italic; + this.underlined = underlined; + this.strikethrough = strikethrough; + this.obfuscated = obfuscated; + this.shadow = shadow; + this.unicode = unicode; + } + + public int getColor() { + return color.getAsInt(); + } + + public boolean isBold() { + return bold; + } + + public boolean isObfuscated() { + return obfuscated; + } + + public boolean isStrikethrough() { + return strikethrough; + } + + public boolean isUnderlined() { + return underlined; + } + + public boolean isItalic() { + return italic; + } + + public boolean isShadow() { + return shadow; + } + + public boolean isUnicode() { + return unicode; + } +} diff --git a/src/main/java/forestry/api/gui/style/TextStyle.java b/src/main/java/forestry/api/gui/style/TextStyle.java new file mode 100644 index 000000000..bd8d26cb6 --- /dev/null +++ b/src/main/java/forestry/api/gui/style/TextStyle.java @@ -0,0 +1,115 @@ +package forestry.api.gui.style; + +import java.util.function.IntSupplier; + +public class TextStyle implements ITextStyle{ + private IntSupplier color = () -> 0xFFFFFF; + private boolean bold = false; + private boolean italic = false; + private boolean underlined = false; + private boolean strikethrough = false; + private boolean obfuscated = false; + private boolean shadow = false; + private boolean unicode = false; + + public TextStyle() { + } + + public TextStyle(IntSupplier color, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean obfuscated, boolean shadow, boolean unicode) { + this.color = color; + this.bold = bold; + this.italic = italic; + this.underlined = underlined; + this.strikethrough = strikethrough; + this.obfuscated = obfuscated; + this.shadow = shadow; + this.unicode = unicode; + } + + public TextStyle(int color, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean obfuscated, boolean shadow, boolean unicode) { + this.color = () -> color; + this.bold = bold; + this.italic = italic; + this.underlined = underlined; + this.strikethrough = strikethrough; + this.obfuscated = obfuscated; + this.shadow = shadow; + this.unicode = unicode; + } + + public TextStyle setColor(int color) { + this.color = ()-> color; + return this; + } + + public void setColor(IntSupplier color) { + this.color = color; + } + + public int getColor() { + return color.getAsInt(); + } + + public TextStyle setBold(boolean bold) { + this.bold = bold; + return this; + } + + public boolean isBold() { + return bold; + } + + public TextStyle setObfuscated(boolean obfuscated) { + this.obfuscated = obfuscated; + return this; + } + + public boolean isObfuscated() { + return obfuscated; + } + + public TextStyle setStrikethrough(boolean strikethrough) { + this.strikethrough = strikethrough; + return this; + } + + public boolean isStrikethrough() { + return strikethrough; + } + + public TextStyle setUnderlined(boolean underlined) { + this.underlined = underlined; + return this; + } + + public boolean isUnderlined() { + return underlined; + } + + public TextStyle setItalic(boolean italic) { + this.italic = italic; + return this; + } + + public boolean isItalic() { + return italic; + } + + public TextStyle setShadow(boolean shadow) { + this.shadow = shadow; + return this; + } + + public boolean isShadow() { + return shadow; + } + + public boolean isUnicode() { + return unicode; + } + + public TextStyle setUnicode(boolean unicode) { + this.unicode = unicode; + return this; + } +} diff --git a/src/main/java/forestry/api/gui/style/TextStyleBuilder.java b/src/main/java/forestry/api/gui/style/TextStyleBuilder.java new file mode 100644 index 000000000..39d664895 --- /dev/null +++ b/src/main/java/forestry/api/gui/style/TextStyleBuilder.java @@ -0,0 +1,86 @@ +package forestry.api.gui.style; + +import java.util.function.IntSupplier; + +public final class TextStyleBuilder{ + private IntSupplier color; + private boolean bold; + private boolean italic; + private boolean underlined; + private boolean strikethrough; + private boolean obfuscated; + private boolean shadow; + private boolean unicode; + + public TextStyleBuilder(){ + this(0xFFFFFF, false, false, false, false, false, false, false); + } + + public TextStyleBuilder(ITextStyle style){ + this(style.getColor(), style.isBold(), style.isItalic(), style.isUnderlined(), style.isStrikethrough(), style.isObfuscated(), style.isShadow(), style.isUnicode()); + } + + public TextStyleBuilder(int color, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean obfuscated, boolean shadow, boolean unicode) { + this(()->color, bold, italic, underlined, strikethrough, obfuscated, shadow, unicode); + } + + public TextStyleBuilder(IntSupplier color, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean obfuscated, boolean shadow, boolean unicode) { + this.color = color; + this.bold = bold; + this.italic = italic; + this.underlined = underlined; + this.strikethrough = strikethrough; + this.obfuscated = obfuscated; + this.shadow = shadow; + this.unicode = unicode; + } + + public TextStyleBuilder color(int color) { + this.color = () -> color; + return this; + } + + public TextStyleBuilder color(IntSupplier color) { + this.color = color; + return this; + } + + public TextStyleBuilder bold(boolean bold) { + this.bold = bold; + return this; + } + + public TextStyleBuilder obfuscated(boolean obfuscated) { + this.obfuscated = obfuscated; + return this; + } + + public TextStyleBuilder strikethrough(boolean strikethrough) { + this.strikethrough = strikethrough; + return this; + } + + public TextStyleBuilder underlined(boolean underlined) { + this.underlined = underlined; + return this; + } + + public TextStyleBuilder italic(boolean italic) { + this.italic = italic; + return this; + } + + public TextStyleBuilder shadow(boolean shadow) { + this.shadow = shadow; + return this; + } + + public TextStyleBuilder unicode(boolean unicode) { + this.unicode = unicode; + return this; + } + + public ITextStyle build(){ + return new ImmutableTextStyle(color, bold, italic, underlined, strikethrough, obfuscated, shadow, unicode); + } +} diff --git a/src/main/java/forestry/api/gui/style/package-info.java b/src/main/java/forestry/api/gui/style/package-info.java new file mode 100644 index 000000000..aade1c14d --- /dev/null +++ b/src/main/java/forestry/api/gui/style/package-info.java @@ -0,0 +1,12 @@ +/******************************************************************************* + * Copyright 2011-2014 SirSengir + * + * This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details. + ******************************************************************************/ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package forestry.api.gui.style; + +import javax.annotation.ParametersAreNonnullByDefault; + +import mcp.MethodsReturnNonnullByDefault; diff --git a/src/main/java/forestry/api/lepidopterology/ButterflyManager.java b/src/main/java/forestry/api/lepidopterology/ButterflyManager.java index d0b589be6..702e3b342 100644 --- a/src/main/java/forestry/api/lepidopterology/ButterflyManager.java +++ b/src/main/java/forestry/api/lepidopterology/ButterflyManager.java @@ -5,20 +5,31 @@ ******************************************************************************/ package forestry.api.lepidopterology; +import javax.annotation.Nullable; + public class ButterflyManager { /** * Convenient access to AlleleManager.alleleRegistry.getSpeciesRoot("rootButterflies") + * + * @implNote Only null if the "lepidopterology" module is not enabled. */ + @Nullable public static IButterflyRoot butterflyRoot; /** * Used to create new butterflies. + * + * @implNote Only null if the "lepidopterology" module is not enabled. */ + @Nullable public static IButterflyFactory butterflyFactory; /** * Used to create new butterfly mutations. + * + * @implNote Only null if the "lepidopterology" module is not enabled. */ + @Nullable public static IButterflyMutationFactory butterflyMutationFactory; } diff --git a/src/main/java/forestry/apiculture/genetics/BeeDatabaseTab.java b/src/main/java/forestry/apiculture/genetics/BeeDatabaseTab.java index 09c023b7e..6b2f0729a 100644 --- a/src/main/java/forestry/apiculture/genetics/BeeDatabaseTab.java +++ b/src/main/java/forestry/apiculture/genetics/BeeDatabaseTab.java @@ -1,5 +1,7 @@ package forestry.apiculture.genetics; +import java.util.function.Function; + import net.minecraft.item.ItemStack; import net.minecraftforge.fml.relauncher.Side; @@ -10,65 +12,74 @@ import forestry.api.apiculture.EnumBeeChromosome; import forestry.api.apiculture.EnumBeeType; import forestry.api.apiculture.IBee; import forestry.api.genetics.AlleleManager; -import forestry.api.genetics.IAlleleInteger; +import forestry.api.genetics.DatabaseMode; import forestry.api.genetics.IAlleleSpecies; -import forestry.api.genetics.IAlleleTolerance; import forestry.api.genetics.IDatabaseTab; import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; +import forestry.api.gui.style.ITextStyle; +import forestry.api.gui.style.TextStyleBuilder; import forestry.api.lepidopterology.EnumButterflyChromosome; import forestry.core.genetics.alleles.AlleleBoolean; +import forestry.core.gui.elements.GuiElementFactory; import forestry.core.render.ColourProperties; import forestry.core.utils.StringUtil; import forestry.core.utils.Translator; @SideOnly(Side.CLIENT) public class BeeDatabaseTab implements IDatabaseTab { - private final boolean active; + private static final ITextStyle BINOMIAL = new TextStyleBuilder().color(()-> ColourProperties.INSTANCE.get("gui.beealyzer.binomial")).build(); - BeeDatabaseTab(boolean active) { - this.active = active; + private final DatabaseMode mode; + + BeeDatabaseTab(DatabaseMode mode) { + this.mode = mode; } @Override - public void createElements(IElementGenetic container, IBee bee, ItemStack itemStack) { + public DatabaseMode getMode() { + return mode; + } + + @Override + public void createElements(IDatabaseElement container, IBee bee, ItemStack itemStack) { EnumBeeType type = BeeManager.beeRoot.getType(itemStack); if (type == null) { return; } IAlleleSpecies primarySpecies = bee.getGenome().getPrimary(); + IAlleleSpecies secondarySpecies = bee.getGenome().getSecondary(); - container.text(Translator.translateToLocal("for.gui.database.tab." + (active ? "active" : "inactive") + "_species.name"), GuiElementAlignment.TOP_CENTER, 0xcfb53b); + container.label(Translator.translateToLocal("for.gui.database.tab." + (mode == DatabaseMode.ACTIVE ? "active" : "inactive") + "_species.name"), GuiElementAlignment.TOP_CENTER, GuiElementFactory.DATABASE_TITLE); - container.addAlleleRow(Translator.translateToLocal("for.gui.species"), bee, EnumBeeChromosome.SPECIES, active); + container.addLine(Translator.translateToLocal("for.gui.species"), EnumBeeChromosome.SPECIES); - IAlleleTolerance tempTolerance = (IAlleleTolerance) (active ? bee.getGenome().getActiveAllele(EnumBeeChromosome.TEMPERATURE_TOLERANCE) : bee.getGenome().getInactiveAllele(EnumBeeChromosome.TEMPERATURE_TOLERANCE)); + Function toleranceText = a ->{ + IAlleleSpecies species = a ? primarySpecies : secondarySpecies; + return AlleleManager.climateHelper.toDisplay(species.getTemperature()); + }; + container.addLine(Translator.translateToLocal("for.gui.climate"), toleranceText, EnumBeeChromosome.TEMPERATURE_TOLERANCE); + container.addToleranceLine(EnumBeeChromosome.TEMPERATURE_TOLERANCE); - //container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.climate"), GuiElementAlignment.CENTER); - container.addToleranceInfo(Translator.translateToLocal("for.gui.climate"), tempTolerance, primarySpecies, AlleleManager.climateHelper.toDisplay(primarySpecies.getTemperature())); + container.addLine(Translator.translateToLocal("for.gui.humidity"), toleranceText, EnumBeeChromosome.HUMIDITY_TOLERANCE); + container.addToleranceLine(EnumBeeChromosome.HUMIDITY_TOLERANCE); - IAlleleTolerance humidTolerance = (IAlleleTolerance) (active ? bee.getGenome().getActiveAllele(EnumBeeChromosome.HUMIDITY_TOLERANCE) : bee.getGenome().getInactiveAllele(EnumBeeChromosome.HUMIDITY_TOLERANCE)); + container.addLine(Translator.translateToLocal("for.gui.lifespan"), EnumBeeChromosome.LIFESPAN); - //container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.humidity"), GuiElementAlignment.CENTER); - container.addToleranceInfo(Translator.translateToLocal("for.gui.humidity"), humidTolerance, primarySpecies, AlleleManager.climateHelper.toDisplay(primarySpecies.getHumidity())); + container.addLine(Translator.translateToLocal("for.gui.speed"), EnumBeeChromosome.SPEED); + container.addLine(Translator.translateToLocal("for.gui.pollination"), EnumBeeChromosome.FLOWERING); + container.addLine(Translator.translateToLocal("for.gui.flowers"), EnumBeeChromosome.FLOWER_PROVIDER); - container.addAlleleRow(Translator.translateToLocal("for.gui.lifespan"), bee, EnumBeeChromosome.LIFESPAN, active); + container.addFertilityLine(Translator.translateToLocal("for.gui.fertility"), EnumBeeChromosome.FERTILITY, 0); - container.addAlleleRow(Translator.translateToLocal("for.gui.speed"), bee, EnumBeeChromosome.SPEED, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.pollination"), bee, EnumBeeChromosome.FLOWERING, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.flowers"), bee, EnumBeeChromosome.FLOWER_PROVIDER, active); - - IAlleleInteger primaryFertility = (IAlleleInteger) (active ? bee.getGenome().getActiveAllele(EnumBeeChromosome.FERTILITY) : bee.getGenome().getInactiveAllele(EnumBeeChromosome.FERTILITY)); - container.addFertilityInfo(Translator.translateToLocal("for.gui.fertility"), primaryFertility, 0); - - container.addAlleleRow(Translator.translateToLocal("for.gui.area"), bee, EnumBeeChromosome.TERRITORY, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.effect"), bee, EnumBeeChromosome.EFFECT, active); + container.addLine(Translator.translateToLocal("for.gui.area"), EnumBeeChromosome.TERRITORY); + container.addLine(Translator.translateToLocal("for.gui.effect"), EnumBeeChromosome.EFFECT); String yes = Translator.translateToLocal("for.yes"); String no = Translator.translateToLocal("for.no"); String diurnal, nocturnal; - if(active) { + if(mode == DatabaseMode.ACTIVE) { if (bee.getGenome().getNeverSleeps()) { nocturnal = diurnal = yes; } else { @@ -84,21 +95,15 @@ public class BeeDatabaseTab implements IDatabaseTab { } } - container.addRow(Translator.translateToLocal("for.gui.diurnal"), diurnal, false); + container.addLine(Translator.translateToLocal("for.gui.diurnal"), diurnal, false); - //elementHelper.addText(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.diurnal"), GuiElementAlignment.CENTER); - //elementHelper.addText(diurnal, GuiElementAlignment.CENTER, elementHelper.factory().getColorCoding(false)); + container.addLine(Translator.translateToLocal("for.gui.nocturnal"), nocturnal, false); - container.addRow(Translator.translateToLocal("for.gui.nocturnal"), nocturnal, false); + Function flyer = active -> StringUtil.readableBoolean(active ? bee.getGenome().getToleratesRain() : ((AlleleBoolean) bee.getGenome().getInactiveAllele(EnumBeeChromosome.TOLERATES_RAIN)).getValue(), yes, no); + container.addLine(Translator.translateToLocal("for.gui.flyer"), flyer, EnumBeeChromosome.TOLERATES_RAIN); - //elementHelper.addText(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.nocturnal"), GuiElementAlignment.CENTER); - //elementHelper.addText(nocturnal, GuiElementAlignment.CENTER, elementHelper.factory().getColorCoding(false)); - - String flyer = StringUtil.readableBoolean(active ? bee.getGenome().getToleratesRain() : ((AlleleBoolean) bee.getGenome().getInactiveAllele(EnumBeeChromosome.TOLERATES_RAIN)).getValue(), yes, no); - container.addAlleleRow(Translator.translateToLocal("for.gui.flyer"), (a) -> flyer, bee, EnumButterflyChromosome.TOLERANT_FLYER, active); - - String cave = StringUtil.readableBoolean(active ? bee.getGenome().getCaveDwelling() : ((AlleleBoolean) bee.getGenome().getInactiveAllele(EnumBeeChromosome.CAVE_DWELLING)).getValue(), yes, no); - container.addAlleleRow(Translator.translateToLocal("for.gui.cave"), (a) -> cave, bee, EnumButterflyChromosome.FIRE_RESIST, active); + Function cave = active -> StringUtil.readableBoolean(active ? bee.getGenome().getCaveDwelling() : ((AlleleBoolean) bee.getGenome().getInactiveAllele(EnumBeeChromosome.CAVE_DWELLING)).getValue(), yes, no); + container.addLine(Translator.translateToLocal("for.gui.cave"), cave, EnumBeeChromosome.CAVE_DWELLING); String displayText; if (type == EnumBeeType.PRINCESS || type == EnumBeeType.QUEEN) { @@ -107,12 +112,12 @@ public class BeeDatabaseTab implements IDatabaseTab { displayTextKey = "for.bees.stock.ignoble"; } displayText = Translator.translateToLocal(displayTextKey); - container.text(displayText, GuiElementAlignment.TOP_CENTER, ColourProperties.INSTANCE.get("gui.beealyzer.binomial")); + container.label(displayText, GuiElementAlignment.TOP_CENTER, BINOMIAL); } } @Override public ItemStack getIconStack() { - return BeeDefinition.MEADOWS.getMemberStack(active ? EnumBeeType.PRINCESS : EnumBeeType.DRONE); + return BeeDefinition.MEADOWS.getMemberStack(mode == DatabaseMode.ACTIVE ? EnumBeeType.PRINCESS : EnumBeeType.DRONE); } } diff --git a/src/main/java/forestry/apiculture/genetics/BeePlugin.java b/src/main/java/forestry/apiculture/genetics/BeePlugin.java index 3b87cf9f3..f93e73d78 100644 --- a/src/main/java/forestry/apiculture/genetics/BeePlugin.java +++ b/src/main/java/forestry/apiculture/genetics/BeePlugin.java @@ -12,6 +12,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.apiculture.IAlleleBeeSpecies; import forestry.api.apiculture.IBee; +import forestry.api.genetics.DatabaseMode; import forestry.apiculture.ModuleApiculture; import forestry.apiculture.items.EnumHoneyComb; import forestry.core.config.Config; @@ -26,8 +27,8 @@ public class BeePlugin extends DatabasePlugin { protected final Map iconStacks = new HashMap<>(); private BeePlugin() { - super(new BeeDatabaseTab(true), - new BeeDatabaseTab(false), + super(new BeeDatabaseTab(DatabaseMode.ACTIVE), + new BeeDatabaseTab(DatabaseMode.INACTIVE), new ProductsTab(()->ModuleApiculture.getItems().beeComb.get(EnumHoneyComb.HONEY, 1)), new MutationsTab(()-> ModuleApiculture.getItems().frameImpregnated.getItemStack())); NonNullList beeList = NonNullList.create(); diff --git a/src/main/java/forestry/arboriculture/genetics/TreeDatabaseTab.java b/src/main/java/forestry/arboriculture/genetics/TreeDatabaseTab.java index 68febb20f..385b187fd 100644 --- a/src/main/java/forestry/arboriculture/genetics/TreeDatabaseTab.java +++ b/src/main/java/forestry/arboriculture/genetics/TreeDatabaseTab.java @@ -16,75 +16,82 @@ import forestry.api.arboriculture.EnumTreeChromosome; import forestry.api.arboriculture.IAlleleFruit; import forestry.api.arboriculture.IAlleleTreeSpecies; import forestry.api.arboriculture.ITree; +import forestry.api.genetics.DatabaseMode; import forestry.api.genetics.IAlleleInteger; import forestry.api.genetics.IDatabaseTab; import forestry.api.genetics.IFruitFamily; +import forestry.api.gui.GuiConstants; import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; +import forestry.api.gui.style.ITextStyle; import forestry.arboriculture.genetics.alleles.AlleleFruits; import forestry.core.gui.elements.GuiElementFactory; import forestry.core.utils.Translator; @SideOnly(Side.CLIENT) public class TreeDatabaseTab implements IDatabaseTab { - private final boolean active; + private final DatabaseMode mode; - TreeDatabaseTab(boolean active) { - this.active = active; + TreeDatabaseTab(DatabaseMode mode) { + this.mode = mode; } @Override - public void createElements(IElementGenetic container, ITree tree, ItemStack itemStack) { + public DatabaseMode getMode() { + return mode; + } + + @Override + public void createElements(IDatabaseElement container, ITree tree, ItemStack itemStack) { IAlleleTreeSpecies primarySpecies = tree.getGenome().getPrimary(); - IAlleleTreeSpecies species = active ? primarySpecies : tree.getGenome().getSecondary(); - int speciesColor = GuiElementFactory.INSTANCE.getColorCoding(species.isDominant()); + IAlleleTreeSpecies species = mode == DatabaseMode.ACTIVE ? primarySpecies : tree.getGenome().getSecondary(); + ITextStyle speciesStyle = GuiElementFactory.INSTANCE.getStateStyle(species.isDominant()); - container.text(Translator.translateToLocal("for.gui.database.tab." + (active ? "active" : "inactive") + "_species.name"), GuiElementAlignment.TOP_CENTER, 0xcfb53b); + container.label(Translator.translateToLocal("for.gui.database.tab." + (mode == DatabaseMode.ACTIVE ? "active" : "inactive") + "_species.name"), GuiElementAlignment.TOP_CENTER, GuiElementFactory.DATABASE_TITLE); - container.addAlleleRow(Translator.translateToLocal("for.gui.species"), tree, EnumTreeChromosome.SPECIES, active); + container.addLine(Translator.translateToLocal("for.gui.species"), EnumTreeChromosome.SPECIES); - container.addAlleleRow(Translator.translateToLocal("for.gui.saplings"), tree, EnumTreeChromosome.FERTILITY, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.maturity"), tree, EnumTreeChromosome.MATURATION, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.height"), tree, EnumTreeChromosome.HEIGHT, active); + container.addLine(Translator.translateToLocal("for.gui.saplings"), EnumTreeChromosome.FERTILITY); + container.addLine(Translator.translateToLocal("for.gui.maturity"), EnumTreeChromosome.MATURATION); + container.addLine(Translator.translateToLocal("for.gui.height"), EnumTreeChromosome.HEIGHT); - IAlleleInteger girth = (IAlleleInteger) (active ? tree.getGenome().getActiveAllele(EnumTreeChromosome.GIRTH) : tree.getGenome().getInactiveAllele(EnumTreeChromosome.GIRTH)); - container.addRow(Translator.translateToLocal("for.gui.girth"), String.format("%sx%s", girth.getValue(), girth.getValue()), girth.isDominant()); + container.addLine(Translator.translateToLocal("for.gui.girth"), (IAlleleInteger girth, Boolean active) -> String.format("%sx%s", girth.getValue(), girth.getValue()), EnumTreeChromosome.GIRTH); - container.addAlleleRow(Translator.translateToLocal("for.gui.yield"), tree, EnumTreeChromosome.YIELD, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.sappiness"), tree, EnumTreeChromosome.SAPPINESS, active); + container.addLine(Translator.translateToLocal("for.gui.yield"), EnumTreeChromosome.YIELD); + container.addLine(Translator.translateToLocal("for.gui.sappiness"), EnumTreeChromosome.SAPPINESS); - container.addAlleleRow(Translator.translateToLocal("for.gui.effect"), tree, EnumTreeChromosome.EFFECT, active); + container.addLine(Translator.translateToLocal("for.gui.effect"), EnumTreeChromosome.EFFECT); - container.addRow(Translator.translateToLocal("for.gui.native"), Translator.translateToLocal("for.gui." + tree.getGenome().getPrimary().getPlantType().toString().toLowerCase(Locale.ENGLISH)), species.isDominant()); + container.addLine(Translator.translateToLocal("for.gui.native"), Translator.translateToLocal("for.gui." + tree.getGenome().getPrimary().getPlantType().toString().toLowerCase(Locale.ENGLISH)), species.isDominant()); - container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.supports"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.supports"), GuiElementAlignment.TOP_CENTER, GuiConstants.UNDERLINED_STYLE); List families = new ArrayList<>(tree.getGenome().getPrimary().getSuitableFruit()); for (IFruitFamily fruitFamily : families) { - container.text(fruitFamily.getName(), GuiElementAlignment.TOP_CENTER, speciesColor); + container.label(fruitFamily.getName(), GuiElementAlignment.TOP_CENTER, speciesStyle); } - IAlleleFruit fruit = (IAlleleFruit) (active ? tree.getGenome().getActiveAllele(EnumTreeChromosome.FRUITS) : tree.getGenome().getInactiveAllele(EnumTreeChromosome.FRUITS)); - int colorCoding = GuiElementFactory.INSTANCE.getColorCoding(tree.getGenome().getActiveAllele(EnumTreeChromosome.FRUITS).isDominant()); + IAlleleFruit fruit = (IAlleleFruit) (mode == DatabaseMode.ACTIVE ? tree.getGenome().getActiveAllele(EnumTreeChromosome.FRUITS) : tree.getGenome().getInactiveAllele(EnumTreeChromosome.FRUITS)); + ITextStyle textStyle = GuiElementFactory.INSTANCE.getStateStyle(tree.getGenome().getActiveAllele(EnumTreeChromosome.FRUITS).isDominant()); - container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.fruits"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.fruits"), GuiElementAlignment.TOP_CENTER, GuiConstants.UNDERLINED_STYLE); String strike = ""; if (!species.getSuitableFruit().contains(fruit.getProvider().getFamily()) && fruit != AlleleFruits.fruitNone) { strike = TextFormatting.STRIKETHROUGH.toString(); } - container.text(strike + fruit.getProvider().getDescription(), GuiElementAlignment.TOP_CENTER, colorCoding); + container.label(strike + fruit.getProvider().getDescription(), GuiElementAlignment.TOP_CENTER, textStyle); IFruitFamily family = fruit.getProvider().getFamily(); if (family != null && !family.getUID().equals(EnumFruitFamily.NONE.getUID())) { - container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.family"), GuiElementAlignment.TOP_CENTER); - container.text(family.getName(), GuiElementAlignment.TOP_CENTER, colorCoding); + container.label(Translator.translateToLocal("for.gui.family"), GuiElementAlignment.TOP_CENTER, GuiConstants.UNDERLINED_STYLE); + container.label(family.getName(), GuiElementAlignment.TOP_CENTER, textStyle); } } @Override public ItemStack getIconStack() { - return TreeDefinition.Cherry.getMemberStack(active ? EnumGermlingType.SAPLING : EnumGermlingType.POLLEN); + return TreeDefinition.Cherry.getMemberStack(mode == DatabaseMode.ACTIVE ? EnumGermlingType.SAPLING : EnumGermlingType.POLLEN); } } diff --git a/src/main/java/forestry/arboriculture/genetics/TreePlugin.java b/src/main/java/forestry/arboriculture/genetics/TreePlugin.java index 817c05ee3..fea6f3a07 100644 --- a/src/main/java/forestry/arboriculture/genetics/TreePlugin.java +++ b/src/main/java/forestry/arboriculture/genetics/TreePlugin.java @@ -12,6 +12,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.arboriculture.IAlleleTreeSpecies; import forestry.api.arboriculture.ITree; +import forestry.api.genetics.DatabaseMode; import forestry.arboriculture.ModuleArboriculture; import forestry.core.config.Config; import forestry.core.genetics.analyzer.DatabasePlugin; @@ -26,8 +27,8 @@ public class TreePlugin extends DatabasePlugin { protected final Map iconStacks = new HashMap<>(); private TreePlugin() { - super(new TreeDatabaseTab(true), - new TreeDatabaseTab(false), + super(new TreeDatabaseTab(DatabaseMode.ACTIVE), + new TreeDatabaseTab(DatabaseMode.INACTIVE), new ProductsTab(()-> ItemFruit.EnumFruit.CHERRY.getStack()), new MutationsTab(()->ModuleArboriculture.getItems().grafter.getItemStack())); NonNullList treeList = NonNullList.create(); diff --git a/src/main/java/forestry/book/data/content/ImageContent.java b/src/main/java/forestry/book/data/content/ImageContent.java index 8d1a23c59..2030e2b80 100644 --- a/src/main/java/forestry/book/data/content/ImageContent.java +++ b/src/main/java/forestry/book/data/content/ImageContent.java @@ -35,7 +35,7 @@ public class ImageContent extends BookContent { return false; } if (center) { - page.panel(page.getWidth(), pageHeight - page.getHeight()) + page.pane(page.getWidth(), pageHeight - page.getHeight()) .add(new DrawableElement(0, 0, width, height, data)).setAlign(GuiElementAlignment.MIDDLE_CENTER); } else { page.add(new DrawableElement(0, 0, width, height, data)).setAlign(GuiElementAlignment.TOP_CENTER); diff --git a/src/main/java/forestry/book/gui/GuiForesterBook.java b/src/main/java/forestry/book/gui/GuiForesterBook.java index 6292985aa..c445ca475 100644 --- a/src/main/java/forestry/book/gui/GuiForesterBook.java +++ b/src/main/java/forestry/book/gui/GuiForesterBook.java @@ -1,12 +1,9 @@ package forestry.book.gui; import javax.annotation.Nullable; -import java.io.IOException; import java.util.Collections; import java.util.List; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.entity.player.InventoryPlayer; @@ -21,12 +18,11 @@ import forestry.api.book.IForesterBook; import forestry.book.gui.buttons.GuiButtonBack; import forestry.book.gui.buttons.GuiButtonPage; import forestry.core.config.Constants; -import forestry.core.gui.GuiUtil; +import forestry.core.gui.GuiWindow; import forestry.core.gui.IGuiSizable; -import forestry.core.gui.elements.ElementManager; @SideOnly(Side.CLIENT) -public abstract class GuiForesterBook extends GuiScreen implements IGuiSizable { +public abstract class GuiForesterBook extends GuiWindow implements IGuiSizable { public static final ResourceLocation TEXTURE = new ResourceLocation(Constants.MOD_ID, Constants.TEXTURE_PATH_GUI + "/almanac/almanac.png"); static final int LEFT_PAGE_START_X = 16; static final int RIGHT_PAGE_START_X = 132; @@ -41,15 +37,11 @@ public abstract class GuiForesterBook extends GuiScreen implements IGuiSizable { @Nullable private static GuiForesterBook guiScreen; protected final IForesterBook book; - final ElementManager elementManager; - - protected int guiLeft; - protected int guiTop; protected GuiForesterBook(IForesterBook book) { + super(X_SIZE, Y_SIZE); this.book = book; setGuiScreen(this); - this.elementManager = new ElementManager<>(this); } public IForesterBook getBook() { @@ -59,9 +51,6 @@ public abstract class GuiForesterBook extends GuiScreen implements IGuiSizable { @Override public void initGui() { super.initGui(); - this.guiLeft = (this.width - X_SIZE) / 2; - this.guiTop = (this.height - Y_SIZE) / 2; - elementManager.init(guiLeft, guiTop); this.buttonList.clear(); if (hasButtons()) { GuiButtonPage leftButton = addButton(new GuiButtonPage(0, guiLeft + 24, guiTop + Y_SIZE - 20, true)); @@ -100,13 +89,15 @@ public abstract class GuiForesterBook extends GuiScreen implements IGuiSizable { fontRenderer.setUnicodeFlag(unicode); - elementManager.draw(mouseX, mouseY); + drawTooltips(mouseX, mouseY); + } + @Override + protected void drawTooltips(int mouseX, int mouseY) { + super.drawTooltips(mouseX, mouseY); InventoryPlayer playerInv = mc.player.inventory; if (playerInv.getItemStack().isEmpty()) { - GuiUtil.drawToolTips(this, buttonList, mouseX, mouseY); - elementManager.drawTooltip(mouseX, mouseY); List tooltip = getTooltip(mouseX, mouseY); if (!tooltip.isEmpty()) { ScaledResolution scaledresolution = new ScaledResolution(mc); @@ -124,57 +115,6 @@ public abstract class GuiForesterBook extends GuiScreen implements IGuiSizable { protected abstract String getTitle(); - @Override - protected void keyTyped(char typedChar, int keyCode) throws IOException { - if (elementManager.keyTyped(typedChar, keyCode)) { - return; - } - super.keyTyped(typedChar, keyCode); - } - - @Override - protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { - super.mouseClicked(mouseX, mouseY, mouseButton); - elementManager.mouseClicked(mouseX, mouseY, mouseButton); - } - - @Override - protected void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) { - super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick); - elementManager.mouseClickMove(mouseX, mouseY, clickedMouseButton); - } - - @Override - protected void mouseReleased(int mouseX, int mouseY, int state) { - super.mouseReleased(mouseX, mouseY, state); - elementManager.mouseReleased(mouseX, mouseY, state); - } - - @Override - public int getGuiLeft() { - return guiLeft; - } - - @Override - public int getGuiTop() { - return guiTop; - } - - @Override - public int getSizeX() { - return X_SIZE; - } - - @Override - public int getSizeY() { - return Y_SIZE; - } - - @Override - public Minecraft getMC() { - return mc; - } - @Nullable public static GuiForesterBook getGuiScreen() { return guiScreen; diff --git a/src/main/java/forestry/book/gui/GuiForestryBookEntries.java b/src/main/java/forestry/book/gui/GuiForestryBookEntries.java index 73b2692b4..53cb8d958 100644 --- a/src/main/java/forestry/book/gui/GuiForestryBookEntries.java +++ b/src/main/java/forestry/book/gui/GuiForestryBookEntries.java @@ -32,8 +32,8 @@ public class GuiForestryBookEntries extends GuiForesterBook { int offset = entryIndex * 24; addEntryButtons(offset, LEFT_PAGE_START_X, LEFT_PAGE_START_Y); addEntryButtons(offset + 12, RIGHT_PAGE_START_X, RIGHT_PAGE_START_Y); - elementManager.init(guiLeft, guiTop); - elementManager.clear(); + window.init(guiLeft, guiTop); + window.clear(); } private void addEntryButtons(int indexStart, int xStart, int yStart){ @@ -79,11 +79,6 @@ public class GuiForestryBookEntries extends GuiForesterBook { } } - @Override - protected List getTooltip(int mouseX, int mouseY) { - return elementManager.getTooltip(mouseX, mouseY); - } - private void displayCategories() { mc.displayGuiScreen(new GuiForestryBookCategories(book)); } diff --git a/src/main/java/forestry/book/gui/GuiForestryBookPages.java b/src/main/java/forestry/book/gui/GuiForestryBookPages.java index 9206ee562..e21ea9ace 100644 --- a/src/main/java/forestry/book/gui/GuiForestryBookPages.java +++ b/src/main/java/forestry/book/gui/GuiForestryBookPages.java @@ -21,7 +21,6 @@ import forestry.api.gui.IGuiElement; import forestry.book.gui.buttons.GuiButtonBack; import forestry.book.gui.buttons.GuiButtonPage; import forestry.book.gui.buttons.GuiButtonSubEntry; -import forestry.core.gui.elements.layouts.ElementGroup; @SideOnly(Side.CLIENT) public class GuiForestryBookPages extends GuiForesterBook { @@ -42,9 +41,8 @@ public class GuiForestryBookPages extends GuiForesterBook { this.category = category; this.entry = entry; this.parent = parent; - ElementGroup group = elementManager.group(); - leftPage = group.panel(LEFT_PAGE_START_X, LEFT_PAGE_START_Y, PAGE_WIDTH, PAGE_HEIGHT); - rightPage = group.panel(RIGHT_PAGE_START_X, RIGHT_PAGE_START_Y, PAGE_WIDTH, PAGE_HEIGHT); + leftPage = window.pane(LEFT_PAGE_START_X, LEFT_PAGE_START_Y, PAGE_WIDTH, PAGE_HEIGHT); + rightPage = window.pane(RIGHT_PAGE_START_X, RIGHT_PAGE_START_Y, PAGE_WIDTH, PAGE_HEIGHT); pages = ImmutableList.copyOf(entry.getPageFactory().load(entry, GuiForesterBook.PAGE_HEIGHT - 13, GuiForesterBook.PAGE_HEIGHT, GuiForesterBook.PAGE_WIDTH)); setPages(0); } diff --git a/src/main/java/forestry/book/gui/elements/IndexElement.java b/src/main/java/forestry/book/gui/elements/IndexElement.java index 9ed0ca9bd..f093190f6 100644 --- a/src/main/java/forestry/book/gui/elements/IndexElement.java +++ b/src/main/java/forestry/book/gui/elements/IndexElement.java @@ -6,14 +6,19 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.gui.GuiElementAlignment; +import forestry.api.gui.events.GuiEvent; +import forestry.api.gui.style.ITextStyle; +import forestry.api.gui.style.TextStyleBuilder; import forestry.book.data.IndexEntry; import forestry.book.gui.GuiForesterBook; import forestry.book.gui.GuiForestryBookPages; -import forestry.core.gui.elements.TextElement; +import forestry.core.gui.elements.LabelElement; import forestry.core.gui.elements.layouts.VerticalLayout; @SideOnly(Side.CLIENT) public class IndexElement extends VerticalLayout { + private static final ITextStyle INDEX_STYLE = new TextStyleBuilder().unicode(true).color(0x000000).build(); + public IndexElement(int xPos, int yPos, IndexEntry[] data) { super(xPos, yPos, 108); for (IndexEntry index : data) { @@ -21,33 +26,35 @@ public class IndexElement extends VerticalLayout { } } - private class IndexEntryElement extends TextElement { + private class IndexEntryElement extends LabelElement { private final IndexEntry data; public IndexEntryElement(IndexEntry data) { - super(-1, 9, data.title, GuiElementAlignment.TOP_LEFT, 0, true); - width += TextElement.FONT_RENDERER.getStringWidth(" > "); + super(0, 0, -1, 9, data.title, GuiElementAlignment.TOP_LEFT, INDEX_STYLE); + setWidth(width + LabelElement.FONT_RENDERER.getStringWidth(" > ")); this.data = data; + addSelfEventHandler(GuiEvent.DownEvent.class, event -> { + GuiForesterBook bookGui = GuiForesterBook.getGuiScreen(); + if (bookGui instanceof GuiForestryBookPages) { + GuiForestryBookPages pagesGui = (GuiForestryBookPages) bookGui; + pagesGui.switchPage(data.page); + } + }); } @Override public void drawElement(int mouseX, int mouseY) { - boolean mouseOver = isMouseOver(mouseX, mouseY); + boolean mouseOver = isMouseOver(); boolean unicode = FONT_RENDERER.getUnicodeFlag(); String preFix = mouseOver ? TextFormatting.GOLD + " > " : TextFormatting.DARK_GRAY + "- "; - FONT_RENDERER.setUnicodeFlag(this.unicode); - FONT_RENDERER.drawString(preFix + text, 0, 0, color); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + FONT_RENDERER.drawString(preFix + text, 0, 0, style.getColor()); FONT_RENDERER.setUnicodeFlag(unicode); } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - super.mouseClicked(mouseX, mouseY, mouseButton); - GuiForesterBook bookGui = GuiForesterBook.getGuiScreen(); - if (bookGui instanceof GuiForestryBookPages) { - GuiForestryBookPages pagesGui = (GuiForestryBookPages) bookGui; - pagesGui.switchPage(data.page); - } + public boolean canMouseOver() { + return true; } } } diff --git a/src/main/java/forestry/book/gui/elements/MultiblockElement.java b/src/main/java/forestry/book/gui/elements/MultiblockElement.java index 67134e011..bb4f14dca 100644 --- a/src/main/java/forestry/book/gui/elements/MultiblockElement.java +++ b/src/main/java/forestry/book/gui/elements/MultiblockElement.java @@ -30,6 +30,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import forestry.api.gui.events.GuiEvent; import forestry.book.data.structure.BlockData; import forestry.book.data.structure.StructureBlockAccess; import forestry.book.data.structure.StructureInfo; @@ -76,6 +77,14 @@ public class MultiblockElement extends GuiElement { rotX = 25; rotY = -45; + + addSelfEventHandler(GuiEvent.DownEvent.class, event -> lastClick = new int[]{event.getX(), event.getY()}); + addSelfEventHandler(GuiEvent.UpEvent.class, event -> lastClick = null); + } + + @Override + public boolean canMouseOver() { + return true; } @Override @@ -155,17 +164,4 @@ public class MultiblockElement extends GuiElement { GlStateManager.enableBlend(); RenderHelper.disableStandardItemLighting(); } - - @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - super.mouseClicked(mouseX, mouseY, mouseButton); - - lastClick = new int[]{mouseX, mouseY}; - } - - @Override - public void mouseReleased(int mouseX, int mouseY, int clickedMouseButton) { - super.mouseReleased(mouseX, mouseY, clickedMouseButton); - lastClick = null; - } } diff --git a/src/main/java/forestry/book/gui/elements/MutationElement.java b/src/main/java/forestry/book/gui/elements/MutationElement.java index ef17f0fb2..fde9bee1c 100644 --- a/src/main/java/forestry/book/gui/elements/MutationElement.java +++ b/src/main/java/forestry/book/gui/elements/MutationElement.java @@ -7,6 +7,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.genetics.IMutation; import forestry.api.genetics.ISpeciesRoot; +import forestry.api.gui.GuiConstants; import forestry.api.gui.GuiElementAlignment; import forestry.book.gui.GuiForesterBook; import forestry.core.gui.Drawable; @@ -41,7 +42,7 @@ public class MutationElement extends SelectionElement { } else { conditionText = String.format("%.0f%%", mutation.getBaseChance()); } - selectedElement.text(58, conditionText, GuiElementAlignment.TOP_LEFT, 0).addTooltip(conditions); + selectedElement.label(conditionText, 58, 0, -1, 12, GuiElementAlignment.TOP_LEFT, GuiConstants.BLACK_STYLE).addTooltip(conditions); selectedElement.drawable(62, 6, MUTATION_ARROW).addTooltip(conditions); // selectedElement.item(1, 1, root.getMemberStack(mutation.getAllele0(), root.getTypeForMutation(0))); diff --git a/src/main/java/forestry/book/gui/elements/SelectionElement.java b/src/main/java/forestry/book/gui/elements/SelectionElement.java index 1fac92c0a..d32534d5c 100644 --- a/src/main/java/forestry/book/gui/elements/SelectionElement.java +++ b/src/main/java/forestry/book/gui/elements/SelectionElement.java @@ -2,11 +2,10 @@ package forestry.book.gui.elements; import javax.annotation.Nullable; -import net.minecraft.util.text.TextFormatting; - import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import forestry.api.gui.GuiConstants; import forestry.api.gui.GuiElementAlignment; import forestry.book.gui.GuiForesterBook; import forestry.core.gui.Drawable; @@ -41,7 +40,7 @@ public abstract class SelectionElement extends PaneLayout { this.recipes = recipes; if (recipes.length > 1) { drawable(0, 0, CRAFTING_COUNT).setAlign(GuiElementAlignment.BOTTOM_CENTER); - text = panel(width, this.height); + text = pane(width, this.height); leftButton = add(new ButtonElement(-27, -2, LEFT_BUTTON, e -> setIndex(index - 1))); leftButton.setAlign(GuiElementAlignment.BOTTOM_CENTER); @@ -52,7 +51,7 @@ public abstract class SelectionElement extends PaneLayout { leftButton = null; rightButton = null; } - selectedElement = GuiElementFactory.INSTANCE.createPanel(0, 2, width, this.height); + selectedElement = GuiElementFactory.INSTANCE.createPane(0, 2, width, this.height); } protected final void setIndex(int index) { @@ -64,7 +63,7 @@ public abstract class SelectionElement extends PaneLayout { onIndexUpdate(index, recipes[index]); if (text != null) { text.clear(); - text.text(TextFormatting.BLACK.toString() + (index + 1) + "/" + recipes.length, GuiElementAlignment.BOTTOM_CENTER, 0).setYPosition(2); + text.label((index + 1) + "/" + recipes.length, GuiElementAlignment.BOTTOM_CENTER, GuiConstants.BLACK_STYLE).setYPosition(2); } if (leftButton != null) { leftButton.setEnabled(index > 0); diff --git a/src/main/java/forestry/book/gui/elements/TextDataElement.java b/src/main/java/forestry/book/gui/elements/TextDataElement.java index 4e6bca08d..4a96a5d70 100644 --- a/src/main/java/forestry/book/gui/elements/TextDataElement.java +++ b/src/main/java/forestry/book/gui/elements/TextDataElement.java @@ -130,6 +130,6 @@ public class TextDataElement extends GuiElement { public void addData(TextData textData) { textElements.add(textData); - height = -1; + setHeight(-1); } } diff --git a/src/main/java/forestry/core/genetics/SpeciesDisplayHelper.java b/src/main/java/forestry/core/genetics/SpeciesDisplayHelper.java index 5984ab9a4..a35546a4e 100644 --- a/src/main/java/forestry/core/genetics/SpeciesDisplayHelper.java +++ b/src/main/java/forestry/core/genetics/SpeciesDisplayHelper.java @@ -3,15 +3,9 @@ package forestry.core.genetics; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; -import javax.annotation.Nullable; -import java.util.Optional; -import java.util.function.Function; - import net.minecraft.item.ItemStack; import forestry.api.genetics.IAlleleSpecies; -import forestry.api.genetics.IAlyzerPlugin; -import forestry.api.genetics.IDatabasePlugin; import forestry.api.genetics.IIndividual; import forestry.api.genetics.ISpeciesDisplayHelper; import forestry.api.genetics.ISpeciesRoot; @@ -19,37 +13,25 @@ import forestry.api.genetics.ISpeciesType; public class SpeciesDisplayHelper implements ISpeciesDisplayHelper { private final Table iconStacks = HashBasedTable.create(); - @Nullable - private final IAlyzerPlugin alyzerPlugin; - @Nullable - private final IDatabasePlugin databasePlugin; private final ISpeciesRoot root; - public SpeciesDisplayHelper(ISpeciesRoot root, @Nullable Function alyzerPlugin, @Nullable Function databasePlugin) { + public SpeciesDisplayHelper(ISpeciesRoot root) { this.root = root; + ISpeciesType type = root.getIconType(); for (IIndividual individual : root.getIndividualTemplates()) { - for (ISpeciesType type : root.getTypes()) { - ItemStack itemStack = root.getMemberStack(individual, type); - iconStacks.put(type, individual.getGenome().getPrimary().getUID(), itemStack); - } + ItemStack itemStack = root.getMemberStack(individual, type); + iconStacks.put(type, individual.getGenome().getPrimary().getUID(), itemStack); } - this.alyzerPlugin = alyzerPlugin == null ? null : alyzerPlugin.apply(this); - this.databasePlugin = databasePlugin == null ? null : databasePlugin.apply(this); - } - - @Override - public Optional getDatabasePlugin() { - return Optional.ofNullable(databasePlugin); - } - - @Override - public Optional getAlyzerPlugin() { - return Optional.ofNullable(alyzerPlugin); } @Override public ItemStack getDisplayStack(IAlleleSpecies species, ISpeciesType type) { - return iconStacks.get(species.getUID(), type); + ItemStack stack = iconStacks.get(type, species.getUID()); + if(stack == null){ + stack = root.getMemberStack(species, type); + iconStacks.put(type, species.getUID(), stack); + } + return stack; } @Override diff --git a/src/main/java/forestry/core/genetics/analyzer/AnalyzerTab.java b/src/main/java/forestry/core/genetics/analyzer/AnalyzerTab.java index 626bc6028..3350364c1 100644 --- a/src/main/java/forestry/core/genetics/analyzer/AnalyzerTab.java +++ b/src/main/java/forestry/core/genetics/analyzer/AnalyzerTab.java @@ -5,13 +5,13 @@ import net.minecraft.util.text.translation.I18n; import forestry.api.genetics.IDatabaseTab; import forestry.api.genetics.IIndividual; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; import forestry.core.ModuleCore; public enum AnalyzerTab implements IDatabaseTab { ANALYZE { @Override - public void createElements(IElementGenetic container, IIndividual individual, ItemStack itemStack) { + public void createElements(IDatabaseElement container, IIndividual individual, ItemStack itemStack) { } diff --git a/src/main/java/forestry/core/genetics/analyzer/MutationsTab.java b/src/main/java/forestry/core/genetics/analyzer/MutationsTab.java index c9fe91532..3c0ea97af 100644 --- a/src/main/java/forestry/core/genetics/analyzer/MutationsTab.java +++ b/src/main/java/forestry/core/genetics/analyzer/MutationsTab.java @@ -7,7 +7,6 @@ import java.util.function.Supplier; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; -import net.minecraft.util.text.TextFormatting; import forestry.api.genetics.IAlleleSpecies; import forestry.api.genetics.IBreedingTracker; @@ -15,8 +14,9 @@ import forestry.api.genetics.IGenome; import forestry.api.genetics.IIndividual; import forestry.api.genetics.IMutation; import forestry.api.genetics.ISpeciesRoot; +import forestry.api.gui.GuiConstants; import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; import forestry.api.gui.IElementLayoutHelper; import forestry.core.gui.elements.GuiElementFactory; import forestry.core.utils.Translator; @@ -27,7 +27,7 @@ public class MutationsTab extends DatabaseTab { } @Override - public void createElements(IElementGenetic container, IIndividual individual, ItemStack itemStack) { + public void createElements(IDatabaseElement container, IIndividual individual, ItemStack itemStack) { IGenome genome = individual.getGenome(); ISpeciesRoot speciesRoot = genome.getSpeciesRoot(); IAlleleSpecies species = genome.getPrimary(); @@ -38,7 +38,7 @@ public class MutationsTab extends DatabaseTab { IElementLayoutHelper groupHelper = container.layoutHelper((x, y) -> GuiElementFactory.INSTANCE.createHorizontal(x + 1, y, 16), 100, 0); Collection mutations = getValidMutations(speciesRoot.getCombinations(species)); if(!mutations.isEmpty()) { - container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.database.mutations.further"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.database.mutations.further"), GuiElementAlignment.TOP_CENTER, GuiConstants.UNDERLINED_STYLE); mutations.forEach(mutation -> groupHelper.add(GuiElementFactory.INSTANCE.createMutation(0, 0, 50, 16, mutation, species, breedingTracker))); groupHelper.finish(true); } @@ -46,7 +46,7 @@ public class MutationsTab extends DatabaseTab { if(mutations.isEmpty()){ return; } - container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.database.mutations.resultant"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.database.mutations.resultant"), GuiElementAlignment.TOP_CENTER, GuiConstants.UNDERLINED_STYLE); mutations.forEach(mutation -> groupHelper.add(GuiElementFactory.INSTANCE.createMutationResultant(0, 0, 50, 16, mutation, breedingTracker))); groupHelper.finish(true); } diff --git a/src/main/java/forestry/core/genetics/analyzer/ProductsTab.java b/src/main/java/forestry/core/genetics/analyzer/ProductsTab.java index 84c77e58c..552b77f6c 100644 --- a/src/main/java/forestry/core/genetics/analyzer/ProductsTab.java +++ b/src/main/java/forestry/core/genetics/analyzer/ProductsTab.java @@ -10,7 +10,7 @@ import forestry.api.apiculture.IBee; import forestry.api.arboriculture.ITree; import forestry.api.genetics.IIndividual; import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; import forestry.api.gui.IElementLayoutHelper; import forestry.core.gui.elements.GuiElementFactory; import forestry.core.gui.elements.ItemElement; @@ -22,11 +22,11 @@ public class ProductsTab extends DatabaseTab { } @Override - public void createElements(IElementGenetic container, IIndividual individual, ItemStack itemStack) { + public void createElements(IDatabaseElement container, IIndividual individual, ItemStack itemStack) { IElementLayoutHelper groupHelper = container.layoutHelper((x, y) -> GuiElementFactory.INSTANCE.createHorizontal(x + 4, y, 18).setDistance(2), 90, 0); Collection products = getProducts(individual); if(!products.isEmpty()) { - container.text(Translator.translateToLocal("for.gui.beealyzer.produce"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.beealyzer.produce"), GuiElementAlignment.TOP_CENTER); products.forEach(product -> groupHelper.add(new ItemElement(0, 0, product))); groupHelper.finish(); } @@ -36,7 +36,7 @@ public class ProductsTab extends DatabaseTab { return; } - container.text(Translator.translateToLocal("for.gui.beealyzer.specialty"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.beealyzer.specialty"), GuiElementAlignment.TOP_CENTER); specialties.forEach(specialty -> groupHelper.add(new ItemElement(0, 0, specialty))); groupHelper.finish(); } diff --git a/src/main/java/forestry/core/gui/ContainerAnalyzerProvider.java b/src/main/java/forestry/core/gui/ContainerAnalyzerProvider.java index 2861eec42..cdce2160a 100644 --- a/src/main/java/forestry/core/gui/ContainerAnalyzerProvider.java +++ b/src/main/java/forestry/core/gui/ContainerAnalyzerProvider.java @@ -112,6 +112,7 @@ public class ContainerAnalyzerProvider extends ContainerTi NBTTagCompound nbttagcompound = new NBTTagCompound(); individual.writeToNBT(nbttagcompound); + specimen = specimen.copy(); specimen.setTagCompound(nbttagcompound); if (requiresEnergy) { diff --git a/src/main/java/forestry/core/gui/GuiAnalyzerProvider.java b/src/main/java/forestry/core/gui/GuiAnalyzerProvider.java index ce6defb5c..f629d406d 100644 --- a/src/main/java/forestry/core/gui/GuiAnalyzerProvider.java +++ b/src/main/java/forestry/core/gui/GuiAnalyzerProvider.java @@ -61,9 +61,8 @@ public abstract class GuiAnalyzerProvider extends GuiForest this.slots = slots; this.firstSlot = firstSlot; - this.analyzer = GuiElementFactory.INSTANCE.createAnalyzer(-189 - screenDistance, 0, hasBoarder, this); + this.analyzer = GuiElementFactory.INSTANCE.createAnalyzer(window, -189 - screenDistance, 0, hasBoarder, this); updateVisibility(); - this.elementManager.add(analyzer); SlotAnalyzer slotAnalyzer = null; if (container instanceof ContainerAnalyzerProvider) { @@ -96,7 +95,7 @@ public abstract class GuiAnalyzerProvider extends GuiForest if (analyzer.isVisible()) { this.guiLeft = (this.width - this.xSize + analyzer.getWidth() + (screenDistance)) / 2; } - elementManager.init(guiLeft, guiTop + (ySize - 166) / 2); + window.init(guiLeft, guiTop + (ySize - 166) / 2); /*analyzer.setXOffset(guiLeft); analyzer.setYOffset(guiTop + (ySize - 166) / 2);*/ diff --git a/src/main/java/forestry/core/gui/GuiForestry.java b/src/main/java/forestry/core/gui/GuiForestry.java index c6380b0c6..242a04c2f 100755 --- a/src/main/java/forestry/core/gui/GuiForestry.java +++ b/src/main/java/forestry/core/gui/GuiForestry.java @@ -31,8 +31,11 @@ import net.minecraftforge.fluids.IFluidTank; import forestry.api.core.IErrorLogicSource; import forestry.api.core.IErrorSource; +import forestry.api.gui.IGuiElement; +import forestry.api.gui.events.GuiEvent; +import forestry.api.gui.events.GuiEventDestination; import forestry.core.config.Config; -import forestry.core.gui.elements.ElementManager; +import forestry.core.gui.elements.Window; import forestry.core.gui.ledgers.ClimateLedger; import forestry.core.gui.ledgers.HintLedger; import forestry.core.gui.ledgers.LedgerManager; @@ -53,8 +56,8 @@ public abstract class GuiForestry extends GuiContainer impl public final ResourceLocation textureFile; protected final WidgetManager widgetManager; protected final LedgerManager ledgerManager; - protected final ElementManager elementManager; protected final TextLayoutHelper textLayout; + protected final Window window; protected GuiForestry(String texture, C container) { this(new ForestryResource(texture), container); @@ -65,7 +68,7 @@ public abstract class GuiForestry extends GuiContainer impl this.widgetManager = new WidgetManager(this); this.ledgerManager = new LedgerManager(this); - this.elementManager = new ElementManager(this); + this.window = new Window<>(xSize, ySize, this); this.textureFile = texture; @@ -84,12 +87,24 @@ public abstract class GuiForestry extends GuiContainer impl this.ledgerManager.setMaxWidth(maxLedgerWidth); this.ledgerManager.clear(); - this.elementManager.init(guiLeft, guiTop); + this.window.init(guiLeft, guiTop); addLedgers(); } - + + @Override + public void setWorldAndResolution(Minecraft mc, int width, int height) { + window.setSize(width, height); + super.setWorldAndResolution(mc, width, height); + } + + @Override + public void updateScreen() { + window.updateClient(); + } + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + window.setMousePosition(mouseX, mouseY); this.drawDefaultBackground(); super.drawScreen(mouseX, mouseY, partialTicks); this.renderHoveredToolTip(mouseX, mouseY); @@ -155,7 +170,8 @@ public abstract class GuiForestry extends GuiContainer impl // / Handle ledger clicks ledgerManager.handleMouseClicked(mouseX, mouseY, mouseButton); widgetManager.handleMouseClicked(mouseX, mouseY, mouseButton); - elementManager.mouseClicked(mouseX, mouseY, mouseButton); + IGuiElement origin = (window.getMousedOverElement() == null) ? this.window : this.window.getMousedOverElement(); + window.postEvent(new GuiEvent.DownEvent(origin, mouseX, mouseY, mouseButton), GuiEventDestination.ALL); } @Override @@ -163,24 +179,22 @@ public abstract class GuiForestry extends GuiContainer impl if (widgetManager.handleMouseRelease(mouseX, mouseY, mouseButton)) { return; } - elementManager.mouseReleased(mouseX, mouseY, mouseButton); + IGuiElement origin = (window.getMousedOverElement() == null) ? this.window : this.window.getMousedOverElement(); + window.postEvent(new GuiEvent.UpEvent(origin, mouseX, mouseY, mouseButton), GuiEventDestination.ALL); super.mouseReleased(mouseX, mouseY, mouseButton); } - @Override - protected void mouseClickMove(int mouseX, int mouseY, int mouseButton, long time) { - - widgetManager.handleMouseMove(mouseX, mouseY, mouseButton, time); - - super.mouseClickMove(mouseX, mouseY, mouseButton, time); - } - @Override protected void keyTyped(char typedChar, int keyCode) throws IOException { - if(elementManager.keyTyped(typedChar, keyCode)){ - return; + if (keyCode == 1 || (keyCode == this.mc.gameSettings.keyBindInventory.getKeyCode() && this.window.getFocusedElement() == null)) { + this.mc.displayGuiScreen(null); + + if (this.mc.currentScreen == null) { + this.mc.setIngameFocus(); + } } - super.keyTyped(typedChar, keyCode); + IGuiElement origin = (window.getFocusedElement() == null) ? this.window : this.window.getFocusedElement(); + window.postEvent(new GuiEvent.KeyEvent(origin, typedChar, keyCode), GuiEventDestination.ALL); } @Nullable @@ -224,7 +238,7 @@ public abstract class GuiForestry extends GuiContainer impl GuiUtil.drawToolTips(this, widgetManager.getWidgets(), mouseX, mouseY); GuiUtil.drawToolTips(this, buttonList, mouseX, mouseY); GuiUtil.drawToolTips(this, inventorySlots.inventorySlots, mouseX, mouseY); - elementManager.drawTooltip(mouseX, mouseY); + window.drawTooltip(mouseX, mouseY); } } @@ -246,7 +260,7 @@ public abstract class GuiForestry extends GuiContainer impl GlStateManager.popMatrix(); GlStateManager.color(1.0F, 1.0F, 1.0F); - elementManager.draw(mouseX, mouseY); + window.draw(mouseX, mouseY); bindTexture(textureFile); } diff --git a/src/main/java/forestry/core/gui/GuiWindow.java b/src/main/java/forestry/core/gui/GuiWindow.java new file mode 100644 index 000000000..9348ca7b4 --- /dev/null +++ b/src/main/java/forestry/core/gui/GuiWindow.java @@ -0,0 +1,141 @@ +package forestry.core.gui; + +import java.io.IOException; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.player.InventoryPlayer; + +import forestry.api.gui.IGuiElement; +import forestry.api.gui.events.GuiEvent; +import forestry.api.gui.events.GuiEventDestination; +import forestry.core.gui.elements.Window; + +import org.lwjgl.input.Mouse; + +/** + * GuiScreen implementation of a gui that contains {@link forestry.api.gui.IGuiElement}s. + */ +public class GuiWindow extends GuiScreen implements IGuiSizable { + protected final Window window; + protected final int xSize; + protected final int ySize; + protected int guiLeft; + protected int guiTop; + + public GuiWindow(int xSize, int ySize) { + this.xSize = xSize; + this.ySize = ySize; + this.window = new Window<>(xSize, ySize, this); + addElements(); + } + + protected void addElements(){ + } + + @Override + public boolean doesGuiPauseGame() { + return false; + } + + @Override + public void updateScreen() { + window.updateClient(); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + window.setMousePosition(mouseX, mouseY); + super.drawScreen(mouseX, mouseY, partialTicks); + window.draw(mouseX, mouseY); + } + + protected void drawTooltips(int mouseX, int mouseY){ + InventoryPlayer playerInv = mc.player.inventory; + + if (playerInv.getItemStack().isEmpty()) { + GuiUtil.drawToolTips(this, buttonList, mouseX, mouseY); + GlStateManager.pushMatrix(); + GlStateManager.translate(guiLeft, guiTop, 0.0F); + window.drawTooltip(mouseX, mouseY); + GlStateManager.popMatrix(); + } + } + + @Override + public void initGui() { + super.initGui(); + this.guiLeft = (this.width - xSize) / 2; + this.guiTop = (this.height - ySize) / 2; + window.init(guiLeft, guiTop); + } + + @Override + public void setWorldAndResolution(Minecraft mc, int width, int height) { + window.setSize(width, height); + super.setWorldAndResolution(mc, width, height); + } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException { + if (keyCode == 1) { + this.mc.displayGuiScreen(null); + + if (this.mc.currentScreen == null) { + this.mc.setIngameFocus(); + } + } + IGuiElement origin = (window.getFocusedElement() == null) ? this.window : this.window.getFocusedElement(); + window.postEvent(new GuiEvent.KeyEvent(origin, typedChar, keyCode), GuiEventDestination.ALL); + } + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { + super.mouseClicked(mouseX, mouseY, mouseButton); + IGuiElement origin = (window.getMousedOverElement() == null) ? this.window : this.window.getMousedOverElement(); + window.postEvent(new GuiEvent.DownEvent(origin, mouseX, mouseY, mouseButton), GuiEventDestination.ALL); + } + + @Override + protected void mouseReleased(int mouseX, int mouseY, int state) { + super.mouseReleased(mouseX, mouseY, state); + IGuiElement origin = (window.getMousedOverElement() == null) ? this.window : this.window.getMousedOverElement(); + window.postEvent(new GuiEvent.UpEvent(origin, mouseX, mouseY, state), GuiEventDestination.ALL); + } + + @Override + public void handleMouseInput() throws IOException { + super.handleMouseInput(); + int dWheel = Mouse.getDWheel(); + if (dWheel != 0) { + window.postEvent(new GuiEvent.WheelEvent(window, dWheel), GuiEventDestination.ALL); + } + } + + @Override + public int getGuiLeft() { + return guiLeft; + } + + @Override + public int getGuiTop() { + return guiTop; + } + + @Override + public int getSizeX() { + return xSize; + } + + @Override + public int getSizeY() { + return ySize; + } + + @Override + public Minecraft getMC() { + return mc; + } + +} diff --git a/src/main/java/forestry/core/gui/elements/AbstractItemElement.java b/src/main/java/forestry/core/gui/elements/AbstractItemElement.java index 5ef8cb8e1..9b54c534c 100644 --- a/src/main/java/forestry/core/gui/elements/AbstractItemElement.java +++ b/src/main/java/forestry/core/gui/elements/AbstractItemElement.java @@ -35,7 +35,7 @@ public abstract class AbstractItemElement extends GuiElement { @Override public void drawElement(int mouseX, int mouseY) { - ItemStack itemStack = getItemStack(); + ItemStack itemStack = getStack(); if (!itemStack.isEmpty()) { RenderHelper.enableGUIStandardItemLighting(); GlStateManager.enableRescaleNormal(); @@ -47,9 +47,14 @@ public abstract class AbstractItemElement extends GuiElement { @SideOnly(Side.CLIENT) @Override public List getTooltip(int mouseX, int mouseY) { - ItemStack itemStack = getItemStack(); + ItemStack itemStack = getStack(); return ItemTooltipUtil.getInformation(itemStack); } - protected abstract ItemStack getItemStack(); + @Override + public boolean hasTooltip() { + return true; + } + + protected abstract ItemStack getStack(); } diff --git a/src/main/java/forestry/core/gui/elements/ButtonElement.java b/src/main/java/forestry/core/gui/elements/ButtonElement.java index 6a9b68fe6..57b2818f2 100644 --- a/src/main/java/forestry/core/gui/elements/ButtonElement.java +++ b/src/main/java/forestry/core/gui/elements/ButtonElement.java @@ -2,6 +2,7 @@ package forestry.core.gui.elements; import java.util.function.Consumer; +import forestry.api.gui.events.GuiEvent; import forestry.core.gui.Drawable; import forestry.core.gui.buttons.StandardButtonTextureSets; import forestry.core.utils.SoundUtil; @@ -20,6 +21,13 @@ public class ButtonElement extends GuiElement { for (int i = 0; i < 3; i++) { textures[i] = new Drawable(drawable.textureLocation, drawable.u, drawable.v + drawable.vHeight * i, drawable.uWidth, drawable.vHeight); } + addSelfEventHandler(GuiEvent.DownEvent.class, event ->{ + if (!enabled) { + return; + } + onPressed(); + SoundUtil.playButtonClick(); + }); } public ButtonElement(int xPos, int yPos, StandardButtonTextureSets textureSets, Consumer onClicked) { @@ -28,16 +36,28 @@ public class ButtonElement extends GuiElement { for (int i = 0; i < 3; i++) { textures[i] = new Drawable(textureSets.getTexture(), textureSets.getX(), textureSets.getY() + textureSets.getHeight() * i, textureSets.getWidth(), textureSets.getHeight()); } + addSelfEventHandler(GuiEvent.DownEvent.class, event ->{ + if (!enabled) { + return; + } + onPressed(); + SoundUtil.playButtonClick(); + }); } @Override public void drawElement(int mouseX, int mouseY) { - boolean mouseOver = isMouseOver(mouseX, mouseY); + boolean mouseOver = isMouseOver(); int hoverState = getHoverState(mouseOver); Drawable drawable = textures[hoverState]; drawable.draw(0, 0); } + @Override + public boolean canMouseOver() { + return true; + } + public boolean isEnabled() { return enabled; } @@ -58,16 +78,6 @@ public class ButtonElement extends GuiElement { return i; } - - @Override - public void mouseClicked(int mouseX, int mouseY, int mouseEvent) { - if (!enabled) { - return; - } - onPressed(); - SoundUtil.playButtonClick(); - } - public void onPressed() { onClicked.accept(this); } diff --git a/src/main/java/forestry/core/gui/elements/DatabaseElement.java b/src/main/java/forestry/core/gui/elements/DatabaseElement.java new file mode 100644 index 000000000..6bb817874 --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/DatabaseElement.java @@ -0,0 +1,248 @@ +package forestry.core.gui.elements; + +import com.google.common.base.Preconditions; + +import javax.annotation.Nullable; +import java.util.function.BiFunction; +import java.util.function.Function; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; + +import forestry.api.genetics.DatabaseMode; +import forestry.api.genetics.IAllele; +import forestry.api.genetics.IAlleleInteger; +import forestry.api.genetics.IAlleleTolerance; +import forestry.api.genetics.IBreedingTracker; +import forestry.api.genetics.IChromosomeType; +import forestry.api.genetics.IGenome; +import forestry.api.genetics.IIndividual; +import forestry.api.genetics.IMutation; +import forestry.api.gui.GuiElementAlignment; +import forestry.api.gui.IDatabaseElement; +import forestry.api.gui.IElementGroup; +import forestry.api.gui.IElementLayout; +import forestry.api.gui.IGuiElement; +import forestry.api.gui.style.ITextStyle; +import forestry.core.gui.elements.layouts.PaneLayout; +import forestry.core.gui.elements.layouts.VerticalLayout; +import forestry.core.utils.Translator; + +public class DatabaseElement extends VerticalLayout implements IDatabaseElement { + private DatabaseMode mode = DatabaseMode.ACTIVE; + @Nullable + private IIndividual individual; + private int secondColumn = 0; + private int thirdColumn = 0; + + public DatabaseElement(int width) { + super(0, 0, width); + this.secondColumn = width / 2; + } + + @Override + public void init(DatabaseMode mode, IIndividual individual, int secondColumn, int thirdColumn) { + this.mode = mode; + this.individual = individual; + this.secondColumn = secondColumn; + this.thirdColumn = thirdColumn; + } + + @Nullable + public IIndividual getIndividual() { + return individual; + } + + public IGenome getGenome(){ + Preconditions.checkNotNull(individual, "Database Element has not been initialised."); + return individual.getGenome(); + } + + @Override + public void addFertilityLine(String chromosomeName, IChromosomeType chromosome, int texOffset) { + IGenome genome = getGenome(); + IAllele activeAllele = genome.getActiveAllele(chromosome); + IAllele inactiveAllele = genome.getInactiveAllele(chromosome); + if(mode == DatabaseMode.BOTH){ + if(!(activeAllele instanceof IAlleleInteger) || !(inactiveAllele instanceof IAlleleInteger)){ + return; + } + addLine(chromosomeName, GuiElementFactory.INSTANCE.createFertilityInfo((IAlleleInteger) activeAllele, texOffset), GuiElementFactory.INSTANCE.createFertilityInfo((IAlleleInteger) inactiveAllele, texOffset)); + }else{ + boolean active = mode == DatabaseMode.ACTIVE; + IAllele allele = active ? activeAllele : inactiveAllele; + if(!(allele instanceof IAlleleInteger)){ + return; + } + addLine(chromosomeName, GuiElementFactory.INSTANCE.createFertilityInfo((IAlleleInteger) allele, texOffset)); + } + } + + @Override + public void addToleranceLine(IChromosomeType chromosome) { + IAllele allele = getGenome().getActiveAllele(chromosome); + if(!(allele instanceof IAlleleTolerance)){ + return; + } + addLine(" " + Translator.translateToLocal("for.gui.tolerance"), GuiElementFactory.INSTANCE.createToleranceInfo((IAlleleTolerance) allele)); + } + + @Override + public void addMutation(int x, int y, int width, int height, IMutation mutation, IAllele species, IBreedingTracker breedingTracker) { + IGuiElement element = GuiElementFactory.INSTANCE.createMutation(x, y, width, height, mutation, species, breedingTracker); + if (element == null) { + return; + } + add(element); + } + + @Override + public void addMutationResultant(int x, int y, int width, int height, IMutation mutation, IBreedingTracker breedingTracker) { + IGuiElement element = GuiElementFactory.INSTANCE.createMutationResultant(x, y, width, height, mutation, breedingTracker); + if (element == null) { + return; + } + add(element); + } + + @Override + public void addLine(String firstText, String secondText, boolean dominant) { + addLine(firstText, secondText, GuiElementFactory.GUI_STYLE, GuiElementFactory.INSTANCE.getStateStyle(dominant)); + } + + @Override + public void addLine(String leftText, Function toText, boolean dominant) { + if(mode == DatabaseMode.BOTH){ + addLine(leftText, toText.apply(true), toText.apply(false), dominant, dominant); + }else { + addLine(leftText, toText.apply(mode == DatabaseMode.ACTIVE), dominant); + } + } + + /*@Override + public void addRow(String firstText, String secondText, String thirdText, IIndividual individual, IChromosomeType chromosome) { + addRow(firstText, secondText, thirdText, GuiElementFactory.GUI_STYLE, + GuiElementFactory.INSTANCE.getStateStyle(individual.getGenome().getActiveAllele(chromosome).isDominant()), + GuiElementFactory.INSTANCE.getStateStyle(individual.getGenome().getInactiveAllele(chromosome).isDominant())); + }*/ + + @Override + public void addLine(String leftText, Function toText, IChromosomeType chromosome) { + IGenome genome = getGenome(); + IAllele activeAllele = genome.getActiveAllele(chromosome); + IAllele inactiveAllele = genome.getInactiveAllele(chromosome); + if(mode == DatabaseMode.BOTH){ + addLine(leftText, toText.apply(true), toText.apply(false), activeAllele.isDominant(), inactiveAllele.isDominant()); + }else{ + boolean active = mode == DatabaseMode.ACTIVE; + IAllele allele = active ? activeAllele : inactiveAllele; + addLine(leftText, toText.apply(active), allele.isDominant()); + } + } + + public void addLine(String firstText, String secondText, String thirdText, boolean secondDominant, boolean thirdDominant) { + + } + + @Override + public final void addLine(String chromosomeName, IChromosomeType chromosome) { + addLine(chromosomeName, (allele, b) -> allele.getAlleleName(), chromosome); + } + + @Override + public void addLine(String firstText, String secondText, ITextStyle firstStyle, ITextStyle secondStyle) { + IElementLayout first = addSplitText(width, firstText, GuiElementAlignment.TOP_LEFT, firstStyle); + IElementLayout second = addSplitText(width, secondText, GuiElementAlignment.TOP_LEFT, secondStyle); + addLine(first, second); + } + + private IElementLayout addSplitText(int width, String text, GuiElementAlignment alignment, ITextStyle style) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; + IElementLayout vertical = new VerticalLayout(width); + for (String splitText : fontRenderer.listFormattedStringToWidth(text, 70)) { + vertical.label(splitText, alignment, style); + } + return vertical; + } + + private void addLine(String chromosomeName, IGuiElement right) { + int center = width / 2; + IGuiElement first = addSplitText(center, chromosomeName, GuiElementAlignment.TOP_LEFT, GuiElementFactory.GUI_STYLE); + addLine(first, right); + } + + private void addLine(String chromosomeName, IGuiElement second, IGuiElement third) { + int center = width / 2; + IGuiElement first = addSplitText(center, chromosomeName, GuiElementAlignment.TOP_LEFT, GuiElementFactory.GUI_STYLE); + addLine(first, second, third); + } + + private void addLine(IGuiElement first, IGuiElement second, IGuiElement third) { + IElementGroup panel = new PaneLayout(width, 0); + first.setAlign(GuiElementAlignment.MIDDLE_LEFT); + second.setAlign(GuiElementAlignment.MIDDLE_LEFT); + third.setAlign(GuiElementAlignment.MIDDLE_LEFT); + panel.add(first); + panel.add(second); + panel.add(third); + second.setXPosition(secondColumn); + third.setXPosition(thirdColumn); + add(panel); + } + + private void addLine(IGuiElement first, IGuiElement second) { + IElementGroup panel = new PaneLayout(width, 0); + first.setAlign(GuiElementAlignment.MIDDLE_LEFT); + second.setAlign(GuiElementAlignment.MIDDLE_LEFT); + panel.add(first); + panel.add(second); + second.setXPosition(secondColumn); + add(panel); + } + + @Override + public void addLine(String chromosomeName, BiFunction toText, IChromosomeType chromosome) { + addAlleleRow(chromosomeName, toText, chromosome, null); + } + + @Override + public void addLine(String chromosomeName, BiFunction toText, IChromosomeType chromosome, boolean dominant) { + addAlleleRow(chromosomeName, toText, chromosome, dominant); + } + + @SuppressWarnings("unchecked") + private void addAlleleRow(String chromosomeName, BiFunction toString, IChromosomeType chromosome, @Nullable Boolean dominant) { + IGenome genome = getGenome(); + A activeAllele = (A) genome.getActiveAllele(chromosome); + A inactiveAllele = (A) genome.getInactiveAllele(chromosome); + if(mode == DatabaseMode.BOTH){ + addLine(chromosomeName, toString.apply(activeAllele, true), toString.apply(inactiveAllele, false), dominant != null ? dominant : activeAllele.isDominant(), dominant != null ? dominant : inactiveAllele.isDominant()); + }else{ + boolean active = mode == DatabaseMode.ACTIVE; + A allele = active ? activeAllele : inactiveAllele; + addLine(chromosomeName, toString.apply(allele, active), dominant != null ? dominant : allele.isDominant()); + } + } + + @Override + public void addSpeciesLine(String firstText, @Nullable String secondText, IChromosomeType chromosome) { + /*IAlleleSpecies primary = individual.getGenome().getPrimary(); + IAlleleSpecies secondary = individual.getGenome().getSecondary(); + + textLayout.drawLine(text0, textLayout.column0); + int columnwidth = textLayout.column2 - textLayout.column1 - 2; + + Map iconStacks = chromosome.getSpeciesRoot().getAlyzerPlugin().getIconStacks(); + + GuiUtil.drawItemStack(this, iconStacks.get(primary.getUID()), guiLeft + textLayout.column1 + columnwidth - 20, guiTop + 10); + GuiUtil.drawItemStack(this, iconStacks.get(secondary.getUID()), guiLeft + textLayout.column2 + columnwidth - 20, guiTop + 10); + + String primaryName = customPrimaryName == null ? primary.getAlleleName() : customPrimaryName; + String secondaryName = customSecondaryName == null ? secondary.getAlleleName() : customSecondaryName; + + drawSplitLine(primaryName, textLayout.column1, columnwidth, individual, chromosome, false); + drawSplitLine(secondaryName, textLayout.column2, columnwidth, individual, chromosome, true); + + textLayout.newLine();*/ + } +} diff --git a/src/main/java/forestry/core/gui/elements/ElementList.java b/src/main/java/forestry/core/gui/elements/ElementList.java new file mode 100644 index 000000000..8155c6641 --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/ElementList.java @@ -0,0 +1,125 @@ +package forestry.core.gui.elements; + +import javax.annotation.Nullable; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Predicate; + +import forestry.api.gui.IGuiElement; +import forestry.api.gui.IWindowElement; +import forestry.api.gui.events.GuiEventDestination; +import forestry.core.gui.elements.layouts.VerticalLayout; +import forestry.core.gui.event.EventValueChanged; + +/** + * A element list with selectable elements. + */ +public class ElementList extends VerticalLayout { + private final Map allOptions = new LinkedHashMap<>(); + private final Map visibleOptions = new LinkedHashMap<>(); + private final BiFunction optionFactory; + @Nullable + private final V defaultValue; + @Nullable + private V value; + @Nullable + private Predicate validator; + + public ElementList(int xPos, int yPos, int width, BiFunction optionFactory, @Nullable V defaultValue) { + super(xPos, yPos, width); + this.optionFactory = optionFactory; + this.defaultValue = defaultValue; + this.value = defaultValue; + } + + @Nullable + public V getCurrentValue() { + return this.value; + } + + public void setCurrentValue(@Nullable final V value) { + if (value == this.value) { + return; + } + this.value = value; + /*if (this.optionWidgets.containsKey(value)) { + final IWidget child = this.optionWidgets.get(value); + this.parent.ensureVisible(child.getYPos(), child.getYPos() + child.getHeight(), this.getHeight()); + }*/ + IWindowElement window = getWindow(); + window.postEvent(new EventValueChanged(this, value), GuiEventDestination.ALL); + } + + public void updateVisibleOptions(){ + elements.clear(); + setHeight(0); + visibleOptions.clear(); + + for (Map.Entry entry : this.allOptions.entrySet()) { + if (isVisible(entry.getKey())) { + add(entry.getValue()); + visibleOptions.put(entry.getKey(), entry.getValue()); + }else{ + entry.getValue().setYPosition(0); + } + } + setCurrentValue(this.getCurrentValue()); + } + + public boolean isVisible(V value){ + return validator == null || validator.test(value); + } + + public void setValidator(Predicate validator) { + if (this.validator != validator) { + this.validator = validator; + updateVisibleOptions(); + } + } + + public int getIndexOf(@Nullable V value) { + if(value == null){ + return -1; + } + int index = 0; + for (V option : this.getOptions()) { + if (option.equals(value)) { + return index; + } + ++index; + } + return -1; + } + + public int getCurrentIndex() { + return this.getIndexOf(getCurrentValue()); + } + + public void setIndex(int currentIndex) { + int index = 0; + for (V option : this.getOptions()) { + if (index == currentIndex) { + this.setCurrentValue(option); + return; + } + ++index; + } + this.setCurrentValue(defaultValue); + } + + public Collection getOptions() { + return visibleOptions.keySet(); + } + + public void setOptions(Collection options){ + clear(); + allOptions.clear(); + for(V option : options){ + IGuiElement element = optionFactory.apply(option, this); + allOptions.put(option, element); + } + updateVisibleOptions(); + } +} diff --git a/src/main/java/forestry/core/gui/elements/ElementManager.java b/src/main/java/forestry/core/gui/elements/ElementManager.java index 37e66c7ce..5068ab1db 100644 --- a/src/main/java/forestry/core/gui/elements/ElementManager.java +++ b/src/main/java/forestry/core/gui/elements/ElementManager.java @@ -3,10 +3,6 @@ package forestry.core.gui.elements; import java.util.List; import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.renderer.GlStateManager; - -import net.minecraftforge.fml.client.config.GuiUtils; import forestry.api.gui.IGuiElement; import forestry.core.gui.IGuiSizable; @@ -15,13 +11,10 @@ import forestry.core.gui.elements.layouts.PaneLayout; public class ElementManager { /* Attributes - Final */ - private final GuiState state; private final ElementGroup container; public ElementManager(G gui) { - this.state = new GuiState<>(gui); this.container = new PaneLayout(0, 0); - this.container.setGuiState(state); } public ElementGroup group(){ @@ -41,8 +34,6 @@ public class ElementManager { } public void draw(int mouseX, int mouseY) { - state.setMouseX(mouseX); - state.setMouseY(mouseY); container.draw(mouseX, mouseY); } @@ -51,29 +42,19 @@ public class ElementManager { } public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - container.mouseClicked(mouseX, mouseY, mouseButton); } public void mouseClickMove(int mouseX, int mouseY, int mouseButton) { - container.mouseClickMove(mouseX, mouseY, mouseButton); } public void mouseReleased(int mouseX, int mouseY, int mouseButton) { - container.mouseReleased(mouseX, mouseY, mouseButton); } public boolean keyTyped(char typedChar, int keyCode) { - return container.keyTyped(typedChar, keyCode); + return false; } public void drawTooltip(int mouseX, int mouseY) { - List lines = getTooltip(mouseX, mouseY); - if (!lines.isEmpty()) { - GlStateManager.pushMatrix(); - ScaledResolution scaledresolution = new ScaledResolution(state.getMinecraft()); - GuiUtils.drawHoveringText(lines, mouseX, mouseY, scaledresolution.getScaledWidth(), scaledresolution.getScaledHeight(), -1, state.getFontRenderer()); - GlStateManager.popMatrix(); - } } public boolean isMouseOver(int mouseX, int mouseY) { diff --git a/src/main/java/forestry/core/gui/elements/GeneticAnalyzer.java b/src/main/java/forestry/core/gui/elements/GeneticAnalyzer.java index 73587f98e..79912f405 100644 --- a/src/main/java/forestry/core/gui/elements/GeneticAnalyzer.java +++ b/src/main/java/forestry/core/gui/elements/GeneticAnalyzer.java @@ -16,11 +16,12 @@ import forestry.api.genetics.IGeneticAnalyzerProvider; import forestry.api.genetics.IIndividual; import forestry.api.genetics.ISpeciesRoot; import forestry.api.gui.IGuiElement; +import forestry.api.gui.IWindowElement; +import forestry.api.gui.events.GuiEvent; import forestry.core.config.Constants; import forestry.core.gui.Drawable; import forestry.core.gui.buttons.StandardButtonTextureSets; import forestry.core.gui.elements.layouts.ElementGroup; -import forestry.core.gui.elements.layouts.ScrollableElement; import forestry.core.gui.widgets.IScrollable; import forestry.core.network.packets.PacketGuiSelectRequest; import forestry.core.utils.NetworkUtil; @@ -44,6 +45,7 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I /*Attributes - Gui Elements */ private final ScrollBarElement scrollBar; private final ScrollableElement scrollable; + private final DatabaseElement scrollableContent; private final GeneticAnalyzerTabs tabs; private final IGuiElement itemElement; private final ButtonElement leftButton; @@ -51,11 +53,11 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I private final ButtonElement analyzeButton; /* Attributes - State */ - private boolean visible = true; private int selectedSlot = -1; - public GeneticAnalyzer(int xPos, int yPos, boolean rightBoarder, IGeneticAnalyzerProvider provider) { + public GeneticAnalyzer(IWindowElement window, int xPos, int yPos, boolean rightBoarder, IGeneticAnalyzerProvider provider) { super(xPos - (rightBoarder ? 6 : 0), yPos, 189 + (rightBoarder ? 6 : 0), 194); + window.add(this); this.provider = provider; //Background Texture @@ -63,6 +65,9 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I //Text Area scrollable = new ScrollableElement(32 + 10, 8, 145, 150); add(scrollable); + scrollableContent = new DatabaseElement(145); + scrollable.setContent(scrollableContent); + scrollable.add(scrollableContent); //Scrollbar scrollBar = new ScrollBarElement(width - 10 - (rightBoarder ? 6 : 0), 12, SCROLLBAR_BACKGROUND, false, SCROLLBAR_SLIDER); scrollBar.setVisible(false); @@ -78,10 +83,20 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I analyzeButton = add(new ButtonElement(itemElement.getX() + 80, itemElement.getY() + 2, ANALYZER_BUTTON, (button) -> NetworkUtil.sendToServer(new PacketGuiSelectRequest(0, provider.getSelectedSlot(selectedSlot))))); add(new AbstractItemElement(itemElement.getX() + 44, itemElement.getY() + 9) { @Override - protected ItemStack getItemStack() { + protected ItemStack getStack() { return provider.getSpecimen(selectedSlot); } }); + addEventHandler(GuiEvent.KeyEvent.class, event -> { + int keyCode = event.getKey(); + if ((keyCode == Keyboard.KEY_DOWN || keyCode == Keyboard.KEY_RIGHT) && rightButton.isEnabled()) { + rightButton.onPressed(); + } else if ((keyCode == Keyboard.KEY_UP || keyCode == Keyboard.KEY_LEFT) && leftButton.isEnabled()) { + leftButton.onPressed(); + } else if(keyCode == Keyboard.KEY_RETURN && analyzeButton.isEnabled()){ + analyzeButton.onPressed(); + } + }); } @Override @@ -91,10 +106,6 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I updateSelected(); } - public boolean isVisible() { - return visible; - } - public void setVisible(boolean visible) { this.visible = visible; } @@ -111,7 +122,7 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I @SuppressWarnings("unchecked") @Override public void update() { - if (!visible) { + if (!isVisible()) { return; } ItemStack stack = provider.getSpecimen(selectedSlot); @@ -125,15 +136,14 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I tabs.setPlugin(databasePlugin); IDatabaseTab tab = tabs.getSelected(); //Clean the element area - scrollable.clear(); + scrollableContent.clear(); //Create the new elements - tab.createElements(scrollable, individual, stack); - //Update the visible elements - scrollable.updateVisibleElements(0); + scrollableContent.init(tab.getMode(), individual, scrollableContent.getWidth() / 2, 0); + tab.createElements(scrollableContent, individual, stack); //Update the scrollbar - int invisibleElements = scrollable.getInvisibleElementCount(); - if (invisibleElements > 0) { - scrollBar.setParameters(this, 0, invisibleElements, 1); + int invisibleArea = scrollable.getInvisibleArea(); + if (invisibleArea > 0) { + scrollBar.setParameters(this, 0, invisibleArea, 1); scrollBar.setVisible(true); } else { scrollBar.setValue(0); @@ -146,7 +156,7 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I } } //Clean the element area - scrollable.clear(); + scrollableContent.clear(); FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; String key = "for.gui.portablealyzer.help"; //if(state == DatabaseScreenLogic.ScreenState.NO_PLUGIN){ @@ -154,20 +164,12 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I //} List lines = fontRenderer.listFormattedStringToWidth(Translator.translateToLocal(key), scrollable.getWidth()); for (String text : lines) { - scrollable.text(text); + scrollableContent.label(text); } //Disable the scrollbar scrollBar.setVisible(false); } - @Override - public void draw(int mouseX, int mouseY) { - if (!visible) { - return; - } - super.draw(mouseX, mouseY); - } - @Override public void drawTooltip(GuiScreen gui, int mouseX, int mouseY) { if (!visible) { @@ -190,23 +192,9 @@ public class GeneticAnalyzer extends ElementGroup implements IGeneticAnalyzer, I return super.mouseClicked(mouseX, mouseY, mouseEvent); }*/ - @Override - public boolean keyTyped(char typedChar, int keyCode) { - if ((keyCode == Keyboard.KEY_DOWN || keyCode == Keyboard.KEY_RIGHT) && rightButton.isEnabled()) { - rightButton.onPressed(); - return true; - } else if ((keyCode == Keyboard.KEY_UP || keyCode == Keyboard.KEY_LEFT) && leftButton.isEnabled()) { - leftButton.onPressed(); - return true; - } - return false; - } - @Override public boolean isFocused(int mouseX, int mouseY) { - mouseX += getX(); - mouseY += getY(); - return isMouseOver(mouseX, mouseY); + return isMouseOver(); } @Override diff --git a/src/main/java/forestry/core/gui/elements/GeneticAnalyzerTabs.java b/src/main/java/forestry/core/gui/elements/GeneticAnalyzerTabs.java index acdf13c99..d78fb9afa 100644 --- a/src/main/java/forestry/core/gui/elements/GeneticAnalyzerTabs.java +++ b/src/main/java/forestry/core/gui/elements/GeneticAnalyzerTabs.java @@ -10,6 +10,7 @@ import net.minecraft.item.ItemStack; import forestry.api.genetics.IDatabasePlugin; import forestry.api.genetics.IDatabaseTab; import forestry.api.gui.IGuiElement; +import forestry.api.gui.events.GuiEvent; import forestry.core.genetics.analyzer.AnalyzerTab; import forestry.core.gui.Drawable; import forestry.core.gui.GuiUtil; @@ -43,7 +44,7 @@ public class GeneticAnalyzerTabs extends VerticalLayout { public IDatabaseTab getSelected() { IGuiElement element = elements.get(selected); - if (!(element instanceof Tab) || !((Tab) element).isVisible()) { + if (!(element instanceof Tab) || !element.isVisible()) { return AnalyzerTab.ANALYZE; } return ((Tab) element).tab; @@ -81,6 +82,18 @@ public class GeneticAnalyzerTabs extends VerticalLayout { public Tab(int xPos, int yPos, int index) { super(xPos, yPos, 35, 26); this.index = index; + addSelfEventHandler(GuiEvent.DownEvent.class, event -> { + if (isVisible()) { + select(index); + SoundUtil.playButtonClick(); + analyzer.update(); + } + }); + } + + @Override + public boolean canMouseOver() { + return true; } public boolean isVisible() { @@ -114,15 +127,6 @@ public class GeneticAnalyzerTabs extends VerticalLayout { RenderHelper.disableStandardItemLighting(); } } - - @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - if (isVisible() && isMouseOver(mouseX, mouseY)) { - select(index); - SoundUtil.playButtonClick(); - analyzer.update(); - } - } } } diff --git a/src/main/java/forestry/core/gui/elements/GuiElement.java b/src/main/java/forestry/core/gui/elements/GuiElement.java index 3137b956b..c0cd98ac2 100644 --- a/src/main/java/forestry/core/gui/elements/GuiElement.java +++ b/src/main/java/forestry/core/gui/elements/GuiElement.java @@ -1,34 +1,62 @@ package forestry.core.gui.elements; +import com.google.common.base.MoreObjects; + import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.Consumer; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.GlStateManager; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + import forestry.api.gui.GuiElementAlignment; import forestry.api.gui.IGuiElement; -import forestry.api.gui.IGuiState; +import forestry.api.gui.IWindowElement; +import forestry.api.gui.events.ElementEvent; +import forestry.api.gui.events.GuiElementEvent; +import forestry.api.gui.events.GuiEventDestination; +import org.lwjgl.opengl.GL11; + +@SideOnly(Side.CLIENT) public class GuiElement extends Gui implements IGuiElement { /* Attributes - Final */ + //Tooltip of the element + protected final List tooltip = new ArrayList<>(); + //Event handler of this element + private final Collection> eventHandlers = new ArrayList<>(); + /* Attributes - State*/ //Element Position protected int xPos; protected int yPos; - //Tooltip of the element - protected final List tooltip = new ArrayList<>(); - /* Attributes - State*/ + protected int xOffset; + protected int yOffset; //Size of this element protected int width; protected int height; - protected GuiElementAlignment align = GuiElementAlignment.TOP_LEFT; + //The start coordinates of the crop + protected int cropX; + protected int cropY; + protected int cropWidth = -1; + protected int cropHeight = -1; + //The element to that the crop coordinates are relative to. + @Nullable + protected IGuiElement cropElement = null; + //Element Alignment relative to the parent + private GuiElementAlignment align = GuiElementAlignment.TOP_LEFT; + + protected boolean visible = true; + //The element container that contains this element @Nullable protected IGuiElement parent; - @Nullable - private IGuiState state; public GuiElement(int width, int height) { this(0, 0, width, height); @@ -42,21 +70,36 @@ public class GuiElement extends Gui implements IGuiElement { } @Override - public int getX() { - int x = 0; - if (parent != null && parent.getWidth() > width) { - x = (int) ((parent.getWidth() - width) * align.getXOffset()); - } - return xPos + x; + public void onCreation(){ + //Default-Implementation } @Override - public int getY() { - int y = 0; - if (parent != null && parent.getHeight() > height) { - y = (int) ((parent.getHeight() - height) * align.getYOffset()); + public void onDeletion() { + IWindowElement window = getWindow(); + window.postEvent(new ElementEvent.Deletion(this), GuiEventDestination.ALL); + } + + @Override + public final int getX() { + int x = 0; + int parentWidth = parent != null ? parent.getWidth() : -1; + int w = getWidth(); + if (parentWidth >= 0 && parentWidth > w) { + x = (int) ((parentWidth - w) * align.getXOffset()); } - return yPos + y; + return xPos + x + xOffset; + } + + @Override + public final int getY() { + int y = 0; + int parentHeight = parent != null ? parent.getHeight() : -1; + int h = getHeight(); + if (parentHeight >= 0 && parentHeight > h) { + y = (int) ((parentHeight - h) * align.getYOffset()); + } + return yPos + y + yOffset; } public final int getAbsoluteX() { @@ -68,34 +111,35 @@ public class GuiElement extends Gui implements IGuiElement { } @Override - public void draw(int mouseX, int mouseY) { + public final void draw(int mouseX, int mouseY) { + if(!isVisible()){ + return; + } GlStateManager.pushMatrix(); GlStateManager.translate(getX(), getY(), 0.0F); + if(isCropped()){ + GL11.glEnable(GL11.GL_SCISSOR_TEST); + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution res = new ScaledResolution(mc); + double scaleWidth = mc.displayWidth / res.getScaledWidth_double(); + double scaleHeight = mc.displayHeight / res.getScaledHeight_double(); + IGuiElement cropRelative = cropElement != null ? cropElement : this; + int posX = cropRelative.getAbsoluteX(); + int posY = cropRelative.getAbsoluteY(); + GL11.glScissor((int) ((posX + cropX) * scaleWidth), (int) (mc.displayHeight - ((posY + cropY + cropHeight) * scaleHeight)), (int) (cropWidth * scaleWidth), (int) (cropHeight * scaleHeight)); + } drawElement(mouseX, mouseY); + if(isCropped()){ + GL11.glDisable(GL11.GL_SCISSOR_TEST); + } + GlStateManager.popMatrix(); } public void drawElement(int mouseX, int mouseY) { - // - } - - @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - } - - @Override - public void mouseReleased(int mouseX, int mouseY, int mouseButton) { - } - - @Override - public void mouseClickMove(int mouseX, int mouseY, int mouseButton) { - } - - @Override - public boolean keyTyped(char typedChar, int keyCode) { - return false; + //Default-Implementation } @Override @@ -109,36 +153,57 @@ public class GuiElement extends Gui implements IGuiElement { } @Override - public void setSize(int width, int height) { - this.width = width; - this.height = height; - } - - @Override - public void setXPosition(int xPos) { - this.xPos = xPos; - } - - @Override - public void setYPosition(int yPos) { - this.yPos = yPos; - } - - @Override - public void setLocation(int xPos, int yPos) { - this.xPos = xPos; - this.yPos = yPos; - } - - @Override - public void setBounds(int xPos, int yPos, int width, int height) { - setLocation(xPos, yPos); + public void setHeight(int height) { setSize(width, height); } @Override - public void setAlign(GuiElementAlignment align) { + public void setWidth(int width) { + setSize(width, height); + } + + @Override + public IGuiElement setSize(int width, int height) { + this.width = width; + this.height = height; + return this; + } + + @Override + public IGuiElement setOffset(int xOffset, int yOffset) { + this.xOffset = xOffset; + this.yOffset = yOffset; + return this; + } + + @Override + public void setXPosition(int xPos) { + setLocation(xPos, yPos); + } + + @Override + public void setYPosition(int yPos) { + setLocation(xPos, yPos); + } + + @Override + public IGuiElement setLocation(int xPos, int yPos) { + this.xPos = xPos; + this.yPos = yPos; + return this; + } + + @Override + public IGuiElement setBounds(int xPos, int yPos, int width, int height) { + setLocation(xPos, yPos); + setSize(width, height); + return this; + } + + @Override + public IGuiElement setAlign(GuiElementAlignment align) { this.align = align; + return this; } @Override @@ -146,28 +211,109 @@ public class GuiElement extends Gui implements IGuiElement { return align; } - @Override - public boolean isMouseOver(int mouseX, int mouseY) { - int x = getX(); - int y = getY(); - return mouseX >= x && mouseX <= getWidth() + x && mouseY >= y && mouseY <= getHeight() + y; + /* CROPPED */ + public void setCroppedZone(@Nullable IGuiElement cropElement, int cropX, int cropY, int cropWidth, int cropHeight) { + this.cropElement = cropElement; + this.cropX = cropX; + this.cropY = cropY; + this.cropWidth = cropWidth; + this.cropHeight = cropHeight; } - public final boolean isMouseOver() { - IGuiState guiState = getGuiState(); - if (guiState == null) { + @Nullable + public IGuiElement getCropElement() { + return cropElement; + } + + public int getCropX() { + return cropX; + } + + public int getCropY() { + return cropY; + } + + public int getCropWidth() { + return cropWidth; + } + + public int getCropHeight() { + return cropHeight; + } + + public boolean isCropped(){ + return cropElement != null && cropWidth >= 0 && cropHeight >= 0; + } + + @Override + public IWindowElement getWindow() { + if (this.parent == null) { + throw new IllegalStateException("Tried to access the window element of an element that doesn't had one."); + } else { + return this.parent.getWindow(); + } + } + + @Override + public boolean isMouseOver(int mouseX, int mouseY) { + if(!isVisible()){ return false; } - int mouseX; - int mouseY; - if (parent != null) { - mouseX = guiState.getRelativeMouseX(parent); - mouseY = guiState.getRelativeMouseY(parent); - } else { - mouseX = guiState.getMouseX(); - mouseY = guiState.getMouseY(); + int x = getX(); + int y = getY(); + return mouseX >= 0 && mouseX <= getWidth() + 0 && mouseY >= 0 && mouseY <= getHeight() + 0; + } + + @Override + public final boolean isMouseOver() { + IWindowElement window = getWindow(); + int mouseX = window.getRelativeMouseX(this); + int mouseY = window.getRelativeMouseY(this); + if (!isCropped()) { + return isMouseOver(mouseX, mouseY); } - return isMouseOver(mouseX, mouseY); + IGuiElement cropRelative = cropElement != null ? cropElement : this; + int posX = cropRelative.getAbsoluteX() - this.getAbsoluteX(); + int posY = cropRelative.getAbsoluteY() - this.getAbsoluteY(); + boolean inCrop = mouseX >= posX && mouseY >= posY && mouseX <= posX + cropWidth && mouseY <= posY + cropHeight; + return inCrop && isMouseOver(mouseX, mouseY); + } + + /** + * Called if this element get updated on the client side. + */ + @SideOnly(Side.CLIENT) + protected void onUpdateClient() { + //Default-Implementation + } + + @Override + @SideOnly(Side.CLIENT) + public void updateClient() { + if (!this.isVisible()) { + return; + } + this.onUpdateClient(); + } + + @Override + public boolean isVisible() { + return visible; + } + + @Override + public void show() { + this.visible = true; + } + + @Override + public void hide() { + this.visible = false; + } + + @Override + public boolean isEnabled() { + return parent == null || parent.isEnabled(); } @Nullable @@ -177,8 +323,9 @@ public class GuiElement extends Gui implements IGuiElement { } @Override - public void setParent(@Nullable IGuiElement parent) { + public IGuiElement setParent(@Nullable IGuiElement parent) { this.parent = parent; + return this; } @Override @@ -198,6 +345,11 @@ public class GuiElement extends Gui implements IGuiElement { return this; } + @Override + public boolean hasTooltip() { + return !tooltip.isEmpty(); + } + @Override public void clearTooltip() { tooltip.clear(); @@ -208,16 +360,31 @@ public class GuiElement extends Gui implements IGuiElement { return tooltip; } - @Nullable + /* Events */ @Override - public IGuiState getGuiState() { - if (state == null && parent != null) { - state = parent.getGuiState(); - } - return state; + public void addEventHandler(Consumer eventHandler) { + eventHandlers.add(eventHandler); } - public void setGuiState(@Nullable IGuiState state) { - this.state = state; + @Override + @SuppressWarnings("unchecked") + public void receiveEvent(GuiElementEvent event) { + for(Consumer eventHandler : eventHandlers){ + ((Consumer)eventHandler).accept(event); + } + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("x", getX()) + .add("y", getY()) + .add("w", width) + .add("h", height) + .add("a", align) + .add("v", isVisible()) + .add("xO", xOffset) + .add("yO", yOffset) + .toString(); } } diff --git a/src/main/java/forestry/core/gui/elements/GuiElementFactory.java b/src/main/java/forestry/core/gui/elements/GuiElementFactory.java index 9a1266302..f04bd8100 100644 --- a/src/main/java/forestry/core/gui/elements/GuiElementFactory.java +++ b/src/main/java/forestry/core/gui/elements/GuiElementFactory.java @@ -18,10 +18,15 @@ import forestry.api.genetics.IGeneticAnalyzer; import forestry.api.genetics.IGeneticAnalyzerProvider; import forestry.api.genetics.IMutation; import forestry.api.genetics.ISpeciesRoot; +import forestry.api.gui.GuiConstants; +import forestry.api.gui.GuiElementAlignment; import forestry.api.gui.IElementGroup; import forestry.api.gui.IElementLayout; import forestry.api.gui.IGuiElement; import forestry.api.gui.IGuiElementFactory; +import forestry.api.gui.IWindowElement; +import forestry.api.gui.style.ITextStyle; +import forestry.api.gui.style.TextStyleBuilder; import forestry.core.config.Constants; import forestry.core.genetics.mutations.EnumMutateChance; import forestry.core.gui.Drawable; @@ -34,6 +39,10 @@ import forestry.core.render.ColourProperties; public class GuiElementFactory implements IGuiElementFactory { /* Constants */ + public static final ITextStyle DOMINANT_STYLE = new TextStyleBuilder().color(() -> ColourProperties.INSTANCE.get("gui.beealyzer.dominant")).build(); + public static final ITextStyle RECESSIVE_STYLE = new TextStyleBuilder().color(() -> ColourProperties.INSTANCE.get("gui.beealyzer.recessive")).build(); + public static final ITextStyle GUI_STYLE = new TextStyleBuilder().color(() -> ColourProperties.INSTANCE.get("gui.screen")).build(); + public static final ITextStyle DATABASE_TITLE = new TextStyleBuilder().color(0xcfb53b).build(); private static final ResourceLocation TEXTURE = new ResourceLocation(Constants.MOD_ID, Constants.TEXTURE_PATH_GUI + "/database_mutation_screen.png"); /* Drawables */ @@ -50,8 +59,8 @@ public class GuiElementFactory implements IGuiElementFactory { } @Override - public IGeneticAnalyzer createAnalyzer(int xPos, int yPos, boolean rightBoarder, IGeneticAnalyzerProvider provider) { - return new GeneticAnalyzer(xPos, yPos, rightBoarder, provider); + public IGeneticAnalyzer createAnalyzer(IWindowElement window, int xPos, int yPos, boolean rightBoarder, IGeneticAnalyzerProvider provider) { + return new GeneticAnalyzer(window, xPos, yPos, rightBoarder, provider); } @Override @@ -65,7 +74,7 @@ public class GuiElementFactory implements IGuiElementFactory { } @Override - public ElementGroup createPanel(int xPos, int yPos, int width, int height) { + public ElementGroup createPane(int xPos, int yPos, int width, int height) { return new PaneLayout(xPos, yPos, width, height); } @@ -77,24 +86,32 @@ public class GuiElementFactory implements IGuiElementFactory { } } + public final ITextStyle getStateStyle(boolean dominant){ + return dominant ? DOMINANT_STYLE : RECESSIVE_STYLE; + } + + public final ITextStyle getGuiStyle(){ + return GUI_STYLE; + } + public IGuiElement createFertilityInfo(IAlleleInteger fertilityAllele, int texOffset) { String fertilityString = Integer.toString(fertilityAllele.getValue()) + " x"; AbstractElementLayout layout = createHorizontal(0, 0, 0).setDistance(2); - layout.text(fertilityString, getColorCoding(fertilityAllele.isDominant())); + layout.label(fertilityString, getStateStyle(fertilityAllele.isDominant())); layout.drawable(0, -1, new Drawable(TEXTURE, 60, 240 + texOffset, 12, 8)); return layout; } public IGuiElement createToleranceInfo(IAlleleTolerance toleranceAllele, IAlleleSpecies species, String text) { IElementLayout layout = createHorizontal(0, 0, 0).setDistance(0); - layout.text(text, getColorCoding(species.isDominant())); + layout.label(text, getStateStyle(species.isDominant())); layout.add(createToleranceInfo(toleranceAllele)); return layout; } public IElementLayout createToleranceInfo(IAlleleTolerance toleranceAllele) { - int textColor = getColorCoding(toleranceAllele.isDominant()); + ITextStyle textStyle = getStateStyle(toleranceAllele.isDominant()); EnumTolerance tolerance = toleranceAllele.getValue(); String text = "(" + toleranceAllele.getAlleleName() + ")"; @@ -107,7 +124,7 @@ public class GuiElementFactory implements IGuiElementFactory { case BOTH_4: case BOTH_5: layout.add(createBothSymbol(0, -1)); - layout.text(text, textColor); + layout.label(text, textStyle); break; case DOWN_1: case DOWN_2: @@ -115,7 +132,7 @@ public class GuiElementFactory implements IGuiElementFactory { case DOWN_4: case DOWN_5: layout.add(createDownSymbol(0, -1)); - layout.text(text, textColor); + layout.label(text, textStyle); break; case UP_1: case UP_2: @@ -123,11 +140,11 @@ public class GuiElementFactory implements IGuiElementFactory { case UP_4: case UP_5: layout.add(createUpSymbol(0, -1)); - layout.text(text, textColor); + layout.label(text, textStyle); break; default: layout.add(createNoneSymbol(0, -1)); - layout.text("(0)", textColor); + layout.label("(0)", textStyle); break; } return layout; @@ -224,7 +241,7 @@ public class GuiElementFactory implements IGuiElementFactory { boolean researched = breedingTracker.isResearched(combination); if (researched) { - element.text(x + 9, y + 1, 10, 10, "+"); + element.label("+", x + 9, y + 1, 10, 10, GuiElementAlignment.TOP_LEFT, GuiConstants.DEFAULT_STYLE); } } diff --git a/src/main/java/forestry/core/gui/elements/GuiState.java b/src/main/java/forestry/core/gui/elements/GuiState.java deleted file mode 100644 index ce4560b52..000000000 --- a/src/main/java/forestry/core/gui/elements/GuiState.java +++ /dev/null @@ -1,98 +0,0 @@ -package forestry.core.gui.elements; - -import javax.annotation.Nullable; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.renderer.texture.TextureManager; - -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; - -import forestry.api.gui.IGuiElement; -import forestry.api.gui.IGuiState; -import forestry.core.gui.IGuiSizable; - -@SideOnly(Side.CLIENT) -public class GuiState implements IGuiState { - private final G gui; - private int mouseX = -1; - private int mouseY = -1; - private int mouseButton = -1; - @Nullable - private Minecraft mc = null; - - public GuiState(G gui) { - this.gui = gui; - } - - public int getMouseX() { - return mouseX; - } - - public void setMouseX(int mouseX) { - this.mouseX = mouseX; - } - - @Override - public int getRelativeMouseX(IGuiElement element) { - return mouseX - element.getAbsoluteX(); - } - - public int getMouseY() { - return mouseY; - } - - public void setMouseY(int mouseY) { - this.mouseY = mouseY; - } - - @Override - public int getRelativeMouseY(IGuiElement element) { - return mouseX - element.getAbsoluteY(); - } - - public int getScreenWidth() { - return gui.width; - } - - public int getScreenHeight() { - return gui.height; - } - - public int getGuiLeft() { - return gui.getGuiLeft(); - } - - public int getGuiTop() { - return gui.getGuiTop(); - } - - @Override - public int getGuiHeight() { - return gui.getSizeX(); - } - - @Override - public int getGuiWidth() { - return gui.getSizeY(); - } - - public Minecraft getMinecraft() { - if (mc == null) { - mc = Minecraft.getMinecraft(); - } - return mc; - } - - @Override - public TextureManager getTextureManager() { - return getMinecraft().getTextureManager(); - } - - @Override - public FontRenderer getFontRenderer() { - return getMinecraft().fontRenderer; - } -} diff --git a/src/main/java/forestry/core/gui/elements/IngredientElement.java b/src/main/java/forestry/core/gui/elements/IngredientElement.java index 447cdd3a1..0a73c003a 100644 --- a/src/main/java/forestry/core/gui/elements/IngredientElement.java +++ b/src/main/java/forestry/core/gui/elements/IngredientElement.java @@ -28,7 +28,8 @@ public class IngredientElement extends AbstractItemElement { this.items = items; } - protected ItemStack getItemStack() { + @Override + protected ItemStack getStack() { if (items.isEmpty()) { return ItemStack.EMPTY; } diff --git a/src/main/java/forestry/core/gui/elements/ItemElement.java b/src/main/java/forestry/core/gui/elements/ItemElement.java index d03cf3f93..1044deb67 100644 --- a/src/main/java/forestry/core/gui/elements/ItemElement.java +++ b/src/main/java/forestry/core/gui/elements/ItemElement.java @@ -12,16 +12,25 @@ package forestry.core.gui.elements; import net.minecraft.item.ItemStack; -public class ItemElement extends AbstractItemElement { - /* Attributes - Final */ - private final ItemStack itemStack; +import forestry.api.gui.IItemElement; - public ItemElement(int xPos, int yPos, ItemStack itemStack) { +public class ItemElement extends AbstractItemElement implements IItemElement{ + /* Attributes */ + private ItemStack stack; + + public ItemElement(int xPos, int yPos, ItemStack stack) { super(xPos, yPos); - this.itemStack = itemStack; + this.stack = stack; } - protected ItemStack getItemStack() { - return itemStack; + @Override + public ItemStack getStack() { + return stack; + } + + @Override + public IItemElement setStack(ItemStack stack) { + this.stack = stack; + return this; } } diff --git a/src/main/java/forestry/core/gui/elements/LabelElement.java b/src/main/java/forestry/core/gui/elements/LabelElement.java new file mode 100644 index 000000000..889be1f00 --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/LabelElement.java @@ -0,0 +1,104 @@ +package forestry.core.gui.elements; + +import com.google.common.collect.ImmutableMap; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; + +import forestry.api.gui.GuiElementAlignment; +import forestry.api.gui.ILabelElement; +import forestry.api.gui.style.ITextStyle; +import forestry.core.utils.GuiElementUtil; + +public class LabelElement extends GuiElement implements ILabelElement { + /* Constants */ + public static final FontRenderer FONT_RENDERER = Minecraft.getMinecraft().fontRenderer; + + /* Attributes - State */ + protected ITextStyle style; + protected String text; + protected String rawText; + protected boolean textLength = false; + + public LabelElement(String text, GuiElementAlignment align, ITextStyle style) { + this(0, 0, -1, FONT_RENDERER.FONT_HEIGHT, text, align, style); + } + + public LabelElement(int xPos, int yPos, int width, int height, String text, GuiElementAlignment align, ITextStyle style) { + super(xPos, yPos, width, height); + textLength = width < 0; + this.style = style; + this.rawText = text; + this.text = GuiElementUtil.getFormattedString(style, text); + setAlign(align); + if (textLength) { + boolean uni = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + setWidth(FONT_RENDERER.getStringWidth(this.text)); + FONT_RENDERER.setUnicodeFlag(uni); + } + } + + @Override + public ILabelElement setStyle(ITextStyle style) { + this.style = style; + this.text = GuiElementUtil.getFormattedString(style, rawText); + if(textLength){ + boolean uni = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + setWidth(FONT_RENDERER.getStringWidth(this.text)); + FONT_RENDERER.setUnicodeFlag(uni); + } + return this; + } + + @Override + public ITextStyle getStyle() { + return style; + } + + @Override + public String getText() { + return text; + } + + @Override + public ILabelElement setText(String text) { + this.rawText = text; + this.text = GuiElementUtil.getFormattedString(style, text); + if(textLength){ + boolean uni = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + setWidth(FONT_RENDERER.getStringWidth(this.text)); + FONT_RENDERER.setUnicodeFlag(uni); + } + return this; + } + + @Override + public String getRawText() { + return rawText; + } + + @Override + public Collection getLines() { + return Collections.singletonList(text); + } + + @Override + public Map getRawLines() { + return ImmutableMap.of(style, rawText); + } + + @Override + public void drawElement(int mouseX, int mouseY) { + boolean unicode = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + FONT_RENDERER.drawString(text, 0, 0, style.getColor()); + FONT_RENDERER.setUnicodeFlag(unicode); + } +} diff --git a/src/main/java/forestry/core/gui/elements/ScrollBarElement.java b/src/main/java/forestry/core/gui/elements/ScrollBarElement.java index ce1962f57..283bcca6f 100644 --- a/src/main/java/forestry/core/gui/elements/ScrollBarElement.java +++ b/src/main/java/forestry/core/gui/elements/ScrollBarElement.java @@ -32,9 +32,6 @@ public class ScrollBarElement extends ElementGroup { public ScrollBarElement(int xPos, int yPos, int width, int height, Drawable sliderTexture) { super(xPos, yPos, width, height); - this.width = width; - this.height = height; - isScrolling = false; wasClicked = false; visible = true; @@ -63,6 +60,11 @@ public class ScrollBarElement extends ElementGroup { setValue(currentValue); } + @Override + public boolean canMouseOver() { + return true; + } + public void setVisible(boolean visible) { this.visible = visible; } @@ -141,12 +143,12 @@ public class ScrollBarElement extends ElementGroup { } else { setValue((int) (minValue + (float) step * Math.round(value))); } - } else if (slider.isMouseOver(mouseX - getX(), mouseY - getY())) { //clicked on the slider + } else if (slider.isMouseOver()) { //clicked on the slider if (mouseDown) { isScrolling = true; initialMouseClickY = y - slider.getY(); } - } else if (mouseDown && !wasClicked && isMouseOver(mouseX, mouseY)) { //clicked on the bar but not on the slider + } else if (mouseDown && !wasClicked && isMouseOver()) { //clicked on the bar but not on the slider int range = maxValue - minValue; float value = ((float) y - (float) slider.height / 2.0F) / (float) (height - slider.height); value *= (float) range; diff --git a/src/main/java/forestry/core/gui/elements/ScrollableElement.java b/src/main/java/forestry/core/gui/elements/ScrollableElement.java new file mode 100644 index 000000000..ecf142257 --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/ScrollableElement.java @@ -0,0 +1,61 @@ +package forestry.core.gui.elements; + +import com.google.common.collect.Lists; + +import javax.annotation.Nullable; + +import forestry.api.gui.IGuiElement; +import forestry.api.gui.events.GuiEvent; +import forestry.core.gui.elements.layouts.ElementGroup; +import forestry.core.gui.widgets.IScrollable; + +public class ScrollableElement extends ElementGroup implements IScrollable { + @Nullable + private IGuiElement content; + private float scrollPercentage; + private float step; + + public ScrollableElement(int xPos, int yPos, int width, int height) { + super(xPos, yPos, width, height); + addSelfEventHandler(GuiEvent.WheelEvent.class, event->{ + //movePercentage(event.getDWheel()); + }); + } + + public int getInvisibleArea() { + step = (12 * 0.5F); + if(content == null){ + return 0; + } + return (int) ((content.getHeight() - height) / (step)); + } + + protected void movePercentage(float percentage){ + scrollPercentage = (percentage * step); + } + + @Override + public void onScroll(int value) { + scrollPercentage = (value * step); + if(content != null){ + content.setOffset(0, -((int) scrollPercentage)); + } + } + + public void setContent(@Nullable IGuiElement content) { + this.content = content; + if(content != null){ + content.setCroppedZone(this, 0, 0, width, height); + } + } + + @Override + public void clear() { + remove(Lists.newArrayList(elements)); + } + + @Override + public boolean isFocused(int mouseX, int mouseY) { + return isMouseOver(); + } +} diff --git a/src/main/java/forestry/core/gui/elements/SplitTextElement.java b/src/main/java/forestry/core/gui/elements/SplitTextElement.java new file mode 100644 index 000000000..4f5163326 --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/SplitTextElement.java @@ -0,0 +1,73 @@ +package forestry.core.gui.elements; + +import com.google.common.collect.ImmutableMap; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import net.minecraft.client.renderer.GlStateManager; + +import forestry.api.gui.GuiElementAlignment; +import forestry.api.gui.ITextElement; +import forestry.api.gui.style.ITextStyle; +import forestry.core.utils.GuiElementUtil; + +import static forestry.core.gui.elements.LabelElement.FONT_RENDERER; + +public class SplitTextElement extends GuiElement implements ITextElement { + + private List lines = new ArrayList<>(); + private String rawText; + private ITextStyle style; + + public SplitTextElement(int xPos, int yPos, int width, String rawText, GuiElementAlignment align, ITextStyle style) { + super(xPos, yPos, width, 0); + this.rawText = rawText; + this.style = style; + setAlign(align); + boolean uni = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + this.lines.addAll(FONT_RENDERER.listFormattedStringToWidth(GuiElementUtil.getFormattedString(style, rawText), width)); + FONT_RENDERER.setUnicodeFlag(uni); + setHeight(lines.size() * FONT_RENDERER.FONT_HEIGHT); + } + + @Override + public Collection getLines() { + return lines; + } + + @Override + public ITextElement setText(String text) { + this.rawText = text; + boolean uni = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + lines.clear(); + lines.addAll(FONT_RENDERER.listFormattedStringToWidth(GuiElementUtil.getFormattedString(style, rawText), width)); + FONT_RENDERER.setUnicodeFlag(uni); + setHeight(lines.size() * FONT_RENDERER.FONT_HEIGHT); + return this; + } + + @Override + public Map getRawLines() { + return ImmutableMap.of(style, rawText); + } + + @Override + public void drawElement(int mouseX, int mouseY) { + boolean unicode = FONT_RENDERER.getUnicodeFlag(); + FONT_RENDERER.setUnicodeFlag(style.isUnicode()); + int posY = 0; + for(String text : lines) { + int posX = width - FONT_RENDERER.getStringWidth(text); + posX *= getAlign().getXOffset(); + FONT_RENDERER.drawString(text, posX, posY, style.getColor()); + posY += FONT_RENDERER.FONT_HEIGHT; + } + FONT_RENDERER.setUnicodeFlag(unicode); + GlStateManager.color(1.0f, 1.0f, 1.0f); + } +} diff --git a/src/main/java/forestry/core/gui/elements/TankElement.java b/src/main/java/forestry/core/gui/elements/TankElement.java index fce12855e..80a8604a1 100644 --- a/src/main/java/forestry/core/gui/elements/TankElement.java +++ b/src/main/java/forestry/core/gui/elements/TankElement.java @@ -149,6 +149,11 @@ public class TankElement extends GuiElement { return toolTip; } + @Override + public boolean hasTooltip() { + return true; + } + private static void setGLColorFromInt(int color) { float red = (color >> 16 & 0xFF) / 255.0F; float green = (color >> 8 & 0xFF) / 255.0F; diff --git a/src/main/java/forestry/core/gui/elements/TextEditElement.java b/src/main/java/forestry/core/gui/elements/TextEditElement.java new file mode 100644 index 000000000..25abce35d --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/TextEditElement.java @@ -0,0 +1,10 @@ +package forestry.core.gui.elements; + +public class TextEditElement extends GuiElement { + + //private final GuiTextField field; + + public TextEditElement(int xPos, int yPos, int width, int height) { + super(xPos, yPos, width, height); + } +} diff --git a/src/main/java/forestry/core/gui/elements/TextElement.java b/src/main/java/forestry/core/gui/elements/TextElement.java deleted file mode 100644 index d4b3e5fb7..000000000 --- a/src/main/java/forestry/core/gui/elements/TextElement.java +++ /dev/null @@ -1,46 +0,0 @@ -package forestry.core.gui.elements; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; - -import forestry.api.gui.GuiElementAlignment; - -public class TextElement extends GuiElement { - /* Constants */ - public static final FontRenderer FONT_RENDERER = Minecraft.getMinecraft().fontRenderer; - - /* Attributes - Final */ - protected final String text; - protected final int color; - protected final boolean unicode; - - public TextElement(int width, int height, String text, GuiElementAlignment align, int color, boolean unicode) { - this(0, 0, width, height, text, align, color, unicode); - } - - public TextElement(int xPos, int yPos, int width, int height, String text, GuiElementAlignment align, int color, boolean unicode) { - super(xPos, yPos, width, height); - this.text = text; - setAlign(align); - this.color = color; - this.unicode = unicode; - if (width < 0) { - boolean uni = FONT_RENDERER.getUnicodeFlag(); - FONT_RENDERER.setUnicodeFlag(this.unicode); - this.width = FONT_RENDERER.getStringWidth(text); - FONT_RENDERER.setUnicodeFlag(uni); - } - } - - public String getText() { - return text; - } - - @Override - public void drawElement(int mouseX, int mouseY) { - boolean unicode = FONT_RENDERER.getUnicodeFlag(); - FONT_RENDERER.setUnicodeFlag(this.unicode); - FONT_RENDERER.drawString(text, 0, 0, color); - FONT_RENDERER.setUnicodeFlag(unicode); - } -} diff --git a/src/main/java/forestry/core/gui/elements/Window.java b/src/main/java/forestry/core/gui/elements/Window.java new file mode 100644 index 000000000..0a2a64c54 --- /dev/null +++ b/src/main/java/forestry/core/gui/elements/Window.java @@ -0,0 +1,346 @@ +package forestry.core.gui.elements; + +import javax.annotation.Nullable; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.List; +import java.util.ListIterator; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.texture.TextureManager; + +import net.minecraftforge.fml.client.config.GuiUtils; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import forestry.api.gui.IElementGroup; +import forestry.api.gui.IGuiElement; +import forestry.api.gui.IWindowElement; +import forestry.api.gui.events.ElementEvent; +import forestry.api.gui.events.GuiEvent; +import forestry.api.gui.events.GuiEventDestination; +import forestry.core.gui.IGuiSizable; +import forestry.core.gui.elements.layouts.ElementGroup; + +import org.lwjgl.input.Mouse; + +/** + * This element is the top parent. + */ +@SideOnly(Side.CLIENT) +public class Window extends ElementGroup implements IWindowElement { + protected final G gui; + @Nullable + private Minecraft mc = null; + //The last x position of the mouse + protected int mouseX = -1; + //The last y position of the mouse + protected int mouseY = -1; + @Nullable + private IGuiElement mousedOverElement; + @Nullable + private IGuiElement draggedElement; + @Nullable + private IGuiElement focusedElement; + + public Window(int width, int height, G gui) { + super(0, 0, width, height); + this.gui = gui; + addEventHandler(ElementEvent.Deletion.class, deletion -> { + IGuiElement element = deletion.getOrigin(); + if (isMouseOver(element)) { + setMousedOverElement(null); + } + if (isDragged(element)) { + setDraggedElement(null); + } + if (isFocused(element)) { + setFocusedElement(null); + } + }); + } + + @Override + public IWindowElement getWindow() { + return this; + } + + @Override + public IGuiElement setParent(@Nullable IGuiElement parent) { + return this; + } + + public void init(int guiLeft, int guiTop){ + setLocation(guiLeft, guiTop); + } + + /* Element Events */ + @Nullable + public IGuiElement getDraggedElement() { + return this.draggedElement; + } + + public void setDraggedElement(@Nullable IGuiElement widget) { + this.setDraggedElement(widget, -1); + } + + public void setDraggedElement(@Nullable IGuiElement widget, int button) { + if (this.draggedElement == widget) { + return; + } + if (this.draggedElement != null) { + postEvent(new ElementEvent.EndDrag(this.draggedElement), GuiEventDestination.ALL); + } + this.draggedElement = widget; + if (this.draggedElement != null) { + postEvent(new ElementEvent.StartDrag(this.draggedElement, button), GuiEventDestination.ALL); + } + } + + @Override + @Nullable + public IGuiElement getMousedOverElement() { + return this.mousedOverElement; + } + + public void setMousedOverElement(@Nullable IGuiElement widget) { + if (this.mousedOverElement == widget) { + return; + } + if (this.mousedOverElement != null) { + postEvent(new ElementEvent.EndMouseOver(this.mousedOverElement), GuiEventDestination.ALL); + } + this.mousedOverElement = widget; + if (this.mousedOverElement != null) { + postEvent(new ElementEvent.StartMouseOver(this.mousedOverElement), GuiEventDestination.ALL); + } + } + + @Nullable + public IGuiElement getFocusedElement() { + return this.focusedElement; + } + + public void setFocusedElement(@Nullable IGuiElement widget) { + IGuiElement newElement = widget; + if (this.focusedElement == newElement) { + return; + } + if (newElement != null && !newElement.canFocus()) { + newElement = null; + } + if (this.focusedElement != null) { + postEvent(new ElementEvent.LoseFocus(this.focusedElement), GuiEventDestination.ALL); + } + this.focusedElement = newElement; + if (this.focusedElement != null) { + postEvent(new ElementEvent.GainFocus(this.focusedElement), GuiEventDestination.ALL); + } + } + + @Override + public boolean isMouseOver(final IGuiElement element) { + return this.getMousedOverElement() == element; + } + + public boolean isDragged(final IGuiElement element) { + return this.getDraggedElement() == element; + } + + public boolean isFocused(final IGuiElement element) { + return this.getFocusedElement() == element; + } + + @Override + @SideOnly(Side.CLIENT) + public void updateClient() { + if (!isVisible()) { + return; + } + updateWindow(); + onUpdateClient(); + for (IGuiElement widget : getElements()) { + widget.updateClient(); + } + } + + protected void updateWindow() { + this.setMousedOverElement(this.calculateMousedOverElement()); + if (this.getFocusedElement() != null && (!this.getFocusedElement().isVisible() || !this.getFocusedElement().isEnabled())) { + this.setFocusedElement(null); + } + if (!Mouse.isButtonDown(0)) { + if (this.draggedElement != null) { + this.setDraggedElement(null); + } + } + } + + @Nullable + private IGuiElement calculateMousedOverElement() { + Deque queue = this.calculateMousedOverElements(); + while (!queue.isEmpty()) { + IGuiElement element = queue.removeFirst(); + if (element.isEnabled() && element.isVisible() && element.canMouseOver()) { + return element; + } + } + return null; + } + + private Deque calculateMousedOverElements() { + Deque list = new ArrayDeque<>(); + for (IGuiElement element : this.getQueuedElements(this)) { + if (element.isMouseOver()) { + list.addLast(element); + } + } + return list; + } + + private Collection getQueuedElements(final IGuiElement element) { + List widgets = new ArrayList<>(); + if(element instanceof IElementGroup) { + IElementGroup group = (IElementGroup) element; + boolean addChildren = true; + if (element.isCropped()) { + int mouseX = getRelativeMouseX(element); + int mouseY = getRelativeMouseY(element); + IGuiElement cropRelative = element.getCropElement() != null ? element.getCropElement() : this; + int posX = cropRelative.getAbsoluteX() - element.getAbsoluteX(); + int posY = cropRelative.getAbsoluteY() - element.getAbsoluteY(); + addChildren = mouseX >= posX && mouseY >= posY && mouseX <= posX + element.getCropWidth() && mouseY <= posY + element.getCropWidth(); + } + if (addChildren) { + ListIterator iterator = group.getElements().listIterator(group.getElements().size()); + while (iterator.hasPrevious()) { + final IGuiElement child = iterator.previous(); + widgets.addAll(this.getQueuedElements(child)); + } + } + } + widgets.add(element); + return widgets; + } + + public void drawTooltip(int mouseX, int mouseY) { + List lines = getTooltip(mouseX, mouseY); + if (!lines.isEmpty()) { + GlStateManager.pushMatrix(); + ScaledResolution scaledresolution = new ScaledResolution(getMinecraft()); + GuiUtils.drawHoveringText(lines, mouseX - getX(), mouseY - getY(), scaledresolution.getScaledWidth(), scaledresolution.getScaledHeight(), -1, getFontRenderer()); + GlStateManager.popMatrix(); + } + } + + @Override + public List getTooltip(int mouseX, int mouseY) { + List tooltip = new ArrayList<>(getTooltip()); + Deque queue = this.calculateMousedOverElements(); + while (!queue.isEmpty()) { + IGuiElement element = queue.removeFirst(); + if (element.isEnabled() && element.isVisible() && element.hasTooltip()) { + tooltip.addAll(element.getTooltip(getRelativeMouseX(element), getRelativeMouseY(element))); + } + } + return tooltip; + } + + /* Mouse */ + public void setMousePosition(int mouseX, int mouseY){ + float dx = (float) mouseX - (float) this.mouseX; + float dy = (float) mouseY - (float) this.mouseY; + if (dx != 0.0f || dy != 0.0f) { + if (draggedElement != null) { + postEvent(new GuiEvent.DragEvent(draggedElement, dx, dy), GuiEventDestination.ALL); + } else { + postEvent(new GuiEvent.MoveEvent(this, dx, dy), GuiEventDestination.ALL); + } + } + if(mouseX != this.mouseX || mouseY != this.mouseY){ + this.mouseX = mouseX; + this.mouseY = mouseY; + setMousedOverElement(calculateMousedOverElement()); + } + } + + @Override + public int getMouseX() { + return mouseX; + } + + @Override + public int getMouseY() { + return mouseY; + } + + @Override + public int getRelativeMouseX(@Nullable IGuiElement element) { + if(element == null){ + return mouseX; + } + return mouseX - element.getAbsoluteX(); + } + + @Override + public int getRelativeMouseY(@Nullable IGuiElement element) { + if(element == null){ + return mouseY; + } + return mouseY - element.getAbsoluteY(); + } + + /* Gui Screen */ + @Override + public int getScreenWidth() { + return gui.width; + } + + @Override + public int getScreenHeight() { + return gui.height; + } + + @Override + public int getGuiLeft() { + return gui.getGuiLeft(); + } + + @Override + public int getGuiTop() { + return gui.getGuiTop(); + } + + @Override + public int getGuiHeight() { + return gui.getSizeX(); + } + + @Override + public int getGuiWidth() { + return gui.getSizeY(); + } + + protected Minecraft getMinecraft() { + if (mc == null) { + mc = Minecraft.getMinecraft(); + } + return mc; + } + + @Override + public TextureManager getTextureManager() { + return getMinecraft().getTextureManager(); + } + + @Override + public FontRenderer getFontRenderer() { + return getMinecraft().fontRenderer; + } +} diff --git a/src/main/java/forestry/core/gui/elements/layouts/AbstractElementLayout.java b/src/main/java/forestry/core/gui/elements/layouts/AbstractElementLayout.java index 800568e9a..e1271a1cb 100644 --- a/src/main/java/forestry/core/gui/elements/layouts/AbstractElementLayout.java +++ b/src/main/java/forestry/core/gui/elements/layouts/AbstractElementLayout.java @@ -1,7 +1,11 @@ package forestry.core.gui.elements.layouts; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + import forestry.api.gui.IElementLayout; +@SideOnly(Side.CLIENT) public abstract class AbstractElementLayout extends ElementGroup implements IElementLayout { /* The distance between the different elements of this group. */ public int distance; diff --git a/src/main/java/forestry/core/gui/elements/layouts/ElementGroup.java b/src/main/java/forestry/core/gui/elements/layouts/ElementGroup.java index e2a861b8a..7cfe8dbe9 100644 --- a/src/main/java/forestry/core/gui/elements/layouts/ElementGroup.java +++ b/src/main/java/forestry/core/gui/elements/layouts/ElementGroup.java @@ -6,16 +6,26 @@ import java.util.List; import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import forestry.api.gui.GuiConstants; import forestry.api.gui.GuiElementAlignment; import forestry.api.gui.IElementGroup; import forestry.api.gui.IElementLayoutHelper; import forestry.api.gui.IGuiElement; +import forestry.api.gui.IItemElement; +import forestry.api.gui.ILabelElement; +import forestry.api.gui.ITextElement; +import forestry.api.gui.style.ITextStyle; import forestry.core.gui.Drawable; import forestry.core.gui.elements.DrawableElement; import forestry.core.gui.elements.GuiElement; import forestry.core.gui.elements.ItemElement; -import forestry.core.gui.elements.TextElement; +import forestry.core.gui.elements.LabelElement; +import forestry.core.gui.elements.SplitTextElement; +@SideOnly(Side.CLIENT) public abstract class ElementGroup extends GuiElement implements IElementGroup { protected final List elements = new ArrayList<>(); @@ -26,16 +36,20 @@ public abstract class ElementGroup extends GuiElement implements IElementGroup { public E add(E element) { elements.add(element); element.setParent(this); + element.onCreation(); return element; } public E remove(E element) { elements.remove(element); + element.onDeletion(); return element; } public void clear() { - elements.clear(); + for (IGuiElement element : new ArrayList<>(elements)) { + remove(element); + } } @Override @@ -51,50 +65,15 @@ public abstract class ElementGroup extends GuiElement implements IElementGroup { } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - int mX = mouseX - getX(); - int mY = mouseY - getY(); - elements.stream() - .filter(element -> element.isMouseOver(mX, mY)) - .forEach(element -> element.mouseClicked(mX, mY, mouseButton)); - } - - @Override - public void mouseReleased(int mouseX, int mouseY, int mouseButton) { - int mX = mouseX - getX(); - int mY = mouseY - getY(); - elements.stream() - .filter(element -> element.isMouseOver(mX, mY)) - .forEach(element -> element.mouseReleased(mX, mY, mouseButton)); - } - - @Override - public void mouseClickMove(int mouseX, int mouseY, int mouseButton) { - int mX = mouseX - getX(); - int mY = mouseY - getY(); - elements.stream() - .filter(element -> element.isMouseOver(mX, mY)) - .forEach(element -> element.mouseClickMove(mX, mY, mouseButton)); - } - - @Override - public boolean keyTyped(char typedChar, int keyCode) { - return elements.stream().anyMatch(element -> element.keyTyped(typedChar, keyCode)); - } - - @Override - public List getTooltip(int mouseX, int mouseY) { - mouseX -= getX(); - mouseY -= getY(); - for (IGuiElement element : elements) { - if (element.isMouseOver(mouseX, mouseY)) { - List tooltip = element.getTooltip(mouseX, mouseY); - if (!tooltip.isEmpty()) { - return tooltip; - } - } + @SideOnly(Side.CLIENT) + public void updateClient() { + if (!isVisible()) { + return; + } + onUpdateClient(); + for (IGuiElement widget : getElements()) { + widget.updateClient(); } - return tooltip; } @Nullable @@ -112,58 +91,60 @@ public abstract class ElementGroup extends GuiElement implements IElementGroup { } @Override - public IGuiElement item(int xPos, int yPos, ItemStack itemStack) { - IGuiElement element = new ItemElement(xPos, yPos, itemStack); + public IItemElement item(int xPos, int yPos, ItemStack itemStack) { + IItemElement element = new ItemElement(xPos, yPos, itemStack); add(element); return element; } @Override - public IGuiElement text(String text) { - return text(0, text, 0xFFFFFF); + public ILabelElement label(String text) { + return label(text, GuiConstants.DEFAULT_STYLE); } @Override - public IGuiElement text(String text, int color) { - return text(0, text, color); + public ILabelElement label(String text, ITextStyle style) { + return label(text, GuiElementAlignment.TOP_LEFT, style); } @Override - public IGuiElement text(String text, GuiElementAlignment align) { - return text(text, align, 0xFFFFFF); + public ILabelElement label(String text, GuiElementAlignment align) { + return label(text, align, GuiConstants.DEFAULT_STYLE); } @Override - public IGuiElement text(String text, GuiElementAlignment align, int color, boolean unicode) { - return text(0, 12, text, align, color, unicode); + public ILabelElement label(String text, GuiElementAlignment align, ITextStyle textStyle) { + return label(text, -1, 12, align, textStyle); } @Override - public IGuiElement text(String text, GuiElementAlignment align, int color) { - return text(0, text, align, color); - } - - public IGuiElement text(int x, String text, int color) { - return text(x, text, GuiElementAlignment.TOP_LEFT, color); - } - - public IGuiElement text(int x, String text, GuiElementAlignment align, int color) { - return text(x, 12, text, align, color); + public ILabelElement label(String text, int width, int height, GuiElementAlignment align, ITextStyle textStyle) { + return label(text, 0, 0, width, height, align, textStyle); } @Override - public IGuiElement text(int x, int height, String text, GuiElementAlignment align, int color) { - return text(x, height, text, align, color, false); + public ILabelElement label(String text, int x, int y, int width, int height, GuiElementAlignment align, ITextStyle textStyle) { + return add(new LabelElement(x, y, width, height, text, align, textStyle)); } @Override - public IGuiElement text(int x, int y, int width, int height, String text) { - return add(new TextElement(x, y, width, height, text, GuiElementAlignment.TOP_LEFT, 0xFFFFFF, false)); + public ITextElement splitText(String text, int width) { + return splitText(text, width, GuiConstants.DEFAULT_STYLE); } @Override - public IGuiElement text(int x, int height, String text, GuiElementAlignment align, int color, boolean unicode) { - return add(new TextElement(x, 0, -1, height, text, align, color, unicode)); + public ITextElement splitText(String text, int width, ITextStyle textStyle) { + return splitText(text, width, GuiElementAlignment.TOP_LEFT, textStyle); + } + + @Override + public ITextElement splitText(String text, int width, GuiElementAlignment align, ITextStyle textStyle) { + return splitText(text, 0, 0, width, align, textStyle); + } + + @Override + public ITextElement splitText(String text, int x, int y, int width, GuiElementAlignment align, ITextStyle textStyle) { + return add(new SplitTextElement(x, y, width, text, align, textStyle)); } @Override @@ -187,12 +168,12 @@ public abstract class ElementGroup extends GuiElement implements IElementGroup { } @Override - public ElementGroup panel(int xPos, int yPos, int width, int height) { + public ElementGroup pane(int xPos, int yPos, int width, int height) { return add(new PaneLayout(xPos, yPos, width, height)); } @Override - public ElementGroup panel(int width, int height) { + public ElementGroup pane(int width, int height) { return add(new PaneLayout(0, 0, width, height)); } diff --git a/src/main/java/forestry/core/gui/elements/layouts/HorizontalLayout.java b/src/main/java/forestry/core/gui/elements/layouts/HorizontalLayout.java index c0869e5b7..11b1f787a 100644 --- a/src/main/java/forestry/core/gui/elements/layouts/HorizontalLayout.java +++ b/src/main/java/forestry/core/gui/elements/layouts/HorizontalLayout.java @@ -8,22 +8,20 @@ public class HorizontalLayout extends AbstractElementLayout { super(xPos, yPos, 0, height); } - public void setWidth(int width) { - this.width = width; - } - public E add(E element) { elements.add(element); element.setParent(this); element.setXPosition(width); - width += element.getWidth() + distance; + setWidth(width + (element.getWidth() + distance)); + element.onCreation(); return element; } public E remove(E element) { elements.remove(element); - width -= element.getWidth() + distance; + setWidth(width - (element.getWidth() + distance)); element.setXPosition(0); + element.onDeletion(); return element; } diff --git a/src/main/java/forestry/core/gui/elements/layouts/ScrollableElement.java b/src/main/java/forestry/core/gui/elements/layouts/ScrollableElement.java deleted file mode 100644 index d2c5368cd..000000000 --- a/src/main/java/forestry/core/gui/elements/layouts/ScrollableElement.java +++ /dev/null @@ -1,210 +0,0 @@ -package forestry.core.gui.elements.layouts; - -import com.google.common.collect.Lists; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.function.Function; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.renderer.GlStateManager; - -import forestry.api.genetics.IAllele; -import forestry.api.genetics.IAlleleInteger; -import forestry.api.genetics.IAlleleSpecies; -import forestry.api.genetics.IAlleleTolerance; -import forestry.api.genetics.IBreedingTracker; -import forestry.api.genetics.IChromosomeType; -import forestry.api.genetics.IIndividual; -import forestry.api.genetics.IMutation; -import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; -import forestry.api.gui.IElementGroup; -import forestry.api.gui.IElementLayout; -import forestry.api.gui.IGuiElement; -import forestry.core.gui.elements.GuiElementFactory; -import forestry.core.gui.widgets.IScrollable; -import forestry.core.render.ColourProperties; -import forestry.core.utils.Translator; - -import org.lwjgl.opengl.GL11; - -public class ScrollableElement extends VerticalLayout implements IScrollable, IElementGenetic { - private final List visibleElements = new ArrayList<>(); - private final int sizeY; - private int elementOffset; - private float scrollPercentage; - private float step; - - public ScrollableElement(int xPos, int yPos, int width, int height) { - super(xPos, yPos, width); - this.sizeY = height; - } - - public void updateVisibleElements(int offset) { - visibleElements.clear(); - int height = 0; - - int widgetEnd = yPos + sizeY; - for (int i = 0; i < elements.size(); i++) { - IGuiElement element = elements.get(i); - if (i < offset) { - height += element.getHeight(); - continue; - } - elementOffset = -height; - int elementHeight = element.getY() + element.getHeight() + elementOffset; - if (elementHeight > widgetEnd) { - continue; - } - visibleElements.add(element); - } - } - - public int getInvisibleElementCount() { - step = (12 * 0.5F); - return (int) ((getHeight() - sizeY) / (step)); - } - - @Override - public void drawElement(int mouseX, int mouseY) { - GL11.glEnable(GL11.GL_SCISSOR_TEST); - Minecraft mc = Minecraft.getMinecraft(); - ScaledResolution res = new ScaledResolution(mc); - double scaleWidth = mc.displayWidth / res.getScaledWidth_double(); - double scaleHeight = mc.displayHeight / res.getScaledHeight_double(); - GL11.glScissor((int) ((getAbsoluteX()) * scaleWidth), (int) (mc.displayHeight - ((getAbsoluteY() + sizeY) * scaleHeight)), (int) (width * scaleWidth), (int) (sizeY * scaleHeight)); - GlStateManager.pushMatrix(); - GlStateManager.translate(0.0F, -((int) scrollPercentage), 0.0F); - for (IGuiElement element : elements) { - element.draw(mouseX, mouseY); - } - GlStateManager.popMatrix(); - GL11.glDisable(GL11.GL_SCISSOR_TEST); - } - - @Override - public List getTooltip(int mouseX, int mouseY) { - mouseX -= getX(); - mouseY -= getY(); - for (IGuiElement element : visibleElements) { - if (element.isMouseOver()) { - List toolTip = element.getTooltip(mouseX, mouseY); - if (!toolTip.isEmpty()) { - return toolTip; - } - } - } - return Collections.emptyList(); - } - - @Override - public void addFertilityInfo(String chromosomeName, IAlleleInteger fertilityAllele, int texOffset) { - addRow(chromosomeName, GuiElementFactory.INSTANCE.createFertilityInfo(fertilityAllele, texOffset)); - } - - @Override - public void addToleranceInfo(String chromosomeName, IAlleleTolerance toleranceAllele, IAlleleSpecies species, String text) { - addRow(chromosomeName, text, species.isDominant()); - addRow(" " + Translator.translateToLocal("for.gui.tolerance"), GuiElementFactory.INSTANCE.createToleranceInfo(toleranceAllele)); - } - - @Override - public void addMutation(int x, int y, int width, int height, IMutation mutation, IAllele species, IBreedingTracker breedingTracker) { - IGuiElement element = GuiElementFactory.INSTANCE.createMutation(x, y, width, height, mutation, species, breedingTracker); - if (element == null) { - return; - } - add(element); - } - - @Override - public void addMutationResultant(int x, int y, int width, int height, IMutation mutation, IBreedingTracker breedingTracker) { - IGuiElement element = GuiElementFactory.INSTANCE.createMutationResultant(x, y, width, height, mutation, breedingTracker); - if (element == null) { - return; - } - add(element); - } - - @Override - public void addRow(String firstText, String secondText, boolean dominant) { - addRow(firstText, secondText, ColourProperties.INSTANCE.get("gui.screen"), GuiElementFactory.INSTANCE.getColorCoding(dominant)); - } - - @Override - public final void addAlleleRow(String chromosomeName, IIndividual individual, IChromosomeType chromosome, boolean active) { - addAlleleRow(chromosomeName, IAllele::getAlleleName, individual, chromosome, active); - } - - @Override - public void addRow(String firstText, String secondText, int firstColor, int secondColor) { - IElementLayout first = addSplitText(width, firstText, GuiElementAlignment.TOP_LEFT, firstColor); - IElementLayout second = addSplitText(width, secondText, GuiElementAlignment.TOP_LEFT, secondColor); - addRow(first, second); - } - - private IElementLayout addSplitText(int width, String text, GuiElementAlignment alignment, int color) { - FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; - IElementLayout vertical = new VerticalLayout(width); - for (String splitText : fontRenderer.listFormattedStringToWidth(text, 70)) { - vertical.text(splitText, alignment, color); - } - return vertical; - } - - private void addRow(String chromosomeName, IGuiElement right) { - int center = width / 2; - IGuiElement first = addSplitText(center, chromosomeName, GuiElementAlignment.TOP_LEFT, ColourProperties.INSTANCE.get("gui.screen")); - addRow(first, right); - } - - private void addRow(IGuiElement first, IGuiElement second) { - int center = width / 2; - IElementGroup panel = new PaneLayout(width, 0); - if (first.getHeight() > second.getHeight()) { - first.setAlign(GuiElementAlignment.MIDDLE_LEFT); - } else if (second.getHeight() > first.getHeight()) { - second.setAlign(GuiElementAlignment.MIDDLE_LEFT); - } - panel.add(first); - panel.add(second); - second.setXPosition(center); - add(panel); - } - - @SuppressWarnings("unchecked") - @Override - public final void addAlleleRow(String chromosomeName, Function toString, IIndividual individual, IChromosomeType chromosome, boolean active) { - A allele; - if (active) { - allele = (A) individual.getGenome().getActiveAllele(chromosome); - } else { - allele = (A) individual.getGenome().getInactiveAllele(chromosome); - } - addRow(chromosomeName, toString.apply(allele), allele.isDominant()); - } - - @Override - public void onScroll(int value) { - scrollPercentage = (value * step); - } - - @Override - public void clear() { - remove(Lists.newArrayList(elements)); - } - - @Override - public boolean isFocused(int mouseX, int mouseY) { - return isMouseOver(mouseX, mouseY); - } - - /*@Override - public int getHeight() { - return sizeY; - }*/ -} diff --git a/src/main/java/forestry/core/gui/elements/layouts/VerticalLayout.java b/src/main/java/forestry/core/gui/elements/layouts/VerticalLayout.java index bc1b38dbe..833678dd4 100644 --- a/src/main/java/forestry/core/gui/elements/layouts/VerticalLayout.java +++ b/src/main/java/forestry/core/gui/elements/layouts/VerticalLayout.java @@ -1,7 +1,11 @@ package forestry.core.gui.elements.layouts; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + import forestry.api.gui.IGuiElement; +@SideOnly(Side.CLIENT) public class VerticalLayout extends AbstractElementLayout { public VerticalLayout(int width) { this(0, 0, width); @@ -11,22 +15,20 @@ public class VerticalLayout extends AbstractElementLayout { super(xPos, yPos, width, 0); } - public void setHeight(int height) { - this.height = height; - } - public E add(E element) { elements.add(element); element.setParent(this); element.setYPosition(height); - height += element.getHeight() + distance; + setHeight(height + (element.getHeight() + distance)); + element.onCreation(); return element; } public E remove(E element) { elements.remove(element); - height -= element.getHeight() + distance; + setHeight(height - (element.getHeight() + distance)); element.setYPosition(0); + element.onDeletion(); return element; } @@ -34,7 +36,7 @@ public class VerticalLayout extends AbstractElementLayout { height = 0; for(IGuiElement element : elements){ element.setYPosition(height); - height += element.getHeight() + distance; + setHeight(height + (element.getHeight() + distance)); } } diff --git a/src/main/java/forestry/core/gui/event/EventValueChanged.java b/src/main/java/forestry/core/gui/event/EventValueChanged.java new file mode 100644 index 000000000..f53844e44 --- /dev/null +++ b/src/main/java/forestry/core/gui/event/EventValueChanged.java @@ -0,0 +1,21 @@ +package forestry.core.gui.event; + +import javax.annotation.Nullable; + +import forestry.api.gui.IGuiElement; +import forestry.api.gui.events.GuiElementEvent; + +public class EventValueChanged extends GuiElementEvent { + @Nullable + private final V value; + + public EventValueChanged(IGuiElement origin, V value) { + super(origin); + this.value = value; + } + + @Nullable + public V getValue() { + return value; + } +} diff --git a/src/main/java/forestry/core/gui/event/package-info.java b/src/main/java/forestry/core/gui/event/package-info.java new file mode 100644 index 000000000..39c1f0565 --- /dev/null +++ b/src/main/java/forestry/core/gui/event/package-info.java @@ -0,0 +1,10 @@ +@ParametersAreNonnullByDefault +@FieldsAreNonnullByDefault +@MethodsReturnNonnullByDefault +package forestry.core.gui.event; + +import javax.annotation.ParametersAreNonnullByDefault; + +import mcp.MethodsReturnNonnullByDefault; + +import forestry.core.utils.FieldsAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/forestry/core/utils/GuiElementUtil.java b/src/main/java/forestry/core/utils/GuiElementUtil.java index fe3c64496..405aba7c6 100644 --- a/src/main/java/forestry/core/utils/GuiElementUtil.java +++ b/src/main/java/forestry/core/utils/GuiElementUtil.java @@ -1,34 +1,32 @@ package forestry.core.utils; -import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.util.text.TextFormatting; -import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IGuiElement; +import forestry.api.gui.style.ITextStyle; public class GuiElementUtil { private GuiElementUtil() { } - public static void preRender(IGuiElement element, GuiElementAlignment align){ - IGuiElement parent = element.getParent(); - int xPos = element.getX(); - int yPos = element.getY(); - if(parent != null){ - if(parent.getWidth() > element.getWidth()){ - xPos += (parent.getWidth() - element.getWidth()) * align.getXOffset(); - } - if (parent.getHeight() > element.getHeight()) { - yPos += (parent.getHeight() - element.getHeight()) * align.getYOffset(); - } + public static String getFormattedString(ITextStyle style, String rawText) { + StringBuilder modifiers = new StringBuilder(); + if (style.isBold()) { + modifiers.append(TextFormatting.BOLD); } - GlStateManager.pushMatrix(); - GlStateManager.translate(xPos, yPos, 0.0F); - GlStateManager.color(1.0F, 1.0F, 1.0F); - - } - - public static void posRender(IGuiElement element){ - GlStateManager.popMatrix(); + if (style.isItalic()) { + modifiers.append(TextFormatting.ITALIC); + } + if (style.isUnderlined()) { + modifiers.append(TextFormatting.UNDERLINE); + } + if (style.isStrikethrough()) { + modifiers.append(TextFormatting.STRIKETHROUGH); + } + if (style.isObfuscated()) { + modifiers.append(TextFormatting.OBFUSCATED); + } + modifiers.append(rawText); + return modifiers.toString(); } } diff --git a/src/main/java/forestry/lepidopterology/genetics/ButterflyDatabaseTab.java b/src/main/java/forestry/lepidopterology/genetics/ButterflyDatabaseTab.java index ac02855d2..f40c83359 100644 --- a/src/main/java/forestry/lepidopterology/genetics/ButterflyDatabaseTab.java +++ b/src/main/java/forestry/lepidopterology/genetics/ButterflyDatabaseTab.java @@ -1,98 +1,108 @@ package forestry.lepidopterology.genetics; +import java.util.function.Function; + import net.minecraft.item.ItemStack; -import net.minecraft.util.text.TextFormatting; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.DatabaseMode; import forestry.api.genetics.IAlleleInteger; -import forestry.api.genetics.IAlleleTolerance; +import forestry.api.genetics.IAlleleSpecies; import forestry.api.genetics.IDatabaseTab; import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; import forestry.api.lepidopterology.EnumButterflyChromosome; import forestry.api.lepidopterology.EnumFlutterType; import forestry.api.lepidopterology.IAlleleButterflySpecies; import forestry.api.lepidopterology.IButterfly; import forestry.core.genetics.GenericRatings; import forestry.core.genetics.alleles.AlleleBoolean; +import forestry.core.gui.elements.GuiElementFactory; import forestry.core.utils.StringUtil; import forestry.core.utils.Translator; @SideOnly(Side.CLIENT) public class ButterflyDatabaseTab implements IDatabaseTab { - private final boolean active; + private final DatabaseMode mode; - ButterflyDatabaseTab(boolean active) { - this.active = active; + ButterflyDatabaseTab(DatabaseMode mode) { + this.mode = mode; } @Override - public void createElements(IElementGenetic container, IButterfly butterfly, ItemStack itemStack) { + public DatabaseMode getMode() { + return mode; + } + + @Override + public void createElements(IDatabaseElement database, IButterfly butterfly, ItemStack itemStack) { IAlleleButterflySpecies primarySpecies = butterfly.getGenome().getPrimary(); + IAlleleButterflySpecies secondarySpecies = butterfly.getGenome().getSecondary(); - container.text(Translator.translateToLocal("for.gui.database.tab." + (active ? "active" : "inactive") + "_species.name"), GuiElementAlignment.TOP_CENTER, 0xcfb53b); + database.label(Translator.translateToLocal("for.gui.database.tab." + (mode == DatabaseMode.ACTIVE ? "active" : "inactive") + "_species.name"), GuiElementAlignment.TOP_CENTER, GuiElementFactory.DATABASE_TITLE); - container.addAlleleRow(Translator.translateToLocal("for.gui.species"), butterfly, EnumButterflyChromosome.SPECIES, active); + database.addLine(Translator.translateToLocal("for.gui.species"), EnumButterflyChromosome.SPECIES); - container.addAlleleRow(Translator.translateToLocal("for.gui.size"), butterfly, EnumButterflyChromosome.SIZE, active); + database.addLine(Translator.translateToLocal("for.gui.size"), EnumButterflyChromosome.SIZE); - container.addAlleleRow(Translator.translateToLocal("for.gui.lifespan"), butterfly, EnumButterflyChromosome.LIFESPAN, active); + database.addLine(Translator.translateToLocal("for.gui.lifespan"), EnumButterflyChromosome.LIFESPAN); - container.addAlleleRow(Translator.translateToLocal("for.gui.speed"), butterfly,EnumButterflyChromosome.SPEED, active); + database.addLine(Translator.translateToLocal("for.gui.speed"),EnumButterflyChromosome.SPEED); - container.addAlleleRow(Translator.translateToLocal("for.gui.metabolism"), (IAlleleInteger a)-> GenericRatings.rateMetabolism(a.getValue()), butterfly, EnumButterflyChromosome.METABOLISM, active); + database.addLine(Translator.translateToLocal("for.gui.metabolism"), (IAlleleInteger allele, Boolean a) -> GenericRatings.rateMetabolism(allele.getValue()), EnumButterflyChromosome.METABOLISM); - IAlleleInteger fertility = (IAlleleInteger) (active ? butterfly.getGenome().getActiveAllele(EnumButterflyChromosome.FERTILITY) : butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.FERTILITY)); - container.addFertilityInfo(Translator.translateToLocal("for.gui.fertility"), fertility, 8); + database.addFertilityLine(Translator.translateToLocal("for.gui.fertility"), EnumButterflyChromosome.FERTILITY, 8); - container.addAlleleRow(Translator.translateToLocal("for.gui.flowers"), butterfly, EnumButterflyChromosome.FLOWER_PROVIDER, active); - container.addAlleleRow(Translator.translateToLocal("for.gui.effect"), butterfly, EnumButterflyChromosome.EFFECT, active); + database.addLine(Translator.translateToLocal("for.gui.flowers"), EnumButterflyChromosome.FLOWER_PROVIDER); + database.addLine(Translator.translateToLocal("for.gui.effect"), EnumButterflyChromosome.EFFECT); - IAlleleTolerance tempTolerance = (IAlleleTolerance) (active ? butterfly.getGenome().getActiveAllele(EnumButterflyChromosome.TEMPERATURE_TOLERANCE) : butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.TEMPERATURE_TOLERANCE)); + Function toleranceText = a ->{ + IAlleleSpecies species = a ? primarySpecies : secondarySpecies; + return AlleleManager.climateHelper.toDisplay(species.getTemperature()); + }; + database.addLine(Translator.translateToLocal("for.gui.climate"), toleranceText, EnumButterflyChromosome.TEMPERATURE_TOLERANCE); + database.addToleranceLine(EnumButterflyChromosome.TEMPERATURE_TOLERANCE); - container.addToleranceInfo(Translator.translateToLocal("for.gui.climate"), tempTolerance, primarySpecies, AlleleManager.climateHelper.toDisplay(primarySpecies.getTemperature())); - - IAlleleTolerance humidTolerance = (IAlleleTolerance) (active ? butterfly.getGenome().getActiveAllele(EnumButterflyChromosome.HUMIDITY_TOLERANCE) : butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.HUMIDITY_TOLERANCE)); - - container.addToleranceInfo(Translator.translateToLocal("for.gui.humidity"), humidTolerance, primarySpecies, AlleleManager.climateHelper.toDisplay(primarySpecies.getHumidity())); + database.addLine(Translator.translateToLocal("for.gui.humidity"), toleranceText, EnumButterflyChromosome.HUMIDITY_TOLERANCE); + database.addToleranceLine(EnumButterflyChromosome.HUMIDITY_TOLERANCE); String yes = Translator.translateToLocal("for.yes"); String no = Translator.translateToLocal("for.no"); - String diurnal, nocturnal; - if(active) { + { + String diurnalFirst; + String diurnalSecond; + String nocturnalFirst; + String nocturnalSecond; if (butterfly.getGenome().getNocturnal()) { - nocturnal = diurnal = yes; + nocturnalFirst = diurnalFirst = yes; } else { - nocturnal = butterfly.getGenome().getPrimary().isNocturnal() ? yes : no; - diurnal = !butterfly.getGenome().getPrimary().isNocturnal() ? yes : no; + nocturnalFirst = butterfly.getGenome().getPrimary().isNocturnal() ? yes : no; + diurnalFirst = !butterfly.getGenome().getPrimary().isNocturnal() ? yes : no; } - }else { if (((AlleleBoolean) butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.NOCTURNAL)).getValue()) { - nocturnal = diurnal = yes; + nocturnalSecond = diurnalSecond = yes; } else { - nocturnal = butterfly.getGenome().getSecondary().isNocturnal() ? yes : no; - diurnal = !butterfly.getGenome().getSecondary().isNocturnal() ? yes : no; + nocturnalSecond = butterfly.getGenome().getSecondary().isNocturnal() ? yes : no; + diurnalSecond = !butterfly.getGenome().getSecondary().isNocturnal() ? yes : no; } + + database.addLine(Translator.translateToLocal("for.gui.diurnal"), (Boolean a) -> a ? diurnalFirst : diurnalSecond, false); + database.addLine(Translator.translateToLocal("for.gui.nocturnal"), (Boolean a) -> a ? nocturnalFirst : nocturnalSecond, false); } - container.text(TextFormatting.UNDERLINE + Translator.translateToLocal("for.gui.diurnal"), GuiElementAlignment.TOP_CENTER); - container.addRow(Translator.translateToLocal("for.gui.diurnal"), diurnal, false); + Function flyer = active -> StringUtil.readableBoolean(active ? butterfly.getGenome().getTolerantFlyer() : ((AlleleBoolean) butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.TOLERANT_FLYER)).getValue(), yes, no); + database.addLine(Translator.translateToLocal("for.gui.flyer"), flyer, EnumButterflyChromosome.TOLERANT_FLYER); - container.addRow(Translator.translateToLocal("for.gui.nocturnal"), nocturnal, false); - - String flyer = StringUtil.readableBoolean(active ? butterfly.getGenome().getTolerantFlyer() : ((AlleleBoolean) butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.TOLERANT_FLYER)).getValue(), yes, no); - container.addAlleleRow(Translator.translateToLocal("for.gui.flyer"), (a) -> flyer, butterfly, EnumButterflyChromosome.TOLERANT_FLYER, active); - - String fireresist = StringUtil.readableBoolean(active ? butterfly.getGenome().getFireResist() : ((AlleleBoolean) butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.FIRE_RESIST)).getValue(), yes, no); - container.addAlleleRow(Translator.translateToLocal("for.gui.fireresist"), (a) -> fireresist, butterfly, EnumButterflyChromosome.FIRE_RESIST, active); + Function fireresist = active -> StringUtil.readableBoolean(active ? butterfly.getGenome().getFireResist() : ((AlleleBoolean) butterfly.getGenome().getInactiveAllele(EnumButterflyChromosome.FIRE_RESIST)).getValue(), yes, no); + database.addLine(Translator.translateToLocal("for.gui.fireresist"), fireresist, EnumButterflyChromosome.FIRE_RESIST); } @Override public ItemStack getIconStack() { - return ButterflyDefinition.BlueWing.getMemberStack(active ? EnumFlutterType.BUTTERFLY : EnumFlutterType.CATERPILLAR); + return ButterflyDefinition.BlueWing.getMemberStack(mode == DatabaseMode.ACTIVE ? EnumFlutterType.BUTTERFLY : EnumFlutterType.CATERPILLAR); } } diff --git a/src/main/java/forestry/lepidopterology/genetics/ButterflyPlugin.java b/src/main/java/forestry/lepidopterology/genetics/ButterflyPlugin.java index 0f598bdda..e47dc2845 100644 --- a/src/main/java/forestry/lepidopterology/genetics/ButterflyPlugin.java +++ b/src/main/java/forestry/lepidopterology/genetics/ButterflyPlugin.java @@ -10,6 +10,7 @@ import net.minecraft.util.NonNullList; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import forestry.api.genetics.DatabaseMode; import forestry.api.lepidopterology.EnumFlutterType; import forestry.api.lepidopterology.IAlleleButterflySpecies; import forestry.api.lepidopterology.IButterfly; @@ -25,8 +26,8 @@ public class ButterflyPlugin extends DatabasePlugin { protected final Map iconStacks = new HashMap<>(); private ButterflyPlugin() { - super(new ButterflyDatabaseTab(true), - new ButterflyDatabaseTab(false), + super(new ButterflyDatabaseTab(DatabaseMode.ACTIVE), + new ButterflyDatabaseTab(DatabaseMode.INACTIVE), new ButterflyProductsTab(), new MutationsTab(()->ButterflyDefinition.Glasswing.getMemberStack(EnumFlutterType.COCOON))); NonNullList butterflyList = NonNullList.create(); diff --git a/src/main/java/forestry/lepidopterology/genetics/ButterflyProductsTab.java b/src/main/java/forestry/lepidopterology/genetics/ButterflyProductsTab.java index fc8619669..ce19e1de9 100644 --- a/src/main/java/forestry/lepidopterology/genetics/ButterflyProductsTab.java +++ b/src/main/java/forestry/lepidopterology/genetics/ButterflyProductsTab.java @@ -9,7 +9,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.genetics.IDatabaseTab; import forestry.api.gui.GuiElementAlignment; -import forestry.api.gui.IElementGenetic; +import forestry.api.gui.IDatabaseElement; import forestry.api.gui.IElementLayoutHelper; import forestry.api.lepidopterology.EnumFlutterType; import forestry.api.lepidopterology.IButterfly; @@ -23,25 +23,25 @@ public class ButterflyProductsTab implements IDatabaseTab { } @Override - public void createElements(IElementGenetic container, IButterfly individual, ItemStack itemStack) { + public void createElements(IDatabaseElement container, IButterfly individual, ItemStack itemStack) { IElementLayoutHelper groupHelper = container.layoutHelper((x, y) -> GuiElementFactory.INSTANCE.createHorizontal(x + 4, y, 18).setDistance(2), 90, 0); Collection butterflyLoot = individual.getGenome().getPrimary().getButterflyLoot().keySet(); if(!butterflyLoot.isEmpty()) { - container.text(Translator.translateToLocal("for.gui.loot.butterfly"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.loot.butterfly"), GuiElementAlignment.TOP_CENTER); butterflyLoot.forEach(stack -> groupHelper.add(new ItemElement(0, 0, stack))); groupHelper.finish(); } Collection caterpillarLoot = individual.getGenome().getPrimary().getCaterpillarLoot().keySet(); if(!caterpillarLoot.isEmpty()){ - container.text(Translator.translateToLocal("for.gui.loot.caterpillar"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.loot.caterpillar"), GuiElementAlignment.TOP_CENTER); caterpillarLoot.forEach(stack -> groupHelper.add(new ItemElement(0, 0, stack))); groupHelper.finish(); } Collection cocoonLoot = individual.getGenome().getCocoon().getCocoonLoot().keySet(); if(!cocoonLoot.isEmpty()){ - container.text(Translator.translateToLocal("for.gui.loot.cocoon"), GuiElementAlignment.TOP_CENTER); + container.label(Translator.translateToLocal("for.gui.loot.cocoon"), GuiElementAlignment.TOP_CENTER); cocoonLoot.forEach(stack -> groupHelper.add(new ItemElement(0, 0, stack))); groupHelper.finish(); } diff --git a/src/main/resources/assets/forestry/textures/gui/research_unused.png b/src/main/resources/assets/forestry/textures/gui/research_unused.png new file mode 100644 index 0000000000000000000000000000000000000000..9ccde3429eee717947bf229098fa5980d7560574 GIT binary patch literal 1334 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6&6|H(?D8gCb z5n0T@z;_sg8IR|$NC676mw5WRvOi*F6;@+BbpPN71_qYHo-U3d6}R5rvF*F%Ai?&a zaLXy)po{K{N_XyS({FlWdsIw@(4dz6V9m|kN8sVf4^66^z%~KRDq5s&C}=Kc~fR>eXYZ~VLwyC{`=CRleWdJ zcji#cF_X4#@fy~aDT(@wPD)Ir(APyw=K)$IU>XS zz~o?o-}TbEr*{M)s?&=g{rjvu_&t zTQD(1vK_EpeQLt_#CaQ-8RTRcoWt2ycUKqG9khGR7O#E|;EW-qEh9hyjYxJIR zT0!I}oCk7PP<-qfEs(Urq?5lGuaw%pNI$%NomQ~<<{76yFPo>% z#o3_gd-qJzyYq(yA1qAwS?wiP&iKreA!X;U-!gp+!t3<5*9orG>c7c4L!Hr}YvVo* z!&#AcHucW^{dvy?rUq-7{^S4u{Vm#QVzGnSqwwqgt>3p1tcNCo4-snpmZQ z|2u}RUECimSsa=m!RGOSFQF^;z?8E(&;Hr495@^{)!u%7k#YA3YC1v6dc&H$4ni^Hzf-JwTq64@D^hyi1Pb#)gw z7EBpTHqZF3cff{|K|y)4Im^W}O7mkIDuoype{en7Tv@~dNf+e|&$r}C?^#+@4bPo(tGyptE;K3{P~W9A@$cAK890YHBTxZ$lBUfq`%>A-qpb8@2q?;pRr}w zr&`T^VcXjU>?N_{+itJ5dUYpuc|-Nm^?eKh>-3p`+MF327Rr59c3@;^6^124jFg%3 zGReLn!q@I9{0@?`F^q(!f0hPCV3ro~a91duwEl2K2}_XrPb(`X+YI)CSq+LMdjiul zWMv!gKmrvxs~{W5!Qc!Gbd3o>=cvTDe~fvId@B}o!yes-B6cCi<28i4~me^^u#7SEd*bEh3-g{P~Z%Q~loCIF&H B*en15 literal 0 HcmV?d00001