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; | |
12 | import jexer.TText2; | |
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; | |
0861d62a | 24 | private TText2 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 | |
0861d62a | 39 | textField = new TText2(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 NR |
43 | |
44 | navigationButtons = new ArrayList<TButton>(4); | |
45 | ||
396e924c | 46 | // -3 because 0-based and 2 for borders |
edfd3577 NR |
47 | int row = getHeight() - 3; |
48 | ||
0861d62a NR |
49 | addButton(" ", 0, row, null); // for bg colour when << button is pressed |
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 | })); | |
70 | ||
0861d62a | 71 | chapterName = addLabel("", 14, row); |
edfd3577 NR |
72 | chapterName.setWidth(getWidth() - 10); |
73 | setChapter(chapter); | |
c1873e56 NR |
74 | } |
75 | ||
76 | @Override | |
77 | public void onResize(TResizeEvent resize) { | |
78 | super.onResize(resize); | |
79 | ||
80 | // Resize the text field | |
0861d62a | 81 | textField.setWidth(resize.getWidth() - 2); |
396e924c | 82 | textField.setHeight(resize.getHeight() - 2); |
c1873e56 | 83 | textField.reflow(); |
edfd3577 NR |
84 | |
85 | // -3 because 0-based and 2 for borders | |
86 | int row = getHeight() - 3; | |
87 | ||
88 | String name = chapterName.getLabel(); | |
89 | while (name.length() < resize.getWidth() - chapterName.getX()) { | |
90 | name += " "; | |
91 | } | |
92 | chapterName.setLabel(name); | |
93 | chapterName.setWidth(resize.getWidth() - 10); | |
94 | chapterName.setY(row); | |
95 | ||
96 | for (TButton button : navigationButtons) { | |
97 | button.setY(row); | |
98 | } | |
99 | } | |
100 | ||
101 | private void setChapter(int chapter) { | |
102 | if (chapter < 0) { | |
103 | chapter = 0; | |
104 | } | |
105 | ||
106 | if (chapter > getStory().getChapters().size()) { | |
107 | chapter = getStory().getChapters().size(); | |
108 | } | |
109 | ||
110 | if (chapter != this.chapter) { | |
111 | this.chapter = chapter; | |
112 | Chapter chap; | |
113 | String name; | |
114 | if (chapter == 0) { | |
115 | chap = getStory().getMeta().getResume(); | |
116 | name = String.format(" %s", chap.getName()); | |
117 | } else { | |
118 | chap = getStory().getChapters().get(chapter - 1); | |
119 | name = String.format(" %d/%d: %s", chapter, getStory() | |
120 | .getChapters().size(), chap.getName()); | |
121 | } | |
122 | ||
123 | while (name.length() < getWidth() - chapterName.getX()) { | |
124 | name += " "; | |
125 | } | |
126 | ||
127 | chapterName.setLabel(name); | |
128 | ||
129 | StringBuilder builder = new StringBuilder(); | |
130 | // TODO: i18n | |
131 | String c = String.format("Chapter %d: %s", chapter, chap.getName()); | |
132 | builder.append(c).append("\n"); | |
133 | for (int i = 0; i < c.length(); i++) { | |
134 | builder.append("═"); | |
135 | } | |
136 | builder.append("\n\n"); | |
137 | for (Paragraph para : chap) { | |
138 | builder.append(para.getContent()).append("\n\n"); | |
139 | } | |
140 | textField.setText(builder.toString()); | |
141 | textField.reflow(); | |
142 | } | |
c1873e56 NR |
143 | } |
144 | ||
145 | private Story getStory() { | |
146 | if (story == null) { | |
edfd3577 | 147 | // TODO: progress bar? |
c1873e56 NR |
148 | story = Instance.getLibrary().getStory(meta.getLuid(), null); |
149 | } | |
150 | return story; | |
151 | } | |
152 | ||
153 | private static String desc(MetaData meta) { | |
154 | return String.format("%s: %s", meta.getLuid(), meta.getTitle()); | |
155 | } | |
156 | } |