1
2 package org.catacomb.dataview;
3
4
5
6 import org.catacomb.dataview.gui.FramePlayer;
7 import org.catacomb.druid.gui.edit.DruButton;
8 import org.catacomb.druid.gui.edit.DruSlider;
9 import org.catacomb.interlish.structure.*;
10 import org.catacomb.movie.gif.AnimatedGifEncoder;
11 import org.catacomb.numeric.data.DataExtractor;
12 import org.catacomb.report.E;
13 import org.catacomb.util.AWTUtil;
14
15
16 import java.awt.image.BufferedImage;
17 import java.io.File;
18
19
20
21
22
23
24
25 public class FrameController implements Controller, GUISourced {
26
27 DruSlider frameSlider;
28
29 String sourcePath;
30 String displayPath;
31
32 int[] indexes;
33 String[] displayValues;
34
35
36 DataviewController dataviewController;
37
38 DataExtractor dataExtractor;
39
40
41 double speed = 1.;
42
43 int shownFrame;
44
45 FramePlayer framePlayer;
46
47 boolean isPaused;
48
49 DruButton pauseButton;
50
51
52
53 public FrameController(String src, String dply) {
54 super();
55 sourcePath = src;
56 displayPath = dply;
57 }
58
59
60 public void setDataviewController(DataviewController dvc) {
61 dataviewController = dvc;
62 }
63
64 public void setDataSource(DataExtractor dex) {
65 dataExtractor = dex;
66 if (frameSlider != null) {
67 applyData();
68 }
69 }
70
71 public void attached() {
72 }
73
74
75 public void markNeeded() {
76 dataExtractor.mark(sourcePath);
77 dataExtractor.mark(displayPath);
78 }
79
80
81
82 public void applyData() {
83 indexes = dataExtractor.getIntVector(sourcePath);
84 double[] adv = dataExtractor.getVector(displayPath);
85
86 displayValues = new String[indexes.length];
87 for (int i = 0; i < indexes.length; i++) {
88 displayValues[i] = "" + adv[indexes[i]];
89
90 }
91 frameSlider.setValues(displayValues);
92 showFrame(0);
93 }
94
95
96 public void showFrame(int iframein) {
97 int iframe = iframein;
98 if (iframe < 0) {
99 iframe = 0;
100 }
101
102 if (iframe >= indexes.length) {
103 iframe = indexes.length-1;
104 }
105
106 dataviewController.showFrame(indexes[iframe]);
107 if (frameSlider != null) {
108 frameSlider.showValue(iframe);
109 }
110 shownFrame = iframe;
111 }
112
113
114
115 public String getGUITargets() {
116 return "Slider PauseButton";
117 }
118
119
120 public String getGUISources() {
121 return "*";
122 }
123
124 public void show(Object obj) {
125 }
126
127
128 public void setSlider(DruSlider dfs) {
129 frameSlider = dfs;
130 if (dataExtractor != null) {
131 applyData();
132 }
133 }
134
135
136 public void setPauseButton(DruButton db) {
137 pauseButton = db;
138 }
139
140
141
142 public void sliderMoved() {
143 int ival = frameSlider.getValue();
144 showFrame(ival);
145 }
146
147
148 public void rewind() {
149 stop();
150 showFrame(0);
151 }
152
153
154
155
156
157 public void pause() {
158 if (isPaused) {
159 dePause();
160 start();
161
162 } else {
163 rePause();
164 stop();
165 }
166 }
167
168
169 public void dePause() {
170 isPaused = false;
171 pauseButton.setLabelText(" pause ");
172 }
173
174 private void rePause() {
175 isPaused = true;
176 pauseButton.setLabelText("resume");
177 }
178
179
180 public void play() {
181 stop();
182 rewind();
183 start();
184 dePause();
185 }
186
187
188 private void start() {
189 if (indexes != null) {
190 framePlayer = null;
191 framePlayer.start();
192 }
193 }
194
195
196 public void stop() {
197 if (framePlayer != null) {
198 framePlayer.stop();
199 }
200 }
201
202
203
204
205 public void faster() {
206 speed *= 1.3;
207 }
208
209 public void slower() {
210 speed /= 1.3;
211 }
212
213
214 public boolean canAdvance() {
215 return (indexes != null && shownFrame < indexes.length-1);
216 }
217
218
219 public void advance() {
220 showFrame(shownFrame + 1);
221 }
222
223
224 public double getSpeed() {
225 return speed;
226 }
227
228
229
230 public void record() {
231 E.missing();
232
233
234
235
236
237
238
239 }
240
241
242 public void miniRecord() {
243 E.missing();
244
245
246
247
248
249
250 }
251
252
253
254 public void makeMovie(File f) {
255 makeAnimatedGif(f);
256 }
257
258 public void makeThumbnailMovie(File f) {
259 makeMiniAnimatedGif(f, 160, 160);
260 }
261
262
263
264 public void makeAnimatedGif(File f) {
265 stop();
266 rewind();
267
268
269 AnimatedGifEncoder enc = new AnimatedGifEncoder();
270
271 enc.start(f);
272 enc.setDelay(160);
273
274 int ifr = 0;
275 E.info("animated gif - frame " + ifr);
276 enc.addFrame(dataviewController.getBufferedImage(1));
277
278 while (canAdvance()) {
279 advance();
280 ifr += 1;
281 E.info("animated gif - frame " + ifr);
282 enc.addFrame(dataviewController.getBufferedImage(1));
283 }
284
285 enc.finish();
286
287 }
288
289
290
291
292
293
294 public void makeMiniAnimatedGif(File f, int wsclin, int hsclin) {
295 int wscl = wsclin;
296 int hscl = hsclin;
297 stop();
298 rewind();
299
300
301 AnimatedGifEncoder enc = new AnimatedGifEncoder();
302
303 enc.start(f);
304 enc.setDelay(200);
305
306 int nfr = indexes.length;
307
308 int stepsize = nfr / 10;
309 if (stepsize < 1) {
310 stepsize = 1;
311 }
312
313 int ifr = 0;
314
315 int ithick = 1;
316
317 while (canAdvance()) {
318 E.info("animated gif - frame " + ifr);
319 BufferedImage bim = dataviewController.getBufferedImage(ithick);
320
321 if (ifr == 0) {
322 int wf = bim.getWidth();
323 int hf = bim.getHeight();
324 double fo = ((float)wscl) / hscl;
325 double ff = ((float)wf) / hf;
326 if (ff > fo) {
327 hscl = (int)(wscl / ff);
328 } else {
329 wscl = (int)(hscl * ff);
330 }
331 ithick = ((wf + wscl/2) / wscl);
332
333 bim = dataviewController.getBufferedImage(ithick);
334 }
335
336 BufferedImage bufim = AWTUtil.getScaledBufferedImage(bim, wscl, hscl);
337
338 enc.addFrame(bufim);
339 ifr += 1;
340
341 for (int i = 0; i < stepsize; i++) {
342 if (canAdvance()) {
343 advance();
344 }
345 }
346 }
347
348 enc.finish();
349
350 }
351
352
353 }
354
355