123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- var t = require;
- var e = module;
- var i = exports;
- var s =
- (this && this.__decorate) ||
- function (t, e, i, s) {
- var o,
- a = arguments.length,
- n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
- if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
- else
- for (var r = t.length - 1; 0 <= r; r--)
- (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
- return 3 < a && n && Object.defineProperty(e, i, n), n;
- };
- Object.defineProperty(i, "__esModule", {value: !0});
- const {ccclass: o, property: a} = cc._decorator;
- e = class extends cc.Component {
- constructor() {
- super(...arguments),
- (this._tempPos0 = {x: 0, y: 0}),
- (this.inited = !1),
- (this.size = cc.size(0, 0)),
- (this.area = cc.rect(-9e9, -9e9, 9e9, 9e9)),
- (this.cameraNode = null),
- (this.camera = null),
- (this.x = 0),
- (this.y = 0),
- (this.width = 1024),
- (this.height = 1024),
- (this.scale = 1),
- (this.defaultScale = 1),
- (this.extBorder = 100),
- (this.defaultMinScale = 0.367),
- (this.photoMinScale = 0.367),
- (this.minScale = 0.367),
- (this.maxScale = 1.2),
- (this.zoomStep = 0.05),
- (this.pivotX = 0),
- (this.pivotY = 0),
- (this.changed = !1),
- (this.delayChange = !1),
- (this.EVENT_CHANGED = "VIEWPORT_CHANGED"),
- (this.limitLines = {LT: null, RT: null, RB: null, LB: null}),
- (this.corner = {x: 0, y: 0}),
- (this.viewTween = null);
- }
- onLoad() {
- (this.minScale = this.defaultMinScale), (this.aabb = []), (this.eventTarget = new cc.EventTarget());
- }
- start() {
- this.init();
- }
- init() {
- (this.originalSize = cc.size(cc.visibleRect.width, cc.visibleRect.height)),
- (this.cameraNode = this.cameraNode || this.node),
- (this.camera = cc.Camera.findCamera(this.node)),
- (this.width = this.originalSize.width / this.scale),
- (this.height = this.originalSize.height / this.scale),
- (this.x1 = this.area.x),
- (this.y1 = this.area.y),
- (this.x2 = this.area.x + this.area.width),
- (this.y2 = this.area.x + this.area.height),
- (this.pivotX = this.x),
- (this.pivotY = this.y),
- this.updateAABB(),
- (this.inited = !0),
- (this.changed = !0);
- }
- setOriginalSize(t, e) {
- (this.originalSize.width = t || cc.visibleRect.width),
- (this.originalSize.height = e || cc.visibleRect.height),
- (this.width = this.originalSize.width / this.scale),
- (this.height = this.originalSize.height / this.scale),
- this.updateAABB(),
- (this.changed = !0);
- }
- setArea(t, e, i, s) {
- (this.area.x = t),
- (this.area.y = e),
- (this.area.width = i),
- (this.area.height = s),
- (this.x1 = t),
- (this.y1 = e),
- (this.x2 = t + i),
- (this.y2 = e + s);
- }
- moveBy(t, e, i = !1) {
- this.moveTo(this.x + t, this.y + e, i);
- }
- moveTo(t, e, i = !1) {
- (t = Math.max(this.x1, Math.min(t, this.x2 - this.width))),
- (e = Math.max(this.y1, Math.min(e, this.y2 - this.height))),
- (this.x === t && this.y === e && !i) ||
- ((this.x = t), (this.y = e), this.updateAABB(), (this.changed = !0));
- }
- moveCenterTo(t, e, i = !1) {
- (t -= this.width / 2), (e -= this.height / 2), this.moveTo(t, e, i);
- }
- screenToView(t, e) {
- return (
- (this._tempPos0.x = t / this.scale + this.x), (this._tempPos0.y = e / this.scale + this.y), this._tempPos0
- );
- }
- setPivot(t, e) {
- (this.pivotX = t), (this.pivotY = e);
- }
- setPivotInScreen(t, e) {
- (this.pivotX = t / this.scale + this.x), (this.pivotY = e / this.scale + this.y);
- }
- getScale() {
- return this.scale;
- }
- setScale(t, e = !1) {
- var i, s, o;
- (t = Math.max(this.minScale, Math.min(t, this.maxScale))),
- (this.scale === t && !e) ||
- ((this.width = this.originalSize.width / t),
- (this.height = this.originalSize.height / t),
- (i = this.pivotX - this.x),
- (s = this.pivotY - this.y),
- (i *= o = 1 - this.scale / t),
- (s *= o),
- (this.scale = t),
- (this.changed = !0),
- this.moveTo(this.x + i, this.y + s, e));
- }
- _zoomInOut(t, e, i) {
- if ("number" == typeof (this.pivotX = e) && "number" == typeof i) this.pivotY = i;
- else {
- const t = this.x + this.width / 2,
- e = this.y + this.height / 2;
- (this.pivotX = t), (this.pivotY = e);
- }
- this.setScale(this.scale + this.zoomStep * (t ? 1 : -1));
- }
- zoomIn(t, e) {
- this._zoomInOut(!0, t, e);
- }
- zoomOut(t, e) {
- this._zoomInOut(!1, t, e);
- }
- pointLimit(t, e) {
- if (!t || !e) return !1;
- const i = e[0],
- s = e[1];
- if (0 <= (i.x - t.x) * (s.y - t.y) - (i.y - t.y) * (s.x - t.x)) return !1;
- var o = s.x - i.x,
- e = s.y - i.y;
- if (0 == o) t.x = i.x;
- else if (0 == e) t.y = i.y;
- else {
- const a = e / o,
- s = -o / e,
- n = i.y - a * i.x,
- r = t.y - s * t.x;
- (t.x = (r - n) / (a - s)), (t.y = a * t.x + n);
- }
- return !0;
- }
- scrollBy(t, e, i = 0, s) {
- const {x: o, y: a, width: n, height: r} = this,
- l = this.limitLines,
- h = this.corner;
- let c,
- d,
- u = !1;
- !u && (t < 0 || e < 0) && ((c = o), (d = a), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.LT))),
- !u &&
- (0 < t || e < 0) &&
- ((c = o + n), (d = a), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.RT))),
- !u &&
- (0 < t || 0 < e) &&
- ((c = o + n), (d = a + r), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.RB))),
- !u &&
- (t < 0 || 0 < e) &&
- ((c = o), (d = a + r), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.LB))),
- u && ((t = h.x - c), (e = h.y - d)),
- this.scrollTo(o + t, a + e, i, s);
- }
- scrollTo(t, e, i = 0, s) {
- const o = this;
- var a;
- this.viewTween && (this.viewTween.stop(), (this.viewTween = null)),
- 0 < i
- ? ((a = {x: o.x, y: o.y}),
- (this.viewTween = cc
- .tween(a)
- .to(
- i,
- {x: t, y: e},
- {
- onUpdate: t => {
- o.moveTo(t.x, t.y), (this.delayChange = !0);
- }
- }
- )
- .call(() => {
- (this.viewTween = null), s && s();
- })
- .start()))
- : o.moveTo(t, e);
- }
- scrollCenterTo(t, e, i = 0, s) {
- (t -= this.width / 2), (e -= this.height / 2), this.scrollTo(t, e, i, s);
- }
- scaleBy(t, e = 0, i) {
- this.scaleTo(this.scale + t, e, i);
- }
- scaleTo(t, e = 0, i) {
- var s;
- (t = Math.max(this.minScale, Math.min(t, this.maxScale))),
- this.scale !== t &&
- (this.viewTween && (this.viewTween.stop(), (this.viewTween = null)),
- 0 < e
- ? ((s = {scale: this.scale}),
- (this.viewTween = cc
- .tween(s)
- .to(
- e,
- {scale: t},
- {
- onUpdate: t => {
- this.setScale(t.scale), (this.delayChange = !0);
- }
- }
- )
- .call(() => {
- (this.viewTween = null), i && i();
- })
- .start()))
- : this.setScale(t));
- }
- scrollAndScaleTo(a, n, t, e = 0, i) {
- if (
- (this.setScale(t),
- (t = Math.max(this.minScale, Math.min(t || this.scale, this.maxScale))),
- this.setPivot(a, n),
- this.viewTween && (this.viewTween.stop(), (this.viewTween = null)),
- 0 < e)
- ) {
- const {width: r, height: l} = this.originalSize,
- s = this.scale,
- o = (a - this.x) * s,
- h = (n - this.y) * s,
- c = {dx: o - (this.width / 2) * s, dy: h - (this.height / 2) * s, scale: s},
- d = {dx: 0, dy: 0, scale: t};
- this.viewTween = cc
- .tween(c)
- .to(e, d, {
- onUpdate: t => {
- var e = t.scale,
- i = (this.width = r / e),
- s = (this.height = l / e);
- this.scale = e;
- var o = a - t.dx / e,
- e = n - t.dy / e;
- (this.x = o - i / 2),
- (this.y = e - s / 2),
- this.updateAABB(),
- (this.changed = !0),
- (this.delayChange = !0);
- }
- })
- .call(() => {
- (this.viewTween = null), i && i();
- })
- .start();
- } else this.moveCenterTo(a, n);
- }
- getCenter() {
- var t = this.x + this.width / 2,
- e = this.y + this.height / 2;
- return (this._tempPos0.x = t), (this._tempPos0.y = e), this._tempPos0;
- }
- updateAABB() {
- const t = this.aabb;
- (t[0] = this.x - this.extBorder),
- (t[1] = this.y - this.extBorder),
- (t[2] = this.x + this.width + this.extBorder),
- (t[3] = this.y + this.height + this.extBorder);
- }
- isVisible(t) {
- var e = this.aabb;
- return e[0] < t[2] && e[1] < t[3] && e[2] > t[0] && e[3] > t[1];
- }
- isVisiblePoint(t, e) {
- var i = this.aabb;
- return i[0] < t && i[1] < e && i[2] > t && i[3] > e;
- }
- onChanged(t, e = !1) {
- e ? this.eventTarget.once(this.EVENT_CHANGED, t) : this.eventTarget.on(this.EVENT_CHANGED, t);
- }
- offChanged(t) {
- this.eventTarget.off(this.EVENT_CHANGED, t);
- }
- syncView(t = !1) {
- if (this.changed || t) {
- const t = this.x + this.width / 2,
- e = -(this.y + this.height / 2);
- this.cameraNode.setPosition(t, e),
- (this.camera.zoomRatio = this.scale),
- this.changed && (this.eventTarget.emit(this.EVENT_CHANGED, this), (this.changed = !1));
- }
- }
- setInputHandler(t) {
- this.inputHandler && this.removeInputHandler(),
- t &&
- ((this.inputHandler = t),
- (this.allowMove = !0),
- (this.allowScale = !0),
- (this._onPan = (t, e, i, s) => {
- var o;
- this.allowMove &&
- !this.viewTween &&
- ((o = this.scale),
- (1 < Math.abs(i) || 1 < Math.abs(s)) && ((i *= 1.6), (s *= 1.6)),
- this.scrollBy(-i / o, -s / o));
- }),
- t.onPan(this._onPan),
- (this._onPinch = (t, e, i) => {
- this.allowScale &&
- !this.viewTween &&
- ((e = (this.scale * i) / e), this.setPivotInScreen(t.x, t.y), this.scaleTo(e));
- }),
- (this._onKeyUp = t => {
- if (this.allowScale && !this.viewTween)
- switch (t.keyCode) {
- case cc.macro.KEY.equal:
- case cc.macro.KEY["+"]:
- this.zoomIn();
- break;
- case cc.macro.KEY.dash:
- case cc.macro.KEY["-"]:
- this.zoomOut();
- }
- }),
- t.onKeyUp(this._onKeyUp),
- (this._onMouseWhell = t => {
- this.allowScale &&
- !this.viewTween &&
- ((t = t.getScrollY()),
- cc.log("onMouseWheel:", t),
- t < -4 ? this.zoomIn() : 4 < t && this.zoomOut());
- }),
- t.onMouseWheel(this._onMouseWhell));
- }
- removeInputHandler() {
- const t = this.inputHandler;
- t &&
- ((this.allowMove = !1),
- (this.allowScale = !1),
- t.offPan(this._onPan),
- (this._onPan = null),
- t.offPinch(this._onPinch),
- (this._onPinch = null),
- t.offKeyUp(this._onKeyUp),
- (this._onKeyUp = null),
- t.offMouseWheel(this._onMouseWhell),
- (this._onMouseWhell = null),
- (this.inputHandler = null));
- }
- };
- s([a(cc.size)], e.prototype, "size", void 0),
- s([a], e.prototype, "area", void 0),
- s([a(cc.Node)], e.prototype, "cameraNode", void 0),
- (e = s([o], e)),
- (i.default = e);
|