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
| package com.foxaweb.pageflip {
import flash.geom.Point;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.display.Shape;
/**
* Computes, generates, and draws a pageflip.
*
* @notice PageFlip drawer
* @author Foxy
* @version 1.0
* @date 2007-01-18
*
* Original author :
* -----------------
* Didier Brun aka Foxy
* webmaster@foxaweb.com
* http://www.foxaweb.com
*
* AUTHOR * *****************************************************************************
*
* authorName : Didier Brun - www.foxaweb.com
* contribution : the original class
* date : 2007-01-18
*
* VISIT www.byteArray.org
*
*
* LICENSE ******************************************************************************
*
* This class is under RECIPROCAL PUBLIC LICENSE.
* http://www.opensource.org/licenses/rpl.php
*
* Please, keep this header and the list of all authors
*
*
*
* Nomenclature
* ------------
*
* PT(0,0) PT(1,0)
* ---------------------------------------------------
* | <-------------------PW----------------------> |
* | ^ Offset(0,0) x--> |
* | | |
* | | y |
* | | | |
* | | | |
* | | V |
* | | pPoints[] |
* | | |
* | | |
* | | |
* | | (T3) |
* | PH ---|
* | | --- /
* | | --- /
* | | --- /
* | | --- /
* | | --- /
* | | --- /
* | | PTD --- cPoints[] /
* | | \ /
* | | \ /
* | | \ /
* | | \ /
* | | \ /
* | V \ /
* |-------------------------------- \/
* PT(0,1) PT(1,1)
*/
public class PageFlip {
// ------------------------------------------------
//
// ---o public static methods
//
// ------------------------------------------------
/**
* Compute and generate a new flip.
*
* @param ptd Point indicating the position of the PTD point (the drag one) relative to the upper-left corner.
* @param pt Point indicating the original position of the dragged point. The two possible values for its x and y properties are 0 or 1. pt(0,0) is the upper-left corner, for example, pt (1,1) is the bottom-right one.
* @param pw int indicating the sheet width in pixels.
* @param ph int indicating the sheet height in pixels.
* @param ish If true, horizontal mode is provided, if false, vertical.
* @param sens Number indicating the constraints sensibility. This parametter is a multiplicator for the constraints values. It's intended to prevent some awefull flickering effects. Its possible value is ranged between 0.9 and 1. 0.9 -> when ptd move is free (drag'n'drop), 1 -> when ptd move is progresive (tween when release). At best, you should never swap it from .9 to 1. A progressive incrementation is better. If flickering effects don't disturb you or if your ptd moves is coded, keep this parametter to 1.
*
* @return Object containing:<br />
* cPoints:Array - Array of points which describes the flipped part of the sheet. Note that in case of the ptd point is aligned with its original position or if the height of the shape is very small (<1) this array is set to null.<br />
* pPoints:Array - Array of points wich describes the fixed part of the sheet.<br />
* matrix:Matrix - Transformation matrix for the flipped part of the sheet.<br />
* width:Number - Sheet width.<br />
* height:Number - Sheet height.
*
*/
public static function computeFlip(ptd:Point,pt:Point,pw:int,ph:int,ish:Boolean,sens:int):Object{
// useful vars
var dfx:Number=ptd.x-pw*pt.x;
var dfy:Number=ptd.y-ph*pt.y;
var spt:Point=pt.clone();
var opw:int=pw;
var oph:int=ph;
// offset corections
var temp:Number;
// transform matrix
var mat:Matrix=new Matrix();
if (!ish){
// size
temp=pw;
pw=ph;
ph=temp;
// ptd
temp=ptd.x;
ptd.x=ptd.y;
ptd.y=temp;
// pt
temp=pt.x;
spt.x=pt.y;
spt.y=temp;
}
// pt1 & pt2 are the two fixed points of the sheet. opposed to ptd drag one.
var pt1:Point=new Point(0,0);
var pt2:Point=new Point(0,ph);
// default points array
// cPoints -> the fliped part
var cPoints:Array=[null,null,null,null];
// pPoints -> the fixed part
var pPoints:Array=[new Point(0,0),new Point(pw,0),null,null,new Point(0,ph)];
// compute some flip
flipDrag(ptd,spt,pw,ph);
// ditstance
// it allows you to have a valid position for ptd.
// the limit is the diagonal of the sheet here
limitPoint(ptd,pt1,(pw*pw+ph*ph)*sens);
// the limit is about the opposite fixed point
limitPoint(ptd,pt2,(pw*pw)*sens);
// first fliped point
cPoints[0]=new Point(ptd.x,ptd.y);
var dy:Number=pt2.y-ptd.y;
var tot:Number=pw-ptd.x-pt1.x;
var drx:Number=getDx(dy,tot);
// fliped angle
var theta:Number=Math.atan2(dy,drx);
if (dy==0)theta=0;
// another fliped angle
var beta:Number=Math.PI/2-theta;
var hyp:Number=(pw-cPoints[0].x)/Math.cos(beta);
// vhyp is the hypotenuse of the fliped part
var vhyp:Number=hyp;
// if hyp is greater than the height of the sheet or hyp is
// negative, the fliped part has 4 points
// else, it's just a 3 points part (simple corner).
if (hyp>ph || hyp<0)vhyp=ph;
// second fliped point
cPoints[1]=new Point( cPoints[0].x+Math.cos(-beta)*vhyp,
cPoints[0].y+Math.sin(-beta)*vhyp);
// last fliped point
cPoints[3]=new Point(cPoints[0].x+drx,pt2.y);
// if we have a 4 points shape
if (hyp!=vhyp){
dy=pt1.y-cPoints[1].y;
tot=pw-cPoints[1].x;
drx=getDx(dy,tot);
// push the before the last point
cPoints[2]=new Point(cPoints[1].x+drx,pt1.y);
// we can now find the fixed points of the sheet
pPoints[1]=cPoints[2].clone();
pPoints[2]=cPoints[3].clone();
pPoints.splice(3,1);
}else{
// else we delete the point
cPoints.splice(2,1);
// we can now find the fixed points of the sheet
pPoints[2]=cPoints[1].clone();
pPoints[3]=cPoints[2].clone();
}
// these two polygons are always convex !
// now we can flip the two arrays
flipPoints(cPoints,spt,pw,ph);
flipPoints(pPoints,spt,pw,ph);
// if !ish (vertical mode)
// we have to change the points orientation
if (!ish){
oriPoints(cPoints,spt,pw,ph);
oriPoints(pPoints,spt,pw,ph);
}
// flipped part transfrom matrix
var gama:Number=theta;
if (pt.y==0)gama=-gama;
if (pt.x==0)gama=Math.PI+Math.PI-gama;
if (!ish)gama=Math.PI-gama;
mat.a=Math.cos(gama);
mat.b=Math.sin(gama);
mat.c=-Math.sin(gama);
mat.d=Math.cos(gama);
ordMatrix(mat,spt,opw,oph,ish,cPoints,pPoints,gama,beta);
// here we fix some mathematical bugs or instabilities
if (vhyp==0)cPoints=null;
if (Math.abs(dfx)<1 && Math.abs(dfy)<1)cPoints=null;
// now we just have to return all the stuff
return {cPoints:cPoints,pPoints:pPoints,matrix:mat,width:opw,height:oph};
}
/**
* Draw a sheet using two Bitmap objects.
*
* @param ocf computeFlip() returned object
* @param mc Target
* @param bmp0 First page bitmap (left-top aligned)
* @param bmp1 Second page bitmap (left-top aligned)
*
*/
public static function drawBitmapSheet(ocf:Object,mc:Shape,bmp0:BitmapData,bmp1:BitmapData):void{
// affectations
var wid:Number=ocf.width;
var hei:Number=ocf.height;
var nb:Number;
var ppts:Array=ocf.pPoints;
var cpts:Array=ocf.cPoints;
// draw the fixed part
mc.graphics.beginBitmapFill(bmp0,new Matrix(),false,true);
nb=ppts.length;
mc.graphics.moveTo(ppts[nb-1].x,ppts[nb-1].y);
while (--nb>=0)mc.graphics.lineTo(ppts[nb].x,ppts[nb].y);
mc.graphics.endFill();
// draw the flipped part
if (cpts==null)return;
mc.graphics.beginBitmapFill(bmp1,ocf.matrix,false,true);
nb=cpts.length;
mc.graphics.moveTo(cpts[nb-1].x,cpts[nb-1].y);
while (--nb>=0)mc.graphics.lineTo(cpts[nb].x,cpts[nb].y);
mc.graphics.endFill();
}
// ------------------------------------------------
//
// ---o private static methods
//
// ------------------------------------------------
/**
* orientation correction
* @private
*/
private static function oriPoints(pts:Array,po:Point,pw:Number,ph:Number):void{
var nb:Number=pts.length;
var temp:Number;
while (--nb>=0){
temp=pts[nb].x;
pts[nb].x=pts[nb].y;
pts[nb].y=temp;
}
}
/**
* ptdarg correction
* @private
*/
private static function flipDrag(ptd:Point,po:Point,pw:Number,ph:Number):void{
// flip y
if (po.y==0)ptd.y=ph-ptd.y;
// flip x
if (po.x==0)ptd.x=pw-ptd.x;
}
/**
* flip correction
* @private
*/
private static function flipPoints(pts:Array,po:Point,pw:Number,ph:Number):void{
var nb:Number=pts.length;
// flip
if (po.y==0 || po.x==0){
while (--nb>=0){
if (po.y==0)pts[nb].y=ph-pts[nb].y;
if (po.x==0)pts[nb].x=pw-pts[nb].x;
}
}
}
/**
* compute some trigonometry equation
*
* this one is more stable than Math.atan2 for our case
*
* @private
*/
private static function getDx(dy:Number,tot:Number):Number{
return (tot*tot-dy*dy)/(tot*2);
}
/**
* limit the ptdrag position
* @private
*/
private static function limitPoint(ptd:Point,pt:Point,dsquare:Number):void{
var theta:Number;
var lim:Number;
var dy:Number=ptd.y-pt.y;
var dx:Number=ptd.x-pt.x;
var dis:Number=dx*dx+dy*dy;
// we save some times using square
if (dis>dsquare){
theta=Math.atan2(dy,dx);
lim=Math.sqrt(dsquare);
ptd.x=pt.x+Math.cos(theta)*lim;
ptd.y=pt.y+Math.sin(theta)*lim;
}
}
/**
* matric correction
* @private
*/
private static function ordMatrix(mat:Matrix,spt:Point,opw:Number,oph:Number,ish:Boolean,cPoints:Array,pPoint:Array,gama:Number,beta:Number):void{
if (spt.x==1 && spt.y==0){
mat.tx=cPoints[0].x;
mat.ty=cPoints[0].y;
if (!ish){
mat.tx=cPoints[0].x-Math.cos(gama)*opw-Math.cos(-beta)*oph;
mat.ty=cPoints[0].y-Math.sin(gama)*opw-Math.sin(-beta)*oph;
}
}
if (spt.x==1 && spt.y==1){
mat.tx=cPoints[0].x+Math.cos(-beta)*oph;
mat.ty=cPoints[0].y+Math.sin(-beta)*oph;
if (!ish){
mat.tx=cPoints[0].x+Math.cos(-beta)*oph;
mat.ty=cPoints[0].y-Math.sin(-beta)*oph;
}
}
if (spt.x==0 && spt.y==0){
mat.tx=cPoints[0].x-Math.cos(gama)*opw;
mat.ty=cPoints[0].y-Math.sin(gama)*opw;
}
if (spt.x==0 && spt.y==1){
mat.tx=cPoints[0].x-Math.cos(gama)*opw-Math.cos(-beta)*oph;
mat.ty=cPoints[0].y-Math.sin(gama)*opw+Math.sin(-beta)*oph;
if (!ish){
mat.tx=cPoints[0].x;
mat.ty=cPoints[0].y;
}
}
}
}
} |