Skip to content

Commit

Permalink
Added book functionality
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
fuj1n committed Apr 11, 2016
1 parent 91be353 commit 0d75474
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -144,7 +146,8 @@ public String getTitle() {
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>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
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 {
Expand All @@ -169,7 +171,8 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {

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
Expand Down Expand Up @@ -212,7 +215,8 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -62,7 +62,9 @@ public static String drawText(int x, int y, int boxWidth, int boxHeight, TextDat
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;
Expand Down Expand Up @@ -151,6 +153,22 @@ public static String drawText(int x, int y, int boxWidth, int boxHeight, TextDat
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);
}
Expand All @@ -171,19 +189,19 @@ public static String[] cropStringBySize(String s, String modifiers, int width, i
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;

Expand Down Expand Up @@ -55,14 +56,23 @@ public ElementImage(int x, int y, int width, int height, ImageData image, int co

@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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;

Expand Down Expand Up @@ -32,8 +33,6 @@ public void draw(int mouseX, int mouseY, float partialTicks, FontRenderer fontRe
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 {
Expand All @@ -43,7 +42,21 @@ public void draw(int mouseX, int mouseY, float partialTicks, FontRenderer fontRe
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"scale": 2
},
{
"text": "For I am the wizard that you seek!",
"text": "For I am the wizard that you seek! $(language.name)$",
"paragraph": true
}
]
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/mantle/books/TestBook/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
{
"name": "section 2",
"icon": {
"file": "images/test.png"
"item": {
"id": "minecraft:command_block"
}
},
"data": "sections/section1.json",
"requirements": [
Expand Down

0 comments on commit 0d75474

Please sign in to comment.