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