-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathCELTRANS.C
482 lines (418 loc) · 10 KB
/
CELTRANS.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* celtrans.c - code for rotate,stretch and moving the cel, Eventually will
call raster twisting routine in gfx/rastrans.c */
#include "errcodes.h"
#include "rastrans.h"
#include "jimk.h"
#include "flicel.h"
#include "auto.h"
#include "floatgfx.h"
Boolean isin_fcel(Flicel *fcel,SHORT x,SHORT y)
{
return(isin_bpoly(&fcel->xf,fcel->rc,x,y));
}
void vstretch_cel(int toolmode)
{
Errcode err;
Rcel *rc;
Short_xy stretch1;
Short_xy cent1;
Short_xy ocent;
SHORT x0, y0, w0, h0;
SHORT swidth, sheight;
SHORT neww, newh;
Short_xy rotcorn; /* (un) rotated corner of cel */
Short_xy rotm; /* rotated mouse point */
int quad;
SHORT multw, multh, propmult;
if (thecel == NULL)
return;
rc = thecel->rc;
if(!toolmode)
{
cent1 = thecel->cd.cent;
stretch1 = thecel->cd.stretch;
save_undo();
if((err = draw_flicel(thecel,DRAW_FIRST,NEW_CFIT)) < 0)
goto error;
}
for (;;)
{
if(toolmode)
{
if(!tti_input())
goto cleanup;
cent1 = thecel->cd.cent;
stretch1 = thecel->cd.stretch;
}
else if((err = quadpoly_in_place(thecel->xf.bpoly)) < 0)
{
if(err != Err_abort)
goto error;
zoom_unundo();
draw_flicel(thecel,DRAW_RENDER,NEW_CFIT);
dirties();
goto cleanup;
}
swidth = rc->width + thecel->cd.stretch.x;
sheight = rc->height + thecel->cd.stretch.y;
/* get unrotated cel stretched corner */
rotcorn.x = thecel->cd.cent.x - (swidth>>1);
rotcorn.y = thecel->cd.cent.y - (sheight>>1);
/* rotate mouse to cel's original orientation */
x0 = rotm.x = icb.mx;
y0 = rotm.y = icb.my;
frotate_points2d(itheta_tofloat(-thecel->cd.rotang.z,FCEL_TWOPI),
&thecel->cd.cent,&rotm,&rotm,1);
quad = quad9(rotm.x,rotm.y,rotcorn.x,rotcorn.y,swidth,sheight);
ocent = thecel->cd.cent;
w0 = swidth;
h0 = sheight;
/* compare signs to determine whether to negate calc or not */
neww = (rotm.x - thecel->cd.cent.x);
if((neww < 0 && swidth > 0) || (neww > 0 && swidth < 0))
multw = -2;
else
multw = 2;
newh = (rotm.y - thecel->cd.cent.y);
if((newh < 0 && sheight > 0) || (newh > 0 && sheight < 0))
multh = -2;
else
multh = 2;
if((swidth < 0 && sheight > 0) || (swidth > 0 && sheight < 0))
propmult = -rc->height;
else
propmult = rc->height;
for(;;)
{
neww = multw*(rotm.x - thecel->cd.cent.x);
newh = multh*(rotm.y - thecel->cd.cent.y);
switch(quad)
{
case 0: /* proportional corners cases */
case 8:
sheight = (propmult*(long)neww)/rc->width;
case 3: /* to right and left */
case 5:
swidth = neww;
break;
case 2: /* non proportional corners cases */
case 6:
swidth = neww; /* fall through and set sheight too */
case 1: /* above */
case 7: /* below */
sheight = newh;
break;
case 4: /* inside center of cel */
thecel->cd.cent.x = ocent.x + icb.mx - x0;
thecel->cd.cent.y = ocent.y + icb.my - y0;
break;
}
top_textf("%5d%% x %5d%% y",
sscale_by(100,swidth, rc->width),
sscale_by(100, sheight, rc->height) );
thecel->cd.stretch.x = swidth - rc->width;
thecel->cd.stretch.y = sheight - rc->height;
if((err = draw_flicel(thecel,DRAW_DELTA,OLD_CFIT)) < 0)
goto error;
wait_input(MBPEN|MBRIGHT|MMOVE|KEYHIT);
if(JSTHIT(MBPEN|MBRIGHT|KEYHIT))
{
if(JSTHIT(MBRIGHT|KEYHIT))
{
thecel->cd.stretch.x = w0 - rc->width;
thecel->cd.stretch.y = h0 - rc->height;
thecel->cd.cent = ocent;
}
else if(!JSTHIT(MMOVE)) /* if no move no need to redraw */
{
if(toolmode)
goto cleanup;
break;
}
if((err = draw_flicel(thecel,DRAW_DELTA,OLD_CFIT)) < 0)
goto error;
if(toolmode)
goto cleanup;
break;
}
/* get new rotated mouse position */
rotm.x = icb.mx;
rotm.y = icb.my;
frotate_points2d(itheta_tofloat(-thecel->cd.rotang.z,FCEL_TWOPI),
&thecel->cd.cent,&rotm,&rotm,1);
}
}
error:
if(!toolmode)
zoom_unundo(); /* restore screen and put cel back where it was */
thecel->cd.stretch = stretch1;
thecel->cd.cent = cent1;
refresh_flicel_pos(thecel);
softerr(err,"cel_stretch");
cleanup:
cleanup_toptext();
return;
}
void vrotate_cel(int toolmode)
/* Stuff to implement cel/turn. */
{
Errcode err;
Short_xy cent;
Short_xy cent1;
Short_xyz theta1;
Boolean do_move;
SHORT ltheta;
SHORT otheta;
SHORT ttheta;
Short_xyz dtheta;
SHORT theta_accum;
SHORT x0,y0;
int dx,dy;
#define arctan(dx,dy) (float_toitheta(atan2(dy,dx),FCEL_TWOPI))
if(thecel == NULL)
return;
if(!toolmode)
{
save_undo();
if((err = draw_flicel(thecel,DRAW_FIRST,NEW_CFIT)) < 0)
goto error;
theta1 = thecel->cd.rotang;
cent1 = thecel->cd.cent;
}
dtheta.x = dtheta.y = 0;
for(;;)
{
if(toolmode)
{
if(!tti_input())
goto cleanup;
theta1 = thecel->cd.rotang;
cent1 = thecel->cd.cent;
do_move = 0;
}
else
{
if((err = quadpoly_in_place(thecel->xf.bpoly)) < 0)
{
if(err != Err_abort)
goto error;
zoom_unundo();
draw_flicel(thecel,DRAW_RENDER,NEW_CFIT);
dirties();
goto cleanup;
}
do_move = isin_fcel(thecel,icb.mx,icb.my);
}
otheta = thecel->cd.rotang.z;
cent = thecel->cd.cent;
theta_accum = 0;
dx = icb.mx - cent.x;
dy = icb.my - cent.y;
if(dx || dy)
ltheta = arctan(dx,dy);
else
ltheta = 0;
x0 = icb.mx;
y0 = icb.my;
for(;;)
{
if (do_move)
{
thecel->cd.cent.x = cent.x + icb.mx - x0;
thecel->cd.cent.y = cent.y + icb.my - y0;
}
else
{
dx = icb.mx - cent.x;
dy = icb.my - cent.y;
if(dx || dy)
{
ttheta = arctan(dx,dy);
theta_accum += ttheta - ltheta;
ltheta = ttheta;
}
else
ttheta = ltheta;
if((dtheta.z = constrain_angle(theta_accum)) != 0)
{
theta_accum -= dtheta.z;
rotate_flicel(thecel,&dtheta);
}
}
if((err = draw_flicel(thecel,DRAW_DELTA,OLD_CFIT)) < 0)
goto error;
#ifdef NOT_ACCURATE
top_textf("z %4d", sscale_by(thecel->cd.rotang.z,360,TWOPI));
#endif /* NOT_ACCURATE */
wait_any_input();
if(JSTHIT(MBRIGHT|KEYHIT|MBPEN))
{
if(JSTHIT(MBRIGHT|KEYHIT))
{
thecel->cd.rotang.z = otheta;
set_fcel_center(thecel,cent.x,cent.y);
}
else if(!JSTHIT(MMOVE))
{
if(toolmode)
goto cleanup;
break;
}
if((err = draw_flicel(thecel,DRAW_DELTA,OLD_CFIT)) < 0)
goto error;
if(toolmode)
goto cleanup;
break;
}
}
}
error:
if(!toolmode)
zoom_unundo();
/* put back cel where you started */
thecel->cd.rotang = theta1;
thecel->cd.cent = cent1;
refresh_flicel_pos(thecel);
softerr(err,"cel_rotate");
cleanup:
cleanup_toptext();
return;
}
/*** translation of the cel ************************/
static void fcel_toptext_info(Flicel *fc, Short_xy *ocent)
{
Rcel *rc = fc->rc;
soft_top_textf("!%4d%4d%4d%4d%4d%4d", "top_celpos",
fc->cd.cent.x,
fc->cd.cent.y,
fc->cd.cent.x - ocent->x,
fc->cd.cent.y - ocent->y,
sscale_by(100,rc->width + thecel->cd.stretch.x,rc->width),
sscale_by(100,rc->height + thecel->cd.stretch.y,rc->height));
}
/* to keep track of how far user moved the cel this time... */
typedef struct paste1dat {
Short_xy ncent, ocent;
} Paste1dat;
static int paste1(Paste1dat *pdat, int ix,int it,int scale,Autoarg *aa)
/* auto vec for moving a cel in a straight line. Ie paste multi */
{
Errcode err;
int dx,dy;
if(aa->cur_frame == 0) /* first frame set end position to current */
pdat->ncent = thecel->cd.cent;
dx = pdat->ncent.x - pdat->ocent.x;
dy = pdat->ncent.y - pdat->ocent.y;
set_fcel_center(thecel, pdat->ocent.x + itmult(dx,scale),
pdat->ocent.y + itmult(dy,scale));
err = draw_flicel(thecel,DRAW_RENDER,NEW_CFIT);
return(err);
}
Errcode inc_thecel()
/* Advance cel to next frame if vs.paste_inc_cel says so. */
{
if(vs.paste_inc_cel && thecel->flif.hdr.frame_count > 1)
return(inc_fcel_frame(thecel));
else
return(Success);
}
Errcode mp_thecel(int paste,int toolmode)
/* Move or paste a flicel. */
{
Errcode err;
Paste1dat pdat;
SHORT ox, oy;
if(thecel == NULL)
return(Err_abort);
pdat.ocent = thecel->cd.cent;
if(toolmode)
{
if(toolmode == 2)
{
thecel->cd.cent.x = icb.mx;
thecel->cd.cent.y = icb.my;
if((err = draw_flicel(thecel,DRAW_DELTA,NEW_CFIT)) < 0)
goto error;
}
}
else
{
save_undo();
if((err = draw_flicel(thecel,DRAW_FIRST,NEW_CFIT)) < 0)
goto error;
if((err = quadpoly_in_place(thecel->xf.bpoly)) < 0)
{
if(err == Err_abort)
goto do_paste;
goto error;
}
}
for (;;)
{
fcel_toptext_info(thecel,&pdat.ocent);
ox = icb.mx;
oy = icb.my;
wait_input(MMOVE|KEYHIT|MBPEN|MBRIGHT);
if(JSTHIT(MMOVE))
{
translate_flicel(thecel,icb.mx - ox,icb.my - oy);
draw_flicel(thecel,DRAW_DELTA,OLD_CFIT);
}
if(JSTHIT(KEYHIT|MBPEN|MBRIGHT))
{
if(JSTHIT(KEYHIT|MBRIGHT)) /* user abort */
{
err = Err_abort;
goto error;
}
break;
}
}
do_paste:
if(!toolmode)
zoom_unundo();
if(paste)
{
cleanup_toptext();
if(toolmode)
unsee_flicel(thecel);
if (vs.multi)
{
if((err = go_autodraw(paste1,&pdat,
(AUTO_USESCEL|AUTO_UNZOOM|AUTO_PUSHINKS))) < 0)
{
goto error;
}
}
else
{
if((err = draw_flicel(thecel,DRAW_RENDER,NEW_CFIT)) < Success)
goto error;
if ((err = inc_thecel()) < Success)
goto error;
dirties();
}
}
err = 0;
goto done;
error:
thecel->cd.cent = pdat.ocent;
refresh_flicel_pos(thecel);
if(toolmode)
draw_flicel(thecel,DRAW_DELTA,OLD_CFIT);
else
zoom_unundo();
done:
cleanup_toptext();
return(err);
}
void move_the_cel(void)
/* Interactively move cel, but don't paste it when done */
{
mp_thecel(0,0);
}
void paste_the_cel(void)
/* Interactively move and then paste cel */
{
mp_thecel(1,0);
}