39 lines
2.6 KiB
TypeScript
39 lines
2.6 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 {TemplateRoomRules} from './template-room-rules.ts';
|
|
const {ccclass,property}=_decorator;
|
|
@ccclass('TemplateCreateRoomView')
|
|
export class TemplateCreateRoomView extends CreateRoomPage {
|
|
@property([ToggleContainer]) groups:ToggleContainer[]=[];
|
|
@property([Toggle]) options:Toggle[]=[];
|
|
@property([Label]) optionLabels:Label[]=[];
|
|
@property(Button) submitButton:Button|null=null;
|
|
@property([Button]) cancelButtons:Button[]=[];
|
|
private readonly rules=new TemplateRoomRules();
|
|
private context:CreateRoomPageContext|null=null;
|
|
private busy=false;
|
|
private releases:Array<()=>void>=[];
|
|
open(context:CreateRoomPageContext):void{
|
|
this.close();
|
|
if(this.groups.length!==2||this.options.length!==4||this.optionLabels.length!==4||!this.submitButton||this.cancelButtons.length!==2)throw Error('TemplateCreateRoomView requires serialized controls');
|
|
this.rules.open(context.previousRoomtype);this.context=context;
|
|
// A saved source index may have no matching option; initialize that state explicitly.
|
|
this.groups.forEach(group=>group.allowSwitchOff=true);
|
|
this.optionLabels.forEach((label,i)=>label.string=this.rules.labels[i]);
|
|
this.options.forEach((toggle,i)=>{
|
|
toggle.isChecked=this.rules.selection[Math.floor(i/2)]===i%2;
|
|
const changed=()=>{if(!this.busy&&toggle.isChecked)this.rules.select(Math.floor(i/2) as 0|1,i%2);};
|
|
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=()=>context.cancel();button.node.on(Button.EventType.CLICK,cancel);this.releases.push(()=>button.node.off(Button.EventType.CLICK,cancel));});this.setBusy(false);
|
|
}
|
|
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();}
|
|
}
|