Merge remote-tracking branch 'lin/Lenheart' into YiZiTing
This commit is contained in:
		
						commit
						6223bb18f0
					
				|  | @ -0,0 +1,9 @@ | ||||||
|  | { | ||||||
|  |   "ver": "1.2.0", | ||||||
|  |   "importer": "directory", | ||||||
|  |   "imported": true, | ||||||
|  |   "uuid": "f177f8e4-aca5-4209-ab04-8be793eaeb16", | ||||||
|  |   "files": [], | ||||||
|  |   "subMetas": {}, | ||||||
|  |   "userData": {} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,124 @@ | ||||||
|  | import { _decorator, CCInteger, CCString, Component, Node, Sprite, SpriteFrame, input, Input, __private, NodeEventType, EventTouch, EventMouse } from 'cc'; | ||||||
|  | import { BaseSpriteFrame } from './BaseSpriteFrame'; | ||||||
|  | const { ccclass, property } = _decorator; | ||||||
|  | 
 | ||||||
|  | @ccclass('BaseButton') | ||||||
|  | export class BaseButton extends Component { | ||||||
|  | 
 | ||||||
|  |     @property({ type: CCString, displayName: 'img路径', tooltip: "img路径" }) | ||||||
|  |     ImgPath: string; | ||||||
|  | 
 | ||||||
|  |     @property({ type: [CCInteger], displayName: '普通img编号', tooltip: "普通img编号" }) | ||||||
|  |     NormalImgIndex: number; | ||||||
|  | 
 | ||||||
|  |     @property({ type: [CCInteger], displayName: '悬停img编号', tooltip: "悬停img编号" }) | ||||||
|  |     HoverImgIndex: number; | ||||||
|  | 
 | ||||||
|  |     @property({ type: [CCInteger], displayName: '按下img编号', tooltip: "按下img编号" }) | ||||||
|  |     PressImgIndex: number; | ||||||
|  | 
 | ||||||
|  |     @property({ type: [CCInteger], displayName: '失效img编号', tooltip: "失效img编号" }) | ||||||
|  |     DisableImgIndex: number; | ||||||
|  | 
 | ||||||
|  |     //普通精灵帧
 | ||||||
|  |     NormalImgSpriteFrame: SpriteFrame; | ||||||
|  | 
 | ||||||
|  |     //悬停精灵帧
 | ||||||
|  |     HoverImgSpriteFrame: SpriteFrame; | ||||||
|  | 
 | ||||||
|  |     //按下精灵帧
 | ||||||
|  |     PressImgSpriteFrame: SpriteFrame; | ||||||
|  | 
 | ||||||
|  |     //失效精灵帧
 | ||||||
|  |     DisableImgSpriteFrame: SpriteFrame; | ||||||
|  | 
 | ||||||
|  |     //精灵对象
 | ||||||
|  |     SpriteObj: Sprite; | ||||||
|  | 
 | ||||||
|  |     //初始化状态
 | ||||||
|  |     InitState = false; | ||||||
|  | 
 | ||||||
|  |     //按钮状态
 | ||||||
|  |     ButtonState = 0; // 0 普通 1悬停 2按下 8失效
 | ||||||
|  | 
 | ||||||
|  |     start() { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         //判断是否有精灵 如果没有 就给他搞一个
 | ||||||
|  |         if (this.node.getComponent(Sprite)) | ||||||
|  |             this.SpriteObj = this.node.getComponent(Sprite); | ||||||
|  |         else | ||||||
|  |             this.SpriteObj = this.node.addComponent(Sprite); | ||||||
|  | 
 | ||||||
|  |         new BaseSpriteFrame(this.ImgPath, this.NormalImgIndex, _SpriteFrame => { this.NormalImgSpriteFrame = _SpriteFrame }); | ||||||
|  |         new BaseSpriteFrame(this.ImgPath, this.HoverImgIndex, _SpriteFrame => { this.HoverImgSpriteFrame = _SpriteFrame }); | ||||||
|  |         new BaseSpriteFrame(this.ImgPath, this.PressImgIndex, _SpriteFrame => { this.PressImgSpriteFrame = _SpriteFrame }); | ||||||
|  |         new BaseSpriteFrame(this.ImgPath, this.DisableImgIndex, _SpriteFrame => { this.DisableImgSpriteFrame = _SpriteFrame }); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     UploadSpriteFrame(_SpriteFrame) { | ||||||
|  |         if (this.SpriteObj.spriteFrame != _SpriteFrame) { | ||||||
|  |             this.SpriteObj.spriteFrame = _SpriteFrame; | ||||||
|  | 
 | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     update(deltaTime: number) { | ||||||
|  |         if (!this.InitState) { | ||||||
|  |             if (this.NormalImgSpriteFrame && this.HoverImgSpriteFrame && this.PressImgSpriteFrame) { | ||||||
|  |                 this.InitState = true; | ||||||
|  |                 this.UploadSpriteFrame(this.NormalImgSpriteFrame); | ||||||
|  | 
 | ||||||
|  |                 // this.node.on(Input.EventType.TOUCH_START, this.OnPress, this);
 | ||||||
|  |                 // this.node.on(Input.EventType.TOUCH_END, this.OnEnd, this);
 | ||||||
|  |                 this.node.on(Node.EventType.MOUSE_ENTER, this.OnHever, this); | ||||||
|  |                 this.node.on(Node.EventType.MOUSE_LEAVE, this.OffHever, this); | ||||||
|  |                 this.node.on(Node.EventType.MOUSE_DOWN, this.OnPress, this); | ||||||
|  |                 this.node.on(Node.EventType.MOUSE_UP, this.OnEnd, this); | ||||||
|  |             } | ||||||
|  |         } else { | ||||||
|  |             switch (this.ButtonState) { | ||||||
|  |                 case 0: | ||||||
|  |                     this.UploadSpriteFrame(this.NormalImgSpriteFrame); | ||||||
|  |                     break; | ||||||
|  |                 case 1: | ||||||
|  |                     this.UploadSpriteFrame(this.HoverImgSpriteFrame); | ||||||
|  |                     break; | ||||||
|  |                 case 2: | ||||||
|  |                     this.UploadSpriteFrame(this.PressImgSpriteFrame); | ||||||
|  |                     break; | ||||||
|  |                 case 8: | ||||||
|  |                     this.UploadSpriteFrame(this.DisableImgSpriteFrame); | ||||||
|  |                     break; | ||||||
|  |                 default: | ||||||
|  |                     break; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     OffHever(event) { | ||||||
|  |         this.ButtonState = 0; | ||||||
|  |         // this.UploadSpriteFrame(this.NormalImgSpriteFrame);
 | ||||||
|  |     } | ||||||
|  |     OnHever(event) { | ||||||
|  |         this.ButtonState = 1; | ||||||
|  |         // this.UploadSpriteFrame(this.HoverImgSpriteFrame);
 | ||||||
|  |     } | ||||||
|  |     OnEnd(event: EventTouch) { | ||||||
|  |         this.ButtonState = 0; | ||||||
|  |         // this.UploadSpriteFrame(this.NormalImgSpriteFrame);
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     OnPress(event: EventMouse) { | ||||||
|  |         //必须是鼠标左键
 | ||||||
|  |         if (event.getButton() != 0) return; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         this.ButtonState = 2; | ||||||
|  |         // this.UploadSpriteFrame(this.PressImgSpriteFrame);
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | { | ||||||
|  |   "ver": "4.0.23", | ||||||
|  |   "importer": "typescript", | ||||||
|  |   "imported": true, | ||||||
|  |   "uuid": "227e9de9-da6c-4457-9ce3-bc6d687c990b", | ||||||
|  |   "files": [], | ||||||
|  |   "subMetas": {}, | ||||||
|  |   "userData": {} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,35 @@ | ||||||
|  | import { _decorator, CCInteger, CCString, Component, Node, Sprite, SpriteFrame, Texture2D, v2 } from 'cc'; | ||||||
|  | import { ImagePack } from '../ImagePack/ImagePack'; | ||||||
|  | import { BaseSpriteFrame } from './BaseSpriteFrame'; | ||||||
|  | const { ccclass, property } = _decorator; | ||||||
|  | 
 | ||||||
|  | @ccclass('BaseSprite') | ||||||
|  | export class BaseSprite extends Component { | ||||||
|  | 
 | ||||||
|  |     @property({ type: CCString, displayName: 'img路径', tooltip: "img路径" }) | ||||||
|  |     ImgPath = ""; | ||||||
|  | 
 | ||||||
|  |     @property({ type: CCInteger, displayName: 'img编号', tooltip: "img编号" }) | ||||||
|  |     ImgIndex = 0; | ||||||
|  | 
 | ||||||
|  |     //精灵对象
 | ||||||
|  |     SpriteObj: Sprite; | ||||||
|  | 
 | ||||||
|  |     start() { | ||||||
|  |         //判断是否有精灵 如果没有 就给他搞一个
 | ||||||
|  |         if (this.node.getComponent(Sprite)) | ||||||
|  |             this.SpriteObj = this.node.getComponent(Sprite); | ||||||
|  |         else | ||||||
|  |             this.SpriteObj = this.node.addComponent(Sprite); | ||||||
|  | 
 | ||||||
|  |         new BaseSpriteFrame(this.ImgPath.toLocaleLowerCase(), this.ImgIndex, (_SpriteFrame) => { | ||||||
|  |             this.SpriteObj.spriteFrame = _SpriteFrame; | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     update(deltaTime: number) { | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @ -2,7 +2,7 @@ | ||||||
|   "ver": "4.0.23", |   "ver": "4.0.23", | ||||||
|   "importer": "typescript", |   "importer": "typescript", | ||||||
|   "imported": true, |   "imported": true, | ||||||
|   "uuid": "9258ddd0-5510-42fc-bf01-0b3888057cd3", |   "uuid": "a8046df1-6c92-4ce6-a53c-1548472bce1e", | ||||||
|   "files": [], |   "files": [], | ||||||
|   "subMetas": {}, |   "subMetas": {}, | ||||||
|   "userData": {} |   "userData": {} | ||||||
|  | @ -0,0 +1,38 @@ | ||||||
|  | import { _decorator, Component, Node, SpriteFrame, Texture2D, v2 } from 'cc'; | ||||||
|  | import { ImagePack } from '../ImagePack/ImagePack'; | ||||||
|  | const { ccclass, property } = _decorator; | ||||||
|  | 
 | ||||||
|  | @ccclass('BaseSpriteFrame') | ||||||
|  | export class BaseSpriteFrame { | ||||||
|  | 
 | ||||||
|  |     //初始化状态
 | ||||||
|  |     InitState: boolean = false; | ||||||
|  |     //精灵帧
 | ||||||
|  |     _SpriteFrame: SpriteFrame; | ||||||
|  |     //回调函数
 | ||||||
|  |     CallBackFunc: Function; | ||||||
|  | 
 | ||||||
|  |     constructor(ImgPath: string, ImgIndex: number, CallBack?: Function) { | ||||||
|  |         if (CallBack) this.CallBackFunc = CallBack; | ||||||
|  |         ImagePack.getInstance().ReadNpkTable("sprite/" + ImgPath.toLocaleLowerCase(), (ImgObj) => { | ||||||
|  |             const Png = ImgObj.Png_List[ImgIndex]; | ||||||
|  |             this._SpriteFrame = new SpriteFrame(); | ||||||
|  |             let tex = new Texture2D(); | ||||||
|  |             tex.reset({ | ||||||
|  |                 width: Png.Width, | ||||||
|  |                 height: Png.Height, | ||||||
|  |                 format: Texture2D.PixelFormat.RGBA8888, | ||||||
|  |                 mipmapLevel: 0, | ||||||
|  |             }); | ||||||
|  |             tex.uploadData(Png.PNGdata); | ||||||
|  |             // 更新 0 级 Mipmap。
 | ||||||
|  |             tex.updateImage(); | ||||||
|  |             this._SpriteFrame.texture = tex; | ||||||
|  |             this._SpriteFrame.offset = v2(Png.Xpos, -Png.Ypos); | ||||||
|  |             this.CallBackFunc(this._SpriteFrame); | ||||||
|  |             this.InitState = true; | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | { | ||||||
|  |   "ver": "4.0.23", | ||||||
|  |   "importer": "typescript", | ||||||
|  |   "imported": true, | ||||||
|  |   "uuid": "ab5b488b-feb1-48e2-a426-d0ce0d4b59b7", | ||||||
|  |   "files": [], | ||||||
|  |   "subMetas": {}, | ||||||
|  |   "userData": {} | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue