Added book functionality

- Added translation strings to TextData using $( and )$ [$$( and )$$ to escape]
- Added the ability to use items for ImageData (TODO scale)
- Added the ability to use the block atlas as the file for ImageData using $BLOCK_ATLAS (doubtful of possible uses)
This commit is contained in:
Arthur Uzulin committed 2016-04-11 22:23:34 +10:00
1 parent 91be353746
commit 0d754749ec
9 files changed
+71 -19

No files matched your search

@@ -10,6 +10,7 @@ public class AppearanceData implements IDataItem {
public int arrowColor = 0xFFFFD3;
public int arrowColorHover = 0xFF541C;
public int lockedSectionColor = 0x000000;
public float scale = 0.5F;
public String title = "";
public String subtitle = "";
public boolean drawPageNumbers = true;
@@ -1,5 +1,6 @@
package slimeknights.mantle.client.book.data;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -24,6 +25,7 @@ public class PageData implements IDataItem {
public String name = null;
public String type = "";
public String data = "";
public float scale = 1.0F;
public transient SectionData parent;
public transient BookRepository source;
@@ -144,7 +146,8 @@ public class PageData implements IDataItem {
addSwap(DataLocation.class, new ValueHotswap<DataLocation>() {
@Override
public void swap(BookRepository source, DataLocation object) {
object.location = source.getResourceLocation(object.file, true);
object.location = object.file == "$BLOCK_ATLAS" ? TextureMap.locationBlocksTexture : source
.getResourceLocation(object.file, true);
}
}.getClass());
addSwap(ItemStackData.class, new ValueHotswap<ItemStackData>() {
@@ -9,6 +9,7 @@ public class ImageData extends DataLocation {
public static final ImageData MISSING = new ImageData();
public ItemStackData item = null;
public int u = 0;
public int v = 0;
public int uw = 256;
@@ -141,14 +141,16 @@ public class GuiBook extends GuiScreen implements IProgressMeter {
float scale = fontRenderer.getStringWidth(book.appearance.title) <= 67 ? 2.5F : 2F;
GlStateManager.scale(scale, scale, 1F);
fontRenderer.drawString(book.appearance.title, (width / 2) / scale + 3 - fontRenderer.getStringWidth(book.appearance.title) / 2, (height / 2 - fontRenderer.FONT_HEIGHT / 2) / scale - 4, 0xAE8000, true);
fontRenderer.drawString(book.appearance.title, (width / 2) / scale + 3 - fontRenderer
.getStringWidth(book.appearance.title) / 2, (height / 2 - fontRenderer.FONT_HEIGHT / 2) / scale - 4, 0xAE8000, true);
GlStateManager.popMatrix();
}
if(!book.appearance.subtitle.isEmpty()) {
GlStateManager.pushMatrix();
GlStateManager.scale(1.5F, 1.5F, 1F);
fontRenderer.drawString(book.appearance.subtitle, (width / 2) / 1.5F + 7 - fontRenderer.getStringWidth(book.appearance.subtitle) / 2, (height / 2 + 100 - fontRenderer.FONT_HEIGHT * 2) / 1.5F, 0xAE8000, true);
fontRenderer.drawString(book.appearance.subtitle, (width / 2) / 1.5F + 7 - fontRenderer
.getStringWidth(book.appearance.subtitle) / 2, (height / 2 + 100 - fontRenderer.FONT_HEIGHT * 2) / 1.5F, 0xAE8000, true);
GlStateManager.popMatrix();
}
} else {
@@ -169,7 +171,8 @@ public class GuiBook extends GuiScreen implements IProgressMeter {
if(book.appearance.drawPageNumbers) {
String pNum = (page - 1) * 2 + 2 + "";
fontRenderer.drawString(pNum, PAGE_WIDTH / 2 - fontRenderer.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false);
fontRenderer.drawString(pNum, PAGE_WIDTH / 2 - fontRenderer
.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false);
}
int mX = (int) ((Mouse
@@ -212,7 +215,8 @@ public class GuiBook extends GuiScreen implements IProgressMeter {
if(book.appearance.drawPageNumbers) {
String pNum = (page - 1) * 2 + 3 + "";
fontRenderer.drawString(pNum, PAGE_WIDTH / 2 - fontRenderer.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false);
fontRenderer.drawString(pNum, PAGE_WIDTH / 2 - fontRenderer
.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false);
}
int mX = (int) ((Mouse
@@ -1,16 +1,16 @@
package slimeknights.mantle.client.gui.book;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.VertexBuffer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.translation.I18n;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javafx.scene.effect.DropShadow;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.client.book.data.element.TextData;
@SideOnly(Side.CLIENT)
@@ -62,7 +62,9 @@ public class TextDataRenderer {
modifiers += TextFormatting.OBFUSCATED;
}
String[] split = cropStringBySize(item.text, modifiers, boxWidth, boxHeight - (atY - y), boxWidth - (atX - x), fr, item.scale);
String text = translateString(item.text);
String[] split = cropStringBySize(text, modifiers, boxWidth, boxHeight - (atY - y), boxWidth - (atX - x), fr, item.scale);
box1X = atX;
box1Y = atY;
@@ -151,6 +153,22 @@ public class TextDataRenderer {
return action;
}
public static String translateString(String s) {
s = s.replace("$$(", "$\0(").replace(")$$", ")\0$");
while(s.contains("$(") && s.contains(")$") && s.indexOf("$(") < s.indexOf(")$")) {
String loc = s.substring(s.indexOf("$(") + 2, s.indexOf(")$"));
s = s.replace("$(" + loc + ")$", I18n.translateToLocal(loc));
}
if(s.indexOf("$(") > s.indexOf(")$") || s.contains(")$")) {
Mantle.logger
.error("[Books] [TextDataRenderer] Detected unbalanced localization symbols \"$(\" and \")$\" in string: \"" + s + "\".");
}
return s.replace("$\0(", "$(").replace(")\0$", ")$");
}
public static String[] cropStringBySize(String s, String modifiers, int width, int height, FontRenderer fr, float scale) {
return cropStringBySize(s, modifiers, width, height, width, fr, scale);
}
@@ -171,19 +189,19 @@ public class TextDataRenderer {
i = oldI;
}
s = s.substring(0, i) + "\r" + s.substring(i + (i == oldI ? 0 : 1));
s = s.substring(0, i).trim() + "\r" + s.substring(i + (i == oldI ? 0 : 1)).trim();
i++;
curWidth = 0;
curHeight += fr.FONT_HEIGHT * scale;
if(curHeight >= height) {
return s.substring(0, i).split("\r");
return s.substring(0, i).trim().split("\r");
}
}
}
return s.split("\r");
return s.trim().split("\r");
}
//BEGIN METHODS FROM GUI
@@ -2,6 +2,7 @@ package slimeknights.mantle.client.gui.book.element;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -55,14 +56,23 @@ public class ElementImage extends SizedBookElement {
@Override
public void draw(int mouseX, int mouseY, float partialTicks, FontRenderer fontRenderer) {
renderEngine.bindTexture(image.location);
float r = ((colorMultiplier >> 16) & 0xff) / 255.F;
float g = ((colorMultiplier >> 8) & 0xff) / 255.F;
float b = (colorMultiplier & 0xff) / 255.F;
GlStateManager.color(r, g, b);
drawScaledCustomSizeModalRect(x, y, image.u, image.v, image.uw, image.vh, width, height, image.texWidth, image.texHeight);
if(image.item == null) {
renderEngine.bindTexture(image.location);
drawScaledCustomSizeModalRect(x, y, image.u, image.v, image.uw, image.vh, width, height, image.texWidth, image.texHeight);
} else {
GlStateManager.pushMatrix();
GlStateManager.scale(width, height, 1F);
RenderHelper.enableGUIStandardItemLighting();
mc.getRenderItem().renderItemAndEffectIntoGUI(image.item.getItems()[0], x, y);
RenderHelper.disableStandardItemLighting();
GlStateManager.popMatrix();
}
}
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.gui.book.element;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.stats.Achievement;
import net.minecraft.util.text.TextFormatting;
@@ -32,8 +33,6 @@ public class ElementSection extends BookElement {
boolean hover = mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT;
if(section.icon != null) {
renderEngine.bindTexture(section.icon.location);
if(unlocked) {
GlStateManager.color(1F, 1F, 1F, hover ? 1F : 0.5F);
} else {
@@ -43,7 +42,21 @@ public class ElementSection extends BookElement {
GlStateManager.color(r, g, b, 0.75F);
}
drawScaledCustomSizeModalRect(x + WIDTH / 2 - IMG_SIZE / 2, y + HEIGHT / 2 - IMG_SIZE / 2, section.icon.u, section.icon.v, section.icon.uw, section.icon.vh, IMG_SIZE, IMG_SIZE, section.icon.texWidth, section.icon.texHeight);
if(section.icon.item == null) {
if(section.icon.location != null) {
renderEngine.bindTexture(section.icon.location);
drawScaledCustomSizeModalRect(x + WIDTH / 2 - IMG_SIZE / 2, y + HEIGHT / 2 - IMG_SIZE / 2, section.icon.u, section.icon.v, section.icon.uw, section.icon.vh, IMG_SIZE, IMG_SIZE, section.icon.texWidth, section.icon.texHeight);
}
} else {
GlStateManager.pushMatrix();
GlStateManager.translate(x + WIDTH / 2 - IMG_SIZE / 2, y + HEIGHT / 2 - IMG_SIZE / 2, 0);
GlStateManager.scale(4F, 4F, 1F);
RenderHelper.enableGUIStandardItemLighting();
mc.getRenderItem().renderItemAndEffectIntoGUI(section.icon.item.getItems()[0], 0, 0);
RenderHelper.disableStandardItemLighting();
GlStateManager.popMatrix();
}
}
fontRenderer.drawString(section.getTitle(), x + WIDTH / 2 - fontRenderer.getStringWidth(section