From: Niki Roo Date: Wed, 28 Jun 2017 17:08:05 +0000 (+0200) Subject: More work on TUI (can now browse a text story) X-Git-Tag: fanfix-swing-0.0.1~12^2~459 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=edfd3577ad35ca77bc0312849c6f4ccfe3394a5f;p=fanfix-swing.git More work on TUI (can now browse a text story) --- diff --git a/libs/jexer-0.0.4-sources.patch.jar b/libs/jexer-0.0.4-sources.patch.jar index 85dc5027..0816729d 100644 Binary files a/libs/jexer-0.0.4-sources.patch.jar and b/libs/jexer-0.0.4-sources.patch.jar differ diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java b/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java index 9572566f..9613e632 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java +++ b/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java @@ -1,5 +1,8 @@ package be.nikiroo.fanfix.reader; +import java.util.ArrayList; +import java.util.List; + import jexer.*; import jexer.event.TResizeEvent; import be.nikiroo.fanfix.Instance; @@ -12,47 +15,55 @@ public class TuiReaderStoryWindow extends TWindow { private MetaData meta; private Story story; private TText textField; + private int chapter = -2; + private List navigationButtons; + private TLabel chapterName; public TuiReaderStoryWindow(TApplication app, MetaData meta) { + this(app, meta, 0); + } + + public TuiReaderStoryWindow(TApplication app, MetaData meta, int chapter) { super(app, desc(meta), 0, 0, 60, 18, CENTERED | RESIZABLE); this.meta = meta; - // /TODO: status bar with info, buttons to change chapter << < Chapter 0 - // : xxx.. > >> (max size for name = getWith() - X) + // TODO: show all meta info before? - // TODO: show all meta info before - // TODO: text.append("Resume:\n\n "); -> to status bar - // -2 because 0-based, 2 for borders, -1 to hide the HScroll textField = addText("", 0, 0, getWidth() - 2, getHeight() - 2); - - Chapter resume = getStory().getMeta().getResume(); - if (resume != null) { - for (Paragraph para : resume) { - // TODO: This is not efficient, should be changed - for (String line : para.getContent().split("\n")) { - textField.addLine(line); - } - } - } - + statusBar = newStatusBar(desc(meta)); statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit"); - + + navigationButtons = new ArrayList(4); + // -3 because 0-based and 2 for borders - TButton first = addButton("<<", 0, getHeight() - 3, - new TAction() { + int row = getHeight() - 3; + + navigationButtons.add(addButton("<<", 0, row, new TAction() { public void DO() { - // TODO + setChapter(0); } - } - ); - addButton("<", 3, getHeight() - 3, null); - addButton(">", 5, getHeight() - 3, null); - addButton(">>", 7, getHeight() - 3, null); - // TODO: pad with space up to end of window - // TODO: do not show "0/x: " for desc, only for other chapters - addLabel(String.format(" %d/%d: %s", resume.getNumber(), getStory().getChapters().size(), resume.getName()), 11, getHeight() - 3); + })); + navigationButtons.add(addButton("<", 3, row, new TAction() { + public void DO() { + setChapter(TuiReaderStoryWindow.this.chapter - 1); + } + })); + navigationButtons.add(addButton(">", 5, row, new TAction() { + public void DO() { + setChapter(TuiReaderStoryWindow.this.chapter + 1); + } + })); + navigationButtons.add(addButton(">>", 7, row, new TAction() { + public void DO() { + setChapter(getStory().getChapters().size()); + } + })); + + chapterName = addLabel("", 11, row); + chapterName.setWidth(getWidth() - 10); + setChapter(chapter); } @Override @@ -63,11 +74,70 @@ public class TuiReaderStoryWindow extends TWindow { textField.setWidth(resize.getWidth()); textField.setHeight(resize.getHeight() - 2); textField.reflow(); + + // -3 because 0-based and 2 for borders + int row = getHeight() - 3; + + String name = chapterName.getLabel(); + while (name.length() < resize.getWidth() - chapterName.getX()) { + name += " "; + } + chapterName.setLabel(name); + chapterName.setWidth(resize.getWidth() - 10); + chapterName.setY(row); + + for (TButton button : navigationButtons) { + button.setY(row); + } + } + + private void setChapter(int chapter) { + if (chapter < 0) { + chapter = 0; + } + + if (chapter > getStory().getChapters().size()) { + chapter = getStory().getChapters().size(); + } + + if (chapter != this.chapter) { + this.chapter = chapter; + Chapter chap; + String name; + if (chapter == 0) { + chap = getStory().getMeta().getResume(); + name = String.format(" %s", chap.getName()); + } else { + chap = getStory().getChapters().get(chapter - 1); + name = String.format(" %d/%d: %s", chapter, getStory() + .getChapters().size(), chap.getName()); + } + + while (name.length() < getWidth() - chapterName.getX()) { + name += " "; + } + + chapterName.setLabel(name); + + StringBuilder builder = new StringBuilder(); + // TODO: i18n + String c = String.format("Chapter %d: %s", chapter, chap.getName()); + builder.append(c).append("\n"); + for (int i = 0; i < c.length(); i++) { + builder.append("═"); + } + builder.append("\n\n"); + for (Paragraph para : chap) { + builder.append(para.getContent()).append("\n\n"); + } + textField.setText(builder.toString()); + textField.reflow(); + } } private Story getStory() { if (story == null) { - // TODO: progress bar + // TODO: progress bar? story = Instance.getLibrary().getStory(meta.getLuid(), null); } return story;