Skip to content

Commit

Permalink
Table of Contents
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
fuj1n committed Mar 18, 2016
1 parent 4e98678 commit 341ef7e
Show file tree
Hide file tree
Showing 37 changed files with 1,145 additions and 259 deletions.
23 changes: 21 additions & 2 deletions src/main/java/slimeknights/mantle/client/book/BookLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import slimeknights.mantle.client.book.data.content.ContentImageText;
import slimeknights.mantle.client.book.data.content.ContentSmelting;
import slimeknights.mantle.client.book.data.content.ContentSmithing;
import slimeknights.mantle.client.book.data.content.ContentStructure;
import slimeknights.mantle.client.book.data.content.ContentText;
import slimeknights.mantle.client.book.data.content.ContentTextImage;
import slimeknights.mantle.client.book.data.content.ContentTextLeftImage;
Expand Down Expand Up @@ -67,6 +68,7 @@ public BookLoader() {
registerPageType("smelting", ContentSmelting.class);
registerPageType("smithing", ContentSmithing.class);
registerPageType("block interaction", ContentBlockInteraction.class);
registerPageType("structure", ContentStructure.class);

// Register action protocols
StringActionProcessor.registerProtocol(new ProtocolGoToPage());
Expand Down Expand Up @@ -94,7 +96,7 @@ public static void registerPageType(String name, Class<? extends PageContent> cl
* @return The class of the page type, ContentError.class if page type not registered
*/
public static Class<? extends PageContent> getPageType(String name) {
return typeToContentMap.getOrDefault(name, ContentError.class);
return typeToContentMap.get(name);
}

/**
Expand All @@ -106,11 +108,28 @@ public static Class<? extends PageContent> getPageType(String name) {
* @return The book object, not immediately populated
*/
public static BookData registerBook(String name, BookRepository... repositories) {
return registerBook(name, true, true, repositories);
}

/**
* 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 appendIndex Whether an index should be added to the front of the book using a BookTransformer
* @param appendContentTable Whether a table of contents should be added to the front of each section using a BookTransformer
* @param repositories All the repositories the book will load the sections from
* @return The book object, not immediately populated
*/
public static BookData registerBook(String name, boolean appendIndex, boolean appendContentTable, BookRepository... repositories) {
BookData info = new BookData(repositories);

books.put(name.contains(":") ? name : Loader.instance().activeModContainer().getModId() + ":" + name, info);

info.addTransformer(new BookTransformer.IndexTranformer());
if (appendIndex)
info.addTransformer(new BookTransformer.IndexTranformer());
if (appendContentTable)
info.addTransformer(new BookTransformer.ContentTableTransformer());

return info;
}
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/slimeknights/mantle/client/book/BookTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
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, but before their contents are, as the pages are counted when the contents of the sections are loaded.
* Called when all the sections within the book are loaded.
*
* @param book The object to the book to transform
* @param book The object to the book to be transformed
*/
public abstract void transform(BookData book);

Expand Down Expand Up @@ -57,4 +59,40 @@ public void update(StatFileWriter writer) {
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]);
}
}
}
}
}
121 changes: 0 additions & 121 deletions src/main/java/slimeknights/mantle/client/book/ResourceHelper.java

This file was deleted.

78 changes: 57 additions & 21 deletions src/main/java/slimeknights/mantle/client/book/data/BookData.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package slimeknights.mantle.client.book.data;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
Expand All @@ -16,10 +19,6 @@
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.book.repository.BookRepository;
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;
import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;

@SideOnly(Side.CLIENT)
public class BookData implements IDataItem {
Expand All @@ -28,6 +27,7 @@ public class BookData implements IDataItem {
public transient ArrayList<SectionData> sections = new ArrayList<>();
public transient AppearanceData appearance = new AppearanceData();
public transient ArrayList<ItemStackData.ItemLink> itemLinks = new ArrayList<>();
public transient HashMap<String, String> strings = new HashMap<>();

protected final transient ArrayList<BookTransformer> transformers = new ArrayList<>();

Expand All @@ -47,6 +47,10 @@ public void load() {
try {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);

for (SectionData section : repoContents) {
section.source = repo;
}
} catch (Exception e) {
SectionData error = new SectionData();
error.name = "errorenous";
Expand All @@ -57,36 +61,68 @@ public void load() {
sections.add(error);
}

if (repo.hasAppearanceData) {
ResourceLocation appearanceLocation = getResourceLocation("appearance.json");
ResourceLocation appearanceLocation = repo.getResourceLocation("appearance.json");

if (resourceExists(appearanceLocation))
try {
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class);
} catch (Exception ignored) {
}
else
appearance = new AppearanceData();
if (repo.resourceExists(appearanceLocation))
try {
appearance = BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(appearanceLocation)), AppearanceData.class);
} catch (Exception ignored) {
}

appearance.load();

ResourceLocation itemLinkLocation = repo.getResourceLocation("items.json");

if (repo.resourceExists(itemLinkLocation)) {
try {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
} catch (Exception ignored) {
}
}

ResourceLocation languageLocation = repo.getResourceLocation("language.lang");

if (repo.resourceExists(languageLocation)) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(repo.getResource(languageLocation).getInputStream()));

appearance.load();
String next = br.readLine();

ResourceLocation itemLinkLocation = getResourceLocation("items.json");
while (next != null) {
if (!next.startsWith("//") && next.contains("=")) {
String key = next.substring(0, next.indexOf('='));
String value = next.substring(next.indexOf('=') + 1);

if (resourceExists(itemLinkLocation)) {
try {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
} catch (Exception ignored) {
strings.put(key, value);
}

next = br.readLine();
}
} catch (Exception ignored) {
}
}
}

for (SectionData section : sections) {
if (section.source == null)
section.source = BookRepository.DUMMY;

section.parent = this;
section.load();
}

for (BookTransformer transformer : transformers)
transformer.transform(this);

// Loads orphaned sections, unless something went wrong, that would only be sections added by a transformer
for (SectionData section : sections) {
section.parent = this;
section.load();
if (section.source == null)
section.source = BookRepository.DUMMY;

if (section.parent == null) {
section.parent = this;
section.load();
}
}
}

Expand Down
Loading

0 comments on commit 341ef7e

Please sign in to comment.