适配GameInspector

This commit is contained in:
xu_yanfeng 2025-01-24 17:42:06 +08:00
parent eac45d1fd0
commit 8c34fd7b91
4 changed files with 77 additions and 31 deletions

View File

@ -45,6 +45,12 @@ export class HintAdapter {
this.draw.clear(); this.draw.clear();
} }
} }
convertMousePos(event: MouseEvent, canvas: HTMLCanvasElement): { x: number; y: number } {
throw new Error("not implemented");
}
hitTest(node: any, x: number, y: number): boolean {
throw new Error("not implemented");
}
initDrawNode() { initDrawNode() {
if (this.draw && !this.draw.isValid) { if (this.draw && !this.draw.isValid) {
this.draw = null; this.draw = null;

View File

@ -13,6 +13,8 @@ export class HintV2 extends HintAdapter {
top: 0, top: 0,
width: 0, width: 0,
height: 0, height: 0,
adjustedTop: 0,
adjustedLeft: 0,
}; };
private _updateCanvasBoundingRect() { private _updateCanvasBoundingRect() {
// @ts-ignore // @ts-ignore
@ -49,10 +51,58 @@ export class HintV2 extends HintAdapter {
return { x: event.clientX, y: event.clientY }; return { x: event.clientX, y: event.clientY };
} }
private hit(event: MouseEvent) { convertMousePos(event: MouseEvent, canvas: HTMLCanvasElement): { x: number; y: number } {
let location = this.getPointByEvent(event); let location = this.getPointByEvent(event);
// @ts-ignore // let p = cc.view._convertMouseToLocationInView(location, this.canvasBoundingRect);
cc.view._convertMouseToLocationInView(location, this.canvasBoundingRect); let p = cc.view.convertToLocationInView(location.x, location.y, this.canvasBoundingRect);
let scaleX = cc.view._scaleX;
let scaleY = cc.view._scaleY;
let viewport = cc.view._viewportRect;
let x = (p.x - viewport.x) / scaleX;
let y = (p.y - viewport.y) / scaleY;
// let position = cc.v2(event.offsetX, event.offsetY);
// let size = cc.view.getDesignResolutionSize();
// let rect = { left: 0, top: 0, width: size.width, height: size.height };
// cc.view._convertMouseToLocationInView(position, rect);
// let wordPos = cc.v2();
// cc.Camera.main.getScreenToWorldPoint(position, wordPos);
return { x, y };
}
hitTest(node: any, x: number, y: number): boolean {
// let rect = item.getBoundingBox();
// let p = item.parent.convertToNodeSpaceAR(wordPos);
// if (rect.contains(p)) {
// return item;
// }
const mask = this._searchComponentsInParent(node, cc.Mask);
const b = node._hitTest(cc.v2(x, y), { mask });
return b;
}
private _searchComponentsInParent(node: any, comp: any) {
if (comp) {
let index = 0;
let list = null;
for (var curr = node; curr && cc.Node.isNode(curr); curr = curr._parent, ++index) {
if (curr.getComponent(comp)) {
let next = {
index: index,
node: curr,
};
if (list) {
list.push(next);
} else {
list = [next];
}
}
}
return list;
}
return null;
} }
resetIndex(): void { resetIndex(): void {
const node = this.draw.node; const node = this.draw.node;
@ -83,17 +133,4 @@ export class HintV2 extends HintAdapter {
}); });
return points; return points;
} }
private getBox(node: any) {
if (node.getBoundingBoxToWorld) {
return node.getBoundingBoxToWorld();
}
if (cc.UITransformComponent) {
const tr = node.getComponent(cc.UITransformComponent);
if (tr && tr.getBoundingBoxToWorld) {
return tr.getBoundingBoxToWorld();
}
}
return null;
}
} }

View File

@ -7,6 +7,18 @@ export class HintV3 extends HintAdapter {
const len = node.parent.children.length; const len = node.parent.children.length;
node.setSiblingIndex(len); node.setSiblingIndex(len);
} }
hitTest(node: any, x: number, y: number): boolean {
let b = node._uiProps?.uiTransformComp?.hitTest({ x, y }, 0);
return b;
}
convertMousePos(event: MouseEvent, canvas: HTMLCanvasElement): { x: number; y: number } {
const rect = canvas.getBoundingClientRect();
let x = event.clientX - rect.x;
let y = rect.y + rect.height - event.clientY;
x *= window.devicePixelRatio;
y *= window.devicePixelRatio;
return { x, y };
}
protected addDraw(scene: any, canvas: any, node: any): void { protected addDraw(scene: any, canvas: any, node: any): void {
if (canvas) { if (canvas) {
// 3.x 需要放到canvas下边 // 3.x 需要放到canvas下边

View File

@ -1,8 +1,8 @@
import ccui from "@xuyanfeng/cc-ui"; import ccui from "@xuyanfeng/cc-ui";
import { IUiMenuItem } from "@xuyanfeng/cc-ui/types/cc-menu/const"; import { IUiMenuItem } from "@xuyanfeng/cc-ui/types/cc-menu/const";
import { Msg } from "../../../core/types";
import { DocumentEvent } from "../../const"; import { DocumentEvent } from "../../const";
import { Inspector } from "../inspector"; import { Inspector } from "../inspector";
import { Msg } from "../../../core/types";
import { DrawOptions, HintAdapter, RectPoints } from "./adapter"; import { DrawOptions, HintAdapter, RectPoints } from "./adapter";
import { HintV2 } from "./hint-v2"; import { HintV2 } from "./hint-v2";
import { HintV3 } from "./hint-v3"; import { HintV3 } from "./hint-v3";
@ -49,22 +49,13 @@ export class Hint {
private updateHint(event: MouseEvent, canvas: HTMLCanvasElement) { private updateHint(event: MouseEvent, canvas: HTMLCanvasElement) {
this.cleanHover(); this.cleanHover();
this.cleanSelected(); this.cleanSelected();
const rect = canvas.getBoundingClientRect();
let x = event.clientX - rect.x;
let y = rect.y + rect.height - event.clientY;
x *= window.devicePixelRatio;
y *= window.devicePixelRatio;
this.inspector.updateTreeInfo(false); this.inspector.updateTreeInfo(false);
const { x, y } = this.hintAdapter.convertMousePos(event, canvas);
const nodes = []; const nodes = [];
this.inspector.forEachNode((item) => { this.inspector.forEachNode((node) => {
let b = false; const b = this.hintAdapter.hitTest(node, x, y);
if (this.inspector.isCreatorV3) { if (b && node.active && node.activeInHierarchy) {
b = item._uiProps?.uiTransformComp?.hitTest({ x, y }, 0); nodes.push(node);
} else {
}
if (b && item.active && item.activeInHierarchy) {
nodes.push(item);
} }
}); });