program FlipPages;
{
Copyright 2012, by Paul TOTH (http://www.execute-info.com)
}
uses Flash8;
{$BACKGROUND $ffffff}
const
{$FRAME_WIDTH 320}
{$FRAME_HEIGHT 200}
frame_width = 320;
frame_height = 200;
page_width = 120;
page_height = 90;
page_diag = Sqrt(page_width * page_width + page_height * page_height);
page_middle = frame_width div 2;
page_left = page_middle - page_width;
page_right = page_middle + page_width;
page_top = (frame_height - page_height) div 2;
page_bottom = frame_height - page_top;
type
TPage = class(MovieClip)
constructor Create(AOwner: MovieClip; AName: string; ADepth, X, Y, Color: Integer);
end;
TEffect = class(MovieClip)
active : Boolean;
ox, oy : Double;
px, py : Double;
Bitmap1: BitmapData;
Bitmap2: BitmapData;
Transf : Matrix;
ro : Double;
h1, h2 : Double;
constructor Create(AOwner: MovieClip);
procedure TopRight(x, y: Integer);
procedure BottomRight(x, y: Integer);
procedure TopLeft(x, y: Integer);
procedure BottomLeft(x, y: Integer);
procedure DrawCorner(x, y: Integer);
function distance(x1, y1, x2, y2: Double): Double;
procedure adjust(ox, oy, max: Double);
end;
TBook = class(MovieClip)
A,B,C,D: TPage;
Effect : TEffect;
constructor Create;
procedure DoMouseDown;
procedure DoMouseUp;
procedure DoMouseMove;
procedure DoRelease;
end;
var
font1: TextFormat;
font2: TextFormat;
book : TBook;
info : TextField;
constructor TPage.Create(AOwner: MovieClip; AName: string; ADepth, X, Y, Color: Integer);
var
t: TextField;
begin
inherited Create(AOwner, AName, ADepth);
_x := X;
_y := Y;
beginFill(Color);
lineStyle(6, $FFFFFF);
moveTo(0, 0);
lineTo(page_width, 0);
lineTo(page_width, page_height);
lineTo(0, page_height);
endFill();
t := TextField.Create(Self, '', 0, 10, 10, page_width - 20, page_height - 20);
t.Text := _name;
t.setTextFormat(font2);
end;
constructor TBook.Create;
begin
inherited Create(nil, 'Page', 0);
beginFill(0);
moveto(0, 0);
lineto(frame_width, 0);
lineto(frame_width, frame_height);
lineto(0, frame_height);
info := TextField.Create(Self, '', 0, 0, 10, frame_width, page_top);
info.text := 'Book (c)2012 by Paul TOTH';
info.setTextformat(font1);
info := TextField.Create(Self, '', -1, 0, page_bottom + 15, frame_width, page_top);
info.text := 'Build with FlashPascal !';
info.setTextformat(font1);
D := TPage.Create(Self, 'D', 1, page_middle, page_top, $0000FF);
A := TPage.Create(Self, 'A', 2, page_left , page_top, $FF0000);
B := TPage.Create(Self, 'B', 3, page_middle, page_top, $FF00FF);
C := TPage.Create(Self, 'C', 4, page_left , page_top, $00FF00);
C._visible := False;
Effect := TEffect.Create(Self);
OnMouseDown := DoMouseDown;
OnMouseUp := DoMouseUp;
OnMouseMove := DoMouseMove;
OnRelease := DoRelease;
end;
procedure TBook.DoMouseDown;
begin
Effect.Active := False;
if B._visible and (_xmouse > page_middle) and (_xmouse < page_right) then
begin
if (_ymouse > page_top) and (_ymouse < page_top + page_height / 2) then
Effect.TopRight(_xmouse, _ymouse)
else
Effect.BottomRight(_xmouse, _ymouse);
end else
if C._visible and (_xmouse < page_middle) and (_xmouse > page_left) then
begin
if (_ymouse > page_top) and (_ymouse < page_top + page_height / 2) then
Effect.TopLeft(_xmouse, _ymouse)
else
Effect.BottomLeft(_xmouse, _ymouse);
end;
end;
procedure TBook.DoMouseUp;
begin
if Effect.Active then
begin
Effect.Active := False;
Effect.Clear();
B._visible := _xmouse > page_middle;
C._visible := _xmouse < page_middle;
end;
end;
procedure TBook.DoMouseMove;
begin
if Effect.Active then
begin
Effect.DrawCorner(_xmouse, _ymouse);
end;
end;
procedure TBook.DoRelease;
begin
// to avoid textfield selection
end;
constructor TEffect.Create(AOwner: MovieClip);
begin
inherited Create(AOwner, 'fx', 5);
Bitmap1 := BitmapData.Create(page_width, page_height);
Bitmap2 := BitmapData.Create(page_width, page_height);
end;
procedure TEffect.TopRight(x, y: Integer);
begin
ox := page_right;
oy := page_top;
ro := 0;
h1 := +page_height;
h2 := +page_height;
Bitmap1.Draw(Book.B);
Bitmap2.Draw(Book.C);
Book.B._visible := False;
DrawCorner(x, y);
end;
procedure TEffect.BottomRight(x, y: Integer);
begin
ox := page_right;
oy := page_bottom;
ro := 3.14;
h1 := -page_height;
h2 := -page_height;
Bitmap1.Draw(Book.B);
Bitmap2.Draw(Book.C);
Book.B._visible := False;
DrawCorner(x, y);
end;
procedure TEffect.TopLeft(x, y: Integer);
begin
ox := page_left;
oy := page_top;
ro := 0;
h1 := +page_height;
h2 := -page_height;
Bitmap1.Draw(Book.C);
Bitmap2.Draw(Book.B);
Book.C._visible := False;
DrawCorner(x, y);
end;
procedure TEffect.BottomLeft(x, y: Integer);
begin
ox := page_left;
oy := page_bottom;
ro := 3.14;
h1 := -page_height;
h2 := +page_height;
Bitmap1.Draw(Book.C);
Bitmap2.Draw(Book.B);
Book.C._visible := False;
DrawCorner(x, y);
end;
procedure TEffect.DrawCorner(x, y: Integer);
var
d, bt: Double;
mx, my: Double;
tx, ty: Double;
rx, ry: Double;
bx, by: Double;
begin
active := True;
// Page Bottom
bt := frame_height - oy;
// Mouse position
px := x;
py := y;
// Adjust distance
adjust(page_middle, bt, page_diag);
adjust(page_middle, oy, page_width);
// Middle
mx := (ox + px) / 2;
my := (oy + py) / 2;
if (ox = px) then // horizontal, div by 0
begin
tx := mx;
ty := oy;
rx := tx;
ry := bt;
bx := px;
by := ry;
end else begin
// Perpendicular
d := (py - oy)/(px - ox);
// Top intersection
tx := mx + (my - oy) * d;
ty := oy;
// Right intersection
ry := my + (mx - ox) / d;
rx := ox;
// Out of the page ?
if (ry > page_bottom) or (ry < page_top) then
begin
rx := tx + (rx - tx) * h1 / (ry - ty);
ry := bt;
end;
// Compute 4th point
d := h2 / Distance(px, py, tx, ty);
bx := px + (py - ty) * d;
by := py - (px - tx) * d;
// out of the page ?
if (bx > page_right) or (bx < page_left) then
begin
// merge with right point to make a triangle
bx := rx;
by := ry;
end;
end;
// Rotation angle
d := ro - atan2(bx - px, by - py);
clear();
// current face
Transf := Matrix.Create();
Transf.Translate(page_middle, page_top);
beginBitmapFill(Bitmap1, Transf);
moveto(page_middle, oy);
lineto(tx, ty);
lineto(rx, ry);
lineto(ox, bt);
lineto(page_middle, bt);
lineto(page_middle, oy);
endFill();
// opposite face
Transf := Matrix.Create();
Transf.Rotate(d);
Transf.Translate(+px, +py);
beginBitmapFill(Bitmap2, Transf);
moveto(tx, ty);
lineto(rx, ry);
lineStyle(6, $ffffff);
lineto(bx, by);
lineto(px, py);
lineto(tx, ty);
endFill();
end;
procedure TEffect.adjust(ox, oy, max: Double);
var
d: Double;
begin
d := distance(px, py, ox, oy);
if d > max then
begin
px := ox + (px - ox) * max / d;
py := oy + (py - oy) * max / d;
end;
end;
function TEffect.Distance(x1, y1, x2, y2: Double): Double;
begin
x1 := x2 - x1;
y1 := y1 - y2;
Result := Sqrt(x1 * x1 + y1 * y1);
end;
begin
font1:=TextFormat.Create('Tahoma',11);
font1.color:=$ffffff;
font1.align:='center';
font2:=TextFormat.Create('Tahoma',32);
font2.color:=$ffffff;
font2.align:='center';
book := TBook.Create();
end.