ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/booklet/BookletUtils.java

383 lines
17 KiB
Java
Raw Normal View History

/*
* This file ("BookletUtils.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.booklet;
2015-11-13 21:40:38 +01:00
import ellpeck.actuallyadditions.achievement.InitAchievements;
2015-12-04 18:14:03 +01:00
import ellpeck.actuallyadditions.booklet.button.BookmarkButton;
import ellpeck.actuallyadditions.booklet.button.IndexButton;
import ellpeck.actuallyadditions.booklet.button.TexturedButton;
import ellpeck.actuallyadditions.booklet.chapter.BookletChapter;
import ellpeck.actuallyadditions.booklet.entry.BookletEntry;
import ellpeck.actuallyadditions.booklet.entry.BookletEntryAllSearch;
import ellpeck.actuallyadditions.booklet.page.BookletPage;
import ellpeck.actuallyadditions.util.*;
import net.minecraft.client.gui.GuiButton;
2015-11-13 21:40:38 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Achievement;
import net.minecraft.util.EnumChatFormatting;
import java.awt.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.Locale;
public class BookletUtils{
/**
* Tries to open a URL in the Browser
2015-12-01 19:48:09 +01:00
*/
public static void openBrowser(String url){
openBrowser(url, url);
}
/**
* Tries to open a URL in the Browser
*
* @param url The URL
* @param shiftUrl The URL to open when Shift is held
*/
public static void openBrowser(String url, String shiftUrl){
try{
if(Desktop.isDesktopSupported()){
if(shiftUrl.equals(url) || KeyUtil.isShiftPressed()){
Desktop.getDesktop().browse(new URI(shiftUrl));
}
else{
Desktop.getDesktop().browse(new URI(url));
}
}
}
catch(Exception e){
ModUtil.LOGGER.error("Something bad happened when trying to open a URL!", e);
}
}
/**
* Draws the Title of the current chapter, current index entry or just "Actually Additions" if neither is present
*/
public static void drawTitle(GuiBooklet booklet){
2015-12-06 01:01:18 +01:00
booklet.mc.getTextureManager().bindTexture(GuiBooklet.resLoc);
//Upper title
booklet.drawTexturedModalRect(booklet.guiLeft+booklet.xSize/2-142/2, booklet.guiTop-12, 0, 240, 142, 12);
//Lower title
booklet.drawTexturedModalRect(booklet.guiLeft+booklet.xSize/2-142/2, booklet.guiTop+booklet.ySize, 0, 243, 142, 13);
String strg = booklet.currentEntrySet.chapter == null ? (booklet.currentEntrySet.entry == null ? StringUtil.localize("itemGroup."+ModUtil.MOD_ID_LOWER) : booklet.currentEntrySet.entry.getLocalizedName()) : booklet.currentEntrySet.chapter.getLocalizedName();
booklet.drawCenteredString(booklet.getFontRenderer(), strg, booklet.guiLeft+booklet.xSize/2, booklet.guiTop-9, StringUtil.DECIMAL_COLOR_WHITE);
}
2015-11-13 21:40:38 +01:00
/**
* Draws an Achievement Info if the page has items that trigger achievements
2015-12-01 19:48:09 +01:00
*
2015-11-13 21:40:38 +01:00
* @param pre If the hover info texts or the icon should be drawn
*/
public static void drawAchievementInfo(GuiBooklet booklet, boolean pre, int mouseX, int mouseY){
if(booklet.currentEntrySet.chapter == null){
2015-11-13 21:40:38 +01:00
return;
}
ArrayList<String> infoList = null;
for(BookletPage page : booklet.currentEntrySet.chapter.pages){
2015-11-16 19:57:46 +01:00
if(page != null && page.getItemStacksForPage() != null){
for(ItemStack stack : page.getItemStacksForPage()){
for(Achievement achievement : InitAchievements.achievementList){
if(stack != null && achievement.theItemStack != null && achievement.theItemStack.isItemEqual(stack)){
if(pre){
booklet.mc.getTextureManager().bindTexture(GuiBooklet.resLoc);
booklet.drawTexturedModalRect(booklet.guiLeft+booklet.xSize+1, booklet.guiTop-18, 166, 154, 22, 21);
return;
}
else{
if(mouseX >= booklet.guiLeft+booklet.xSize+1 && mouseX < booklet.guiLeft+booklet.xSize+1+22 && mouseY >= booklet.guiTop-18 && mouseY < booklet.guiTop-18+21){
if(infoList == null){
infoList = new ArrayList<String>();
infoList.add(EnumChatFormatting.GOLD+"Achievements related to this chapter:");
}
infoList.add("-"+StringUtil.localize(achievement.statId));
infoList.add(EnumChatFormatting.GRAY+"("+achievement.getDescription()+")");
}
2015-11-13 21:40:38 +01:00
}
}
}
}
}
}
if(infoList != null){
booklet.drawHoveringText(infoList, mouseX, mouseY);
2015-11-13 21:40:38 +01:00
}
}
/**
* Pre-renders the booklet page, including
* -the number of a page and its content (text, crafting recipe etc.)
* -the number of a page in a chapter
* -the amount of words and chars in the index (Just for teh lulz)
*/
public static void renderPre(GuiBooklet booklet, int mouseX, int mouseY, int ticksElapsed, boolean mousePressed){
if(booklet.currentEntrySet.entry != null){
//Renders Booklet Page Number and Content
if(booklet.currentEntrySet.chapter != null && booklet.currentEntrySet.page != null){
booklet.drawCenteredString(booklet.getFontRenderer(), booklet.currentEntrySet.page.getID()+"/"+booklet.currentEntrySet.chapter.pages.length, booklet.guiLeft+booklet.xSize/2, booklet.guiTop+172, StringUtil.DECIMAL_COLOR_WHITE);
booklet.currentEntrySet.page.renderPre(booklet, mouseX, mouseY, ticksElapsed, mousePressed);
}
//Renders Chapter Page Number
else{
booklet.drawCenteredString(booklet.getFontRenderer(), booklet.currentEntrySet.pageInIndex+"/"+booklet.indexPageAmount, booklet.guiLeft+booklet.xSize/2, booklet.guiTop+172, StringUtil.DECIMAL_COLOR_WHITE);
}
}
//Renders the amount of words and chars the book has
else{
String wordCountString = StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".amountOfWords", InitBooklet.wordCount);
booklet.getFontRenderer().drawString(EnumChatFormatting.ITALIC+wordCountString, booklet.guiLeft+booklet.xSize-booklet.getFontRenderer().getStringWidth(wordCountString)-15, booklet.guiTop+booklet.ySize-18-booklet.getFontRenderer().FONT_HEIGHT, 0);
String charCountString = StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".amountOfChars", InitBooklet.charCount);
booklet.getFontRenderer().drawString(EnumChatFormatting.ITALIC+charCountString, booklet.guiLeft+booklet.xSize-booklet.getFontRenderer().getStringWidth(charCountString)-15, booklet.guiTop+booklet.ySize-18, 0);
}
}
/**
* Draws all of the hovering texts for the buttons that need explanation in the booklet
*/
@SuppressWarnings("unchecked")
public static void doHoverTexts(GuiBooklet booklet, int mouseX, int mouseY){
2015-12-04 18:14:03 +01:00
//Update all of the buttons' hovering texts
for(Object button : booklet.getButtonList()){
if(button instanceof GuiButton && ((GuiButton)button).visible && ((GuiButton)button).func_146115_a()){
if(button instanceof BookmarkButton){
2015-11-16 19:57:46 +01:00
((BookmarkButton)button).drawHover(mouseX, mouseY);
}
2015-12-04 18:14:03 +01:00
else if(button instanceof TexturedButton){
booklet.drawHoveringText(((TexturedButton)button).textList, mouseX, mouseY);
}
2015-11-16 19:57:46 +01:00
}
}
}
/**
* Updates the search bar, should be called when it is getting typed into
*/
@SuppressWarnings("unchecked")
public static void updateSearchBar(GuiBooklet booklet){
if(booklet.currentEntrySet.entry instanceof BookletEntryAllSearch){
BookletEntryAllSearch currentEntry = (BookletEntryAllSearch)booklet.currentEntrySet.entry;
if(booklet.searchField.getText() != null && !booklet.searchField.getText().isEmpty()){
currentEntry.chapters.clear();
for(BookletChapter chapter : currentEntry.allChapters){
if(chapter.getLocalizedName().toLowerCase(Locale.ROOT).contains(booklet.searchField.getText().toLowerCase(Locale.ROOT))){
currentEntry.chapters.add(chapter);
}
}
}
else{
currentEntry.chapters = (ArrayList<BookletChapter>)currentEntry.allChapters.clone();
}
openIndexEntry(booklet, booklet.currentEntrySet.entry, booklet.currentEntrySet.pageInIndex, false);
}
}
2015-12-01 19:48:09 +01:00
/**
* Opens an index entry in the booklet.
*
* @param resetTextField will clear the text in the searchField and reset the search entry's data
*/
@SuppressWarnings("unchecked")
public static void openIndexEntry(GuiBooklet booklet, BookletEntry entry, int page, boolean resetTextField){
booklet.searchField.setVisible(entry instanceof BookletEntryAllSearch);
booklet.searchField.setFocused(entry instanceof BookletEntryAllSearch);
if(resetTextField){
booklet.searchField.setText("");
if(entry instanceof BookletEntryAllSearch){
entry.chapters = (ArrayList<BookletChapter>)((BookletEntryAllSearch)entry).allChapters.clone();
}
}
booklet.currentEntrySet.page = null;
booklet.currentEntrySet.chapter = null;
2015-12-01 19:48:09 +01:00
booklet.currentEntrySet.entry = entry;
2015-12-01 19:48:09 +01:00
booklet.indexPageAmount = entry == null ? 1 : entry.chapters.size()/booklet.chapterButtons.length+1;
booklet.currentEntrySet.pageInIndex = entry == null ? 1 : (booklet.indexPageAmount <= page || page <= 0 ? booklet.indexPageAmount : page);
2015-12-01 19:48:09 +01:00
booklet.buttonPreviousScreen.visible = entry != null;
booklet.buttonForward.visible = booklet.currentEntrySet.pageInIndex < booklet.indexPageAmount;
booklet.buttonBackward.visible = booklet.currentEntrySet.pageInIndex > 1;
2015-12-01 19:48:09 +01:00
for(int i = 0; i < booklet.chapterButtons.length; i++){
IndexButton button = (IndexButton)booklet.chapterButtons[i];
if(entry == null){
boolean entryExists = InitBooklet.entries.size() > i;
button.visible = entryExists;
if(entryExists){
button.displayString = InitBooklet.entries.get(i).getNameWithColor();
button.chap = null;
}
}
else{
boolean entryExists = entry.chapters.size() > i+(booklet.chapterButtons.length*booklet.currentEntrySet.pageInIndex-booklet.chapterButtons.length);
2015-12-01 19:48:09 +01:00
button.visible = entryExists;
if(entryExists){
BookletChapter chap = entry.chapters.get(i+(booklet.chapterButtons.length*booklet.currentEntrySet.pageInIndex-booklet.chapterButtons.length));
2015-12-01 19:48:09 +01:00
button.displayString = chap.getNameWithColor();
button.chap = chap;
}
}
}
}
/**
* Called when one of the buttons to open an index or a chapter is pressed
*/
public static void handleChapterButtonClick(GuiBooklet booklet, GuiButton button){
int place = Util.arrayContains(booklet.chapterButtons, button);
if(place >= 0){
if(booklet.currentEntrySet.entry != null){
if(booklet.currentEntrySet.chapter == null){
if(place < booklet.currentEntrySet.entry.chapters.size()){
BookletChapter chap = booklet.currentEntrySet.entry.chapters.get(place+(booklet.chapterButtons.length*booklet.currentEntrySet.pageInIndex-booklet.chapterButtons.length));
openChapter(booklet, chap, chap.pages[0]);
}
}
}
else{
if(place < InitBooklet.entries.size()){
openIndexEntry(booklet, InitBooklet.entries.get(place), 1, true);
}
}
}
}
/**
2015-12-01 19:48:09 +01:00
* Opens a chapter in the booklet.
* Can only be done when the chapter is not null and an index entry is opened in the booklet
*/
2015-12-01 19:48:09 +01:00
public static void openChapter(GuiBooklet booklet, BookletChapter chapter, BookletPage page){
if(chapter == null || booklet.currentEntrySet.entry == null){
2015-12-01 19:48:09 +01:00
return;
}
2015-12-01 19:48:09 +01:00
booklet.searchField.setVisible(false);
booklet.searchField.setFocused(false);
booklet.searchField.setText("");
booklet.currentEntrySet.chapter = chapter;
booklet.currentEntrySet.page = page != null && doesChapterHavePage(chapter, page) ? page : chapter.pages[0];
2015-12-01 19:48:09 +01:00
booklet.buttonForward.visible = getNextPage(chapter, booklet.currentEntrySet.page) != null;
booklet.buttonBackward.visible = getPrevPage(chapter, booklet.currentEntrySet.page) != null;
2015-12-01 19:48:09 +01:00
booklet.buttonPreviousScreen.visible = true;
for(GuiButton chapterButton : booklet.chapterButtons){
chapterButton.visible = false;
}
}
/**
2015-12-01 19:48:09 +01:00
* Checks if a chapter has a certain page
*/
2015-12-01 19:48:09 +01:00
private static boolean doesChapterHavePage(BookletChapter chapter, BookletPage page){
for(BookletPage aPage : chapter.pages){
if(aPage == page){
return true;
}
}
2015-12-01 19:48:09 +01:00
return false;
}
/**
* Gets the next available page in the booklet (or null if there is none)
*/
private static BookletPage getNextPage(BookletChapter chapter, BookletPage page){
for(int i = 0; i < chapter.pages.length; i++){
if(chapter.pages[i] == page){
if(i+1 < chapter.pages.length){
return chapter.pages[i+1];
}
}
}
return null;
}
/**
* Gets the previous available page in the booklet (or null if there is none)
*/
private static BookletPage getPrevPage(BookletChapter chapter, BookletPage page){
for(int i = 0; i < chapter.pages.length; i++){
if(chapter.pages[i] == page){
if(i-1 >= 0){
return chapter.pages[i-1];
}
}
}
return null;
}
/**
2015-12-01 19:48:09 +01:00
* Called when the "next page"-button is pressed
*/
2015-12-01 19:48:09 +01:00
public static void handleNextPage(GuiBooklet booklet){
if(booklet.currentEntrySet.entry != null){
if(booklet.currentEntrySet.page != null){
BookletPage page = getNextPage(booklet.currentEntrySet.chapter, booklet.currentEntrySet.page);
2015-12-01 19:48:09 +01:00
if(page != null){
booklet.currentEntrySet.page = page;
2015-12-01 19:48:09 +01:00
}
booklet.buttonForward.visible = getNextPage(booklet.currentEntrySet.chapter, booklet.currentEntrySet.page) != null;
booklet.buttonBackward.visible = getPrevPage(booklet.currentEntrySet.chapter, booklet.currentEntrySet.page) != null;
2015-12-01 19:48:09 +01:00
}
else{
if(booklet.currentEntrySet.pageInIndex+1 <= booklet.indexPageAmount){
openIndexEntry(booklet, booklet.currentEntrySet.entry, booklet.currentEntrySet.pageInIndex+1, !(booklet.currentEntrySet.entry instanceof BookletEntryAllSearch));
}
}
}
}
/**
2015-12-01 19:48:09 +01:00
* Called when the "previous page"-button is pressed
*/
2015-12-01 19:48:09 +01:00
public static void handlePreviousPage(GuiBooklet booklet){
if(booklet.currentEntrySet.entry != null){
if(booklet.currentEntrySet.page != null){
BookletPage page = getPrevPage(booklet.currentEntrySet.chapter, booklet.currentEntrySet.page);
2015-12-01 19:48:09 +01:00
if(page != null){
booklet.currentEntrySet.page = page;
2015-12-01 19:48:09 +01:00
}
booklet.buttonForward.visible = getNextPage(booklet.currentEntrySet.chapter, booklet.currentEntrySet.page) != null;
booklet.buttonBackward.visible = getPrevPage(booklet.currentEntrySet.chapter, booklet.currentEntrySet.page) != null;
2015-12-01 19:48:09 +01:00
}
else{
if(booklet.currentEntrySet.pageInIndex-1 > 0){
openIndexEntry(booklet, booklet.currentEntrySet.entry, booklet.currentEntrySet.pageInIndex-1, !(booklet.currentEntrySet.entry instanceof BookletEntryAllSearch));
}
}
}
2015-12-01 19:48:09 +01:00
}
2015-12-01 19:48:09 +01:00
public static BookletPage getFirstPageForStack(ItemStack stack){
ArrayList<BookletPage> pages = getPagesForStack(stack);
return pages.isEmpty() ? null : pages.get(0);
}
2015-12-01 19:48:09 +01:00
public static ArrayList<BookletPage> getPagesForStack(ItemStack stack){
ArrayList<BookletPage> possiblePages = new ArrayList<BookletPage>();
for(BookletPage page : InitBooklet.pagesWithItemStackData){
if(ItemUtil.contains(page.getItemStacksForPage(), stack, true)){
possiblePages.add(page);
}
}
2015-12-01 19:48:09 +01:00
return possiblePages;
}
}