mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-20 20:12:28 +00:00
Updated book code
- Added book transformers - Added an index page - Fixed mouse position on right pages - Books now save position on close
This commit is contained in:
1 parent
d0bd1aac90
commit
4b5c849893
22 files changed
+584
-167
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package slimeknights.mantle.client.book;
|
||||
|
||||
import java.util.Arrays;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class BookHelper {
|
||||
|
||||
public static String getSavedPage(ItemStack item) {
|
||||
if (item != null && item.hasTagCompound()) {
|
||||
NBTTagCompound mantleBook = item.getTagCompound().getCompoundTag("mantle").getCompoundTag("book");
|
||||
|
||||
if (mantleBook.hasKey("page", Arrays.asList(NBTBase.NBT_TYPES).indexOf("STRING"))) {
|
||||
return mantleBook.getString("page");
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void writeSavedPage(ItemStack item, String page) {
|
||||
NBTTagCompound compound = item.getTagCompound();
|
||||
|
||||
if (compound == null)
|
||||
compound = new NBTTagCompound();
|
||||
|
||||
NBTTagCompound mantle = compound.getCompoundTag("mantle");
|
||||
NBTTagCompound book = mantle.getCompoundTag("book");
|
||||
|
||||
book.setString("page", page);
|
||||
|
||||
mantle.setTag("book", book);
|
||||
compound.setTag("mantle", mantle);
|
||||
item.setTagCompound(compound);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,10 +2,13 @@ package slimeknights.mantle.client.book;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
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.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
@@ -21,7 +24,8 @@ import slimeknights.mantle.client.book.data.content.ContentTextImage;
|
||||
import slimeknights.mantle.client.book.data.content.ContentTextLeftImage;
|
||||
import slimeknights.mantle.client.book.data.content.ContentTextRightImage;
|
||||
import slimeknights.mantle.client.book.data.content.PageContent;
|
||||
import slimeknights.mantle.client.gui.book.GuiBook;
|
||||
import slimeknights.mantle.network.NetworkWrapper;
|
||||
import slimeknights.mantle.network.book.PacketUpdateSavedPage;
|
||||
import static slimeknights.mantle.client.book.ResourceHelper.setBookRoot;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@@ -36,9 +40,16 @@ public class BookLoader implements IResourceManagerReloadListener {
|
||||
/** Internal registry of all books for the purposes of the reloader, maps books to name */
|
||||
private static final HashMap<String, BookData> books = new HashMap<>();
|
||||
|
||||
private static final NetworkWrapper wrapper = new NetworkWrapper("mantle:books");
|
||||
|
||||
private static final Random random = new Random();
|
||||
private static char[] randAlphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public BookLoader() {
|
||||
((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(this);
|
||||
|
||||
wrapper.registerPacketServer(PacketUpdateSavedPage.class);
|
||||
|
||||
// Register page types
|
||||
registerPageType("blank", ContentBlank.class);
|
||||
registerPageType("text", ContentText.class);
|
||||
@@ -90,19 +101,41 @@ public class BookLoader implements IResourceManagerReloadListener {
|
||||
|
||||
books.put(name.contains(":") ? name : Loader.instance().activeModContainer().getModId() + ":" + name, info);
|
||||
|
||||
info.addTransformer(new BookTransformer.IndexTranformer());
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the GuiScreen for the book
|
||||
* Returns a book by its name
|
||||
*
|
||||
* @param name The name of the book, prefixed with modid:
|
||||
* @return The GuiScreen to open for the book
|
||||
* @return The instance of the book data, or null if the book is not registered
|
||||
*/
|
||||
public static GuiBook getBookGui(String name) {
|
||||
public static BookData getBook(String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void updateSavedPage(EntityPlayer player, ItemStack item, String page) {
|
||||
if (player.getHeldItem() == null)
|
||||
return;
|
||||
|
||||
BookHelper.writeSavedPage(item, page);
|
||||
wrapper.network.sendToServer(new PacketUpdateSavedPage(page));
|
||||
}
|
||||
|
||||
public static String randomName() {
|
||||
int length = random.nextInt(10);
|
||||
|
||||
String s = "";
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
s += randAlphabet[random.nextInt(randAlphabet.length)];
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads all the books, called when the resource manager reloads, such as when the resource pack or the language is changed
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package slimeknights.mantle.client.book;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import slimeknights.mantle.client.book.data.BookData;
|
||||
import slimeknights.mantle.client.book.data.PageData;
|
||||
import slimeknights.mantle.client.book.data.SectionData;
|
||||
import slimeknights.mantle.client.book.data.content.ContentSectionList;
|
||||
|
||||
public abstract class BookTransformer {
|
||||
|
||||
/**
|
||||
* Called when all the sections within the book are loaded, but before their contents are, as the pages are counted when the contents of the sections are loaded.
|
||||
*
|
||||
* @param book The object to the book to transform
|
||||
*/
|
||||
public abstract void transform(BookData book);
|
||||
|
||||
protected static class IndexTranformer extends BookTransformer {
|
||||
|
||||
@Override
|
||||
public void transform(BookData book) {
|
||||
SectionData index = new SectionData(true);
|
||||
index.name = "index";
|
||||
|
||||
PageData[] pages = new PageData[(int) Math.ceil(book.sections.size() / 9F)];
|
||||
|
||||
for (int i = 0; i < pages.length; i++) {
|
||||
pages[i] = new PageData(true);
|
||||
|
||||
pages[i].name = "page" + (i + 1);
|
||||
|
||||
ContentSectionList content = new ContentSectionList();
|
||||
pages[i].content = content;
|
||||
|
||||
for (int j = i * 9; j - i * 9 < 9 && j < book.sections.size(); j++) {
|
||||
content.addSection(book.sections.get(j));
|
||||
}
|
||||
}
|
||||
|
||||
index.pages = new ArrayList<>(Arrays.asList(pages));
|
||||
book.sections.add(0, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,12 @@ public class ResourceHelper {
|
||||
}
|
||||
|
||||
public static ResourceLocation getResourceLocation(String path) {
|
||||
return getResourceLocation(path, false);
|
||||
}
|
||||
|
||||
public static ResourceLocation getResourceLocation(String path, boolean safe) {
|
||||
if (path == null)
|
||||
return null;
|
||||
return safe ? new ResourceLocation("") : null;
|
||||
if (!path.contains(":")) {
|
||||
String langPath = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode();
|
||||
String defaultLangPath = "en_US";
|
||||
@@ -34,12 +38,12 @@ public class ResourceHelper {
|
||||
res = new ResourceLocation(bookRoot + "/" + path);
|
||||
if (resourceExists(res))
|
||||
return res;
|
||||
return null;
|
||||
return safe ? new ResourceLocation("") : null;
|
||||
} else {
|
||||
ResourceLocation res = new ResourceLocation(path);
|
||||
if (resourceExists(res))
|
||||
return res;
|
||||
return null;
|
||||
return safe ? new ResourceLocation("") : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,9 +107,7 @@ public class ResourceHelper {
|
||||
builder.append(s);
|
||||
}
|
||||
|
||||
String data = builder.toString().trim();
|
||||
|
||||
return data;
|
||||
return builder.toString().trim();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -10,24 +10,24 @@ public class StringActionProcessor {
|
||||
|
||||
private static final HashMap<String, ActionProtocol> protocols = new HashMap<>();
|
||||
|
||||
public static void registerProtocol(ActionProtocol protocol){
|
||||
if(protocol == null || protocol.protocol == null || protocol.protocol.isEmpty())
|
||||
public static void registerProtocol(ActionProtocol protocol) {
|
||||
if (protocol == null || protocol.protocol == null || protocol.protocol.isEmpty())
|
||||
throw new IllegalArgumentException("Protocol must be defined and must not have an empty protocol identifier.");
|
||||
if(protocols.containsKey(protocol.protocol))
|
||||
if (protocols.containsKey(protocol.protocol))
|
||||
throw new IllegalArgumentException("Protocol " + protocol.protocol + " already registered.");
|
||||
|
||||
protocols.put(protocol.protocol, protocol);
|
||||
}
|
||||
|
||||
//Format: action://param
|
||||
public static void process(String action, GuiBook book){
|
||||
if(!action.contains(PROTOCOL_SEPARATOR))
|
||||
public static void process(String action, GuiBook book) {
|
||||
if (!action.contains(PROTOCOL_SEPARATOR))
|
||||
return;
|
||||
|
||||
String protoId = action.substring(0, action.indexOf(PROTOCOL_SEPARATOR));
|
||||
String protoParam = action.substring(action.indexOf(PROTOCOL_SEPARATOR) + PROTOCOL_SEPARATOR.length());
|
||||
|
||||
if(protocols.containsKey(protoId))
|
||||
if (protocols.containsKey(protoId))
|
||||
protocols.get(protoId).processCommand(book, protoParam);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package slimeknights.mantle.client.book.action.protocol;
|
||||
import slimeknights.mantle.client.gui.book.GuiBook;
|
||||
|
||||
public abstract class ActionProtocol {
|
||||
|
||||
public final String protocol;
|
||||
|
||||
public ActionProtocol(String protocol){
|
||||
public ActionProtocol(String protocol) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public class ProtocolGoToPage extends ActionProtocol {
|
||||
public void processCommand(GuiBook book, String param) {
|
||||
int pageNum = book.book.findPageNumber(param);
|
||||
|
||||
if(pageNum >= 0)
|
||||
if (pageNum >= 0)
|
||||
book.openPage(pageNum, returner);
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,15 @@ package slimeknights.mantle.client.book.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import slimeknights.mantle.client.book.BookLoader;
|
||||
import slimeknights.mantle.client.book.BookTransformer;
|
||||
import slimeknights.mantle.client.gui.book.GuiBook;
|
||||
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
|
||||
import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation;
|
||||
import static slimeknights.mantle.client.book.ResourceHelper.resourceExists;
|
||||
@@ -15,13 +19,15 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class BookData implements IDataItem {
|
||||
|
||||
public transient List<SectionData> sections = new ArrayList<>();
|
||||
public transient ArrayList<SectionData> sections = new ArrayList<>();
|
||||
public transient AppearanceData appearance = new AppearanceData();
|
||||
public transient int pageCount;
|
||||
public transient int fullPageCount;
|
||||
|
||||
public final transient String bookLocation;
|
||||
|
||||
protected final transient ArrayList<BookTransformer> transformers = new ArrayList<>();
|
||||
|
||||
public BookData(String bookLocation) {
|
||||
this.bookLocation = bookLocation;
|
||||
}
|
||||
@@ -30,7 +36,7 @@ public class BookData implements IDataItem {
|
||||
public int cascadeLoad() {
|
||||
int pages = 0;
|
||||
|
||||
sections = Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class));
|
||||
sections = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class)));
|
||||
|
||||
ResourceLocation coverLocation = getResourceLocation("appearance.json");
|
||||
|
||||
@@ -41,16 +47,41 @@ public class BookData implements IDataItem {
|
||||
|
||||
appearance.cascadeLoad();
|
||||
|
||||
for (BookTransformer transformer : transformers)
|
||||
transformer.transform(this);
|
||||
|
||||
for (SectionData section : sections) {
|
||||
section.parent = this;
|
||||
pages += section.cascadeLoad();
|
||||
}
|
||||
|
||||
appearance.cascadeLoad();
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
public SectionData findSection(String name) {
|
||||
for (SectionData section : sections)
|
||||
if (section.name.equals(name.toLowerCase()))
|
||||
return section;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getFirstPageNumber(SectionData section) {
|
||||
int pages = 0;
|
||||
for (SectionData sect : sections) {
|
||||
if (section == sect)
|
||||
return pages + 1;
|
||||
|
||||
pages += sect.pageCount;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public PageData findPage(int number) {
|
||||
if (number < 0)
|
||||
return null;
|
||||
|
||||
int pages = 0;
|
||||
for (SectionData section : sections) {
|
||||
if (pages + section.pageCount > number)
|
||||
@@ -62,25 +93,29 @@ public class BookData implements IDataItem {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int findPageNumber(String location){
|
||||
public PageData findPage(String location) {
|
||||
return findPage(findPageNumber(location));
|
||||
}
|
||||
|
||||
public int findPageNumber(String location) {
|
||||
location = location.toLowerCase();
|
||||
|
||||
int pages = 0;
|
||||
|
||||
if(!location.contains("."))
|
||||
if (!location.contains("."))
|
||||
return -1;
|
||||
|
||||
String sectionName = location.substring(0, location.indexOf('.'));
|
||||
String pageName = location.substring(location.indexOf('.') + 1);
|
||||
|
||||
for (SectionData section : sections) {
|
||||
if(!sectionName.equals(section.name)) {
|
||||
if (!sectionName.equals(section.name)) {
|
||||
pages += section.pageCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (PageData page : section.pages){
|
||||
if(!pageName.equals(page.name)){
|
||||
for (PageData page : section.pages) {
|
||||
if (!pageName.equals(page.name)) {
|
||||
pages++;
|
||||
continue;
|
||||
}
|
||||
@@ -91,4 +126,13 @@ public class BookData implements IDataItem {
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void openGui(@Nullable ItemStack item) {
|
||||
Minecraft.getMinecraft().displayGuiScreen(new GuiBook(this, item));
|
||||
}
|
||||
|
||||
public void addTransformer(BookTransformer transformer) {
|
||||
if (!transformers.contains(transformer))
|
||||
transformers.add(transformer);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package slimeknights.mantle.client.book.data;
|
||||
|
||||
public class BookmarkData {
|
||||
|
||||
public String text = "";
|
||||
public int color = 0x464646; // Index bookmark : 0x1ED400
|
||||
public String page = "";
|
||||
@@ -9,7 +10,12 @@ public class BookmarkData {
|
||||
this("");
|
||||
}
|
||||
|
||||
public BookmarkData(String page){
|
||||
public BookmarkData(String page) {
|
||||
this(page, "");
|
||||
}
|
||||
|
||||
public BookmarkData(String page, String text) {
|
||||
this.page = page;
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -15,21 +15,33 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class PageData implements IDataItem {
|
||||
|
||||
public String name;
|
||||
public String type;
|
||||
public String data;
|
||||
public String name = BookLoader.randomName();
|
||||
public String type = "";
|
||||
public String data = "";
|
||||
|
||||
public transient SectionData parent;
|
||||
public transient PageContent content;
|
||||
|
||||
public PageData() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public PageData(boolean custom) {
|
||||
if (custom)
|
||||
data = "no-load";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cascadeLoad() {
|
||||
name = name.toLowerCase();
|
||||
|
||||
IResource pageInfo = getResource(getResourceLocation(data));
|
||||
if (pageInfo != null) {
|
||||
String data = resourceToString(pageInfo);
|
||||
if (!data.isEmpty())
|
||||
content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type));
|
||||
if (!data.equals("no-load")) {
|
||||
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)
|
||||
@@ -44,7 +56,7 @@ public class PageData implements IDataItem {
|
||||
try {
|
||||
f.setAccessible(true);
|
||||
ImageData d = (ImageData) f.get(content);
|
||||
d.location = getResourceLocation(d.file);
|
||||
d.location = getResourceLocation(d.file, true);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ 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;
|
||||
@@ -16,31 +15,45 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class SectionData implements IDataItem {
|
||||
|
||||
public String name;
|
||||
public ImageData icon;
|
||||
public CriteriaData[] unlockCriteria;
|
||||
public String data;
|
||||
public String name = BookLoader.randomName();
|
||||
public String title = "";
|
||||
public ImageData icon = new ImageData();
|
||||
public CriteriaData[] unlockCriteria = new CriteriaData[0];
|
||||
public String data = "";
|
||||
|
||||
public int pageCount;
|
||||
|
||||
public transient List<PageData> pages = new ArrayList<PageData>();
|
||||
public transient BookData parent;
|
||||
public transient ArrayList<PageData> pages = new ArrayList<>();
|
||||
|
||||
public SectionData() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public SectionData(boolean custom) {
|
||||
if (custom)
|
||||
data = "no-load";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cascadeLoad() {
|
||||
name = name.toLowerCase();
|
||||
|
||||
IResource pagesInfo = getResource(getResourceLocation(data));
|
||||
if (pagesInfo != null) {
|
||||
String data = resourceToString(pagesInfo);
|
||||
if (!data.isEmpty())
|
||||
pages = Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class));
|
||||
if (!data.equals("no-load")) {
|
||||
IResource pagesInfo = getResource(getResourceLocation(data));
|
||||
if (pagesInfo != null) {
|
||||
String data = resourceToString(pagesInfo);
|
||||
if (!data.isEmpty())
|
||||
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
|
||||
}
|
||||
}
|
||||
|
||||
for (PageData page : pages) {
|
||||
page.parent = this;
|
||||
page.cascadeLoad();
|
||||
}
|
||||
|
||||
icon.location = getResourceLocation(icon.file);
|
||||
icon.location = getResourceLocation(icon.file, true);
|
||||
|
||||
pageCount = pages.size();
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package slimeknights.mantle.client.book.data.content;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import slimeknights.mantle.client.book.data.SectionData;
|
||||
import slimeknights.mantle.client.gui.book.element.BookElement;
|
||||
import slimeknights.mantle.client.gui.book.element.ElementSection;
|
||||
|
||||
public class ContentSectionList extends PageContent {
|
||||
|
||||
protected ArrayList<SectionData> sections = new ArrayList<>();
|
||||
|
||||
public boolean addSection(SectionData data) {
|
||||
if (sections.size() >= 9) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return sections.add(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(ArrayList<BookElement> list) {
|
||||
int width = (ElementSection.WIDTH + 5) * 3 - 5;
|
||||
int height = (ElementSection.HEIGHT + 5) * 3 - 5;
|
||||
|
||||
for (int i = 0; i < sections.size(); i++) {
|
||||
int ix = i % 3;
|
||||
int iy = (int) Math.floor(i / 3F);
|
||||
|
||||
int x = ix * (ElementSection.WIDTH + 5);
|
||||
int y = iy * (ElementSection.HEIGHT + 5);
|
||||
|
||||
list.add(new ElementSection(x, y, sections.get(i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,20 @@ package slimeknights.mantle.client.gui.book;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import javax.annotation.Nullable;
|
||||
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.item.ItemStack;
|
||||
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.BookHelper;
|
||||
import slimeknights.mantle.client.book.BookLoader;
|
||||
import slimeknights.mantle.client.book.data.BookData;
|
||||
import slimeknights.mantle.client.book.data.PageData;
|
||||
import slimeknights.mantle.client.gui.book.element.BookElement;
|
||||
@@ -30,25 +34,26 @@ public class GuiBook extends GuiScreen {
|
||||
public static final int PAGE_HEIGHT_UNSCALED = 200;
|
||||
|
||||
// For best results, make sure both PAGE_WIDTH_UNSCALED - (PAGE_PADDING + PAGE_MARGIN) * 2 and PAGE_HEIGHT_UNSCALED - (PAGE_PADDING + PAGE_MARGIN) * 2 divide evenly into PAGE_SCALE (without remainder)
|
||||
public static final int PAGE_WIDTH = (int)((PAGE_WIDTH_UNSCALED - (PAGE_PADDING + PAGE_MARGIN) * 2) / PAGE_SCALE);
|
||||
public static final int PAGE_HEIGHT = (int)((PAGE_HEIGHT_UNSCALED - (PAGE_PADDING + PAGE_MARGIN) * 2) / PAGE_SCALE);
|
||||
public static final int PAGE_WIDTH = (int) ((PAGE_WIDTH_UNSCALED - (PAGE_PADDING + PAGE_MARGIN) * 2) / PAGE_SCALE);
|
||||
public static final int PAGE_HEIGHT = (int) ((PAGE_HEIGHT_UNSCALED - (PAGE_PADDING + PAGE_MARGIN) * 2) / PAGE_SCALE);
|
||||
|
||||
public static boolean side = false;
|
||||
|
||||
private GuiArrow previousArrow, nextArrow, backArrow, indexArrow;
|
||||
|
||||
public final BookData book;
|
||||
private ItemStack item;
|
||||
|
||||
private int page = -1;
|
||||
private int oldPage = -2;
|
||||
private ArrayList<BookElement> leftElements = new ArrayList<>();
|
||||
private ArrayList<BookElement> rightElements = new ArrayList<>();
|
||||
//TODO page cache
|
||||
|
||||
private boolean halt = false;
|
||||
|
||||
public GuiBook(BookData book) {
|
||||
public GuiBook(BookData book, @Nullable ItemStack item) {
|
||||
this.book = book;
|
||||
this.item = item;
|
||||
|
||||
openPage(book.findPageNumber(BookHelper.getSavedPage(item)));
|
||||
}
|
||||
|
||||
public void drawerTransform(boolean rightSide) {
|
||||
@@ -63,9 +68,11 @@ public class GuiBook extends GuiScreen {
|
||||
@Override
|
||||
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
|
||||
FontRenderer font = mc.fontRendererObj;
|
||||
GlStateManager.enableAlpha();
|
||||
GlStateManager.enableBlend();
|
||||
|
||||
// The books are unreadable at Gui Scale set to small, so we'll double the scale
|
||||
if(mc.gameSettings.guiScale == 1){
|
||||
if (mc.gameSettings.guiScale == 1) {
|
||||
GlStateManager.scale(2F, 2F, 2F);
|
||||
|
||||
mouseX /= 2;
|
||||
@@ -122,19 +129,17 @@ public class GuiBook extends GuiScreen {
|
||||
|
||||
GlStateManager.scale(PAGE_SCALE, PAGE_SCALE, 1F);
|
||||
|
||||
if(book.appearance.drawPageNumbers) {
|
||||
if (book.appearance.drawPageNumbers) {
|
||||
String pNum = (page - 1) * 2 + 2 + "";
|
||||
font.drawString(pNum, PAGE_WIDTH / 2 - font.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false);
|
||||
}
|
||||
|
||||
for (BookElement element : leftElements) {
|
||||
GlStateManager.color(1F, 1F, 1F, 1F);
|
||||
element.draw((int)((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int)((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks);
|
||||
// Not foreach to prevent conmodification crashes
|
||||
for (int i = 0; i < leftElements.size(); i++) {
|
||||
BookElement element = leftElements.get(i);
|
||||
|
||||
if(halt){
|
||||
halt = false;
|
||||
return;
|
||||
}
|
||||
GlStateManager.color(1F, 1F, 1F, 1F);
|
||||
element.draw((int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks);
|
||||
}
|
||||
|
||||
GlStateManager.popMatrix();
|
||||
@@ -145,7 +150,7 @@ public class GuiBook extends GuiScreen {
|
||||
// Set color back to white
|
||||
GlStateManager.color(1F, 1F, 1F, 1F);
|
||||
|
||||
if ((page < book.fullPageCount && book.pageCount % 2 != 0) && page != book.fullPageCount) {
|
||||
if ((page < book.fullPageCount - 1 || book.pageCount % 2 != 0) && page < book.fullPageCount) {
|
||||
drawModalRectWithCustomSizedTexture(width / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, 512, 512);
|
||||
|
||||
GlStateManager.pushMatrix();
|
||||
@@ -153,20 +158,17 @@ public class GuiBook extends GuiScreen {
|
||||
|
||||
GlStateManager.scale(PAGE_SCALE, PAGE_SCALE, 1F);
|
||||
|
||||
if(book.appearance.drawPageNumbers) {
|
||||
if (book.appearance.drawPageNumbers) {
|
||||
String pNum = (page - 1) * 2 + 3 + "";
|
||||
font.drawString(pNum, PAGE_WIDTH / 2 - font.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false);
|
||||
}
|
||||
|
||||
for (BookElement element : rightElements) {
|
||||
// Not foreach to prevent conmodification crashes
|
||||
for (int i = 0; i < rightElements.size(); i++) {
|
||||
BookElement element = rightElements.get(i);
|
||||
|
||||
GlStateManager.color(1F, 1F, 1F, 1F);
|
||||
element.draw((int)((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2) + PAGE_PADDING + PAGE_MARGIN) / PAGE_SCALE), (int)((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks);
|
||||
|
||||
|
||||
if(halt){
|
||||
halt = false;
|
||||
return;
|
||||
}
|
||||
element.draw((int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks);
|
||||
}
|
||||
|
||||
GlStateManager.popMatrix();
|
||||
@@ -178,28 +180,29 @@ public class GuiBook extends GuiScreen {
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
public void openPage(int page){
|
||||
public void openPage(int page) {
|
||||
openPage(page, false);
|
||||
}
|
||||
|
||||
public void openPage(int page, boolean returner) {
|
||||
int bookPage = -1;
|
||||
if (page < 0)
|
||||
return;
|
||||
|
||||
if(page % 2 == 0){
|
||||
int bookPage;
|
||||
if (page == 1)
|
||||
bookPage = 0;
|
||||
else if (page % 2 == 0)
|
||||
bookPage = (page - 1) / 2 + 1;
|
||||
}else{
|
||||
else
|
||||
bookPage = (page - 2) / 2 + 1;
|
||||
}
|
||||
|
||||
if(bookPage >= -1 && bookPage < book.fullPageCount){
|
||||
if(returner)
|
||||
if (bookPage >= -1 && bookPage < book.fullPageCount) {
|
||||
if (returner)
|
||||
oldPage = this.page;
|
||||
|
||||
this.page = bookPage;
|
||||
buildPages();
|
||||
}
|
||||
|
||||
halt = true;
|
||||
}
|
||||
|
||||
private void buildPages() {
|
||||
@@ -224,9 +227,9 @@ public class GuiBook extends GuiScreen {
|
||||
rightPage.content.build(rightElements);
|
||||
}
|
||||
|
||||
for(BookElement element : leftElements)
|
||||
for (BookElement element : leftElements)
|
||||
element.parent = this;
|
||||
for(BookElement element : rightElements)
|
||||
for (BookElement element : rightElements)
|
||||
element.parent = this;
|
||||
}
|
||||
|
||||
@@ -235,7 +238,7 @@ public class GuiBook extends GuiScreen {
|
||||
super.initGui();
|
||||
|
||||
// The books are unreadable at Gui Scale set to small, so we'll double the scale, and of course half the size so that all our code still works as it should
|
||||
if(mc.gameSettings.guiScale == 1){
|
||||
if (mc.gameSettings.guiScale == 1) {
|
||||
width /= 2F;
|
||||
height /= 2F;
|
||||
}
|
||||
@@ -243,10 +246,12 @@ public class GuiBook extends GuiScreen {
|
||||
previousArrow = new GuiArrow(0, -50, -50, 1, book.appearance.arrowColor, book.appearance.arrowColorHover);
|
||||
nextArrow = new GuiArrow(1, -50, -50, 0, book.appearance.arrowColor, book.appearance.arrowColorHover);
|
||||
backArrow = new GuiArrow(2, width / 2 - GuiArrow.WIDTH / 2, height / 2 - GuiArrow.HEIGHT / 2 + PAGE_HEIGHT / 3, 3, book.appearance.arrowColor, book.appearance.arrowColorHover);
|
||||
indexArrow = new GuiArrow(3, width / 2 - PAGE_WIDTH_UNSCALED, height / 2 - PAGE_HEIGHT_UNSCALED / 2 - 3, 3, book.appearance.arrowColor, book.appearance.arrowColorHover);
|
||||
|
||||
buttonList.add(previousArrow);
|
||||
buttonList.add(nextArrow);
|
||||
buttonList.add(backArrow);
|
||||
buttonList.add(indexArrow);
|
||||
|
||||
buildPages();
|
||||
}
|
||||
@@ -264,6 +269,8 @@ public class GuiBook extends GuiScreen {
|
||||
} else {
|
||||
previousArrow.xPosition = width / 2 - 184;
|
||||
nextArrow.xPosition = width / 2 + 165;
|
||||
|
||||
indexArrow.visible = (page - 1) * 2 + 2 > book.findSection("index").pageCount;
|
||||
}
|
||||
|
||||
previousArrow.yPosition = height / 2 + 75;
|
||||
@@ -272,6 +279,12 @@ public class GuiBook extends GuiScreen {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(GuiButton button) {
|
||||
if (button instanceof GuiBookmark) {
|
||||
openPage(book.findPageNumber(((GuiBookmark) button).data.page));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (button == previousArrow) {
|
||||
page--;
|
||||
if (page < -1)
|
||||
@@ -281,8 +294,10 @@ public class GuiBook extends GuiScreen {
|
||||
if (page > book.fullPageCount - (book.pageCount % 2 != 0 ? 0 : 1))
|
||||
page = book.fullPageCount - 1;
|
||||
} else if (button == backArrow) {
|
||||
if(oldPage >= -2)
|
||||
if (oldPage >= -1)
|
||||
page = oldPage;
|
||||
} else if (button == indexArrow) {
|
||||
openPage(book.findPageNumber("index.page1"));
|
||||
}
|
||||
|
||||
oldPage = -2;
|
||||
@@ -300,22 +315,40 @@ public class GuiBook extends GuiScreen {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
|
||||
{
|
||||
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
|
||||
super.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
|
||||
boolean right = false;
|
||||
|
||||
mouseX = (int)((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE);
|
||||
mouseY = (int)((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE);
|
||||
mouseX = (int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE);
|
||||
mouseY = (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE);
|
||||
|
||||
if(mouseX > PAGE_WIDTH) {
|
||||
mouseX -= PAGE_WIDTH;
|
||||
if (mouseX > PAGE_WIDTH + (PAGE_MARGIN + PAGE_PADDING) / PAGE_SCALE) {
|
||||
mouseX = (int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE);
|
||||
right = true;
|
||||
}
|
||||
|
||||
for(BookElement element : right ? rightElements : leftElements){
|
||||
// Not foreach to prevent conmodification crashes
|
||||
for (int i = 0; right ? i < rightElements.size() : i < leftElements.size(); i++) {
|
||||
BookElement element = right ? rightElements.get(i) : leftElements.get(i);
|
||||
|
||||
element.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuiClosed() {
|
||||
PageData page = this.page == 0 ? book.findPage(0) : book.findPage((this.page - 1) * 2 + 1);
|
||||
|
||||
if (page == null)
|
||||
page = book.findPage((this.page - 1) * 2 + 2);
|
||||
|
||||
if (page != null || this.page == -1)
|
||||
BookLoader.updateSavedPage(mc.thePlayer, item, this.page == -1 ? "" : (page.parent.name + "." + page.name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doesGuiPauseGame() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+56
-58
@@ -1,58 +1,56 @@
|
||||
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 slimeknights.mantle.client.book.data.BookmarkData;
|
||||
import slimeknights.mantle.client.book.data.element.TextData;
|
||||
|
||||
public class Bookmark extends GuiButton {
|
||||
|
||||
private static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png");
|
||||
|
||||
public static final int WIDTH = 32;
|
||||
public static final int HEIGHT = 9;
|
||||
public static final int ONPAGE_WIDTH = 4;
|
||||
public static final int TEX_X = 0, TEX_Y = 400;
|
||||
public static final int ADD_W = 5, ADD_H = 5, ADD_X = 32, ADD_Y = 402;
|
||||
|
||||
public static final float TEXT_SCALE = 0.5F;
|
||||
|
||||
public int type = 0;
|
||||
|
||||
public final BookmarkData data;
|
||||
|
||||
public Bookmark(BookmarkData data) {
|
||||
super(-1337, -500, -500, WIDTH, HEIGHT, "");
|
||||
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawButton(Minecraft mc, int mouseX, int mouseY) {
|
||||
if(visible) {
|
||||
int tex_y = TEX_Y + HEIGHT * type;
|
||||
|
||||
mc.renderEngine.bindTexture(TEX_BOOK);
|
||||
|
||||
float r = ((data.color >> 16) & 0xff) / 255.F;
|
||||
float g = ((data.color >> 8) & 0xff) / 255.F;
|
||||
float b = (data.color & 0xff) / 255.F;
|
||||
|
||||
GlStateManager.color(r, g, b);
|
||||
Gui.drawScaledCustomSizeModalRect(xPosition, yPosition, TEX_X, tex_y, WIDTH, HEIGHT, width, height, 512, 512);
|
||||
|
||||
if (data.text != null && !data.text.isEmpty()) {
|
||||
TextDataRenderer.drawScaledString(mc.fontRendererObj, data.text, xPosition + width / 2 - mc.fontRendererObj.getStringWidth(data.text) / TEXT_SCALE / 2, yPosition + height / 2 - mc.fontRendererObj.FONT_HEIGHT / 2, 0xFFFFFFFF, true, TEXT_SCALE);
|
||||
}
|
||||
|
||||
GlStateManager.color(1F, 1F, 1F);
|
||||
|
||||
if(data.page.equals("ADD")) {
|
||||
Gui.drawModalRectWithCustomSizedTexture(xPosition + width / 2 - ADD_W / 2, yPosition + height / 2 - ADD_H / 2, ADD_X, ADD_Y, ADD_W, ADD_H, 512, 512);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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 slimeknights.mantle.client.book.data.BookmarkData;
|
||||
|
||||
public class GuiBookmark extends GuiButton {
|
||||
|
||||
private static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png");
|
||||
|
||||
public static final int WIDTH = 31;
|
||||
public static final int HEIGHT = 9;
|
||||
public static final int TEX_X = 0, TEX_Y = 400;
|
||||
public static final int ADD_W = 5, ADD_H = 5, ADD_X = 32, ADD_Y = 402;
|
||||
|
||||
public static final float TEXT_SCALE = 0.5F;
|
||||
|
||||
public int type = 0;
|
||||
|
||||
public final BookmarkData data;
|
||||
|
||||
public GuiBookmark(BookmarkData data) {
|
||||
super(-1337, -500, -500, WIDTH, HEIGHT, "");
|
||||
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawButton(Minecraft mc, int mouseX, int mouseY) {
|
||||
if (visible) {
|
||||
int tex_y = TEX_Y + HEIGHT * type;
|
||||
|
||||
mc.renderEngine.bindTexture(TEX_BOOK);
|
||||
|
||||
float r = ((data.color >> 16) & 0xff) / 255.F;
|
||||
float g = ((data.color >> 8) & 0xff) / 255.F;
|
||||
float b = (data.color & 0xff) / 255.F;
|
||||
|
||||
GlStateManager.color(r, g, b);
|
||||
Gui.drawScaledCustomSizeModalRect(xPosition, yPosition, TEX_X, tex_y, WIDTH, HEIGHT, width, height, 512, 512);
|
||||
|
||||
if (data.text != null && !data.text.isEmpty()) {
|
||||
TextDataRenderer.drawScaledString(mc.fontRendererObj, data.text, xPosition + 1, yPosition + height / 2 - mc.fontRendererObj.FONT_HEIGHT * TEXT_SCALE / 2 + 1, 0xFFFFFFFF, true, TEXT_SCALE);
|
||||
}
|
||||
|
||||
GlStateManager.color(1F, 1F, 1F);
|
||||
|
||||
if (data.page.equals("ADD")) {
|
||||
Gui.drawModalRectWithCustomSizedTexture(xPosition + width / 2 - ADD_W / 2, yPosition + height / 2 - ADD_H / 2, ADD_X, ADD_Y, ADD_W, ADD_H, 512, 512);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public class TextDataRenderer {
|
||||
if (item.text == null || item.text.isEmpty())
|
||||
continue;
|
||||
|
||||
if (item.paragraph){
|
||||
if (item.paragraph) {
|
||||
atX = x;
|
||||
atY += fr.FONT_HEIGHT * 2 * prevScale;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class TextDataRenderer {
|
||||
}
|
||||
|
||||
box3W = atX;
|
||||
box3H = (int)(atY + fr.FONT_HEIGHT * item.scale);
|
||||
box3H = (int) (atY + fr.FONT_HEIGHT * item.scale);
|
||||
|
||||
if (item.tooltip != null && item.tooltip.length > 0) {
|
||||
// Uncomment to render bounding boxes for event handling
|
||||
@@ -112,7 +112,7 @@ public class TextDataRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
if(item.onClick != null && !item.onClick.isEmpty())
|
||||
if (item.onClick != null && !item.onClick.isEmpty())
|
||||
if ((mouseX >= box1X && mouseX <= box1W && mouseY >= box1Y && mouseY <= box1H && box1X != box1W && box1Y != box1H) || (mouseX >= box2X && mouseX <= box2W && mouseY >= box2Y && mouseY <= box2H && box2X != box2W && box2Y != box2H) || (mouseX >= box3X && mouseX <= box3W && mouseY >= box3Y && mouseY <= box3H && box3X != box3W && box1Y != box3H)) {
|
||||
action = item.onClick;
|
||||
}
|
||||
@@ -136,12 +136,12 @@ public class TextDataRenderer {
|
||||
|
||||
public static String[] cropStringBySize(String s, String modifiers, int width, int height, int firstWidth, FontRenderer fr, float scale) {
|
||||
int curWidth = 0;
|
||||
int curHeight = (int)(fr.FONT_HEIGHT * scale);
|
||||
int curHeight = (int) (fr.FONT_HEIGHT * scale);
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
curWidth += fr.getStringWidth(modifiers + Character.toString(s.charAt(i))) * scale;
|
||||
|
||||
if ((curHeight == (int)(fr.FONT_HEIGHT * scale) && curWidth > firstWidth) || (curHeight != (int)(fr.FONT_HEIGHT * scale) && curWidth > width)) {
|
||||
if ((curHeight == (int) (fr.FONT_HEIGHT * scale) && curWidth > firstWidth) || (curHeight != (int) (fr.FONT_HEIGHT * scale) && curWidth > width)) {
|
||||
int oldI = i;
|
||||
while (i >= 0 && s.charAt(i) != ' ')
|
||||
i--;
|
||||
@@ -225,10 +225,10 @@ public class TextDataRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
public static void drawScaledString(FontRenderer font, String text, float x, float y, int color, boolean dropShadow, float scale){
|
||||
public static void drawScaledString(FontRenderer font, String text, float x, float y, int color, boolean dropShadow, float scale) {
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.translate(x, y, 0);
|
||||
GlStateManager.scale(scale, scale, 1F);
|
||||
GlStateManager.scale(scale, scale, 1F);
|
||||
font.drawString(text, 0, 0, color, dropShadow);
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public abstract class BookElement extends Gui {
|
||||
|
||||
public abstract void draw(int mouseX, int mouseY, float partialTicks);
|
||||
|
||||
public void mouseClicked(int mouseX, int mouseY, int mouseButton){
|
||||
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package slimeknights.mantle.client.gui.book.element;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import slimeknights.mantle.client.book.data.SectionData;
|
||||
|
||||
public class ElementSection extends BookElement {
|
||||
|
||||
public static final int IMG_SIZE = 64;
|
||||
|
||||
public static final int WIDTH = 85;
|
||||
public static final int HEIGHT = 85;
|
||||
|
||||
private SectionData section;
|
||||
|
||||
public ElementSection(int x, int y, SectionData section) {
|
||||
super(x, y);
|
||||
|
||||
this.section = section;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int mouseX, int mouseY, float partialTicks) {
|
||||
boolean hover = mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT;
|
||||
|
||||
if (section.icon != null) {
|
||||
Minecraft.getMinecraft().renderEngine.bindTexture(section.icon.location);
|
||||
|
||||
GlStateManager.color(1F, 1F, 1F, hover ? 1F : 0.5F);
|
||||
|
||||
drawScaledCustomSizeModalRect(x + WIDTH / 2 - IMG_SIZE / 2, y + HEIGHT / 2 - IMG_SIZE / 2, section.icon.u, section.icon.v, section.icon.uw, section.icon.vh, IMG_SIZE, IMG_SIZE, section.icon.texWidth, section.icon.texHeight);
|
||||
}
|
||||
|
||||
Minecraft.getMinecraft().fontRendererObj.drawString(section.title, x + WIDTH / 2 - Minecraft.getMinecraft().fontRendererObj.getStringWidth(section.title) / 2, y + HEIGHT - Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT, hover ? 0xFF000000 : 0x7F000000);
|
||||
}
|
||||
|
||||
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
|
||||
if (section != null && mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT)
|
||||
parent.openPage(parent.book.getFirstPageNumber(section));
|
||||
}
|
||||
}
|
||||
@@ -27,14 +27,14 @@ public class ElementText extends SizedBookElement {
|
||||
public void draw(int mouseX, int mouseY, float partialTicks) {
|
||||
String action = TextDataRenderer.drawText(x, y, width, height, text, mouseX, mouseY);
|
||||
|
||||
if(doAction){
|
||||
if (doAction) {
|
||||
doAction = false;
|
||||
StringActionProcessor.process(action, parent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int mouseButton){
|
||||
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
|
||||
doAction = true;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public class NetworkWrapper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Packet will be received on both cliend and server side.
|
||||
* Packet will be received on both client and server side.
|
||||
*/
|
||||
public void registerPacket(Class<? extends AbstractPacket> packetClazz) {
|
||||
registerPacketClient(packetClazz);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package slimeknights.mantle.network.book;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.client.network.NetHandlerPlayClient;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import net.minecraftforge.fml.common.network.ByteBufUtils;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import slimeknights.mantle.client.book.BookHelper;
|
||||
import slimeknights.mantle.network.AbstractPacket;
|
||||
|
||||
public class PacketUpdateSavedPage extends AbstractPacket {
|
||||
|
||||
private String pageName;
|
||||
|
||||
public PacketUpdateSavedPage() {
|
||||
|
||||
}
|
||||
|
||||
public PacketUpdateSavedPage(String pageName) {
|
||||
this.pageName = pageName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage handleClient(NetHandlerPlayClient netHandler) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage handleServer(NetHandlerPlayServer netHandler) {
|
||||
if (netHandler.playerEntity != null && pageName != null) {
|
||||
EntityPlayer player = netHandler.playerEntity;
|
||||
|
||||
ItemStack is = player.getHeldItem();
|
||||
|
||||
BookHelper.writeSavedPage(is, pageName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
pageName = ByteBufUtils.readUTF8String(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
ByteBufUtils.writeUTF8String(buf, pageName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user