diff --git a/cc-inspector/src/scripts/inject/hint/adapter.ts b/cc-inspector/src/scripts/inject/hint/adapter.ts index f410399..b635228 100644 --- a/cc-inspector/src/scripts/inject/hint/adapter.ts +++ b/cc-inspector/src/scripts/inject/hint/adapter.ts @@ -45,6 +45,12 @@ export class HintAdapter { 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() { if (this.draw && !this.draw.isValid) { this.draw = null; diff --git a/cc-inspector/src/scripts/inject/hint/hint-v2.ts b/cc-inspector/src/scripts/inject/hint/hint-v2.ts index 6fcb9ce..7dd7aba 100644 --- a/cc-inspector/src/scripts/inject/hint/hint-v2.ts +++ b/cc-inspector/src/scripts/inject/hint/hint-v2.ts @@ -13,6 +13,8 @@ export class HintV2 extends HintAdapter { top: 0, width: 0, height: 0, + adjustedTop: 0, + adjustedLeft: 0, }; private _updateCanvasBoundingRect() { // @ts-ignore @@ -49,10 +51,58 @@ export class HintV2 extends HintAdapter { 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); - // @ts-ignore - cc.view._convertMouseToLocationInView(location, this.canvasBoundingRect); + // let p = 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 { const node = this.draw.node; @@ -83,17 +133,4 @@ export class HintV2 extends HintAdapter { }); 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; - } } diff --git a/cc-inspector/src/scripts/inject/hint/hint-v3.ts b/cc-inspector/src/scripts/inject/hint/hint-v3.ts index dba680a..38a7890 100644 --- a/cc-inspector/src/scripts/inject/hint/hint-v3.ts +++ b/cc-inspector/src/scripts/inject/hint/hint-v3.ts @@ -7,6 +7,18 @@ export class HintV3 extends HintAdapter { const len = node.parent.children.length; 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 { if (canvas) { // 3.x 需要放到canvas下边 diff --git a/cc-inspector/src/scripts/inject/hint/index.ts b/cc-inspector/src/scripts/inject/hint/index.ts index 9a91e12..8d73e73 100644 --- a/cc-inspector/src/scripts/inject/hint/index.ts +++ b/cc-inspector/src/scripts/inject/hint/index.ts @@ -1,8 +1,8 @@ import ccui from "@xuyanfeng/cc-ui"; import { IUiMenuItem } from "@xuyanfeng/cc-ui/types/cc-menu/const"; +import { Msg } from "../../../core/types"; import { DocumentEvent } from "../../const"; import { Inspector } from "../inspector"; -import { Msg } from "../../../core/types"; import { DrawOptions, HintAdapter, RectPoints } from "./adapter"; import { HintV2 } from "./hint-v2"; import { HintV3 } from "./hint-v3"; @@ -49,22 +49,13 @@ export class Hint { private updateHint(event: MouseEvent, canvas: HTMLCanvasElement) { this.cleanHover(); 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); + const { x, y } = this.hintAdapter.convertMousePos(event, canvas); const nodes = []; - this.inspector.forEachNode((item) => { - let b = false; - if (this.inspector.isCreatorV3) { - b = item._uiProps?.uiTransformComp?.hitTest({ x, y }, 0); - } else { - } - if (b && item.active && item.activeInHierarchy) { - nodes.push(item); + this.inspector.forEachNode((node) => { + const b = this.hintAdapter.hitTest(node, x, y); + if (b && node.active && node.activeInHierarchy) { + nodes.push(node); } });