Viewport.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var s =
  5. (this && this.__decorate) ||
  6. function (t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", {value: !0});
  17. const {ccclass: o, property: a} = cc._decorator;
  18. e = class extends cc.Component {
  19. constructor() {
  20. super(...arguments),
  21. (this._tempPos0 = {x: 0, y: 0}),
  22. (this.inited = !1),
  23. (this.size = cc.size(0, 0)),
  24. (this.area = cc.rect(-9e9, -9e9, 9e9, 9e9)),
  25. (this.cameraNode = null),
  26. (this.camera = null),
  27. (this.x = 0),
  28. (this.y = 0),
  29. (this.width = 1024),
  30. (this.height = 1024),
  31. (this.scale = 1),
  32. (this.defaultScale = 1),
  33. (this.extBorder = 100),
  34. (this.defaultMinScale = 0.367),
  35. (this.photoMinScale = 0.367),
  36. (this.minScale = 0.367),
  37. (this.maxScale = 1.2),
  38. (this.zoomStep = 0.05),
  39. (this.pivotX = 0),
  40. (this.pivotY = 0),
  41. (this.changed = !1),
  42. (this.delayChange = !1),
  43. (this.EVENT_CHANGED = "VIEWPORT_CHANGED"),
  44. (this.limitLines = {LT: null, RT: null, RB: null, LB: null}),
  45. (this.corner = {x: 0, y: 0}),
  46. (this.viewTween = null);
  47. }
  48. onLoad() {
  49. (this.minScale = this.defaultMinScale), (this.aabb = []), (this.eventTarget = new cc.EventTarget());
  50. }
  51. start() {
  52. this.init();
  53. }
  54. init() {
  55. (this.originalSize = cc.size(cc.visibleRect.width, cc.visibleRect.height)),
  56. (this.cameraNode = this.cameraNode || this.node),
  57. (this.camera = cc.Camera.findCamera(this.node)),
  58. (this.width = this.originalSize.width / this.scale),
  59. (this.height = this.originalSize.height / this.scale),
  60. (this.x1 = this.area.x),
  61. (this.y1 = this.area.y),
  62. (this.x2 = this.area.x + this.area.width),
  63. (this.y2 = this.area.x + this.area.height),
  64. (this.pivotX = this.x),
  65. (this.pivotY = this.y),
  66. this.updateAABB(),
  67. (this.inited = !0),
  68. (this.changed = !0);
  69. }
  70. setOriginalSize(t, e) {
  71. (this.originalSize.width = t || cc.visibleRect.width),
  72. (this.originalSize.height = e || cc.visibleRect.height),
  73. (this.width = this.originalSize.width / this.scale),
  74. (this.height = this.originalSize.height / this.scale),
  75. this.updateAABB(),
  76. (this.changed = !0);
  77. }
  78. setArea(t, e, i, s) {
  79. (this.area.x = t),
  80. (this.area.y = e),
  81. (this.area.width = i),
  82. (this.area.height = s),
  83. (this.x1 = t),
  84. (this.y1 = e),
  85. (this.x2 = t + i),
  86. (this.y2 = e + s);
  87. }
  88. moveBy(t, e, i = !1) {
  89. this.moveTo(this.x + t, this.y + e, i);
  90. }
  91. moveTo(t, e, i = !1) {
  92. (t = Math.max(this.x1, Math.min(t, this.x2 - this.width))),
  93. (e = Math.max(this.y1, Math.min(e, this.y2 - this.height))),
  94. (this.x === t && this.y === e && !i) ||
  95. ((this.x = t), (this.y = e), this.updateAABB(), (this.changed = !0));
  96. }
  97. moveCenterTo(t, e, i = !1) {
  98. (t -= this.width / 2), (e -= this.height / 2), this.moveTo(t, e, i);
  99. }
  100. screenToView(t, e) {
  101. return (
  102. (this._tempPos0.x = t / this.scale + this.x), (this._tempPos0.y = e / this.scale + this.y), this._tempPos0
  103. );
  104. }
  105. setPivot(t, e) {
  106. (this.pivotX = t), (this.pivotY = e);
  107. }
  108. setPivotInScreen(t, e) {
  109. (this.pivotX = t / this.scale + this.x), (this.pivotY = e / this.scale + this.y);
  110. }
  111. getScale() {
  112. return this.scale;
  113. }
  114. setScale(t, e = !1) {
  115. var i, s, o;
  116. (t = Math.max(this.minScale, Math.min(t, this.maxScale))),
  117. (this.scale === t && !e) ||
  118. ((this.width = this.originalSize.width / t),
  119. (this.height = this.originalSize.height / t),
  120. (i = this.pivotX - this.x),
  121. (s = this.pivotY - this.y),
  122. (i *= o = 1 - this.scale / t),
  123. (s *= o),
  124. (this.scale = t),
  125. (this.changed = !0),
  126. this.moveTo(this.x + i, this.y + s, e));
  127. }
  128. _zoomInOut(t, e, i) {
  129. if ("number" == typeof (this.pivotX = e) && "number" == typeof i) this.pivotY = i;
  130. else {
  131. const t = this.x + this.width / 2,
  132. e = this.y + this.height / 2;
  133. (this.pivotX = t), (this.pivotY = e);
  134. }
  135. this.setScale(this.scale + this.zoomStep * (t ? 1 : -1));
  136. }
  137. zoomIn(t, e) {
  138. this._zoomInOut(!0, t, e);
  139. }
  140. zoomOut(t, e) {
  141. this._zoomInOut(!1, t, e);
  142. }
  143. pointLimit(t, e) {
  144. if (!t || !e) return !1;
  145. const i = e[0],
  146. s = e[1];
  147. if (0 <= (i.x - t.x) * (s.y - t.y) - (i.y - t.y) * (s.x - t.x)) return !1;
  148. var o = s.x - i.x,
  149. e = s.y - i.y;
  150. if (0 == o) t.x = i.x;
  151. else if (0 == e) t.y = i.y;
  152. else {
  153. const a = e / o,
  154. s = -o / e,
  155. n = i.y - a * i.x,
  156. r = t.y - s * t.x;
  157. (t.x = (r - n) / (a - s)), (t.y = a * t.x + n);
  158. }
  159. return !0;
  160. }
  161. scrollBy(t, e, i = 0, s) {
  162. const {x: o, y: a, width: n, height: r} = this,
  163. l = this.limitLines,
  164. h = this.corner;
  165. let c,
  166. d,
  167. u = !1;
  168. !u && (t < 0 || e < 0) && ((c = o), (d = a), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.LT))),
  169. !u &&
  170. (0 < t || e < 0) &&
  171. ((c = o + n), (d = a), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.RT))),
  172. !u &&
  173. (0 < t || 0 < e) &&
  174. ((c = o + n), (d = a + r), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.RB))),
  175. !u &&
  176. (t < 0 || 0 < e) &&
  177. ((c = o), (d = a + r), (h.x = c + t), (h.y = d + e), (u = this.pointLimit(h, l.LB))),
  178. u && ((t = h.x - c), (e = h.y - d)),
  179. this.scrollTo(o + t, a + e, i, s);
  180. }
  181. scrollTo(t, e, i = 0, s) {
  182. const o = this;
  183. var a;
  184. this.viewTween && (this.viewTween.stop(), (this.viewTween = null)),
  185. 0 < i
  186. ? ((a = {x: o.x, y: o.y}),
  187. (this.viewTween = cc
  188. .tween(a)
  189. .to(
  190. i,
  191. {x: t, y: e},
  192. {
  193. onUpdate: t => {
  194. o.moveTo(t.x, t.y), (this.delayChange = !0);
  195. }
  196. }
  197. )
  198. .call(() => {
  199. (this.viewTween = null), s && s();
  200. })
  201. .start()))
  202. : o.moveTo(t, e);
  203. }
  204. scrollCenterTo(t, e, i = 0, s) {
  205. (t -= this.width / 2), (e -= this.height / 2), this.scrollTo(t, e, i, s);
  206. }
  207. scaleBy(t, e = 0, i) {
  208. this.scaleTo(this.scale + t, e, i);
  209. }
  210. scaleTo(t, e = 0, i) {
  211. var s;
  212. (t = Math.max(this.minScale, Math.min(t, this.maxScale))),
  213. this.scale !== t &&
  214. (this.viewTween && (this.viewTween.stop(), (this.viewTween = null)),
  215. 0 < e
  216. ? ((s = {scale: this.scale}),
  217. (this.viewTween = cc
  218. .tween(s)
  219. .to(
  220. e,
  221. {scale: t},
  222. {
  223. onUpdate: t => {
  224. this.setScale(t.scale), (this.delayChange = !0);
  225. }
  226. }
  227. )
  228. .call(() => {
  229. (this.viewTween = null), i && i();
  230. })
  231. .start()))
  232. : this.setScale(t));
  233. }
  234. scrollAndScaleTo(a, n, t, e = 0, i) {
  235. if (
  236. (this.setScale(t),
  237. (t = Math.max(this.minScale, Math.min(t || this.scale, this.maxScale))),
  238. this.setPivot(a, n),
  239. this.viewTween && (this.viewTween.stop(), (this.viewTween = null)),
  240. 0 < e)
  241. ) {
  242. const {width: r, height: l} = this.originalSize,
  243. s = this.scale,
  244. o = (a - this.x) * s,
  245. h = (n - this.y) * s,
  246. c = {dx: o - (this.width / 2) * s, dy: h - (this.height / 2) * s, scale: s},
  247. d = {dx: 0, dy: 0, scale: t};
  248. this.viewTween = cc
  249. .tween(c)
  250. .to(e, d, {
  251. onUpdate: t => {
  252. var e = t.scale,
  253. i = (this.width = r / e),
  254. s = (this.height = l / e);
  255. this.scale = e;
  256. var o = a - t.dx / e,
  257. e = n - t.dy / e;
  258. (this.x = o - i / 2),
  259. (this.y = e - s / 2),
  260. this.updateAABB(),
  261. (this.changed = !0),
  262. (this.delayChange = !0);
  263. }
  264. })
  265. .call(() => {
  266. (this.viewTween = null), i && i();
  267. })
  268. .start();
  269. } else this.moveCenterTo(a, n);
  270. }
  271. getCenter() {
  272. var t = this.x + this.width / 2,
  273. e = this.y + this.height / 2;
  274. return (this._tempPos0.x = t), (this._tempPos0.y = e), this._tempPos0;
  275. }
  276. updateAABB() {
  277. const t = this.aabb;
  278. (t[0] = this.x - this.extBorder),
  279. (t[1] = this.y - this.extBorder),
  280. (t[2] = this.x + this.width + this.extBorder),
  281. (t[3] = this.y + this.height + this.extBorder);
  282. }
  283. isVisible(t) {
  284. var e = this.aabb;
  285. return e[0] < t[2] && e[1] < t[3] && e[2] > t[0] && e[3] > t[1];
  286. }
  287. isVisiblePoint(t, e) {
  288. var i = this.aabb;
  289. return i[0] < t && i[1] < e && i[2] > t && i[3] > e;
  290. }
  291. onChanged(t, e = !1) {
  292. e ? this.eventTarget.once(this.EVENT_CHANGED, t) : this.eventTarget.on(this.EVENT_CHANGED, t);
  293. }
  294. offChanged(t) {
  295. this.eventTarget.off(this.EVENT_CHANGED, t);
  296. }
  297. syncView(t = !1) {
  298. if (this.changed || t) {
  299. const t = this.x + this.width / 2,
  300. e = -(this.y + this.height / 2);
  301. this.cameraNode.setPosition(t, e),
  302. (this.camera.zoomRatio = this.scale),
  303. this.changed && (this.eventTarget.emit(this.EVENT_CHANGED, this), (this.changed = !1));
  304. }
  305. }
  306. setInputHandler(t) {
  307. this.inputHandler && this.removeInputHandler(),
  308. t &&
  309. ((this.inputHandler = t),
  310. (this.allowMove = !0),
  311. (this.allowScale = !0),
  312. (this._onPan = (t, e, i, s) => {
  313. var o;
  314. this.allowMove &&
  315. !this.viewTween &&
  316. ((o = this.scale),
  317. (1 < Math.abs(i) || 1 < Math.abs(s)) && ((i *= 1.6), (s *= 1.6)),
  318. this.scrollBy(-i / o, -s / o));
  319. }),
  320. t.onPan(this._onPan),
  321. (this._onPinch = (t, e, i) => {
  322. this.allowScale &&
  323. !this.viewTween &&
  324. ((e = (this.scale * i) / e), this.setPivotInScreen(t.x, t.y), this.scaleTo(e));
  325. }),
  326. (this._onKeyUp = t => {
  327. if (this.allowScale && !this.viewTween)
  328. switch (t.keyCode) {
  329. case cc.macro.KEY.equal:
  330. case cc.macro.KEY["+"]:
  331. this.zoomIn();
  332. break;
  333. case cc.macro.KEY.dash:
  334. case cc.macro.KEY["-"]:
  335. this.zoomOut();
  336. }
  337. }),
  338. t.onKeyUp(this._onKeyUp),
  339. (this._onMouseWhell = t => {
  340. this.allowScale &&
  341. !this.viewTween &&
  342. ((t = t.getScrollY()),
  343. cc.log("onMouseWheel:", t),
  344. t < -4 ? this.zoomIn() : 4 < t && this.zoomOut());
  345. }),
  346. t.onMouseWheel(this._onMouseWhell));
  347. }
  348. removeInputHandler() {
  349. const t = this.inputHandler;
  350. t &&
  351. ((this.allowMove = !1),
  352. (this.allowScale = !1),
  353. t.offPan(this._onPan),
  354. (this._onPan = null),
  355. t.offPinch(this._onPinch),
  356. (this._onPinch = null),
  357. t.offKeyUp(this._onKeyUp),
  358. (this._onKeyUp = null),
  359. t.offMouseWheel(this._onMouseWhell),
  360. (this._onMouseWhell = null),
  361. (this.inputHandler = null));
  362. }
  363. };
  364. s([a(cc.size)], e.prototype, "size", void 0),
  365. s([a], e.prototype, "area", void 0),
  366. s([a(cc.Node)], e.prototype, "cameraNode", void 0),
  367. (e = s([o], e)),
  368. (i.default = e);