var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* @module annieUI
*/
var annieUI;
(function (annieUI) {
var Sprite = annie.Sprite;
var Shape = annie.Shape;
var Event = annie.Event;
var MouseEvent = annie.MouseEvent;
var Point = annie.Point;
/**
* 电子杂志组件
* @class annieUI.FlipBook
* @public
* @extends annie.Sprite
* @since 1.0.0
*/
var FlipBook = (function (_super) {
__extends(FlipBook, _super);
/**
* 初始化电子杂志
* @method FlipBook
* @param {number} width 单页宽
* @param {number} height 单页高
* @param {number} pageCount 总页数,一般为偶数
* @param {Function} getPageCallBack,通过此回调获取指定页的内容的显示对象
* @since 1.0.3
*/
function FlipBook(width, height, pageCount, getPageCallBack) {
_super.call(this);
//Events
/**
* annieUI.FlipBook组件翻页开始事件
* @event annie.Event.ON_FLIP_START
* @since 1.1.0
*/
/**
* annieUI.FlipBook组件翻页结束事件
* @event annie.Event.ON_FLIP_STOP
* @since 1.1.0
*/
/**
* 电子杂志组件类
* @class annieUI.FlipBook
* @public
* @extends annie.Sprite
* @since 1.0.3
*/
//可设置或可调用接口,页数以单页数计算~
/**
* 总页数
* @property totalPage
* @type {number}
*/
this.totalPage = 0;
/**
* 当前页数
* @property
* @type {number}
* @since 1.0.3
*/
this.currPage = 0;
/**
* 翻页速度,0-1之间,值越小,速度越快
* @property
* @since 1.1.3
* @type {number}
*/
this.speed = 0.4;
this.state = "stop";
this.timerArg0 = 0;
this.timerArg1 = 0;
this.px = 0;
this.py = 0;
this.rPage0 = new Sprite();
this.rPage1 = new Sprite();
this.pageMC = new Sprite();
this.leftPage = null;
this.rightPage = null;
this.rMask0 = new Shape();
this.rMask1 = new Shape();
this.shadow0 = new Shape();
this.shadow1 = new Shape();
this.sMask0 = new Shape();
this.sMask1 = new Shape();
this.pages = [];
this.stageMP = new Point();
/**
* 指定是否能够翻页动作
* @property canFlip
* @since 1.0.3
* @type {boolean}
*/
this.canFlip = true;
var s = this;
s._instanceType = "annieUI.FlipBook";
s.getPageCallback = getPageCallBack;
s.bW = width;
s.bH = height;
s.totalPage = pageCount;
s.currPage = s.toPage = 0;
s.crossGap = Math.sqrt(s.bW * s.bW + s.bH * s.bH);
s.p1 = new Point(0, 0);
s.p2 = new Point(0, s.bH);
s.p3 = new Point(s.bW + s.bW, 0);
s.p4 = new Point(s.bW + s.bW, s.bH);
s.limitP1 = new Point(s.bW, 0);
s.limitP2 = new Point(s.bW, s.bH);
s.toPosArr = [s.p3, s.p4, s.p1, s.p2];
s.myPosArr = [s.p1, s.p2, s.p3, s.p4];
s.rPage0.mouseEnable = false;
s.rPage1.mouseEnable = false;
s.shadow0.mouseEnable = false;
s.shadow1.mouseEnable = false;
s.setShadowMask(s.shadow0, s.bW * 1.5, s.bH * 3);
s.setShadowMask(s.shadow1, s.bW * 1.5, s.bH * 3);
s.rPage1.mask = s.rMask1;
s.shadow1.mask = s.rMask1;
s.shadow0.mask = s.rMask0;
s.rPage0.mask = s.rMask0;
s.shadow0.visible = false;
s.shadow1.visible = false;
s.addChild(s.pageMC);
s.addChild(s.rPage0);
s.addChild(s.shadow0);
s.addChild(s.rPage1);
s.addChild(s.shadow1);
s.addChild(s.rMask0);
s.addChild(s.rMask1);
s.setPage(s.currPage);
s.md = s.onMouseDown.bind(s);
s.mu = s.onMouseUp.bind(s);
s.mm = s.onMouseMove.bind(s);
var em = s.onEnterFrame.bind(s);
s.addEventListener(annie.Event.ADD_TO_STAGE, function (e) {
s.stage.addEventListener(MouseEvent.MOUSE_DOWN, s.md);
s.stage.addEventListener(MouseEvent.MOUSE_UP, s.mu);
s.stage.addEventListener(MouseEvent.MOUSE_MOVE, s.mm);
s.addEventListener(Event.ENTER_FRAME, em);
});
s.addEventListener(annie.Event.REMOVE_TO_STAGE, function (e) {
s.stage.removeEventListener(MouseEvent.MOUSE_DOWN, s.md);
s.stage.removeEventListener(MouseEvent.MOUSE_UP, s.mu);
s.stage.removeEventListener(MouseEvent.MOUSE_MOVE, s.mm);
s.removeEventListener(Event.ENTER_FRAME, em);
});
}
FlipBook.prototype.drawPage = function (num, movePoint) {
var s = this;
var actionPoint;
var bArr;
if (num == 1) {
movePoint = s.checkLimit(movePoint, s.limitP1, s.bW);
movePoint = s.checkLimit(movePoint, s.limitP2, s.crossGap);
bArr = s.getBookArr(movePoint, s.p1, s.p2);
actionPoint = bArr[1];
s.getLayerArr(movePoint, actionPoint, s.p1, s.p2, s.limitP1, s.limitP2);
s.getShadow(s.shadow0, s.p1, movePoint, 0.5);
s.getShadow(s.shadow1, s.p1, movePoint, 0.45);
s.rPage1.rotation = s.angle(movePoint, actionPoint) + 90;
s.rPage1.x = bArr[3].x;
s.rPage1.y = bArr[3].y;
s.rPage0.x = s.p1.x;
s.rPage0.y = s.p1.y;
}
else if (num == 2) {
movePoint = s.checkLimit(movePoint, s.limitP2, s.bW);
movePoint = s.checkLimit(movePoint, s.limitP1, s.crossGap);
bArr = s.getBookArr(movePoint, s.p2, s.p1);
actionPoint = bArr[1];
s.getLayerArr(movePoint, actionPoint, s.p2, s.p1, s.limitP2, s.limitP1);
s.getShadow(s.shadow0, s.p2, movePoint, 0.5);
s.getShadow(s.shadow1, s.p2, movePoint, 0.45);
s.rPage1.rotation = s.angle(movePoint, actionPoint) - 90;
s.rPage1.x = bArr[2].x;
s.rPage1.y = bArr[2].y;
s.rPage0.x = s.p1.x;
s.rPage0.y = s.p1.y;
}
else if (num == 3) {
movePoint = s.checkLimit(movePoint, s.limitP1, s.bW);
movePoint = s.checkLimit(movePoint, s.limitP2, s.crossGap);
bArr = s.getBookArr(movePoint, s.p3, s.p4);
actionPoint = bArr[1];
s.getLayerArr(movePoint, actionPoint, s.p3, s.p4, s.limitP1, s.limitP2);
s.getShadow(s.shadow0, s.p3, movePoint, 0.5);
s.getShadow(s.shadow1, s.p3, movePoint, 0.4);
s.rPage1.rotation = s.angle(movePoint, actionPoint) + 90;
s.rPage1.x = movePoint.x;
s.rPage1.y = movePoint.y;
s.rPage0.x = s.limitP1.x;
s.rPage0.y = s.limitP1.y;
}
else {
movePoint = s.checkLimit(movePoint, s.limitP2, s.bW);
movePoint = s.checkLimit(movePoint, s.limitP1, s.crossGap);
bArr = s.getBookArr(movePoint, s.p4, s.p3);
actionPoint = bArr[1];
s.getLayerArr(movePoint, actionPoint, s.p4, s.p3, s.limitP2, s.limitP1);
s.getShadow(s.shadow0, s.p4, movePoint, 0.5);
s.getShadow(s.shadow1, s.p4, movePoint, 0.4);
s.rPage1.rotation = s.angle(movePoint, actionPoint) - 90;
s.rPage1.x = actionPoint.x;
s.rPage1.y = actionPoint.y;
s.rPage0.x = s.limitP1.x;
s.rPage0.y = s.limitP1.y;
}
s.getShape(s.rMask1, s.layer1Arr);
s.getShape(s.rMask0, s.layer0Arr);
};
FlipBook.prototype.checkLimit = function (point, limitPoint, limitGap) {
var s = this;
var gap = Math.abs(s.pos(limitPoint, point));
var angle = s.angle(limitPoint, point);
if (gap > limitGap) {
var tmp1 = limitGap * Math.sin((angle / 180) * Math.PI);
var tmp2 = limitGap * Math.cos((angle / 180) * Math.PI);
point = new Point(limitPoint.x - tmp2, limitPoint.y - tmp1);
}
return point;
};
FlipBook.prototype.getPage = function (index) {
var s = this;
if (!s.pages[index - 1]) {
s.pages[index - 1] = s.getPageCallback(index);
}
return s.pages[index - 1];
};
FlipBook.prototype.getBookArr = function (point, actionPoint1, actionPoint2) {
var s = this;
var bArr = [];
var gap1 = Math.abs(s.pos(actionPoint1, point) * 0.5);
var angle1 = s.angle(actionPoint1, point);
var tmp1_2 = gap1 / Math.cos((angle1 / 180) * Math.PI);
var tmpPoint1 = new Point(actionPoint1.x - tmp1_2, actionPoint1.y);
var angle2 = s.angle(point, tmpPoint1) - s.angle(point, actionPoint2);
var gap2 = s.pos(point, actionPoint2);
var tmp2_1 = gap2 * Math.sin((angle2 / 180) * Math.PI);
var tmp2_2 = gap2 * Math.cos((angle2 / 180) * Math.PI);
var tmpPoint2 = new Point(actionPoint1.x + tmp2_2, actionPoint1.y + tmp2_1);
var angle3 = s.angle(tmpPoint1, point);
var tmp3_1 = s.bW * Math.sin((angle3 / 180) * Math.PI);
var tmp3_2 = s.bW * Math.cos((angle3 / 180) * Math.PI);
var tmpPoint3 = new Point(tmpPoint2.x + tmp3_2, tmpPoint2.y + tmp3_1);
var tmpPoint4 = new Point(point.x + tmp3_2, point.y + tmp3_1);
bArr.push(point);
bArr.push(tmpPoint2);
bArr.push(tmpPoint3);
bArr.push(tmpPoint4);
return bArr;
};
FlipBook.prototype.getLayerArr = function (point1, point2, actionPoint1, actionPoint2, limitPoint1, limitPoint2) {
var s = this;
var arrLayer1 = [];
var arrLayer2 = [];
var gap1 = Math.abs(s.pos(actionPoint1, point1) * 0.5);
var angle1 = s.angle(actionPoint1, point1);
var tmp1_1 = gap1 / Math.sin((angle1 / 180) * Math.PI);
var tmp1_2 = gap1 / Math.cos((angle1 / 180) * Math.PI);
var tmpPoint1 = new Point(actionPoint1.x - tmp1_2, actionPoint1.y);
var tmpPoint2 = new Point(actionPoint1.x, actionPoint1.y - tmp1_1);
var tmpPoint3 = point2;
var gap2 = Math.abs(s.pos(point1, actionPoint2));
if (gap2 > s.bH) {
arrLayer1.push(tmpPoint3);
var pos = Math.abs(s.pos(tmpPoint3, actionPoint2) * 0.5);
var tmp3 = pos / Math.cos((angle1 / 180) * Math.PI);
tmpPoint2 = new Point(actionPoint2.x - tmp3, actionPoint2.y);
}
else {
arrLayer2.push(actionPoint2);
}
arrLayer1.push(tmpPoint2);
arrLayer1.push(tmpPoint1);
arrLayer1.push(point1);
s.layer1Arr = arrLayer1;
arrLayer2.push(limitPoint2);
arrLayer2.push(limitPoint1);
arrLayer2.push(tmpPoint1);
arrLayer2.push(tmpPoint2);
s.layer0Arr = arrLayer2;
};
FlipBook.prototype.getShape = function (shape, pointArr) {
var num = pointArr.length;
shape.clear();
shape.beginFill("#000");
shape.moveTo(pointArr[0].x, pointArr[0].y);
for (var i = 1; i < num; i++) {
shape.lineTo(pointArr[i].x, pointArr[i].y);
}
shape.endFill();
};
FlipBook.prototype.setShadowMask = function (shape, g_width, g_height) {
shape.beginLinearGradientFill([-g_width * 0.5, 4, g_width * 0.5, 4], [[0, "#000000", 0], [1, "#000000", 0.6]]);
shape.drawRect(-g_width * 0.5, -g_height * 0.5, g_width * 0.5, g_height);
shape.endFill();
shape.beginLinearGradientFill([-g_width * 0.5, 4, g_width * 0.5, 4], [[1, "#000000", 0], [0, "#000000", 0.6]]);
shape.drawRect(0, -g_height * 0.5, g_width * 0.5, g_height);
shape.endFill();
};
FlipBook.prototype.getShadow = function (shape, point1, point2, arg) {
var myScale;
var myAlpha;
var s = this;
shape.visible = true;
shape.x = point2.x + (point1.x - point2.x) * arg;
shape.y = point2.y + (point1.y - point2.y) * arg;
shape.rotation = s.angle(point1, point2);
myScale = Math.abs(point1.x - point2.x) * 0.5 / s.bW;
myAlpha = 1 - myScale * myScale;
shape.scaleX = myScale + 0.1;
shape.alpha = myAlpha + 0.1;
};
FlipBook.prototype.setPage = function (pageNum) {
var s = this;
if (pageNum > 0 && pageNum <= s.totalPage) {
s.leftPage = s.getPage(pageNum);
s.leftPage.x = s.leftPage.y = 0;
s.pageMC.addChild(s.leftPage);
}
if ((pageNum + 1) > 0 && (pageNum + 1) < s.totalPage) {
s.rightPage = s.getPage(pageNum + 1);
s.rightPage.x = s.bW;
s.rightPage.y = 0;
s.pageMC.addChild(s.rightPage);
}
};
FlipBook.prototype.onMouseDown = function (e) {
var s = this;
if (!s.canFlip || s.state != "stop") {
return;
}
s.stageMP.x = e.clientX;
s.stageMP.y = e.clientY;
var p = s.globalToLocal(s.stageMP);
s.stageMP = p;
s.timerArg0 = s.checkArea(p);
s.timerArg0 = s.timerArg0 < 0 ? -s.timerArg0 : s.timerArg0;
if (s.timerArg0 > 0) {
if ((s.timerArg0 < 3 && s.currPage > 0) || (s.timerArg0 > 2 && s.currPage <= s.totalPage - 2)) {
s.state = "start";
s.flushPage();
s.dispatchEvent("onFlipStart");
}
}
};
FlipBook.prototype.onMouseUp = function (e) {
var s = this;
if (s.state == "start") {
s.stageMP.x = e.clientX;
s.stageMP.y = e.clientY;
var p = s.globalToLocal(s.stageMP);
s.timerArg1 = s.checkArea(p);
s.state = "auto";
s.stageMP = p;
}
};
FlipBook.prototype.onMouseMove = function (e) {
var s = this;
if (s.state == "start") {
s.stageMP.x = e.clientX;
s.stageMP.y = e.clientY;
var p = s.globalToLocal(s.stageMP);
s.stageMP = p;
}
};
FlipBook.prototype.checkArea = function (point) {
var s = this;
var tmpN;
var minX = 0;
var maxX = s.bW + s.bW;
var minY = 0;
var maxY = s.bH;
var areaNum = 50;
if (point.x > minX && point.x <= maxX * 0.5) {
tmpN = (point.y > minY && point.y <= (maxY * 0.5)) ? 1 : (point.y > (maxY * 0.5) && point.y < maxY) ? 2 : 0;
if (point.x <= (minX + areaNum)) {
tmpN = (point.y > minY && point.y <= (minY + areaNum)) ? -1 : (point.y > (maxY - areaNum) && point.y < maxY) ? -2 : tmpN;
}
return tmpN;
}
else if (point.x > (maxX * 0.5) && point.x < maxX) {
tmpN = (point.y > minY && point.y <= (maxY * 0.5)) ? 3 : (point.y > (maxY * 0.5) && point.y < maxY) ? 4 : 0;
if (point.x >= (maxX - areaNum)) {
tmpN = (point.y > minY && point.y <= (minY + areaNum)) ? -3 : (point.y > (maxY - areaNum) && point.y < maxY) ? -4 : tmpN;
}
return tmpN;
}
return 0;
};
/**
* 跳到指定的页数
* @method flipTo
* @param {number} index 跳到指定的页数
* @since 1.0.3
*/
FlipBook.prototype.flipTo = function (index) {
var n;
var s = this;
index = index % 2 == 1 ? index - 1 : index;
n = index - s.currPage;
if (s.state == "stop" && index >= 0 && index <= s.totalPage && n != 0) {
s.timerArg0 = n < 0 ? 1 : 3;
s.timerArg1 = -1;
s.toPage = index > s.totalPage ? s.totalPage : index;
s.state = "auto";
s.flushPage();
}
};
/**
* @method nextPage
* @public
* @since 1.1.1
*/
FlipBook.prototype.nextPage = function () {
this.flipTo(this.currPage + 2);
};
/**
* @method prevPage
* @public
* @since 1.1.1
*/
FlipBook.prototype.prevPage = function () {
this.flipTo(this.currPage - 1);
};
/**
* @method startPage
* @public
* @since 1.1.1
*/
FlipBook.prototype.startPage = function () {
this.flipTo(0);
};
/**
* @method endPage
* @public
* @since 1.1.1
*/
FlipBook.prototype.endPage = function () {
this.flipTo(this.totalPage);
};
FlipBook.prototype.flushPage = function () {
var s = this;
var page0;
var page1;
var myPos = s.myPosArr[s.timerArg0 - 1];
var p = null;
if (s.timerArg0 == 1 || s.timerArg0 == 2) {
s.toPage = s.toPage == s.currPage ? s.currPage - 2 : s.toPage;
page0 = s.currPage;
page1 = s.toPage + 1;
this.pageMC.removeChild(s.leftPage);
if (s.toPage > 0) {
p = s.getPage(s.currPage - 2);
p.x = 0;
s.leftPage = p;
s.pageMC.addChild(p);
}
}
else if (s.timerArg0 == 3 || s.timerArg0 == 4) {
s.toPage = s.toPage == s.currPage ? s.currPage + 2 : s.toPage;
page0 = s.currPage + 1;
page1 = s.toPage;
s.pageMC.removeChild(s.rightPage);
if (s.toPage + 1 < s.totalPage) {
p = s.getPage(s.currPage + 3);
p.x = s.bW;
s.rightPage = p;
s.pageMC.addChild(p);
}
}
s.px = myPos.x;
s.py = myPos.y;
if (page0 > 0 && page0 <= s.totalPage) {
p = s.getPage(page0);
p.x = 0;
p.y = 0;
s.rPage0.addChild(p);
}
if (page1 > 0 && page1 <= s.totalPage) {
p = s.getPage(page1);
p.x = 0;
p.y = 0;
s.rPage1.addChild(p);
}
};
FlipBook.prototype.onEnterFrame = function (e) {
var s = this;
var toPos = s.toPosArr[s.timerArg0 - 1];
var myPos = s.myPosArr[s.timerArg0 - 1];
var tox;
var toy;
var toFlag;
var tmpX;
var tmpY;
var u;
if (s.state == "start") {
u = s.speed;
var p = s.stageMP;
s.px += (p.x - s.px) * u >> 0;
s.py += (p.y - s.py) * u >> 0;
var np = new Point(s.px, s.py);
s.drawPage(s.timerArg0, np);
}
else if (s.state == "auto") {
if (Math.abs(toPos.x - s.px) > s.bW * 1.5 && s.timerArg1 > 0) {
//不处于点翻区域并且翻页不过中线时
tox = myPos.x;
toy = myPos.y;
toFlag = 0;
}
else {
tox = toPos.x;
toy = toPos.y;
toFlag = 1;
}
tmpX = (tox - s.px) >> 0;
tmpY = (toy - s.py) >> 0;
if (s.timerArg1 < 0) {
u = s.speed * 0.7;
s.py = s.arc(s.bW, tmpX, toPos.y);
}
else {
u = s.speed;
s.py = tmpY * u + s.py;
}
s.px = tmpX * u + s.px;
s.drawPage(s.timerArg0, new Point(s.px, s.py));
if (tmpX == 0 && tmpY == 0) {
s.rPage0.removeAllChildren();
s.rPage1.removeAllChildren();
s.shadow0.visible = false;
s.shadow1.visible = false;
s.toPage = toFlag == 0 ? s.currPage : s.toPage;
s.currPage = s.toPage;
s.pageMC.removeAllChildren();
s.setPage(s.currPage);
s.state = "stop";
s.dispatchEvent("onFlipStop");
}
}
};
FlipBook.prototype.arc = function (argR, argN1, argN2) {
var arg = argR * 2;
var r = argR * argR + arg * arg;
var a = Math.abs(argN1) - argR;
return argN2 - (Math.sqrt(r - a * a) - arg);
};
FlipBook.prototype.angle = function (target1, target2) {
var tmpX = target1.x - target2.x;
var tmpY = target1.y - target2.y;
var tmp_angle = Math.atan2(tmpY, tmpX) * 180 / Math.PI;
return tmp_angle < 0 ? tmp_angle + 360 : tmp_angle;
};
FlipBook.prototype.pos = function (target1, target2) {
var tmpX = target1.x - target2.x;
var tmpY = target1.y - target2.y;
var tmpS = Math.sqrt(tmpX * tmpX + tmpY * tmpY);
return target1.x > target2.x ? tmpS : -tmpS;
};
FlipBook.prototype.destroy = function () {
var s = this;
s.md = null;
s.mu = null;
s.mm = null;
s.layer0Arr = null;
s.layer1Arr = null;
s.toPosArr = null;
s.myPosArr = null;
s.rPage0 = null;
s.rMask0 = null;
s.rMask1 = null;
s.sMask0 = null;
s.sMask1 = null;
s.leftPage = null;
s.rightPage = null;
s.pageMC = null;
s.rightPage = null;
s.shadow0 = null;
s.shadow1 = null;
s.p1 = null;
s.p2 = null;
s.p3 = null;
s.p4 = null;
s.limitP1 = null;
s.limitP2 = null;
s.pages = null;
s.stageMP = null;
s.getPageCallback = null;
_super.prototype.destroy.call(this);
};
return FlipBook;
}(Sprite));
annieUI.FlipBook = FlipBook;
})(annieUI || (annieUI = {}));