1 #ifndef UI_HELPER_H
2 #define UI_HELPER_H
3 #include "timers.h"
4 #include "xio.h"
5 #include "fractal.h"
6 
7 #define MAXFILTERS 20
8 #define AVRGSIZE 50
9 #define NMESSAGES 5
10 #define BGCOLOR(uih) uih->palette->index[0]
11 #define FGCOLOR(uih) uih->palette->index[1]
12 #define SELCOLOR(uih) uih->palette->index[2]
13 #define NONTRANSPARENTW 1
14 
15 struct uih_message {
16     char *message[NMESSAGES];
17     tl_timer *messagetimer[NMESSAGES];
18     int messagetype[NMESSAGES];
19     struct uih_window *w[NMESSAGES];
20     int pid[NMESSAGES];
21     int messagestart;
22 };
23 struct uih_line {
24     int key;
25     int morph;
26     int color;
27     int posmode;
28     number_t x1, y1, x2, y2;
29     int mposmode;
30     number_t mx1, my1, mx2, my2;
31     struct uih_line *next, *prev;
32     struct uih_window *w;
33 };
34 struct uih_lines {
35     struct uih_line *first;
36     int morphing;
37     int currkey;
38 };
39 
40 struct uih_savedcontext {
41     xio_file file;
42     int mode;
43     number_t speedup, maxstep;
44     number_t xcenter, ycenter;
45     tl_timer *timer;
46     tl_timer *synctimer;
47     struct fractal_context *fcontext;
48     int clearscreen;
49     int fastmode, juliamode, fastrotate, autorotate;
50     number_t rotationspeed;
51     int firsttime;
52     int filter[MAXFILTERS];
53     int pressed;
54     int rotatepressed;
55     int cycling;
56     int shifted;
57     int manualpaletteshift;
58     int direction;
59     int cyclingspeed;
60     int zoomactive;
61     int xtextpos, ytextpos;
62     int writefailed;
63     int nonfractalscreen;
64     int color;
65 };
66 #define MAXLEVEL 10 /*Maximal include level */
67 struct uih_playcontext {
68     xio_file file;
69     xio_file prevfiles[MAXLEVEL + 1];
70     int level;
71     xio_path directory;
72     tl_timer *timer;
73     int waittime;
74     int playframe;
75     int timerin;
76     int frametime, starttime;
77     int morph;
78     int morphtimes[2];
79     int morphjulia;
80     int morphjuliatimes[2];
81     int morphangle;
82     int morphangletimes[2];
83     vinfo destination;
84     vinfo source;
85     number_t srcangle, destangle;
86     number_t sr, si, dr, di;
87     int readfailed;
88     int line;
89     struct uih_lines lines;
90     int morphlinetimes[2];
91 };
92 #define SQR(val) (((double)(val)) * (val))
93 #define MORPHVALUE(time, len, starttime, endtime)                              \
94     (time) < 0                                                                 \
95         ? 0.0                                                                  \
96         : (time) >= (len)                                                      \
97               ? 1.0                                                            \
98               : (time) < (starttime) && (starttime)                            \
99                     ? (SQR((time) / (double)(starttime)) / 2 * (starttime) /   \
100                        ((len) - (starttime) / 2 - (endtime) / 2))              \
101                     : ((len) - (time) < (endtime)) && (endtime)                \
102                           ? 1 - (SQR(((len) - (time)) / (double)(endtime)) /   \
103                                  2 * (endtime) /                               \
104                                  ((len) - (starttime) / 2 - (endtime) / 2))    \
105                           : ((time) - (starttime) / 2) /                       \
106                                 ((double)(len) - (starttime) / 2 -             \
107                                  (endtime) / 2)
108 #define DOMORPH(time, len, starttime, endtime, startval, endval)               \
109     ((startval) +                                                              \
110      ((endval) - (startval)) * MORPHVALUE(time, len, starttime, endtime))
111 
112 #define UNDOLEVEL 256
113 struct uih_undocontext {
114     int last;
115     char *undos[256];
116 };
117 struct uih_context {
118     void (*updatemenus)(struct uih_context *, const char *);
119     /*stuff that should be visible from outside */
120     number_t speedup, maxstep; /*zooming speed */
121 
122     /* Information provided to the user interface: */
123     const char *menuroot;
124     int display;       /*1 when ui wants to display something */
125     int save;          /*1 if save animation is enabled */
126     int play;          /*1 if animation replay is active */
127     int cycling;       /*1 if cycling is enabled */
128     int incalculation; /*1 if calulcation is currently in process */
129     int flags;
130     int interrupt; /*set to interrupt current calculation */
131 
132     /*server's callbacks */
133     int (*passfunc)(struct uih_context *, int, const char *, float);
134     void (*longwait)(struct uih_context *);
135 
136     struct uih_undocontext undo;
137     /*Filter system state */
138     struct image *image;
139     struct palette *palette;
140     struct fractal_context *fcontext; /*fractal information */
141     struct queue *queue;
142     struct filter *uifilter; /*user interface layer */
143     struct filter *rotatef;  /* Special filters handler by ui_helper: */
144     struct filter *zengine;
145     struct filter *fixedcolor;
146     /*Julia/filter mechanizm */
147     struct filter *subwindow, *julia, *smalliter;
148     struct filter *filter[MAXFILTERS];
149 
150     /*General status variables */
151     double mul;             /*speed of last iteration */
152     int rotatemode;         /*ROTATE_NONE, ROTATE_CONTINUOUS or ROTATE_NONE */
153     number_t rotationspeed; /*speed of continuous rotation */
154     int fastmode;           /*when draw in fast mode */
155     int juliamode;
156     int fastrotate;
157     int incomplete; /*1 if image is not completely calculated or in animation */
158     int dirty;      /*1 if image is inexact */
159     int inanimation;              /*1 if uih_update wants to be called soon */
160     int fastanimation;            /*1 if animation needs to be fast */
161     int palettetype, paletteseed; /*0 for default palette,1,2 for random */
162     int clearscreen;              /*1 when ui want to clear screen */
163     int indofractal;              /*1 when caluclation is in the process */
164     int xtextpos, ytextpos;       /*positioning of text */
165     int color;                    /*Color of text */
166     int recalculatemode;          /*information for user interface */
167     int stoppedtimers;            /*1 when timers are stopped */
168     int nletters;                 /*Number of letters displayed at screen */
169     int letterspersec; /*Number of letters per second user should read */
170     char *text[3];     /*Currently displayed text information: */
171     struct uih_window *textwindow[3], *cscreenwindow;
172     int textpos[3], textcolor[3];
173     const char *errstring; /*String of last unprocessed error */
174 
175     void *font; /*Font used by UI */
176     struct uih_window *wtop;
177     int wflipped;
178     int wdisplayed;
179 
180     /*Save variables */
181     int todisplayletters;
182     struct uih_savedcontext *savec;
183     int viewchanged;    /*When to generate setview commands */
184     int palettechanged; /*When to generate setpalette commands */
185     int displaytext;    /*When text was displayed in this frame */
186     int palettepickerenabled; /*If palette picker is used */
187     int nonfractalscreen;
188     /*waiting variables */
189     void (*complettehandler)(
190         void *); /*Handler to be activated when calculation is complete */
191     void *handlerdata;
192     /*replay variables */
193     struct uih_playcontext *playc;
194     int playpos;
195     const char *playstring;
196 
197     /*For constant framerate */
198     struct timeemulator *emulator;
199     int emulatedframetime;
200     int aliasnum;
201     int fixedstep;
202 
203     /*zoom/unzoom */
204     number_t speed, step;
205     number_t xcenter, ycenter;
206     int xcenterm, ycenterm;
207     int zoomactive;
208 
209     /*drag&drop move */
210     int pressed;
211     number_t oldx, oldy;
212     int moved;
213 
214     /*drag&drop rotate */
215     int rotatepressed;
216     number_t oldangle;
217 
218     int ddatalost;
219     int tbreak;
220 
221     int autopilot; /*for uih side of autopilot */
222     int autopilotx, autopiloty, autopilotbuttons;
223 
224     /*calculation time variables */
225     int interruptiblemode;
226     int starttime, endtime;
227     int maxtime;
228 
229     /*dynamical timeout measuring */
230     int times[2][AVRGSIZE]; /*for statistics */
231     int timespos, count[2];
232     double lastspeed, lasttime;
233 
234     /*number_t xsize, ysize; */
235     tl_timer *maintimer, *cyclingtimer, *autopilottimer, *calculatetimer,
236         *doittimer;
237     tl_group *autopilotgroup;
238 
239     /*color cycling values */
240     int direction;
241     int cyclingdirection;
242     int stopped;
243     int cyclingspeed;
244 
245     /*autopilot internal values */
246     int x1, y1, c1;
247     number_t minsize;
248     number_t maxsize;
249     int autopilotversion;
250     int autime;
251     int minlong;
252     int interlevel;
253 
254     /*saved palettes */
255     struct palette *palette2;
256 
257     int paletteshift;
258     int manualpaletteshift;
259 
260     struct uih_message messg;
261 
262     /*Used by uih_update to figure out when save undo */
263     int lastbuttons;
264 
265     int encoding;
266 
267     /* performers really don't want text echoed to the screen */
268     int inhibittextoutput;
269 
270     /* user data */
271     void *data;
272 };
273 
274 typedef void (*uih_getposfunc)(struct uih_context *c, int *x, int *y,
275                                int *width, int *height, void *data);
276 typedef void (*uih_drawfunc)(struct uih_context *c, void *data);
277 struct uih_window {
278     int x, y, width, height;
279     uih_getposfunc getpos;
280     uih_drawfunc draw;
281     struct uih_window *next;
282     struct uih_window *previous;
283     int savedline, savedpos;
284     char *saveddata;
285     void *data;
286     int flags;
287 };
288 typedef struct uih_context uih_context;
289 
290 #define UIH_SAVEALL 2
291 #define UIH_SAVEANIMATION 1
292 #define UIH_SAVEPOS 0
293 
294 #define UIH_PALETTEDRAW -2
295 #define UIH_FILTERANIMATION -1
296 #define UIH_INTERRUPTIBLE 0
297 #define UIH_ANIMATION 1
298 #define UIH_NEW_IMAGE 2
299 #define UIH_UNINTERRUPTIBLE 3
300 #define FRAMETIME (1000000 / FRAMERATE)
301 
302 #define UIH_TEXTTOP 0
303 #define UIH_TEXTMIDDLE 1
304 #define UIH_TEXTBOTTOM 2
305 
306 #define UIH_TEXTLEFT 0
307 #define UIH_TEXTCENTER 1
308 #define UIH_TEXTRIGHT 2
309 
310 // use the same order here as for language1 and language2
311 #define UIH_LANG_SYS_DEFAULT 0
312 #define UIH_LANG_CS 1
313 #define UIH_LANG_EN 2
314 #define UIH_LANG_FR 3
315 #define UIH_LANG_DE 4
316 #define UIH_LANG_HI 5
317 #define UIH_LANG_HU 6
318 #define UIH_LANG_IS 7
319 #define UIH_LANG_IT 8
320 #define UIH_LANG_PT 9
321 #define UIH_LANG_RO 10
322 #define UIH_LANG_RU 11
323 #define UIH_LANG_RS 12
324 #define UIH_LANG_ES 13
325 #define UIH_LANG_SV 14
326 #define UIH_LANG_VI 15
327 
328 #define RANDOM_PALETTE_SIZE 1
329 #define FULLSCREEN 2
330 #define UPDATE_AFTER_PALETTE 4
331 #define UPDATE_AFTER_RESIZE 8
332 #define PALETTE_ROTATION 16
333 #define ASYNC_PALETTE 32
334 #define ROTATE_INSIDE_CALCULATION 64
335 #define PALETTE_REDISPLAYS 128
336 #define SCREENSIZE 256
337 #define PIXELSIZE 512
338 #define RESOLUTION 1024
339 
340 #define BUTTON1 256
341 #define BUTTON2 512
342 #define BUTTON3 1024
343 
344 #define ROTATE_NONE 0
345 #define ROTATE_MOUSE 1
346 #define ROTATE_CONTINUOUS 2
347 
348 #define uih_needrecalculate(context) ((context)->recalculatemode)
349 #define uih_needdisplay(context) ((context)->display)
350 #define GETMAX(a, b) ((a) > (b) ? (a) : (b))
351 #define uih_newimage(c)                                                        \
352     ((c)->display = 1,                                                         \
353      ((c)->recalculatemode = GETMAX((c)->recalculatemode, UIH_NEW_IMAGE)))
354 #define uih_animate_image(c)                                                   \
355     ((c)->display = 1,                                                         \
356      (c)->recalculatemode = GETMAX((c)->recalculatemode, UIH_ANIMATION))
357 
358 #define uih_updatemenus(uih, c)                                                \
359     if (uih->updatemenus != NULL)                                              \
360         uih->updatemenus(uih, c);
361 
362 extern const struct filteraction *const uih_filters[MAXFILTERS];
363 extern const int uih_nfilters;
364 
365 extern const char *const xtextposnames[];
366 extern const char *const ytextposnames[];
367 
368 extern int defthreads;
369 
370 struct uih_context *
371 uih_mkcontext(int flags, struct image *image,
372               int (*passfunc)(struct uih_context *, int, const char *, float),
373               void (*longwait)(struct uih_context *),
374               void (*updatemenus)(struct uih_context *c, const char *));
375 int uih_updateimage(uih_context *c, struct image *img);
376 void uih_freecontext(uih_context *c);
377 
378 void uih_callcomplette(uih_context *c);
379 /*palette functions */
380 void uih_mkdefaultpalette(uih_context *c);
381 void uih_mkpalette(uih_context *c);
382 void uih_savepalette(uih_context *c);
383 void uih_restorepalette(uih_context *c);
384 void uih_loadpalette(uih_context *c, struct palette *palette);
385 
386 /*autopilot handling */
387 void uih_autopilot_on(uih_context *c);
388 void uih_autopilot_off(uih_context *c);
389 
390 /*misc functions */
391 int uih_update(uih_context *c, int mousex, int mousey, int mousebuttons);
392 const char *uih_save(struct uih_context *c, xio_constpath filename, xio_file xpf_data);
393 void uih_tbreak(uih_context *c);
394 double uih_displayed(uih_context *c);
395 void uih_do_fractal(uih_context *c);
396 void uih_prepare_image(uih_context *c);
397 void uih_interrupt(uih_context *c);
398 void uih_stopzooming(uih_context *c);
399 void uih_setspeedup(uih_context *c, number_t speed);
400 void uih_setmaxstep(uih_context *c, number_t speed);
401 void uih_setcomplettehandler(uih_context *c, void(h)(void *), void *d);
402 void uih_recalculate(struct uih_context *c);
403 void uih_initstate(struct uih_context *uih);
404 void uih_screentofractalcoord(uih_context *c, int mousex, int mousey,
405                               number_t *re, number_t *im);
406 
407 /*cycling functions */
408 void uih_cycling_off(struct uih_context *c);
409 void uih_cycling_stop(struct uih_context *c);
410 void uih_cycling_continue(struct uih_context *c);
411 void uih_setcycling(struct uih_context *c, int speed);
412 int uih_cycling_on(struct uih_context *c);
413 int uih_cycling(struct uih_context *c, int mode);
414 
415 /*fractal context manipulation routines */
416 void uih_setformula(uih_context *c, int formula);
417 void uih_setperbutation(uih_context *c, number_t re, number_t im);
418 void uih_perbutation(uih_context *c, int mousex, int mousey);
419 void uih_setmaxiter(uih_context *c, int maxiter);
420 void uih_setbailout(uih_context *c, number_t bailout);
421 void uih_setincoloringmode(uih_context *c, int mode);
422 void uih_setoutcoloringmode(uih_context *c, int mode);
423 void uih_setintcolor(uih_context *c, int mode);
424 void uih_setouttcolor(uih_context *c, int mode);
425 void uih_setplane(uih_context *c, int mode);
426 void uih_setmandelbrot(uih_context *c, int mode, int mousex, int mousey);
427 void uih_setfastmode(uih_context *c, int mode);
428 void uih_setguessing(uih_context *c, int range);
429 void uih_setperiodicity(uih_context *c, int periodicity);
430 void uih_display(uih_context *c);
431 void uih_disablejulia(uih_context *c);
432 int uih_enablejulia(uih_context *c);
433 int uih_setjuliamode(uih_context *c, int mode);
434 void uih_setjuliaseed(uih_context *c, number_t zre, number_t zim);
435 
436 /*filter manipulation */
437 int uih_enablefilter(uih_context *c, int n);
438 void uih_disablefilter(uih_context *c, int n);
439 
440 /*Animation save routines */
441 int uih_save_enable(struct uih_context *uih, xio_file f, int mode);
442 void uih_save_disable(struct uih_context *uih);
443 void uih_saveframe(struct uih_context *uih);
444 void uih_save_position(struct uih_context *uih, xio_file f, int mode);
445 
446 void uih_load(struct uih_context *uih, xio_file f, xio_constpath name);
447 void uih_loadstr(struct uih_context *uih, const char *data);
448 void uih_playstr(struct uih_context *uih, const char *data);
449 void uih_playupdate(struct uih_context *uih);
450 void uih_replaydisable(struct uih_context *uih);
451 void uih_skipframe(struct uih_context *uih);
452 int uih_replayenable(struct uih_context *uih, xio_file f,
453                      xio_constpath filename, int animroot);
454 void uih_command(struct uih_context *uih, const char *command);
455 void uih_playtutorial(struct uih_context *c, const char *name);
456 
457 /* Easy to use functions for handling save/load*/
458 void uih_loadfile(struct uih_context *uih, xio_constpath d);
459 void uih_playfile(struct uih_context *c, xio_constpath d);
460 void uih_loadexample(struct uih_context *c);
461 void uih_loadpngfile(struct uih_context *c, xio_constpath d);
462 void uih_savepngfile(struct uih_context *c, xio_constpath d);
463 void uih_saveposfile(struct uih_context *c, xio_constpath d);
464 char *uih_savepostostr(struct uih_context *c);
465 void uih_savecfg(struct uih_context *c);
466 void uih_saveanimfile(struct uih_context *c, xio_constpath d);
467 void uih_update_lines(uih_context *c);
468 
469 /*timer functions */
470 void uih_stoptimers(uih_context *c);
471 void uih_resumetimers(uih_context *c);
472 void uih_slowdowntimers(uih_context *c, int time);
473 
474 /*text output functions */
475 void uih_clearscreen(uih_context *c);
476 void uih_settextpos(uih_context *c, int x, int y);
477 void uih_text(uih_context *c, const char *text);
478 void uih_letterspersec(uih_context *c, int n);
479 
480 /*image rotation functions */
481 int uih_fastrotate(uih_context *c, int mode);
482 int uih_fastrotateenable(uih_context *c);
483 void uih_fastrotatedisable(uih_context *c);
484 void uih_angle(uih_context *c, number_t angle);
485 void uih_rotatemode(uih_context *c, int mode);
486 void uih_rotate(uih_context *c, int mode);
487 void uih_rotationspeed(uih_context *c, number_t speed);
488 
489 /*Catalog functions */
490 int uih_loadcatalog(uih_context *c, const char *name);
491 void uih_freecatalog(uih_context *c);
492 
493 void uih_registermenus(void);
494 void uih_registermenus_i18n(void);
495 void uih_registermenudialogs_i18n(void);
496 void uih_unregistermenus(void);
497 
498 /*Windows :)*/
499 struct uih_window *uih_registerw(struct uih_context *uih, uih_getposfunc getpos,
500                                  uih_drawfunc draw, void *data, int flags);
501 void uih_removew(struct uih_context *uih, struct uih_window *w);
502 void uih_clearwindows(struct uih_context *uih);
503 void uih_drawwindows(struct uih_context *uih);
504 void uih_setline(struct uih_context *uih, struct uih_window *w, int color,
505                  int x1, int y1, int x2, int y2);
506 struct uih_window *uih_registerline(struct uih_context *uih, int color, int x1,
507                                     int y1, int x2, int y2);
508 
509 /*Messages*/
510 void uih_scrollup(uih_context *c);
511 void uih_clearmessages(uih_context *c);
512 int uih_message(uih_context *c, const char *message);
513 int uih_error(uih_context *c, const char *error);
514 void uih_rmmessage(uih_context *c, int pid);
515 void uih_printmessages(uih_context *c);
516 
517 /*Constant framerate functions*/
518 void uih_emulatetimers(uih_context *c);
519 void uih_constantframetime(uih_context *c, int time);
520 void uih_noconstantframetime(uih_context *c);
521 
522 /*undo and redo*/
523 void uih_saveundo(uih_context *c);
524 void uih_undo(uih_context *c);
525 void uih_redo(uih_context *c);
526 
527 void uih_setfont(uih_context *c);
528 
529 /*animation rendering*/
530 int uih_renderanimation(struct uih_context *gc, const char *basename,
531                         const xio_constpath animation, int width, int height,
532                         float pixelwidth, float pixelheight, int frametime,
533                         int type, int antialiasing, int slowmode,
534                         int letterspersec, const char *catalog);
535 int uih_renderimage(struct uih_context *gc1, xio_file af, xio_constpath path,
536                     struct image *img, int antialias, const char *catalog,
537                     int noise);
538 
539 void uih_initmessages(uih_context *c);
540 void uih_destroymessages(uih_context *c);
541 void uih_inittext(uih_context *c);
542 void uih_destroytext(uih_context *c);
543 
544 void uih_inhibittextsw(uih_context *c);
545 int uih_inhibittextselected(uih_context *c);
546 
547 void uih_updatestatus(uih_context *uih);
548 void uih_status(uih_context *uih);
549 int uih_statusenabled(uih_context *uih);
550 int uih_ministatusenabled(uih_context *uih);
551 void uih_ministatus(uih_context *uih);
552 int uih_cartesiangridenabled(uih_context *uih);
553 void uih_cartesiangrid(uih_context *uih);
554 void uih_sffeset(uih_context *c, sffe *parser, const char *formula);
555 
556 #endif
557