mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-11 07:56:05 +08:00
扩展Node节点功能在编辑器模式下兼容
This commit is contained in:
@@ -1,23 +1,20 @@
|
||||
import { Director, director, js } from "cc";
|
||||
import { EDITOR } from "cc/env";
|
||||
|
||||
/** 全局游戏时间缩放 */
|
||||
if (!EDITOR) {
|
||||
//@ts-ignore
|
||||
if (!Director.prototype["__$cc-director-speed-extension$__"]) {
|
||||
//@ts-ignore
|
||||
if (!Director.prototype["__$cc-director-speed-extension$__"]) {
|
||||
//@ts-ignore
|
||||
Director.prototype["__$cc-director-speed-extension$__"] = true;
|
||||
Director.prototype["__$cc-director-speed-extension$__"] = true;
|
||||
|
||||
let oldTick = director.tick.bind(director);
|
||||
director.tick = function (dt) {
|
||||
dt *= director.globalGameTimeScale;
|
||||
oldTick(dt);
|
||||
};
|
||||
let oldTick = director.tick.bind(director);
|
||||
director.tick = function (dt) {
|
||||
dt *= director.globalGameTimeScale;
|
||||
oldTick(dt);
|
||||
};
|
||||
|
||||
js.mixin(Director.prototype, {
|
||||
globalGameTimeScale: 1,
|
||||
});
|
||||
}
|
||||
js.mixin(Director.prototype, {
|
||||
globalGameTimeScale: 1,
|
||||
});
|
||||
}
|
||||
|
||||
declare module "cc" {
|
||||
|
||||
@@ -1,161 +1,158 @@
|
||||
|
||||
import { EventTouch, Node, Vec2, js, v3 } from "cc";
|
||||
import { DEV, EDITOR } from "cc/env";
|
||||
|
||||
/** 节点拖拽功能 */
|
||||
if (!EDITOR) {
|
||||
//@ts-ignore
|
||||
if (!Node.prototype["__$NodeDragExt$__"]) {
|
||||
//@ts-ignore
|
||||
if (!Node.prototype["__$cc-node-drag-extension$__"]) {
|
||||
//@ts-ignore
|
||||
Node.prototype["__$cc-node-drag-extension$__"] = true;
|
||||
Node.prototype["__$NodeDragExt$__"] = true;
|
||||
|
||||
let _DragEvent = {
|
||||
DRAG_START: "drag_start",
|
||||
DRAG_MOVE: "drag_move",
|
||||
DRAG_END: "drag_end"
|
||||
}
|
||||
let _DragEvent = {
|
||||
DRAG_START: "drag_start",
|
||||
DRAG_MOVE: "drag_move",
|
||||
DRAG_END: "drag_end"
|
||||
}
|
||||
|
||||
js.mixin(Node, {
|
||||
DragEvent: _DragEvent
|
||||
});
|
||||
js.mixin(Node, {
|
||||
DragEvent: _DragEvent
|
||||
});
|
||||
|
||||
//---------------- Node 添加 拖拽属性 ----------------
|
||||
//---------------- Node 添加 拖拽属性 ----------------
|
||||
|
||||
js.mixin(Node.prototype, {
|
||||
_draggable: false,
|
||||
_dragging: false,
|
||||
_dragTesting: false,
|
||||
_dragStartPoint: null,
|
||||
initDrag: function () {
|
||||
if (this._draggable) {
|
||||
this.on(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
||||
this.on(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
||||
this.on(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
||||
this.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
||||
}
|
||||
else {
|
||||
this.off(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
||||
this.off(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
||||
this.off(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
||||
this.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
||||
}
|
||||
},
|
||||
onTouchBegin_0: function (event: EventTouch) {
|
||||
if (this._dragStartPoint == null) {
|
||||
this._dragStartPoint = new Vec2();
|
||||
}
|
||||
DEV && console.log(`cc-node-drag-extension-> onTouchBegin_0 ${this.name}`);
|
||||
|
||||
// event.preventSwallow = true;
|
||||
let pos = event.getUILocation();
|
||||
this._dragStartPoint.set(pos);
|
||||
this._dragTesting = true;
|
||||
},
|
||||
onTouchMove_0: function (event: EventTouch) {
|
||||
if (!this._dragging && this._draggable && this._dragTesting) {
|
||||
let sensitivity = 10;
|
||||
let pos = event.getUILocation();
|
||||
if (Math.abs(this._dragStartPoint.x - pos.x) < sensitivity
|
||||
&& Math.abs(this._dragStartPoint.y - pos.y) < sensitivity) {
|
||||
return;
|
||||
}
|
||||
|
||||
// event.preventSwallow = true;
|
||||
this._dragging = true;
|
||||
this._dragTesting = false;
|
||||
this.emit(Node.DragEvent.DRAG_START, event);
|
||||
}
|
||||
|
||||
if (this._dragging) {
|
||||
let delta = event.getUIDelta();
|
||||
// /** 这里除以 世界缩放,在有缩放的时候拖拽不至于很怪 */
|
||||
// this.position = this.position.add(v3(delta.x / this.worldScale.x, delta.y / this.worldScale.y, 0));
|
||||
this.position = this.position.add(v3(delta.x, delta.y, 0));
|
||||
this.emit(Node.DragEvent.DRAG_MOVE, event);
|
||||
}
|
||||
},
|
||||
|
||||
onTouchEnd_0: function (event: EventTouch) {
|
||||
if (this._dragging) {
|
||||
this._dragging = false;
|
||||
this.emit(Node.DragEvent.DRAG_END, event);
|
||||
}
|
||||
DEV && console.log(`cc-node-drag-extension-> onTouchEnd_0 ${this.name}, _dragging: ${this._dragging}`);
|
||||
},
|
||||
|
||||
onTouchCancel_0: function (event: EventTouch) {
|
||||
if (this._dragging) {
|
||||
this._dragging = false;
|
||||
this.emit(Node.DragEvent.DRAG_END, event);
|
||||
}
|
||||
DEV && console.log(`cc-node-drag-extension-> onTouchCancel_0 ${this.name}, _dragging: ${this._dragging}`);
|
||||
},
|
||||
|
||||
startDrag: function () {
|
||||
//此节点是否在场景中激活
|
||||
if (!this.activeInHierarchy) {
|
||||
return;
|
||||
}
|
||||
this.dragBegin();
|
||||
},
|
||||
|
||||
dragBegin: function () {
|
||||
this._dragging = true;
|
||||
this._dragTesting = true;
|
||||
js.mixin(Node.prototype, {
|
||||
_draggable: false,
|
||||
_dragging: false,
|
||||
_dragTesting: false,
|
||||
_dragStartPoint: null,
|
||||
initDrag: function () {
|
||||
if (this._draggable) {
|
||||
this.on(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
||||
this.on(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
||||
this.on(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
||||
this.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
||||
},
|
||||
dragEnd: function () {
|
||||
if (this._dragging) {
|
||||
this._dragTesting = false;
|
||||
this._dragging = false;
|
||||
}
|
||||
},
|
||||
|
||||
// 停止拖拽
|
||||
stopDrag: function () {
|
||||
this.dragEnd();
|
||||
},
|
||||
|
||||
// 移除 touch 事件
|
||||
removeDragEvent: function () {
|
||||
}
|
||||
else {
|
||||
this.off(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
||||
this.off(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
||||
this.off(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
||||
this.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
||||
}
|
||||
});
|
||||
},
|
||||
onTouchBegin_0: function (event: EventTouch) {
|
||||
if (this._dragStartPoint == null) {
|
||||
this._dragStartPoint = new Vec2();
|
||||
}
|
||||
// DEV && console.log(`NodeDragExt -> onTouchBegin_0 ${this.name}`);
|
||||
|
||||
// 如果 node 设置 node.draggable = true, 则启用 拖拽
|
||||
Object.defineProperty(Node.prototype, "draggable", {
|
||||
get: function () {
|
||||
return this._draggable;
|
||||
},
|
||||
set: function (value) {
|
||||
if (this._draggable != value) {
|
||||
this._draggable = value;
|
||||
this.initDrag();
|
||||
// event.preventSwallow = true;
|
||||
let pos = event.getUILocation();
|
||||
this._dragStartPoint.set(pos);
|
||||
this._dragTesting = true;
|
||||
},
|
||||
onTouchMove_0: function (event: EventTouch) {
|
||||
if (!this._dragging && this._draggable && this._dragTesting) {
|
||||
let sensitivity = 10;
|
||||
let pos = event.getUILocation();
|
||||
if (Math.abs(this._dragStartPoint.x - pos.x) < sensitivity
|
||||
&& Math.abs(this._dragStartPoint.y - pos.y) < sensitivity) {
|
||||
return;
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(Node.prototype, "dragTesting", {
|
||||
get: function () {
|
||||
return this._dragTesting;
|
||||
},
|
||||
set: function (value) {
|
||||
if (this._dragTesting != value) {
|
||||
this._dragTesting = value;
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
//---------------- Node 添加 拖拽属性 ---------------- end
|
||||
}
|
||||
// event.preventSwallow = true;
|
||||
this._dragging = true;
|
||||
this._dragTesting = false;
|
||||
this.emit(Node.DragEvent.DRAG_START, event);
|
||||
}
|
||||
|
||||
if (this._dragging) {
|
||||
let delta = event.getUIDelta();
|
||||
// /** 这里除以 世界缩放,在有缩放的时候拖拽不至于很怪 */
|
||||
// this.position = this.position.add(v3(delta.x / this.worldScale.x, delta.y / this.worldScale.y, 0));
|
||||
this.position = this.position.add(v3(delta.x, delta.y, 0));
|
||||
this.emit(Node.DragEvent.DRAG_MOVE, event);
|
||||
}
|
||||
},
|
||||
|
||||
onTouchEnd_0: function (event: EventTouch) {
|
||||
if (this._dragging) {
|
||||
this._dragging = false;
|
||||
this.emit(Node.DragEvent.DRAG_END, event);
|
||||
}
|
||||
// DEV && console.log(`NodeDragExt -> onTouchEnd_0 ${this.name}, _dragging: ${this._dragging}`);
|
||||
},
|
||||
|
||||
onTouchCancel_0: function (event: EventTouch) {
|
||||
if (this._dragging) {
|
||||
this._dragging = false;
|
||||
this.emit(Node.DragEvent.DRAG_END, event);
|
||||
}
|
||||
// DEV && console.log(`NodeDragExt -> onTouchCancel_0 ${this.name}, _dragging: ${this._dragging}`);
|
||||
},
|
||||
|
||||
startDrag: function () {
|
||||
// 此节点是否在场景中激活
|
||||
if (!this.activeInHierarchy) {
|
||||
return;
|
||||
}
|
||||
this.dragBegin();
|
||||
},
|
||||
|
||||
dragBegin: function () {
|
||||
this._dragging = true;
|
||||
this._dragTesting = true;
|
||||
this.on(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
||||
this.on(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
||||
this.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
||||
},
|
||||
dragEnd: function () {
|
||||
if (this._dragging) {
|
||||
this._dragTesting = false;
|
||||
this._dragging = false;
|
||||
}
|
||||
},
|
||||
|
||||
// 停止拖拽
|
||||
stopDrag: function () {
|
||||
this.dragEnd();
|
||||
},
|
||||
|
||||
// 移除 touch 事件
|
||||
removeDragEvent: function () {
|
||||
this.off(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
||||
this.off(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
||||
this.off(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
||||
this.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
||||
}
|
||||
});
|
||||
|
||||
// 如果 node 设置 node.draggable = true, 则启用 拖拽
|
||||
Object.defineProperty(Node.prototype, "draggable", {
|
||||
get: function () {
|
||||
return this._draggable;
|
||||
},
|
||||
set: function (value) {
|
||||
if (this._draggable != value) {
|
||||
this._draggable = value;
|
||||
this.initDrag();
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(Node.prototype, "dragTesting", {
|
||||
get: function () {
|
||||
return this._dragTesting;
|
||||
},
|
||||
set: function (value) {
|
||||
if (this._dragTesting != value) {
|
||||
this._dragTesting = value;
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
//---------------- Node 添加 拖拽属性 ----------------
|
||||
}
|
||||
|
||||
declare module "cc" {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Button, Canvas, Color, EditBox, Graphics, Label, Layout, Mask, Node, PageView, ProgressBar, RichText, ScrollView, Size, Skeleton, Slider, Sprite, Toggle, UIOpacity, UIRenderer, UITransform, Widget, v3 } from "cc";
|
||||
import { EDITOR } from "cc/env";
|
||||
import { Button, Canvas, Color, EditBox, Graphics, Label, Layout, Mask, Node, PageView, ProgressBar, RichText, ScrollView, Size, Slider, Sprite, Toggle, UIOpacity, UIRenderer, UITransform, Widget, v3 } from "cc";
|
||||
|
||||
// ========= 扩展 cc 提示声明 =========
|
||||
|
||||
@@ -59,268 +58,266 @@ declare module "cc" {
|
||||
}
|
||||
}
|
||||
|
||||
if (!EDITOR) {
|
||||
//@ts-ignore
|
||||
if (!Node.prototype["$__definedProperties__"]) {
|
||||
//@ts-ignore
|
||||
if (!Node.prototype["$__definedProperties__"]) {
|
||||
//@ts-ignore
|
||||
Node.prototype["$__definedProperties__"] = true;
|
||||
Node.prototype["$__definedProperties__"] = true;
|
||||
|
||||
let componentMap: any = {
|
||||
"uiGraphics": Graphics,
|
||||
"uiLabel": Label,
|
||||
"uiRichText": RichText,
|
||||
"uiSprite": Sprite,
|
||||
"uiButton": Button,
|
||||
"uiCanvas": Canvas,
|
||||
"uiEditBox": EditBox,
|
||||
"uiLayout": Layout,
|
||||
"uiPageView": PageView,
|
||||
"uiProgressBar": ProgressBar,
|
||||
"uiScrollView": ScrollView,
|
||||
"uiSlider": Slider,
|
||||
"uiToggle": Toggle,
|
||||
"uiWidget": Widget,
|
||||
"uiOpacity": UIOpacity,
|
||||
"uiTransform": UITransform,
|
||||
"uiMask": Mask,
|
||||
};
|
||||
let componentMap: any = {
|
||||
"uiGraphics": Graphics,
|
||||
"uiLabel": Label,
|
||||
"uiRichText": RichText,
|
||||
"uiSprite": Sprite,
|
||||
"uiButton": Button,
|
||||
"uiCanvas": Canvas,
|
||||
"uiEditBox": EditBox,
|
||||
"uiLayout": Layout,
|
||||
"uiPageView": PageView,
|
||||
"uiProgressBar": ProgressBar,
|
||||
"uiScrollView": ScrollView,
|
||||
"uiSlider": Slider,
|
||||
"uiToggle": Toggle,
|
||||
"uiWidget": Widget,
|
||||
"uiOpacity": UIOpacity,
|
||||
"uiTransform": UITransform,
|
||||
"uiMask": Mask,
|
||||
};
|
||||
|
||||
for (const key in componentMap) {
|
||||
Object.defineProperty(Node.prototype, key, {
|
||||
get: function () {
|
||||
return this.getComponent(componentMap[key]);
|
||||
},
|
||||
set: function (value) { }
|
||||
});
|
||||
}
|
||||
|
||||
/** 获取、设置节点的 X 欧拉角 */
|
||||
Object.defineProperty(Node.prototype, "angle_x", {
|
||||
for (const key in componentMap) {
|
||||
Object.defineProperty(Node.prototype, key, {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.eulerAngles.x;
|
||||
return this.getComponent(componentMap[key]);
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setRotationFromEuler(value, self.eulerAngles.y, self.eulerAngles.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Y 欧拉角 */
|
||||
Object.defineProperty(Node.prototype, "angle_y", {
|
||||
get: function () {
|
||||
return this.eulerAngles.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setRotationFromEuler(self.eulerAngles.x, value, self.eulerAngles.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Z 欧拉角 */
|
||||
Object.defineProperty(Node.prototype, "angle_z", {
|
||||
get: function () {
|
||||
return this.eulerAngles.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setRotationFromEuler(self.eulerAngles.x, self.eulerAngles.y, value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 X 坐标 */
|
||||
Object.defineProperty(Node.prototype, "x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.position.x;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setPosition(value, self.position.y);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Y 坐标 */
|
||||
Object.defineProperty(Node.prototype, "y", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.position.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setPosition(self.position.x, value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Z 坐标 */
|
||||
Object.defineProperty(Node.prototype, "z", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.position.z;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setPosition(self.position.x, self.position.y, value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的宽度 */
|
||||
Object.defineProperty(Node.prototype, "w", {
|
||||
configurable: true,
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.width ?? 0;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).width = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的高度 */
|
||||
Object.defineProperty(Node.prototype, "h", {
|
||||
configurable: true,
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.height ?? 0;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).height = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的尺寸 */
|
||||
Object.defineProperty(Node.prototype, "size", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
let uiTransform = self.getComponent(UITransform)!;
|
||||
return new Size(uiTransform.width, uiTransform.height);
|
||||
},
|
||||
set: function (value: Size) {
|
||||
let self: Node = this;
|
||||
let uiTransform = self.getComponent(UITransform) || self.addComponent(UITransform);
|
||||
uiTransform.width = value.width;
|
||||
uiTransform.height = value.height;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的水平锚点 */
|
||||
Object.defineProperty(Node.prototype, "anchor_x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.anchorX ?? 0.5;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).anchorX = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的垂直锚点 */
|
||||
Object.defineProperty(Node.prototype, "anchor_y", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.anchorY ?? 0.5;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).anchorY = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的透明度 */
|
||||
Object.defineProperty(Node.prototype, "opacity", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
let op = self.getComponent(UIOpacity);
|
||||
if (op != null) {
|
||||
return op.opacity;
|
||||
}
|
||||
|
||||
let render = self.getComponent(UIRenderer);
|
||||
if (render) {
|
||||
return render.color.a;
|
||||
}
|
||||
|
||||
return 255;
|
||||
},
|
||||
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
let op = self.getComponent(UIOpacity);
|
||||
if (op != null) {
|
||||
op.opacity = value;
|
||||
return;
|
||||
}
|
||||
|
||||
let render = self.getComponent(UIRenderer);
|
||||
if (render) {
|
||||
// 直接通过 color.a 设置透明度会有bug,没能直接生效,需要激活节点才生效
|
||||
// (render.color.a as any) = value;
|
||||
|
||||
// 创建一个颜色缓存对象,避免每次都创建新对象
|
||||
if (!this.$__color__) {
|
||||
this.$__color__ = new Color(render.color.r, render.color.g, render.color.b, value);
|
||||
}
|
||||
else {
|
||||
this.$__color__.a = value;
|
||||
}
|
||||
render.color = this.$__color__; // 设置 color 对象则可以立刻生效
|
||||
}
|
||||
else {
|
||||
self.addComponent(UIOpacity).opacity = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的颜色 */
|
||||
Object.defineProperty(Node.prototype, "color", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UIRenderer)?.color;
|
||||
},
|
||||
set: function (value: Color) {
|
||||
let self: Node = this;
|
||||
let render = self.getComponent(UIRenderer);
|
||||
render && (render.color = value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 X 缩放系数 */
|
||||
Object.defineProperty(Node.prototype, "scale_x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.scale.x;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.scale = v3(value, self.scale.y, self.scale.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Y 缩放系数 */
|
||||
Object.defineProperty(Node.prototype, "scale_y", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.scale.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.scale = v3(self.scale.x, value, self.scale.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Z 缩放系数 */
|
||||
Object.defineProperty(Node.prototype, "scale_z", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.scale.z;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.scale = v3(self.scale.x, self.scale.y, value);
|
||||
}
|
||||
set: function (value) { }
|
||||
});
|
||||
}
|
||||
|
||||
/** 获取、设置节点的 X 欧拉角 */
|
||||
Object.defineProperty(Node.prototype, "angle_x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.eulerAngles.x;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setRotationFromEuler(value, self.eulerAngles.y, self.eulerAngles.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Y 欧拉角 */
|
||||
Object.defineProperty(Node.prototype, "angle_y", {
|
||||
get: function () {
|
||||
return this.eulerAngles.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setRotationFromEuler(self.eulerAngles.x, value, self.eulerAngles.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Z 欧拉角 */
|
||||
Object.defineProperty(Node.prototype, "angle_z", {
|
||||
get: function () {
|
||||
return this.eulerAngles.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setRotationFromEuler(self.eulerAngles.x, self.eulerAngles.y, value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 X 坐标 */
|
||||
Object.defineProperty(Node.prototype, "x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.position.x;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setPosition(value, self.position.y);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Y 坐标 */
|
||||
Object.defineProperty(Node.prototype, "y", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.position.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setPosition(self.position.x, value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Z 坐标 */
|
||||
Object.defineProperty(Node.prototype, "z", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.position.z;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.setPosition(self.position.x, self.position.y, value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的宽度 */
|
||||
Object.defineProperty(Node.prototype, "w", {
|
||||
configurable: true,
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.width ?? 0;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).width = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的高度 */
|
||||
Object.defineProperty(Node.prototype, "h", {
|
||||
configurable: true,
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.height ?? 0;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).height = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的尺寸 */
|
||||
Object.defineProperty(Node.prototype, "size", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
let uiTransform = self.getComponent(UITransform)!;
|
||||
return new Size(uiTransform.width, uiTransform.height);
|
||||
},
|
||||
set: function (value: Size) {
|
||||
let self: Node = this;
|
||||
let uiTransform = self.getComponent(UITransform) || self.addComponent(UITransform);
|
||||
uiTransform.width = value.width;
|
||||
uiTransform.height = value.height;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的水平锚点 */
|
||||
Object.defineProperty(Node.prototype, "anchor_x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.anchorX ?? 0.5;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).anchorX = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的垂直锚点 */
|
||||
Object.defineProperty(Node.prototype, "anchor_y", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UITransform)?.anchorY ?? 0.5;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
(self.getComponent(UITransform) || self.addComponent(UITransform)).anchorY = value;
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的透明度 */
|
||||
Object.defineProperty(Node.prototype, "opacity", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
let op = self.getComponent(UIOpacity);
|
||||
if (op != null) {
|
||||
return op.opacity;
|
||||
}
|
||||
|
||||
let render = self.getComponent(UIRenderer);
|
||||
if (render) {
|
||||
return render.color.a;
|
||||
}
|
||||
|
||||
return 255;
|
||||
},
|
||||
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
let op = self.getComponent(UIOpacity);
|
||||
if (op != null) {
|
||||
op.opacity = value;
|
||||
return;
|
||||
}
|
||||
|
||||
let render = self.getComponent(UIRenderer);
|
||||
if (render) {
|
||||
// 直接通过 color.a 设置透明度会有bug,没能直接生效,需要激活节点才生效
|
||||
// (render.color.a as any) = value;
|
||||
|
||||
// 创建一个颜色缓存对象,避免每次都创建新对象
|
||||
if (!this.$__color__) {
|
||||
this.$__color__ = new Color(render.color.r, render.color.g, render.color.b, value);
|
||||
}
|
||||
else {
|
||||
this.$__color__.a = value;
|
||||
}
|
||||
render.color = this.$__color__; // 设置 color 对象则可以立刻生效
|
||||
}
|
||||
else {
|
||||
self.addComponent(UIOpacity).opacity = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的颜色 */
|
||||
Object.defineProperty(Node.prototype, "color", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.getComponent(UIRenderer)?.color;
|
||||
},
|
||||
set: function (value: Color) {
|
||||
let self: Node = this;
|
||||
let render = self.getComponent(UIRenderer);
|
||||
render && (render.color = value);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 X 缩放系数 */
|
||||
Object.defineProperty(Node.prototype, "scale_x", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.scale.x;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.scale = v3(value, self.scale.y, self.scale.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Y 缩放系数 */
|
||||
Object.defineProperty(Node.prototype, "scale_y", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.scale.y;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.scale = v3(self.scale.x, value, self.scale.z);
|
||||
}
|
||||
});
|
||||
|
||||
/** 获取、设置节点的 Z 缩放系数 */
|
||||
Object.defineProperty(Node.prototype, "scale_z", {
|
||||
get: function () {
|
||||
let self: Node = this;
|
||||
return self.scale.z;
|
||||
},
|
||||
set: function (value: number) {
|
||||
let self: Node = this;
|
||||
self.scale = v3(self.scale.x, self.scale.y, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -333,9 +333,9 @@ export class GameComponent extends Component {
|
||||
func.call(this, event);
|
||||
}
|
||||
// 不触发界面根节点触摸事件、不触发长按钮组件的触摸事件
|
||||
else if (event.target != this.node && event.target.getComponent(ButtonTouchLong) == null) {
|
||||
console.error(`名为【${event.target.name}】的按钮事件方法不存在`);
|
||||
}
|
||||
// else if (event.target != this.node && event.target.getComponent(ButtonTouchLong) == null) {
|
||||
// console.warn(`名为【${event.target.name}】的按钮事件方法不存在`);
|
||||
// }
|
||||
}, this);
|
||||
|
||||
// Cocos Creator Button组件批量绑定触摸事件(使用UIButton支持放连点功能)
|
||||
@@ -352,8 +352,9 @@ export class GameComponent extends Component {
|
||||
event.component = this.name.match(regex)![1];
|
||||
b.clickEvents.push(event);
|
||||
}
|
||||
else
|
||||
console.warn(`名为【${node.name}】的按钮事件方法不存在`);
|
||||
// else {
|
||||
// console.warn(`名为【${node.name}】的按钮事件方法不存在`);
|
||||
// }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user