41 lines
3.0 KiB
TypeScript
41 lines
3.0 KiB
TypeScript
import {_decorator,Button,Label,Toggle,ToggleContainer} from 'cc';
|
|
import {CreateRoomPage} from '../../scripts/platform-login/CreateRoomPage.ts';
|
|
import type {CreateRoomPageContext} from '../../framework/sdk/contracts/create-room-page.ts';
|
|
import {ErqiwangRoomRules} from './erqiwang-room-rules.ts';
|
|
const {ccclass,property}=_decorator;
|
|
@ccclass('ErqiwangCreateRoomView')
|
|
export class ErqiwangCreateRoomView extends CreateRoomPage {
|
|
@property([ToggleContainer]) groups:ToggleContainer[]=[];
|
|
@property([Toggle]) options:Toggle[]=[];
|
|
@property([Label]) optionLabels:Label[]=[];
|
|
@property(Button) submitButton:Button|null=null;
|
|
@property([Button]) cancelButtons:Button[]=[];
|
|
@property(Label) feeLabel:Label|null=null;
|
|
private readonly rules=new ErqiwangRoomRules();
|
|
private context:CreateRoomPageContext|null=null;
|
|
private busy=false;
|
|
private releases:Array<()=>void>=[];
|
|
open(context:CreateRoomPageContext):void{
|
|
this.close();
|
|
if(this.groups.length!==3||this.options.length!==8||this.optionLabels.length!==8||!this.feeLabel||!this.submitButton||this.cancelButtons.length!==2)throw Error('ErqiwangCreateRoomView requires serialized controls');
|
|
this.rules.open(context.previousRoomtype);this.context=context;
|
|
// Permit all toggles to reset before restoring each mandatory group.
|
|
this.groups.forEach(group=>group.allowSwitchOff=true);
|
|
this.optionLabels.forEach((label,i)=>label.string=['6局','12局','房主扣卡','AA每人扣卡','可查牌','不查牌','傍王','爬坡'][i]);
|
|
this.options.forEach((toggle,i)=>{
|
|
toggle.isChecked=i<6?this.rules.selection[Math.floor(i/2)]===i%2:this.rules.selection[i-3]===1;
|
|
const changed=()=>{if(this.busy||!this.context)return;if(i<6&&!toggle.isChecked)return;this.rules.select(i<6?Math.floor(i/2):i-3,i<6?i%2:Number(toggle.isChecked));this.renderFee();};
|
|
toggle.node.on(Toggle.EventType.TOGGLE,changed);this.releases.push(()=>toggle.node.off(Toggle.EventType.TOGGLE,changed));
|
|
});
|
|
this.groups.forEach(group=>group.allowSwitchOff=false);
|
|
const bind=(button:Button,action:()=>void)=>{const fn=()=>{if(!this.busy&&this.context)try{action();}catch(error){context.reportError(error instanceof Error?error.message:String(error));}};
|
|
button.node.on(Button.EventType.CLICK,fn);this.releases.push(()=>button.node.off(Button.EventType.CLICK,fn));};
|
|
bind(this.submitButton,()=>context.submit(this.rules.build()));
|
|
this.cancelButtons.forEach(button=>{const cancel=()=>{if(this.context===context)context.cancel();};button.node.on(Button.EventType.CLICK,cancel);this.releases.push(()=>button.node.off(Button.EventType.CLICK,cancel));});this.setBusy(false);this.renderFee();
|
|
}
|
|
private renderFee():void{if(this.feeLabel)this.feeLabel.string=`${this.rules.selection[1]===1?'每人':'房主'}消耗 ${this.rules.roomCards} 张房卡`; }
|
|
setBusy(busy:boolean):void{this.busy=busy;this.options.forEach(toggle=>toggle.interactable=!busy);if(this.submitButton)this.submitButton.interactable=!busy;}
|
|
close():void{for(const release of this.releases.splice(0))release();this.context=null;}
|
|
onDestroy():void{this.close();}
|
|
}
|