Add books WIP

This commit is contained in:
Arthur Uzulin committed 2016-01-26 21:35:28 +11:00
1 parent 2af4286393
commit a7699788ac
45 files changed
+1512 -166

No files matched your search

@@ -4,6 +4,7 @@ import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -31,4 +32,9 @@ public class Mantle {
/* Proxies for sides, used for graphics processing */
@SidedProxy(clientSide = "slimeknights.mantle.client.ClientProxy", serverSide = "slimeknights.mantle.common.CommonProxy")
public static CommonProxy proxy;
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event){
proxy.preInit();
}
}
@@ -1,6 +1,11 @@
package slimeknights.mantle.client;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.common.CommonProxy;
public class ClientProxy extends CommonProxy {
@Override
public void preInit() {
new BookLoader();
}
}
@@ -0,0 +1,108 @@
package slimeknights.mantle.client.book;
import com.google.gson.Gson;
import java.util.HashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.content.*;
import slimeknights.mantle.client.book.data.content.PageContent;
import slimeknights.mantle.client.gui.book.GuiBook;
import static slimeknights.mantle.client.book.ResourceHelper.setBookRoot;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class BookLoader implements IResourceManagerReloadListener {
/** GSON object to be used for book loading purposes */
public static final Gson GSON = new Gson();
/** Maps page content presets to names */
private static final HashMap<String, Class<? extends PageContent>> typeToContentMap = new HashMap<>();
/** Internal registry of all books for the purposes of the reloader, maps books to name */
private static final HashMap<String, BookData> books = new HashMap<>();
public BookLoader() {
((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(this);
registerPageType("blank", ContentBlank.class);
registerPageType("text", ContentText.class);
registerPageType("image", ContentImage.class);
registerPageType("image with text below", ContentImageText.class);
registerPageType("text with image below", ContentTextImage.class);
registerPageType("text with left image etch", ContentTextLeftImage.class);
registerPageType("text with right image etch", ContentTextRightImage.class);
}
/**
* Registers a type of page prefabricate
*
* @param name The name of the page type
* @param clazz The PageContent class for this page type
* @RecommendedInvoke init
*/
public static void registerPageType(String name, Class<? extends PageContent> clazz) {
if (typeToContentMap.containsKey(name))
throw new IllegalArgumentException("Page type " + name + " already in use.");
typeToContentMap.put(name, clazz);
}
/**
* Gets a type of page prefabricate by name
*
* @param name The name of the page type
* @return The class of the page type, ContentError.class if page type not registered
*/
public static Class<? extends PageContent> getPageType(String name) {
return typeToContentMap.getOrDefault(name, ContentError.class);
}
/**
* Adds a book to the loader, and returns a reference object
* Be warned that the returned BookData object is not immediately populated, and is instead populated when the resources are loaded/reloaded
*
* @param name The name of the book, modid: will be automatically appended to the front of the name unless that is already added
* @param location The location of the book folder, prefixed with the resource domain
* @return The book object, not immediately populated
*/
public static BookData registerBook(String name, String location) {
BookData info = new BookData(location);
books.put(name.contains(":") ? name : Loader.instance().activeModContainer().getModId() + ":" + name, info);
return info;
}
/**
* Returns the GuiScreen for the book
*
* @param name The name of the book, prefixed with modid:
* @return The GuiScreen to open for the book
*/
public static GuiBook getBookGui(String name) {
return null;
}
/**
* Reloads all the books, called when the resource manager reloads, such as when the resource pack or the language is changed
*/
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
for (BookData book : books.values()) {
setBookRoot(book.bookLocation);
book.pageCount = book.cascadeLoad();
book.fullPageCount = (int) Math.ceil((book.pageCount - 1) / 2F) + 1;
}
}
}
@@ -0,0 +1,122 @@
package slimeknights.mantle.client.book;
import java.io.IOException;
import java.util.Iterator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResource;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class ResourceHelper {
private static String bookRoot;
private ResourceHelper() {
}
public static ResourceLocation getResourceLocation(String path) {
if (path == null)
return null;
if (!path.contains(":")) {
String langPath = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode();
String defaultLangPath = "en_US";
ResourceLocation res = new ResourceLocation(bookRoot + "/" + langPath + "/" + path);
if (resourceExists(res))
return res;
res = new ResourceLocation(bookRoot + "/" + defaultLangPath + "/" + path);
if (resourceExists(res))
return res;
res = new ResourceLocation(bookRoot + "/" + path);
if (resourceExists(res))
return res;
return null;
} else {
ResourceLocation res = new ResourceLocation(path);
if (resourceExists(res))
return res;
return null;
}
}
public static IResource getResource(ResourceLocation loc) {
if (loc == null)
return null;
try {
return Minecraft.getMinecraft().getResourceManager().getResource(loc);
} catch (IOException e) {
return null;
}
}
public static boolean resourceExists(String location) {
return resourceExists(new ResourceLocation(location));
}
public static boolean resourceExists(ResourceLocation location) {
if (location == null)
return false;
try {
Minecraft.getMinecraft().getResourceManager().getResource(location);
return true;
} catch (IOException e) {
return false;
}
}
public static String resourceToString(IResource resource) {
return resourceToString(resource, true);
}
public static String resourceToString(IResource resource, boolean skipCommments) {
if (resource == null)
return "";
try {
Iterator iterator = IOUtils.readLines(resource.getInputStream(), Charsets.UTF_8).iterator();
StringBuilder builder = new StringBuilder();
boolean isLongComment = false;
while (iterator.hasNext()) {
String s = ((String) iterator.next()).trim();
// Comment skipper
if (skipCommments) {
if (isLongComment) {
if (s.endsWith("*/"))
isLongComment = false;
continue;
} else {
if (s.startsWith("/*")) {
isLongComment = true;
continue;
}
}
if (s.startsWith("//"))
continue;
}
builder.append(s);
}
String data = builder.toString().trim();
return data;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static void setBookRoot(String location) {
bookRoot = location;
}
}
@@ -0,0 +1,67 @@
package slimeknights.mantle.client.book.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation;
import static slimeknights.mantle.client.book.ResourceHelper.resourceExists;
import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class BookData implements IDataItem {
public transient List<SectionData> sections = new ArrayList<SectionData>();
public transient CoverData cover = new CoverData();
public transient int pageCount;
public transient int fullPageCount;
public final transient String bookLocation;
public BookData(String bookLocation) {
this.bookLocation = bookLocation;
}
@Override
public int cascadeLoad() {
int pages = 0;
sections = Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class));
ResourceLocation coverLocation = getResourceLocation("cover.json");
if (resourceExists(coverLocation))
cover = BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("cover.json"))), CoverData.class);
else
cover = new CoverData();
cover.cascadeLoad();
for (SectionData section : sections) {
pages += section.cascadeLoad();
}
cover.cascadeLoad();
return pages;
}
public PageData findPage(int number) {
int pages = 0;
for (SectionData section : sections) {
if (pages + section.pageCount > number)
return section.pages.get(number - pages);
else
pages += section.pageCount;
}
return null;
}
}
@@ -0,0 +1,16 @@
package slimeknights.mantle.client.book.data;
/**
* @author fuj1n
*/
public class CoverData implements IDataItem {
public int color = 0x8B4631;
public String title = "";
public String subtitle = "";
@Override
public int cascadeLoad() {
return 0;
}
}
@@ -0,0 +1,13 @@
package slimeknights.mantle.client.book.data;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public interface IDataItem {
int cascadeLoad();
}
@@ -0,0 +1,58 @@
package slimeknights.mantle.client.book.data;
import java.lang.reflect.Field;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.content.PageContent;
import slimeknights.mantle.client.book.data.element.ImageData;
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation;
import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class PageData implements IDataItem {
public String title;
public String type;
public String data;
public transient PageContent content;
@Override
public int cascadeLoad() {
IResource pageInfo = getResource(getResourceLocation(data));
if (pageInfo != null) {
String data = resourceToString(pageInfo);
if (!data.isEmpty())
content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type));
}
if (content == null)
try {
content = BookLoader.getPageType(type) != null ? BookLoader.getPageType(type).newInstance() : new ContentError();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
for(Field f : content.getClass().getFields()){
if(f.getType().isAssignableFrom(ImageData.class))
try {
f.setAccessible(true);
ImageData d = (ImageData)f.get(content);
d.location = getResourceLocation(d.file);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.println(f.isAccessible());
}
return 0;
}
}
@@ -0,0 +1,50 @@
package slimeknights.mantle.client.book.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.element.CriteriaData;
import slimeknights.mantle.client.book.data.element.ImageData;
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation;
import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class SectionData implements IDataItem {
public String name;
public ImageData icon;
public CriteriaData[] unlockCriteria;
public String data;
public int pageCount;
public transient List<PageData> pages = new ArrayList<PageData>();
@Override
public int cascadeLoad() {
IResource pagesInfo = getResource(getResourceLocation(data));
if (pagesInfo != null) {
String data = resourceToString(pagesInfo);
if (!data.isEmpty())
pages = Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class));
}
for (PageData page : pages) {
page.cascadeLoad();
}
icon.location = getResourceLocation(icon.file);
pageCount = pages.size();
return pageCount;
}
}
@@ -0,0 +1,15 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.gui.book.element.BookElement;
/**
* @author fuj1n
*/
public class ContentBlank extends PageContent{
@Override
public void build(ArrayList<BookElement> list) {
}
}
@@ -0,0 +1,15 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.gui.book.element.BookElement;
/**
* @author fuj1n
*/
public class ContentDummy extends PageContent {
@Override
public void build(ArrayList<BookElement> list) {
//TODO load from JSON
}
}
@@ -0,0 +1,29 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementItem;
import slimeknights.mantle.client.gui.book.element.ElementText;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class ContentError extends PageContent {
@Override
public void build(ArrayList<BookElement> list) {
TextData[] text = new TextData[1];
text[0] = new TextData("Could not load page, perhaps the file is missing or the page type does not exist?");
text[0].color = "dark_red";
text[0].underlined = true;
list.add(new ElementText(15, 15, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 30, text));
}
}
@@ -0,0 +1,23 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
/**
* @author fuj1n
*/
public class ContentImage extends PageContent{
public ImageData image;
@Override
public void build(ArrayList<BookElement> list) {
if(image != null && image.location != null)
list.add(new ElementImage(15, 15, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 30, image));
else
list.add(new ElementImage(image));
}
}
@@ -0,0 +1,29 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementText;
/**
* @author fuj1n
*/
public class ContentImageText extends PageContent {
public ImageData image;
public TextData[] text;
@Override
public void build(ArrayList<BookElement> list) {
if(image != null && image.location != null)
list.add(new ElementImage(15, 15, GuiBook.PAGE_WIDTH - 30, 70, image));
else
list.add(new ElementImage(15, 15, 32, 32, ImageData.MISSING));
if(text != null && text.length > 0)
list.add(new ElementText(15, 90, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 90, text));
}
}
@@ -0,0 +1,23 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementText;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class ContentText extends PageContent {
public TextData[] text;
@Override
public void build(ArrayList<BookElement> list) {
if(text != null && text.length > 0)
list.add(new ElementText(15, 15, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 30, text));
}
}
@@ -0,0 +1,29 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementText;
/**
* @author fuj1n
*/
public class ContentTextImage extends PageContent {
public TextData[] text;
public ImageData image;
@Override
public void build(ArrayList<BookElement> list) {
if(text != null && text.length > 0)
list.add(new ElementText(15, 15, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 95, text));
if(image != null && image.location != null)
list.add(new ElementImage(15, 110, GuiBook.PAGE_WIDTH - 30, 70, image));
else
list.add(new ElementImage(ImageData.MISSING));
}
}
@@ -0,0 +1,33 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementText;
/**
* @author fuj1n
*/
public class ContentTextLeftImage extends PageContent {
public ImageData image;
public TextData[] text1;
public TextData[] text2;
@Override
public void build(ArrayList<BookElement> list) {
if(image != null && image.location != null)
list.add(new ElementImage(15, 15, 50, 50, image));
else
list.add(new ElementImage(15, 15, 50, 50, ImageData.MISSING));
if(text1 != null && text1.length > 0)
list.add(new ElementText(67, 15, GuiBook.PAGE_WIDTH - 82, 50, text1));
if(text2 != null && text2.length > 0)
list.add(new ElementText(15, 70, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 70, text2));
}
}
@@ -0,0 +1,33 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementText;
/**
* @author fuj1n
*/
public class ContentTextRightImage extends PageContent {
public TextData[] text1;
public TextData[] text2;
public ImageData image;
@Override
public void build(ArrayList<BookElement> list) {
if(text1 != null && text1.length > 0)
list.add(new ElementText(15, 15, GuiBook.PAGE_WIDTH - 82, 50, text1));
if(image != null && image.location != null)
list.add(new ElementImage(15 + GuiBook.PAGE_WIDTH - 85, 15, 50, 50, image));
else
list.add(new ElementImage(15 + GuiBook.PAGE_WIDTH - 85, 15, 50, 50, ImageData.MISSING));
if(text2 != null && text2.length > 0)
list.add(new ElementText(15, 70, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 70, text2));
}
}
@@ -0,0 +1,15 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.gui.book.element.BookElement;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public abstract class PageContent {
public abstract void build(ArrayList<BookElement> list);
}
@@ -0,0 +1,12 @@
package slimeknights.mantle.client.book.data.element;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class CriteriaData {
// TODO criteria parsing
}
@@ -0,0 +1,36 @@
package slimeknights.mantle.client.book.data.element;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class ImageData {
public static final ImageData MISSING = new ImageData();
public String file = "";
public int u = 0;
public int v = 0;
public int uw = 256;
public int vh = 256;
public int texWidth = 256;
public int texHeight = 256;
public int x = -1;
public int y = -1;
public int width = -1;
public int height = -1;
public transient ResourceLocation location;
static{
MISSING.location = new ResourceLocation("mantle:textures/gui/missingno.png");
MISSING.texWidth = 32;
MISSING.texHeight = 32;
MISSING.uw = 32;
MISSING.vh = 32;
}
}
@@ -0,0 +1,28 @@
package slimeknights.mantle.client.book.data.element;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class TextData {
public String text;
public String color = "black";
public boolean bold = false;
public boolean italic = false;
public boolean underlined = false;
public boolean strikethrough = false;
public boolean obfuscated = false;
public String onClick = "";
public String[] tooltip = null;
public TextData() {
}
public TextData(String text) {
this.text = text;
}
}
@@ -0,0 +1,45 @@
package slimeknights.mantle.client.gui.book;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class GuiArrow extends GuiButton {
private static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png");
private static final int RIGHT_Y = 0;
private static final int LEFT_Y = 14;
private static final int X = 412;
private static final int HOVER_X = 436;
private static final int WIDTH = 18;
private static final int HEIGHT = 10;
private int direction;
public GuiArrow(int buttonId, int x, int y, int direction) {
super(buttonId, x, y, WIDTH, HEIGHT, "");
this.direction = direction;
}
@Override
public void drawButton(Minecraft mc, int mouseX, int mouseY) {
if (this.visible) {
mc.getTextureManager().bindTexture(TEX_BOOK);
this.hovered = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
GlStateManager.color(1.0F, 1.0F, 1.0F, hovered ? 1.0F : 0.3F);
Gui.drawModalRectWithCustomSizedTexture(xPosition, yPosition, (this.hovered ? HOVER_X : X), (direction == 0 ? RIGHT_Y : LEFT_Y), width, height, 512, 512);
this.mouseDragged(mc, mouseX, mouseY);
}
}
}
@@ -0,0 +1,218 @@
package slimeknights.mantle.client.gui.book;
import java.io.IOException;
import java.util.ArrayList;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.PageData;
import slimeknights.mantle.client.gui.book.element.BookElement;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class GuiBook extends GuiScreen {
private static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png");
private static final ResourceLocation TEX_BOOKFRONT = new ResourceLocation("mantle:textures/gui/bookfront.png");
public static final int PAGE_WIDTH = 206;
public static final int PAGE_HEIGHT = 200;
public static boolean side = false;
private GuiArrow leftArrow, rightArrow;
private final BookData book;
private int page = -1;
private ArrayList<BookElement> leftElements = new ArrayList<>();
private ArrayList<BookElement> rightElements = new ArrayList<>();
//TODO page cache
public GuiBook(BookData book) {
this.book = book;
}
public void drawerTransform(boolean rightSide) {
side = rightSide;
if (rightSide)
GlStateManager.translate(width / 2, height / 2 - PAGE_HEIGHT / 2, 0);
else
GlStateManager.translate(width / 2 - PAGE_WIDTH, height / 2 - PAGE_HEIGHT / 2, 0);
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
FontRenderer font = mc.fontRendererObj;
GlStateManager.pushMatrix();
GlStateManager.color(1F, 1F, 1F);
float coverR = ((book.cover.color >> 16) & 0xff) / 255.F;
float coverG = ((book.cover.color >> 8) & 0xff) / 255.F;
float coverB = (book.cover.color & 0xff) / 255.F;
TextureManager render = this.mc.renderEngine;
if (page == -1) {
render.bindTexture(TEX_BOOKFRONT);
GlStateManager.color(coverR, coverG, coverB);
drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH / 2, height / 2 - PAGE_HEIGHT / 2, 0, 0, PAGE_WIDTH, PAGE_HEIGHT, 512, 512);
GlStateManager.color(1F, 1F, 1F);
if (!book.cover.title.isEmpty()) {
drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH / 2, height / 2 - PAGE_HEIGHT / 2, 0, PAGE_HEIGHT, PAGE_WIDTH, PAGE_HEIGHT, 512, 512);
GlStateManager.pushMatrix();
float scale = font.getStringWidth(book.cover.title) <= 67 ? 2.5F : 2F;
GlStateManager.scale(scale, scale, 1F);
font.drawString(book.cover.title, (width / 2) / scale + 3 - font.getStringWidth(book.cover.title) / 2, (height / 2 - font.FONT_HEIGHT / 2) / scale - 4, 0xAE8000, true);
GlStateManager.popMatrix();
}
if (!book.cover.subtitle.isEmpty()) {
GlStateManager.pushMatrix();
GlStateManager.scale(1.5F, 1.5F, 1F);
font.drawString(book.cover.subtitle, (width / 2) / 1.5F + 7 - font.getStringWidth(book.cover.subtitle) / 2, (height / 2 + 100 - font.FONT_HEIGHT * 2) / 1.5F, 0xAE8000, true);
GlStateManager.popMatrix();
}
} else {
render.bindTexture(TEX_BOOK);
GlStateManager.color(coverR, coverG, coverB);
drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH, height / 2 - PAGE_HEIGHT / 2, 0, 0, PAGE_WIDTH * 2, PAGE_HEIGHT, 512, 512);
GlStateManager.color(1F, 1F, 1F);
if (page != 0) {
drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH, height / 2 - PAGE_HEIGHT / 2, 0, PAGE_HEIGHT, PAGE_WIDTH, PAGE_HEIGHT, 512, 512);
GlStateManager.pushMatrix();
drawerTransform(false);
for (BookElement element : leftElements) {
GlStateManager.color(1F, 1F, 1F, 1F);
element.draw(Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH), this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT / 2), partialTicks);
}
GlStateManager.popMatrix();
}
// Rebind texture as the font renderer binds its own texture
render.bindTexture(TEX_BOOK);
// Set color back to white
GlStateManager.color(1F, 1F, 1F, 1F);
if (!(page == book.fullPageCount - 1 && book.pageCount % 2 == 0)) {
drawModalRectWithCustomSizedTexture(width / 2, height / 2 - PAGE_HEIGHT / 2, PAGE_WIDTH, PAGE_HEIGHT, PAGE_WIDTH, PAGE_HEIGHT, 512, 512);
GlStateManager.pushMatrix();
drawerTransform(true);
for (BookElement element : rightElements) {
GlStateManager.color(1F, 1F, 1F, 1F);
element.draw(Mouse.getX() * this.width / this.mc.displayWidth - (width / 2), this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT / 2), partialTicks);
}
GlStateManager.popMatrix();
}
}
super.drawScreen(mouseX, mouseY, partialTicks);
GlStateManager.popMatrix();
}
private void buildPages() {
leftElements.clear();
rightElements.clear();
if (page == -1)
return;
if (page == 0) {
PageData page = book.findPage(0);
if (page != null)
page.content.build(rightElements);
} else {
PageData leftPage = book.findPage((page - 1) * 2 + 1);
PageData rightPage = book.findPage((page - 1) * 2 + 2);
if (leftPage != null)
leftPage.content.build(leftElements);
if (rightPage != null)
rightPage.content.build(rightElements);
}
}
@Override
public void initGui() {
super.initGui();
leftArrow = new GuiArrow(0, -50, -50, 1);
rightArrow = new GuiArrow(1, -50, -50, 0);
buttonList.add(leftArrow);
buttonList.add(rightArrow);
buildPages();
}
@Override
public void updateScreen() {
super.updateScreen();
leftArrow.visible = page != -1;
rightArrow.visible = page != book.fullPageCount - 1;
if (page == -1) {
rightArrow.xPosition = width / 2 + 80;
} else {
leftArrow.xPosition = width / 2 - 190;
rightArrow.xPosition = width / 2 + 170;
}
leftArrow.yPosition = height / 2 + 89;
rightArrow.yPosition = height / 2 + 89;
}
@Override
public void actionPerformed(GuiButton button) {
if (button == leftArrow) {
page--;
if (page < -1)
page = -1;
} else if (button == rightArrow) {
page++;
if (page > book.fullPageCount - 1)
page = book.fullPageCount - 1;
}
buildPages();
}
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
super.keyTyped(typedChar, keyCode);
if (keyCode == Keyboard.KEY_LEFT || keyCode == Keyboard.KEY_A)
actionPerformed(leftArrow);
else if (keyCode == Keyboard.KEY_RIGHT || keyCode == Keyboard.KEY_D)
actionPerformed(rightArrow);
}
}
@@ -0,0 +1,238 @@
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.WorldRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.element.TextData;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class TextDataRenderer {
private static String[] drawLabel;
public static String drawText(int x, int y, int boxWidth, int boxHeight, TextData[] data, int mouseX, int mouseY) {
String action = "";
drawLabel = null;
FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
int atX = x;
int atY = y;
for (TextData item : data) {
int box1X = 9999, box1Y = 9999, box1W = 9999, box1H = y + fr.FONT_HEIGHT;
int box2X = 9999, box2Y = 9999, box2W = 9999, box2H = 9999;
int box3X = 9999, box3Y = 9999, box3W = 9999, box3H = 9999;
if (item.text == null || item.text.isEmpty())
continue;
String modifiers = "";
modifiers += EnumChatFormatting.getValueByName(item.color);
if (item.bold)
modifiers += EnumChatFormatting.BOLD;
if (item.italic)
modifiers += EnumChatFormatting.ITALIC;
if (item.underlined)
modifiers += EnumChatFormatting.UNDERLINE;
if (item.strikethrough)
modifiers += EnumChatFormatting.STRIKETHROUGH;
if (item.obfuscated)
modifiers += EnumChatFormatting.OBFUSCATED;
String[] split = cropStringBySize(item.text, modifiers, boxWidth, boxHeight - (atY - y), boxWidth - (atX - x), fr);
box1X = atX;
box1Y = atY;
box2X = x;
box2W = x + boxWidth;
for (int i = 0; i < split.length; i++) {
if (i == split.length - 1) {
box3X = atX;
box3Y = atY;
}
String s = split[i];
fr.drawString(modifiers + s, atX, atY, 0, false);
if (i < split.length - 1) {
atY += fr.FONT_HEIGHT;
atX = x;
}
if (i == 0) {
box2Y = atY;
if (atX == x)
box1W = x + boxWidth;
else
box1W = atX;
}
}
box2H = atY;
atX += fr.getStringWidth(split[split.length - 1]);
if (atX - x >= boxWidth) {
atX = x;
atY += fr.FONT_HEIGHT;
}
box3W = atX;
box3H = atY + fr.FONT_HEIGHT;
if (item.tooltip != null && item.tooltip.length > 0) {
if ((mouseX >= box1X && mouseX <= box1W && mouseY >= box1Y && mouseY <= box1H) || (mouseX >= box2X && mouseX <= box2W && mouseY >= box2Y && mouseY <= box2H) || (mouseX >= box3X && mouseX <= box3W && mouseY >= box3Y && mouseY <= box3H)) {
drawLabel = item.tooltip;
action = item.onClick;
}
}
if (atY >= boxHeight) {
fr.drawString("...", atX, atY, 0, false);
break;
}
y = atY;
}
if (drawLabel != null)
drawHoveringText(drawLabel, mouseX, mouseY, fr);
return action;
}
public static String[] cropStringBySize(String s, String modifiers, int width, int height, FontRenderer fr) {
return cropStringBySize(s, modifiers, width, height, width, fr);
}
public static String[] cropStringBySize(String s, String modifiers, int width, int height, int firstWidth, FontRenderer fr) {
int curWidth = 0;
int curHeight = fr.FONT_HEIGHT;
for (int i = 0; i < s.length(); i++) {
curWidth += fr.getStringWidth(modifiers + Character.toString(s.charAt(i)));
if ((curHeight == fr.FONT_HEIGHT && curWidth > firstWidth) || (curHeight != fr.FONT_HEIGHT && curWidth > width)) {
int oldI = i;
while (i >= 0 && s.charAt(i) != ' ')
i--;
if (i <= 0)
i = oldI;
s = s.substring(0, i) + "\r" + s.substring(i + 1);
i++;
curWidth = 0;
curHeight += fr.FONT_HEIGHT;
if (curHeight >= height)
return s.substring(0, i - 1).split("\r");
}
}
return s.split("\r");
}
//BEGIN METHODS FROM GUI
private static void drawHoveringText(String[] textLines, int x, int y, FontRenderer font) {
if (textLines.length > 0) {
GlStateManager.pushAttrib();
GlStateManager.disableRescaleNormal();
GlStateManager.disableLighting();
GlStateManager.disableDepth();
int i = 0;
for (String s : textLines) {
int j = font.getStringWidth(s);
if (j > i) {
i = j;
}
}
int l1 = x + 12;
int i2 = y - 12;
int k = 8;
if (textLines.length > 1) {
k += 2 + (textLines.length - 1) * 10;
}
if (l1 + i > GuiBook.PAGE_WIDTH) {
l1 -= 28 + i;
}
if (i2 + k + 6 > GuiBook.PAGE_HEIGHT) {
i2 = GuiBook.PAGE_HEIGHT - k - 6;
}
int l = -267386864;
drawGradientRect(l1 - 3, i2 - 4, l1 + i + 3, i2 - 3, l, l);
drawGradientRect(l1 - 3, i2 + k + 3, l1 + i + 3, i2 + k + 4, l, l);
drawGradientRect(l1 - 3, i2 - 3, l1 + i + 3, i2 + k + 3, l, l);
drawGradientRect(l1 - 4, i2 - 3, l1 - 3, i2 + k + 3, l, l);
drawGradientRect(l1 + i + 3, i2 - 3, l1 + i + 4, i2 + k + 3, l, l);
int i1 = 1347420415;
int j1 = (i1 & 16711422) >> 1 | i1 & -16777216;
drawGradientRect(l1 - 3, i2 - 3 + 1, l1 - 3 + 1, i2 + k + 3 - 1, i1, j1);
drawGradientRect(l1 + i + 2, i2 - 3 + 1, l1 + i + 3, i2 + k + 3 - 1, i1, j1);
drawGradientRect(l1 - 3, i2 - 3, l1 + i + 3, i2 - 3 + 1, i1, i1);
drawGradientRect(l1 - 3, i2 + k + 2, l1 + i + 3, i2 + k + 3, j1, j1);
for (int k1 = 0; k1 < textLines.length; ++k1) {
String s1 = textLines[k1];
font.drawStringWithShadow(s1, (float) l1, (float) i2, -1);
if (k1 == 0) {
i2 += 2;
}
i2 += 10;
}
GlStateManager.enableLighting();
GlStateManager.enableDepth();
GlStateManager.popAttrib();
}
}
private static void drawGradientRect(int left, int top, int right, int bottom, int startColor, int endColor) {
float f = (float) (startColor >> 24 & 255) / 255.0F;
float f1 = (float) (startColor >> 16 & 255) / 255.0F;
float f2 = (float) (startColor >> 8 & 255) / 255.0F;
float f3 = (float) (startColor & 255) / 255.0F;
float f4 = (float) (endColor >> 24 & 255) / 255.0F;
float f5 = (float) (endColor >> 16 & 255) / 255.0F;
float f6 = (float) (endColor >> 8 & 255) / 255.0F;
float f7 = (float) (endColor & 255) / 255.0F;
GlStateManager.disableTexture2D();
GlStateManager.disableAlpha();
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
GlStateManager.shadeModel(7425);
Tessellator tessellator = Tessellator.getInstance();
WorldRenderer worldrenderer = tessellator.getWorldRenderer();
worldrenderer.begin(7, DefaultVertexFormats.POSITION_COLOR);
worldrenderer.pos((double) right, (double) top, 0D).color(f1, f2, f3, f).endVertex();
worldrenderer.pos((double) left, (double) top, 0D).color(f1, f2, f3, f).endVertex();
worldrenderer.pos((double) left, (double) bottom, 0D).color(f5, f6, f7, f4).endVertex();
worldrenderer.pos((double) right, (double) bottom, 0D).color(f5, f6, f7, f4).endVertex();
tessellator.draw();
GlStateManager.shadeModel(7424);
GlStateManager.enableAlpha();
GlStateManager.enableTexture2D();
}
//END METHODS FROM GUI
}
@@ -0,0 +1,21 @@
package slimeknights.mantle.client.gui.book.element;
import net.minecraft.client.gui.Gui;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public abstract class BookElement extends Gui {
public int x, y;
public BookElement(int x, int y) {
this.x = x;
this.y = y;
}
public abstract void draw(int mouseX, int mouseY, float partialTicks);
}
@@ -0,0 +1,38 @@
package slimeknights.mantle.client.gui.book.element;
import net.minecraft.client.Minecraft;
import slimeknights.mantle.client.book.data.element.ImageData;
/**
* @author fuj1n
*/
public class ElementImage extends SizedBookElement{
public ImageData image;
public ElementImage(ImageData image){
this(image.x, image.y, image.width, image.height, image);
}
public ElementImage(int x, int y, int width, int height, ImageData image) {
super(x, y, width, height);
this.image = image;
if(image.x != -1)
x = image.x;
if(image.y != -1)
y = image.y;
if(image.width != -1)
width = image.width;
if(image.height != -1)
height = image.height;
}
@Override
public void draw(int mouseX, int mouseY, float partialTicks) {
Minecraft.getMinecraft().renderEngine.bindTexture(image.location);
drawScaledCustomSizeModalRect(x, y, image.u, image.v, image.uw, image.vh, width, height, image.texWidth, image.texHeight);
}
}
@@ -0,0 +1,33 @@
package slimeknights.mantle.client.gui.book.element;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
/**
* @author fuj1n
*/
public class ElementItem extends BookElement {
public ItemStack item;
public ElementItem(int x, int y, Item item){
this(x, y, new ItemStack(item));
}
public ElementItem(int x, int y, Block item){
this(x, y, new ItemStack(item));
}
public ElementItem(int x, int y, ItemStack item) {
super(x, y);
this.item = item;
}
@Override
public void draw(int mouseX, int mouseY, float partialTicks) {
Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(item, x, y);
}
}
@@ -0,0 +1,30 @@
package slimeknights.mantle.client.gui.book.element;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.TextDataRenderer;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public class ElementText extends SizedBookElement {
public TextData[] text;
public ElementText(int x, int y, int width, int height, String text) {
this(x, y, width, height, new TextData[]{new TextData(text)});
}
public ElementText(int x, int y, int width, int height, TextData[] text) {
super(x, y, width, height);
this.text = text;
}
@Override
public void draw(int mouseX, int mouseY, float partialTicks) {
TextDataRenderer.drawText(x, y, width, height, text, mouseX, mouseY);
}
}
@@ -0,0 +1,20 @@
package slimeknights.mantle.client.gui.book.element;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
/**
* @author fuj1n
*/
@SideOnly(Side.CLIENT)
public abstract class SizedBookElement extends BookElement {
int width, height;
public SizedBookElement(int x, int y, int width, int height) {
super(x, y);
this.width = width;
this.height = height;
}
}
@@ -1,4 +1,6 @@
package slimeknights.mantle.common;
public class CommonProxy {
public void preInit() {
}
}
@@ -209,7 +209,7 @@ public class PulseManager {
}
private void preInit(FMLPreInitializationEvent evt) {
if (!blockNewRegistrations) conf.flush(); // First preInit call, so flush config
if (!blockNewRegistrations) conf.flush(); // First init call, so flush config
blockNewRegistrations = true;
}