Convert elements with fixed size to SizedBookElements, add a hover-util-method to it. Allow custom tooltips for item elements

This commit is contained in:
Bernhard Bonigl committed 2016-04-11 19:14:58 +02:00
1 parent 96fc4ec5fd
commit 666187612a
4 files changed
+24 -14

No files matched your search

@@ -6,15 +6,17 @@ import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.Collection;
import java.util.List;
import slimeknights.mantle.client.book.action.StringActionProcessor;
@SideOnly(Side.CLIENT)
public class ElementItem extends BookElement {
public class ElementItem extends SizedBookElement {
public static final int ITEM_SIZE_HARDCODED = 16;
public static final int ITEM_SWITCH_TICKS = 90;
@@ -22,6 +24,7 @@ public class ElementItem extends BookElement {
public ItemStack[] itemCycle;
public float scale;
public String action;
public List<String> tooltip;
public int renderTick = 0;
public int currentItem = 0;
@@ -51,7 +54,7 @@ public class ElementItem extends BookElement {
}
public ElementItem(int x, int y, float scale, ItemStack[] itemCycle, String action) {
super(x, y);
super(x, y, MathHelper.floor_float(ITEM_SIZE_HARDCODED * scale), MathHelper.floor_float(ITEM_SIZE_HARDCODED * scale));
this.itemCycle = itemCycle;
this.scale = scale;
@@ -85,14 +88,19 @@ public class ElementItem extends BookElement {
@Override
public void drawOverlay(int mouseX, int mouseY, float partialTicks, FontRenderer fontRenderer) {
if(mouseX >= x && mouseY >= y && mouseX <= x + ITEM_SIZE_HARDCODED * scale && mouseY <= y + ITEM_SIZE_HARDCODED * scale && currentItem < itemCycle.length) {
renderToolTip(fontRenderer, itemCycle[currentItem], mouseX, mouseY);
if(isHovered(mouseX, mouseY) && currentItem < itemCycle.length) {
if(tooltip != null) {
drawHoveringText(tooltip, mouseX, mouseY, fontRenderer);
}
else {
renderToolTip(fontRenderer, itemCycle[currentItem], mouseX, mouseY);
}
}
}
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
if(mouseButton == 0 && mouseX >= x && mouseY >= y && mouseX <= x + ITEM_SIZE_HARDCODED * scale && mouseY <= y + ITEM_SIZE_HARDCODED * scale && currentItem < itemCycle.length) {
if(mouseButton == 0 && isHovered(mouseX, mouseY) && currentItem < itemCycle.length) {
if(action != null) {
StringActionProcessor.process(action, parent);
} else {
@@ -12,7 +12,7 @@ import java.util.List;
import slimeknights.mantle.client.book.data.SectionData;
public class ElementSection extends BookElement {
public class ElementSection extends SizedBookElement {
public static final int IMG_SIZE = 64;
@@ -22,7 +22,7 @@ public class ElementSection extends BookElement {
private SectionData section;
public ElementSection(int x, int y, SectionData section) {
super(x, y);
super(x, y, WIDTH, HEIGHT);
this.section = section;
}
@@ -30,7 +30,7 @@ public class ElementSection extends BookElement {
@Override
public void draw(int mouseX, int mouseY, float partialTicks, FontRenderer fontRenderer) {
boolean unlocked = section.isUnlocked(parent.statFile);
boolean hover = mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT;
boolean hover = isHovered(mouseX, mouseY);
if(section.icon != null) {
if(unlocked) {
@@ -65,8 +65,7 @@ public class ElementSection extends BookElement {
@Override
public void drawOverlay(int mouseX, int mouseY, float partialTicks, FontRenderer fontRenderer) {
if(section != null && !section
.isUnlocked(parent.statFile) && mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT) {
if(section != null && !section.isUnlocked(parent.statFile) && isHovered(mouseX, mouseY)) {
List<String> l = new ArrayList<>();
l.add(TextFormatting.RED + "Locked");
l.add("Requirements:");
@@ -85,8 +84,7 @@ public class ElementSection extends BookElement {
}
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
if(mouseButton == 0 && section != null && section
.isUnlocked(parent.statFile) && mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT) {
if(mouseButton == 0 && section != null && section.isUnlocked(parent.statFile) && isHovered(mouseX, mouseY)) {
parent.openPage(parent.book.getFirstPageNumber(section, parent.statFile));
}
}
@@ -6,7 +6,7 @@ import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public abstract class SizedBookElement extends BookElement {
int width, height;
protected int width, height;
public SizedBookElement(int x, int y, int width, int height) {
super(x, y);
@@ -14,4 +14,8 @@ public abstract class SizedBookElement extends BookElement {
this.width = width;
this.height = height;
}
public boolean isHovered(int mouseX, int mouseY) {
return mouseX > x && mouseY > y && mouseX < x + width && mouseY < y + height;
}
}