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