diff --git a/src/main/java/slimeknights/mantle/Mantle.java b/src/main/java/slimeknights/mantle/Mantle.java index 715843b..683267b 100644 --- a/src/main/java/slimeknights/mantle/Mantle.java +++ b/src/main/java/slimeknights/mantle/Mantle.java @@ -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(); + } } diff --git a/src/main/java/slimeknights/mantle/client/ClientProxy.java b/src/main/java/slimeknights/mantle/client/ClientProxy.java index fb018d3..cfabbca 100644 --- a/src/main/java/slimeknights/mantle/client/ClientProxy.java +++ b/src/main/java/slimeknights/mantle/client/ClientProxy.java @@ -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(); + } } diff --git a/src/main/java/slimeknights/mantle/client/book/BookLoader.java b/src/main/java/slimeknights/mantle/client/book/BookLoader.java new file mode 100644 index 0000000..a552599 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/BookLoader.java @@ -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> typeToContentMap = new HashMap<>(); + + /** Internal registry of all books for the purposes of the reloader, maps books to name */ + private static final HashMap 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 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 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; + } + } +} \ No newline at end of file diff --git a/src/main/java/slimeknights/mantle/client/book/ResourceHelper.java b/src/main/java/slimeknights/mantle/client/book/ResourceHelper.java new file mode 100644 index 0000000..bb3df35 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/ResourceHelper.java @@ -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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/BookData.java b/src/main/java/slimeknights/mantle/client/book/data/BookData.java new file mode 100644 index 0000000..36e879c --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/BookData.java @@ -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 sections = new ArrayList(); + 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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/CoverData.java b/src/main/java/slimeknights/mantle/client/book/data/CoverData.java new file mode 100644 index 0000000..739d7e0 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/CoverData.java @@ -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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/IDataItem.java b/src/main/java/slimeknights/mantle/client/book/data/IDataItem.java new file mode 100644 index 0000000..0dbe054 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/IDataItem.java @@ -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(); +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/PageData.java b/src/main/java/slimeknights/mantle/client/book/data/PageData.java new file mode 100644 index 0000000..6bd9f36 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/PageData.java @@ -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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/SectionData.java b/src/main/java/slimeknights/mantle/client/book/data/SectionData.java new file mode 100644 index 0000000..ae35127 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/SectionData.java @@ -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 pages = new ArrayList(); + + @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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlank.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlank.java new file mode 100644 index 0000000..85c7b6b --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlank.java @@ -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 list) { + + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentDummy.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentDummy.java new file mode 100644 index 0000000..2380747 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentDummy.java @@ -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 list) { + //TODO load from JSON + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java new file mode 100644 index 0000000..ea0076c --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java @@ -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 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)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java new file mode 100644 index 0000000..6e35933 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java @@ -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 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)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java new file mode 100644 index 0000000..7e3be28 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java @@ -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 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)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java new file mode 100644 index 0000000..002505a --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java @@ -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 list) { + if(text != null && text.length > 0) + list.add(new ElementText(15, 15, GuiBook.PAGE_WIDTH - 30, GuiBook.PAGE_HEIGHT - 30, text)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java new file mode 100644 index 0000000..67c038a --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java @@ -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 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)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java new file mode 100644 index 0000000..01498e1 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java @@ -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 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)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java new file mode 100644 index 0000000..2f550bb --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java @@ -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 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)); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java b/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java new file mode 100644 index 0000000..2fa1617 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java @@ -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 list); +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/CriteriaData.java b/src/main/java/slimeknights/mantle/client/book/data/element/CriteriaData.java new file mode 100644 index 0000000..7243244 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/element/CriteriaData.java @@ -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 +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java b/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java new file mode 100644 index 0000000..d51d73f --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java @@ -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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java b/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java new file mode 100644 index 0000000..74c382f --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java @@ -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; + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java b/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java new file mode 100644 index 0000000..c16a112 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java @@ -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); + } + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java b/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java new file mode 100644 index 0000000..edfef87 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java @@ -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 leftElements = new ArrayList<>(); + private ArrayList 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); + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java b/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java new file mode 100644 index 0000000..0cdce08 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java @@ -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 +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java b/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java new file mode 100644 index 0000000..4125011 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java @@ -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); +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java new file mode 100644 index 0000000..9b5aeea --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java @@ -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); + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java new file mode 100644 index 0000000..c2599ec --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java @@ -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); + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementText.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementText.java new file mode 100644 index 0000000..2a8ee49 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementText.java @@ -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); + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/SizedBookElement.java b/src/main/java/slimeknights/mantle/client/gui/book/element/SizedBookElement.java new file mode 100644 index 0000000..d636910 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/SizedBookElement.java @@ -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; + } +} diff --git a/src/main/java/slimeknights/mantle/common/CommonProxy.java b/src/main/java/slimeknights/mantle/common/CommonProxy.java index e2dd4a7..ab788f8 100644 --- a/src/main/java/slimeknights/mantle/common/CommonProxy.java +++ b/src/main/java/slimeknights/mantle/common/CommonProxy.java @@ -1,4 +1,6 @@ package slimeknights.mantle.common; public class CommonProxy { + public void preInit() { + } } diff --git a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java index 3524b00..64f86d5 100644 --- a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java +++ b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java @@ -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; } diff --git a/src/main/resources/assets/mantle/books/TestBook/cover.json b/src/main/resources/assets/mantle/books/TestBook/cover.json new file mode 100644 index 0000000..60c128e --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/cover.json @@ -0,0 +1,5 @@ +{ + "color": 9127473, + "title": "Materials and You", + "subtitle": "By Skyla" +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page2.json b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page2.json new file mode 100644 index 0000000..b4dd8c8 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page2.json @@ -0,0 +1,21 @@ +{ + "text": [ + { + "text": "Long ago in a galaxy far ", + "color": "gold" + }, + { + "text": "far", + "underlined": true, + "tooltip": [ + "You missed the joke", + "By Skyla" + ], + "color": "gold" + }, + { + "text": " away", + "color": "gold" + } + ] +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page3.json b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page3.json new file mode 100644 index 0000000..762a4c6 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page3.json @@ -0,0 +1,5 @@ +{ + "image": { + "file": "images/test.png" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page4_5.json b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page4_5.json new file mode 100644 index 0000000..215eb99 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page4_5.json @@ -0,0 +1,10 @@ +{ + "text": [ + { + "text": "Test of mississipi" + } + ], + "image": { + "file": "images/test.png" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page6_7.json b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page6_7.json new file mode 100644 index 0000000..e151556 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/page6_7.json @@ -0,0 +1,15 @@ +{ + "text1": [ + { + "text": "Test of mississipi" + } + ], + "text2": [ + { + "text": "Test of mississipi" + } + ], + "image": { + "file": "images/test.png" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/images/test.png b/src/main/resources/assets/mantle/books/TestBook/images/test.png new file mode 100644 index 0000000..1ece863 Binary files /dev/null and b/src/main/resources/assets/mantle/books/TestBook/images/test.png differ diff --git a/src/main/resources/assets/mantle/books/TestBook/index.json b/src/main/resources/assets/mantle/books/TestBook/index.json new file mode 100644 index 0000000..6a069c6 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/index.json @@ -0,0 +1,9 @@ +[ + { + "name": "Test Section 1", + "icon": { + "file": "images/testIcon.png" + }, + "data": "sections/section1.json" + } +] \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/sections/section1.json b/src/main/resources/assets/mantle/books/TestBook/sections/section1.json new file mode 100644 index 0000000..6418913 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/sections/section1.json @@ -0,0 +1,36 @@ +[ + { + "title": "Page1", + "type": "blank" + }, + { + "title": "Page2", + "type": "text", + "data": "section1/page2.json" + }, + { + "title": "Page3", + "type": "image", + "data": "section1/page3.json" + }, + { + "title": "Page4", + "type": "image with text below", + "data": "section1/page4_5.json" + }, + { + "title": "Page5", + "type": "text with image below", + "data": "section1/page4_5.json" + }, + { + "title": "Page6", + "type": "text with left image etch", + "data": "section1/page6_7.json" + }, + { + "title": "Page7", + "type": "text with right image etch", + "data": "section1/page6_7.json" + } +] \ No newline at end of file diff --git a/src/main/resources/assets/mantle/manuals/test.xml b/src/main/resources/assets/mantle/manuals/test.xml deleted file mode 100644 index 639fadb..0000000 --- a/src/main/resources/assets/mantle/manuals/test.xml +++ /dev/null @@ -1,165 +0,0 @@ - - - - -Tinker's Log #1: - -Today is a new day. I finally left home and decided to wander around in the wilderness until I find a good place to call my home. - -Tinker's Log #2: - -I've decided to keep a log of my creations and machinations, as well as some insights into what I'm doing. I haven't actually built anything yet. There is a small cave I can board up for the night. I created a stone pickaxe affectionately called "Betsy". - - - -Tinker's Log #3: - -The night is scary. Creepers are everywhere, the zombies are banging on my door, bats block out the sky... it is not fun being alone out here. I want to hide in the corner and cry until they leave. This log doesn't help take my mind off of anything. - -Tinker's Log #4: - -Betsy didn't last long. She just crumpled into dust suddenly and the handle shattered. My parents told me this would happen and this was normal, but it still seems so wrong. If I could make the pickaxe a little better perhaps, maybe I could repair it. - - - -Tinker's Log #5: - -I've spent the past three days looking for paper and ink. Squids were easy enough to find, but the sugar cane was buried deep in a ravine next to a creeper nest. Eventually I dropped rocks on the creepers and watched them explode out of surprise. My eyebrow will grow back in time. - -Tinker's Log #6: - -Waiting on sugar cane to grow. Not much else today. - - - -Tinker's Log #7: - -I've sketched out plans for a new workbench. It's a little strange. The center of the workbench is missing, but sturdy, and there are grooves in the top for supplies. I think I will call it a "table". - -Tinker's Log #8: - -I made a few more tables with different patterns in them. They look useful for different things, so I put different materials and tools by each one. Perhaps I will come up with a use for them in time. - - - -Tinker's Log #9: - -To make tools, you need to know what you're making. The pickaxe and shovel are easy enough to make with practice, the best crafters in my village told me, but I can't seem to make any good ones. Out of frustration I punched a hole in a wooden board. And another. And a few more. By the end I had something that resembled an axe, and that gave me a few ideas... - - - -Tinker's Log #10: - -I punched holes in a few boards carefully in different shapes. I spent a lot of time making them, so they look really good. They all resemble the parts of tools that I can remember: heads, handles, bindings, pieces, and some others. They look really useful, so I placed them by a table for later. - - - -Tinker's Log #11: - -I grabbed a few of the boards and started to make tool parts using them as an outline. The parts themselves feel sturdy, and when I put them together the end result felt a lot better than Betsy did. They're made completely out of stone, something even the finest crafters could never manage. This feels like a great accomplishment. - -Tinker's Log #12: - -Perhaps making a tool completely out of stone wasn't the best idea. It didn't last very long. On the upside, instead of shattering into dust only the tip scratched off. I glued a new tip on the head and it seemed good as new. - - - -Tinker's Log #13: - -I wanted to make a few more tools, but the boards with tool shapes in them went everywhere! I ended up breaking a few out of frustration. They don't fit very well in my chest either. I took a few of them, added a few slots to the bottom of the chest, placed it by my table and called it good. - -I also named them "Patterns", the chest a "Pattern Chest". I'm still angry at them, and it took so long to make new ones... - - - -Tinker's Log #14 - -Today I learned just how hard it was to shape iron. First you have to dig it out of the earth. A wooden pickaxe will shatter the ore; this I found out the hard way. Once you do get that ore there isn't much to do with it besides throw it in the furnace. Melting the iron and the stone seems inefficient, but I do need the metal more than I care about the waste. - -I shaped a few of the bars into tools, and they felt strong. Much stronger, in fact, than the ones back at the village. I've named the pickaxe "Krug" after some of the stories Nana used to tell me at night. How I miss her... - - - -Tinker's Log #15: - -This place holds little for me now. I've packed up my things, grabbed what resources I can carry, and set off. The tables and patterns will have to stay behind. I can't possibly carry them with all the food I have. - -Tinker's Log #16: - -A week later and I've finally found some others to visit. Some of them seem nice, most don't look like me. A few of them have things I never could have dreamed of. They've pointed me at a patch of land that was quiet and secluded. There I should be able to work, for I have ideas from the journey. - - - -Tinker's Log #17: - -After three days and nights of fending off zombies, I've finally created a house. It has a few rooms, a basement, but most importantly a workshop. All of the tables and patterns I had before are here, as well as a few others I wanted to try out from the trip. - -There's a round one I can cook food with and one that's just as good as any hoe, but can chop trees or actually dig the ground up. The villagers tell me these are named "Frying Pan" and "Mattock". The Frying Pan seems heavy enough to smack creepers with. I have a sword named "Shimmer" that I want to alter somehow. - - - -Tinker's Log #18: - -One of the villagers was kind enough to give me some random things. Each one looks useful in their own right. I was lucky enough to find a few diamonds earlier, and I have a few more tools in storage. This should be fun. - - - -Tinker's Log #19: - -After some heavy experimentation, I've ended up with some things that look amazing and others that look less useful than they actually are. A diamond tip on a pickaxe makes for a wondefully hard mining tool, and an emerald on a sword hilt somehow makes it stay together longer, but some things don't do anything. - -Adding flowers to a tool does nothing; they don't even stay on the thing. Mushrooms have a similar effect, and adding more material to a tool just makes it heavier. You wouldn't think redstone would do anything. I've coated a shovel so much that it looks deep red. However, it slides through dirt as if the dirt wasn't there. - - - -Moss is also a strange one. Somehow it seems to be healing the tool, even to the point where it looks like new. - -Tinker's Log #20: - -I tried to make some tools out of gold, like the crafters in my village used to, but instead it broke the table. Some of the villages laughed at me for such an idea... if only they knew. - - - -Tinker's Log #21: - -Some of these tools are more useful than I thought. Adding blaze powder to a weapon seems to give it some kind of burning effect. Lapis has this strange quality of being able to find more materials in the same area, even if I know exactly what I'm looking at. - -Tinker's Log #22: - -I have some ideas for creating tools, but for that I need a large place to create them. The furnace is inadequate, and I need a lot of stone. Combining lava with coal and netherrack seems to instantly cook stone as I mine it, up to the point where I can bypass the furnace entirely. - - - -Tinker's Log #23: - -Today I made something strange. It's round, wooden, and doesn't look like anything I've ever seen. Someone came by and said it was a "wheel" and that they were great fun to roll around. I wonder what else it could be used for? - -Tinker's Log #24: - -I made a few more of the wheels to get some practice in. I had some fun rolling them around, rolling them in pairs, and then one ended up beside my boat. It just looked so natural I had to start putting them together. - - - -Tinker's Log #25: - -I think I'm on to something here. Combining the boat with the wheel made it work something like a minecart, only I can pull it around places. It feels a little small though. - -Tinker's Log #26: - -I have all these tools laying around with little notes on what each one does. Instead of leaving them scattered about, I think I will create one place to store them. - - -Tinker's Log #27: - -I went exploring and found some new ores today. They're colors I've never seen before, and they don't shatter on impact like iron does. I wonder what I can do with these? - -Tinker's Log #28: - -The normal furnace is nice for keeping warm, but it leaves something to be desired for processing metal. I've dug a pit and lined it with some clay, gathered lava nearby, and put ore in the center in an attempt to improve on the design. The results were quite interesting. - - -The more lava I fed into the pit the hotter the material became. Eventually the whole thing liquified. I broke open the side of the pit with a cauldron in an attempt to catch the material, but it went everywhere. There were some remnants left in the pit as well. - -Raw ore still has parts of stone in it. There must be a way to remove the excess. - - \ No newline at end of file diff --git a/src/main/resources/assets/mantle/textures/gui/book.png b/src/main/resources/assets/mantle/textures/gui/book.png index c9007bd..c2cc8f1 100644 Binary files a/src/main/resources/assets/mantle/textures/gui/book.png and b/src/main/resources/assets/mantle/textures/gui/book.png differ diff --git a/src/main/resources/assets/mantle/textures/gui/bookfront.png b/src/main/resources/assets/mantle/textures/gui/bookfront.png new file mode 100644 index 0000000..6e68fda Binary files /dev/null and b/src/main/resources/assets/mantle/textures/gui/bookfront.png differ diff --git a/src/main/resources/assets/mantle/textures/gui/bookleft.png b/src/main/resources/assets/mantle/textures/gui/bookleft.png deleted file mode 100644 index 08b462d..0000000 Binary files a/src/main/resources/assets/mantle/textures/gui/bookleft.png and /dev/null differ diff --git a/src/main/resources/assets/mantle/textures/gui/bookright.png b/src/main/resources/assets/mantle/textures/gui/bookright.png deleted file mode 100644 index 8c255a7..0000000 Binary files a/src/main/resources/assets/mantle/textures/gui/bookright.png and /dev/null differ