You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.4 KiB
139 lines
5.4 KiB
(function (global) {
|
|
'use strict';
|
|
|
|
var WorkbenchAgent = global.PWAgent;
|
|
var U = global.PWAgentUtil;
|
|
var nodeId = U && U.nodeId;
|
|
var walk = U && U.walk;
|
|
if (!WorkbenchAgent) {
|
|
console.error('[调试工作台] agent-pick 需先加载 agent-core');
|
|
return;
|
|
}
|
|
|
|
WorkbenchAgent.prototype._setPickMode = function (mode) {
|
|
this._pickMode = mode === 'inspect' ? 'inspect' : 'game';
|
|
document.documentElement.classList.toggle('pw-inspect', this._pickMode === 'inspect');
|
|
this._bindCanvasPick(this._isActive('pick'));
|
|
return this._pickMode;
|
|
};
|
|
|
|
WorkbenchAgent.prototype._bindCanvasPick = function (on) {
|
|
var canvas = document.getElementById('GameCanvas');
|
|
if (!canvas) return;
|
|
canvas.removeEventListener('pointerdown', this._onPickDown, true);
|
|
if (on && this._pickMode === 'inspect') {
|
|
if (!this._onPickDown) {
|
|
var self = this;
|
|
this._onPickDown = function (ev) {
|
|
if (self._pickMode !== 'inspect') return;
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
var uuid = self._hitTest(ev.clientX, ev.clientY);
|
|
self._selectNode(uuid);
|
|
if (self._isActive('inspector') || self._isActive('hierarchy')) {
|
|
self._post({ type: 'event', name: 'hier.diff', data: self._getHierTree() });
|
|
}
|
|
};
|
|
}
|
|
canvas.addEventListener('pointerdown', this._onPickDown, true);
|
|
}
|
|
};
|
|
|
|
WorkbenchAgent.prototype._hitTest = function (clientX, clientY) {
|
|
var cc = this.cc;
|
|
var canvas = document.getElementById('GameCanvas');
|
|
if (!cc || !canvas) return '';
|
|
var scene = cc.director.getScene();
|
|
if (!scene) return '';
|
|
var stack = [];
|
|
walk(scene, function (n) { stack.push(n); });
|
|
for (var i = stack.length - 1; i >= 0; i--) {
|
|
var node = stack[i];
|
|
if (!node || !node.activeInHierarchy) continue;
|
|
var uit = node.getComponent && node.getComponent(cc.UITransform);
|
|
if (!uit) continue;
|
|
var box = this._nodeClientRect(node);
|
|
if (!box) continue;
|
|
if (clientX >= box.left && clientX <= box.right && clientY >= box.top && clientY <= box.bottom) {
|
|
return nodeId(node);
|
|
}
|
|
}
|
|
return '';
|
|
};
|
|
|
|
WorkbenchAgent.prototype._ensureOverlay = function () {
|
|
var el = document.getElementById('pw-pick-overlay');
|
|
if (!el) {
|
|
el = document.createElement('div');
|
|
el.id = 'pw-pick-overlay';
|
|
el.className = 'pw-pick-overlay';
|
|
el.hidden = true;
|
|
document.body.appendChild(el);
|
|
}
|
|
this._overlay = el;
|
|
};
|
|
|
|
WorkbenchAgent.prototype._nodeClientRect = function (node) {
|
|
var cc = this.cc;
|
|
var canvas = document.getElementById('GameCanvas');
|
|
if (!cc || !canvas || !node || !node.isValid) return null;
|
|
var uit = node.getComponent && node.getComponent(cc.UITransform);
|
|
if (!uit || typeof uit.getBoundingBoxToWorld !== 'function') return null;
|
|
var box = uit.getBoundingBoxToWorld();
|
|
if (!box) return null;
|
|
var canvasRect = canvas.getBoundingClientRect();
|
|
var vs = cc.view && cc.view.getVisibleSize ? cc.view.getVisibleSize() : { width: canvasRect.width, height: canvasRect.height };
|
|
var vw = Math.max(1, vs.width);
|
|
var vh = Math.max(1, vs.height);
|
|
var corners = [
|
|
{ x: box.x, y: box.y },
|
|
{ x: box.x + box.width, y: box.y },
|
|
{ x: box.x + box.width, y: box.y + box.height },
|
|
{ x: box.x, y: box.y + box.height },
|
|
];
|
|
var xs = [];
|
|
var ys = [];
|
|
for (var i = 0; i < 4; i++) {
|
|
xs.push(canvasRect.left + (corners[i].x / vw + 0.5) * canvasRect.width);
|
|
ys.push(canvasRect.top + (0.5 - corners[i].y / vh) * canvasRect.height);
|
|
}
|
|
var left = Math.min.apply(null, xs);
|
|
var right = Math.max.apply(null, xs);
|
|
var top = Math.min.apply(null, ys);
|
|
var bottom = Math.max.apply(null, ys);
|
|
return { left: left, top: top, right: right, bottom: bottom, width: Math.max(2, right - left), height: Math.max(2, bottom - top) };
|
|
};
|
|
|
|
WorkbenchAgent.prototype._updateOverlay = function () {
|
|
var node = this._findNode(this._selectedUuid);
|
|
var overlay = this._overlay;
|
|
if (!overlay) return;
|
|
if (!node) {
|
|
overlay.hidden = true;
|
|
return;
|
|
}
|
|
var box = this._nodeClientRect(node);
|
|
if (!box) {
|
|
overlay.hidden = true;
|
|
return;
|
|
}
|
|
overlay.hidden = false;
|
|
overlay.style.position = 'fixed';
|
|
overlay.style.left = box.left + 'px';
|
|
overlay.style.top = box.top + 'px';
|
|
overlay.style.width = box.width + 'px';
|
|
overlay.style.height = box.height + 'px';
|
|
};
|
|
|
|
WorkbenchAgent.prototype._getOverlayRect = function () {
|
|
var overlay = this._overlay;
|
|
if (!overlay || overlay.hidden) return null;
|
|
return {
|
|
left: parseFloat(overlay.style.left) || 0,
|
|
top: parseFloat(overlay.style.top) || 0,
|
|
width: parseFloat(overlay.style.width) || 0,
|
|
height: parseFloat(overlay.style.height) || 0,
|
|
};
|
|
};
|
|
|
|
})(window);
|
|
|