123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { _decorator, Component, EventTouch, Node, Rect, UITransform, v2, Vec2, Vec3 } from 'cc';
- import { widget_base } from './widget_base';
- import { att_drag_data, widget_item_data } from '../../../data/data';
- import { gameManager } from '../gameManager';
- const { ccclass, property } = _decorator;
- @ccclass('widget_drag')
- export class widget_drag extends widget_base {
- private mDragData:att_drag_data = null;
- private mIsStartMove:boolean = false;
- private offset_x:number = 0;
- private offset_y:number = 0;
- private mDragRect:Rect = null;
- protected init(){
- if(this.mData.att.drag_data!=null){
- this.mDragData = this.mData.att.drag_data
- }
- }
- protected start(): void {
- if(this.mIsActive){
- this.registeredEvent()
- }
- }
- protected registeredEvent(): void {
- this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
- if(!this.mIsActive) return
- console.log("target",et.target)
- this.mIsStartMove = true;
- let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
- let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
- this.node.position = new Vec3(n_p.x,n_p.y);
- })
- this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
- if(!this.mIsActive) return
- let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
- let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
- this.node.position = new Vec3(n_p.x,n_p.y);
- })
- this.node.on(Node.EventType.TOUCH_END,()=>{
- this.onEndEvent()
- })
- }
- private onEndEvent(){
- if(!this.mIsActive) return
- this.mIsStartMove = false;
- this.checkOnecFinish()
- }
- private checkOther(){
- if(this.mDragData.other_drag_list==undefined){
- return false;
- }
- let isCheck = false;
- if(this.mDragData.other_drag_list.length>0){
- for (let index = 0; index < this.mDragData.other_drag_list.length; index++) {
- const item_data = this.mDragData.other_drag_list[index];
- let w = item_data.drag_size_width;
- let h = item_data.drag_size_height;
- let pos = new Vec3(this.mData.att.x+item_data.drag_pos_x,this.mData.att.y+item_data.drag_pos_y)
- let rect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
- let p = this.node.position;
- let is = rect.contains(new Vec2(p.x,p.y))
- if(is){
- if(item_data.other_event_id!=-1){
- this.offEvent()
- console.log("item_data.other_event_id",item_data.other_event_id)
- gameManager.Singleton.exeEvent(item_data.other_event_id)
- isCheck = true;
- break;
- }
-
- }
- }
- }
- return isCheck;
- }
- private checkOnecFinish(){
- if(this.checkMoveToDragRect()){
- this.mIsStartMove = false;
- this.onFinishEvent()
- }else{
- if(this.checkOther()){
- this.mIsStartMove = false;
- }else{
- if(this.mDragData.is_err_drag_back){
- this.node.position = new Vec3(this.mData.att.x,this.mData.att.y)
- }
- }
-
- }
- }
- private getDragRect():Rect{
- let w = this.mDragData.drag_size_width;
- let h = this.mDragData.drag_size_height;
- let pos = new Vec3(this.mData.att.x+this.mDragData.drag_pos_x,this.mData.att.y+this.mDragData.drag_pos_y)
- this.mDragRect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
- return this.mDragRect;
- }
- private checkMoveToDragRect():boolean{
- let p = this.node.position;
- let is = this.getDragRect().contains(new Vec2(p.x,p.y))
- return is
- }
- }
|