Files
Mantle/src/main/java/slimeknights/mantle/client/book/BookTransformer.java
T
Arthur Uzulin 341ef7e41d Table of Contents
- Not ready for prime implementation of 3D structures
- Added table of contents to beginning of each section
- Extended load method to PageContent
- A less hard-coded imagedata loading implementation (DataLocation)
- Repositories handle their own files
- Removed title from SectionData, replaced with translation strings
- BookTransformers now invoked after loading pages
- Fixed TextDataRenderer incorrectly calculating condition for ellipsis
2016-03-18 21:48:16 +11:00

99 lines
3.2 KiB
Java

package slimeknights.mantle.client.book;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.minecraft.stats.StatFileWriter;
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;
import slimeknights.mantle.client.book.data.content.ContentTableOfContents;
import slimeknights.mantle.client.book.data.element.TextData;
public abstract class BookTransformer {
/**
* Called when all the sections within the book are loaded.
*
* @param book The object to the book to be transformed
*/
public abstract void transform(BookData book);
protected static class IndexTranformer extends BookTransformer {
@Override
public void transform(BookData book) {
SectionData index = new SectionData(true) {
@Override
public void update(StatFileWriter writer) {
pages.clear();
List<SectionData> visibleSections = parent.getVisibleSections(writer);
if (visibleSections.isEmpty())
return;
visibleSections.remove(0);
PageData[] pages = new PageData[(int) Math.ceil(visibleSections.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 < visibleSections.size(); j++) {
content.addSection(visibleSections.get(j));
}
}
this.pages = new ArrayList<>(Arrays.asList(pages));
}
};
index.name = "index";
book.sections.add(0, index);
}
}
protected static class ContentTableTransformer extends BookTransformer {
@Override
public void transform(BookData book) {
final int ENTRIES_PER_PAGE = 24;
for (SectionData section : book.sections) {
if (section.name.equals("index"))
continue;
int genPages = (int) Math.ceil(section.getPageCount() * 1.F / ENTRIES_PER_PAGE);
if (genPages == 0)
continue;
PageData[] pages = new PageData[genPages];
for (int i = 0; i < pages.length; i++) {
pages[i] = new PageData(true);
pages[i].name = "tableofcontents" + i;
TextData[] text = new TextData[i > pages.length - 1 ? ENTRIES_PER_PAGE : section.getPageCount() - (genPages - 1) * ENTRIES_PER_PAGE];
for (int j = 0; j < text.length; j++) {
text[j] = new TextData((i * ENTRIES_PER_PAGE + j + 1) + ". " + section.pages.get(i * ENTRIES_PER_PAGE + j).getTitle());
text[j].action = "go-to-page-rtn:" + section.name + "." + section.pages.get(i * ENTRIES_PER_PAGE + j).name;
}
pages[i].content = new ContentTableOfContents(i == 0 ? section.getTitle() : "", text);
}
for (int i = pages.length - 1; i >= 0; i--) {
section.pages.add(0, pages[i]);
}
}
}
}
}