Commit | Line | Data |
---|---|---|
c1873e56 NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
edfd3577 NR |
3 | import java.util.ArrayList; |
4 | import java.util.List; | |
5 | ||
396e924c | 6 | import jexer.*; |
c1873e56 NR |
7 | import jexer.event.TResizeEvent; |
8 | import be.nikiroo.fanfix.Instance; | |
9 | import be.nikiroo.fanfix.data.Chapter; | |
10 | import be.nikiroo.fanfix.data.MetaData; | |
11 | import be.nikiroo.fanfix.data.Paragraph; | |
12 | import be.nikiroo.fanfix.data.Story; | |
13 | ||
14 | public class TuiReaderStoryWindow extends TWindow { | |
15 | private MetaData meta; | |
16 | private Story story; | |
17 | private TText textField; | |
edfd3577 NR |
18 | private int chapter = -2; |
19 | private List<TButton> navigationButtons; | |
20 | private TLabel chapterName; | |
c1873e56 NR |
21 | |
22 | public TuiReaderStoryWindow(TApplication app, MetaData meta) { | |
edfd3577 NR |
23 | this(app, meta, 0); |
24 | } | |
25 | ||
26 | public TuiReaderStoryWindow(TApplication app, MetaData meta, int chapter) { | |
c1873e56 NR |
27 | super(app, desc(meta), 0, 0, 60, 18, CENTERED | RESIZABLE); |
28 | this.meta = meta; | |
29 | ||
edfd3577 | 30 | // TODO: show all meta info before? |
c1873e56 | 31 | |
396e924c NR |
32 | // -2 because 0-based, 2 for borders, -1 to hide the HScroll |
33 | textField = addText("", 0, 0, getWidth() - 2, getHeight() - 2); | |
edfd3577 | 34 | |
396e924c NR |
35 | statusBar = newStatusBar(desc(meta)); |
36 | statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit"); | |
edfd3577 NR |
37 | |
38 | navigationButtons = new ArrayList<TButton>(4); | |
39 | ||
396e924c | 40 | // -3 because 0-based and 2 for borders |
edfd3577 NR |
41 | int row = getHeight() - 3; |
42 | ||
43 | navigationButtons.add(addButton("<<", 0, row, new TAction() { | |
396e924c | 44 | public void DO() { |
edfd3577 | 45 | setChapter(0); |
396e924c | 46 | } |
edfd3577 NR |
47 | })); |
48 | navigationButtons.add(addButton("<", 3, row, new TAction() { | |
49 | public void DO() { | |
50 | setChapter(TuiReaderStoryWindow.this.chapter - 1); | |
51 | } | |
52 | })); | |
53 | navigationButtons.add(addButton(">", 5, row, new TAction() { | |
54 | public void DO() { | |
55 | setChapter(TuiReaderStoryWindow.this.chapter + 1); | |
56 | } | |
57 | })); | |
58 | navigationButtons.add(addButton(">>", 7, row, new TAction() { | |
59 | public void DO() { | |
60 | setChapter(getStory().getChapters().size()); | |
61 | } | |
62 | })); | |
63 | ||
64 | chapterName = addLabel("", 11, row); | |
65 | chapterName.setWidth(getWidth() - 10); | |
66 | setChapter(chapter); | |
c1873e56 NR |
67 | } |
68 | ||
69 | @Override | |
70 | public void onResize(TResizeEvent resize) { | |
71 | super.onResize(resize); | |
72 | ||
73 | // Resize the text field | |
74 | textField.setWidth(resize.getWidth()); | |
396e924c | 75 | textField.setHeight(resize.getHeight() - 2); |
c1873e56 | 76 | textField.reflow(); |
edfd3577 NR |
77 | |
78 | // -3 because 0-based and 2 for borders | |
79 | int row = getHeight() - 3; | |
80 | ||
81 | String name = chapterName.getLabel(); | |
82 | while (name.length() < resize.getWidth() - chapterName.getX()) { | |
83 | name += " "; | |
84 | } | |
85 | chapterName.setLabel(name); | |
86 | chapterName.setWidth(resize.getWidth() - 10); | |
87 | chapterName.setY(row); | |
88 | ||
89 | for (TButton button : navigationButtons) { | |
90 | button.setY(row); | |
91 | } | |
92 | } | |
93 | ||
94 | private void setChapter(int chapter) { | |
95 | if (chapter < 0) { | |
96 | chapter = 0; | |
97 | } | |
98 | ||
99 | if (chapter > getStory().getChapters().size()) { | |
100 | chapter = getStory().getChapters().size(); | |
101 | } | |
102 | ||
103 | if (chapter != this.chapter) { | |
104 | this.chapter = chapter; | |
105 | Chapter chap; | |
106 | String name; | |
107 | if (chapter == 0) { | |
108 | chap = getStory().getMeta().getResume(); | |
109 | name = String.format(" %s", chap.getName()); | |
110 | } else { | |
111 | chap = getStory().getChapters().get(chapter - 1); | |
112 | name = String.format(" %d/%d: %s", chapter, getStory() | |
113 | .getChapters().size(), chap.getName()); | |
114 | } | |
115 | ||
116 | while (name.length() < getWidth() - chapterName.getX()) { | |
117 | name += " "; | |
118 | } | |
119 | ||
120 | chapterName.setLabel(name); | |
121 | ||
122 | StringBuilder builder = new StringBuilder(); | |
123 | // TODO: i18n | |
124 | String c = String.format("Chapter %d: %s", chapter, chap.getName()); | |
125 | builder.append(c).append("\n"); | |
126 | for (int i = 0; i < c.length(); i++) { | |
127 | builder.append("═"); | |
128 | } | |
129 | builder.append("\n\n"); | |
130 | for (Paragraph para : chap) { | |
131 | builder.append(para.getContent()).append("\n\n"); | |
132 | } | |
133 | textField.setText(builder.toString()); | |
134 | textField.reflow(); | |
135 | } | |
c1873e56 NR |
136 | } |
137 | ||
138 | private Story getStory() { | |
139 | if (story == null) { | |
edfd3577 | 140 | // TODO: progress bar? |
c1873e56 NR |
141 | story = Instance.getLibrary().getStory(meta.getLuid(), null); |
142 | } | |
143 | return story; | |
144 | } | |
145 | ||
146 | private static String desc(MetaData meta) { | |
147 | return String.format("%s: %s", meta.getLuid(), meta.getTitle()); | |
148 | } | |
149 | } |