failed experiment.
This commit is contained in:
parent
5776fa7f94
commit
e6694bed23
|
@ -2,64 +2,65 @@ package nl.andrewlalis.blockbookbinder.model;
|
|||
|
||||
import nl.andrewlalis.blockbookbinder.util.ApplicationProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BookPage {
|
||||
private final List<String> lines;
|
||||
public static final int MAX_LINES = ApplicationProperties.getIntProp("book.page_max_lines");
|
||||
private final String[] lines;
|
||||
|
||||
public BookPage() {
|
||||
this.lines = new ArrayList<>(ApplicationProperties.getIntProp("book.page_max_lines"));
|
||||
this.lines = new String[MAX_LINES];
|
||||
Arrays.fill(this.lines, "");
|
||||
}
|
||||
|
||||
public boolean addLine(String line) {
|
||||
if (this.lines.size() >= ApplicationProperties.getIntProp("book.page_max_lines")) {
|
||||
return false;
|
||||
private BookPage(String[] lines) {
|
||||
this.lines = lines;
|
||||
}
|
||||
this.lines.add(line);
|
||||
return true;
|
||||
|
||||
public void setLine(int index, String line) {
|
||||
if (index < 0 || index >= this.lines.length) {
|
||||
throw new IndexOutOfBoundsException(index);
|
||||
}
|
||||
this.lines[index] = line;
|
||||
}
|
||||
|
||||
public String getLine(int index) {
|
||||
if (index < 0 || index >= this.lines.size()) {
|
||||
return null;
|
||||
if (index < 0 || index >= this.lines.length) {
|
||||
throw new IndexOutOfBoundsException(index);
|
||||
}
|
||||
return this.lines.get(index);
|
||||
return this.lines[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index of the line at which this offset occurs.
|
||||
* @param offset The offset, from the start of the page.
|
||||
* @return The index of the line in which the given offset is placed.
|
||||
*/
|
||||
public int getLineIndexAtOffset(int offset) {
|
||||
int lineIndex = 0;
|
||||
String line = this.getLine(lineIndex);
|
||||
if (line == null) return -1;
|
||||
while (offset - line.length() > 0) {
|
||||
offset-= line.length();
|
||||
line = this.getLine(++lineIndex);
|
||||
offset -= line.length();
|
||||
line = this.getLine(lineIndex++);
|
||||
}
|
||||
return lineIndex;
|
||||
}
|
||||
|
||||
public boolean hasContent() {
|
||||
return !this.lines.isEmpty();
|
||||
for (String line : this.lines) {
|
||||
if (!line.isBlank()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public BookPage copy() {
|
||||
BookPage c = new BookPage();
|
||||
for (String line : this.lines) {
|
||||
c.addLine(line);
|
||||
}
|
||||
return c;
|
||||
return new BookPage(Arrays.copyOf(this.lines, MAX_LINES));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.join("\n", this.lines);
|
||||
}
|
||||
|
||||
public static BookPage fromString(String s) {
|
||||
BookPage p = new BookPage();
|
||||
for (String line : s.split("\n")) {
|
||||
p.addLine(line);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ package nl.andrewlalis.blockbookbinder.model.build;
|
|||
|
||||
import nl.andrewlalis.blockbookbinder.model.Book;
|
||||
import nl.andrewlalis.blockbookbinder.model.BookPage;
|
||||
import nl.andrewlalis.blockbookbinder.util.CharWidthMapper;
|
||||
import nl.andrewlalis.blockbookbinder.util.ApplicationProperties;
|
||||
import nl.andrewlalis.blockbookbinder.util.CharWidthMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -15,22 +15,22 @@ public class BookBuilder {
|
|||
* @return A book containing the source text formatted for a minecraft book.
|
||||
*/
|
||||
public Book build(String source) {
|
||||
final int maxLines = ApplicationProperties.getIntProp("book.page_max_lines");
|
||||
List<String> lines = this.convertSourceToLines(this.cleanSource(source));
|
||||
Book book = new Book();
|
||||
BookPage page = new BookPage();
|
||||
int currentPageLineCount = 0;
|
||||
|
||||
for (String line : lines) {
|
||||
page.addLine(line);
|
||||
page.setLine(currentPageLineCount, line);
|
||||
currentPageLineCount++;
|
||||
if (currentPageLineCount == maxLines) {
|
||||
if (currentPageLineCount == BookPage.MAX_LINES) {
|
||||
book.addPage(page);
|
||||
page = new BookPage();
|
||||
currentPageLineCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Check the last page, and only add it if it contains content.
|
||||
if (page.hasContent()) {
|
||||
book.addPage(page);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ public class BookPreviewPanel extends JPanel {
|
|||
private Book book;
|
||||
private int currentPage = 0;
|
||||
|
||||
private final JTextArea previewPageTextArea;
|
||||
private final JEditorPane pageEditorPane;
|
||||
private final BookPageDocumentFilter documentFilter;
|
||||
private final JLabel titleLabel;
|
||||
|
||||
|
@ -35,11 +35,11 @@ public class BookPreviewPanel extends JPanel {
|
|||
this.add(this.titleLabel, BorderLayout.NORTH);
|
||||
this.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
|
||||
this.previewPageTextArea = new JTextArea();
|
||||
this.pageEditorPane = new JEditorPane();
|
||||
this.documentFilter = new BookPageDocumentFilter();
|
||||
AbstractDocument doc = (AbstractDocument) this.previewPageTextArea.getDocument();
|
||||
AbstractDocument doc = (AbstractDocument) this.pageEditorPane.getDocument();
|
||||
doc.setDocumentFilter(this.documentFilter);
|
||||
this.previewPageTextArea.setEditable(true);
|
||||
this.pageEditorPane.setEditable(true);
|
||||
try {
|
||||
InputStream is = this.getClass().getClassLoader().getResourceAsStream("fonts/1_Minecraft-Regular.otf");
|
||||
if (is == null) {
|
||||
|
@ -47,11 +47,11 @@ public class BookPreviewPanel extends JPanel {
|
|||
}
|
||||
Font mcFont = Font.createFont(Font.TRUETYPE_FONT, is);
|
||||
mcFont = mcFont.deriveFont(24.0f);
|
||||
this.previewPageTextArea.setFont(mcFont);
|
||||
this.pageEditorPane.setFont(mcFont);
|
||||
} catch (FontFormatException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
JScrollPane previewPageScrollPane = new JScrollPane(this.previewPageTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
JScrollPane previewPageScrollPane = new JScrollPane(this.pageEditorPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
this.add(previewPageScrollPane, BorderLayout.CENTER);
|
||||
|
||||
JPanel previewButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
|
@ -99,7 +99,7 @@ public class BookPreviewPanel extends JPanel {
|
|||
return;
|
||||
}
|
||||
BookPage currentPage = this.book.getPages().get(this.currentPage);
|
||||
this.previewPageTextArea.setText(currentPage.toString());
|
||||
this.pageEditorPane.setText(currentPage.toString());
|
||||
this.titleLabel.setText("Book Preview (Page " + (this.currentPage + 1) + " of " + this.book.getPageCount() + ")");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue