OpenCAX/entry/src/main/ets/pages/EventSubWin/ExCom.ets

136 lines
5.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TitleButton } from '../LayoutInterface/Interface/ButtonInterface';
import { CloseSubWindow, CreateAndShowSubWindow, WinInfo} from './SWBase';
import NativeOpenCAX from 'libopencax.so';
import { mwInfo } from '../DispWinInfo/DispWinInfo'
import { AppStorageV2, NodeContent} from '@kit.ArkUI';
import { ModelViewTab } from '../ModelViewTab';
@ObservedV2
export class XCompInfo{
@Trace nId?:string;
@Trace xId?:string;
@Trace name?:string;
@Trace icon?:string;
@Trace node?:NodeContent;
constructor(_nId?:string,_xId?: string, _name?: string,_icon?:string,_node?:NodeContent) {
this.nId = _nId ?? undefined;
this.xId = _xId ?? undefined;
this.name=_name?? undefined;
this.icon=_icon?? undefined;
this.node=_node?? undefined;
}
}
@ObservedV2
export class XCompInfos{
@Trace infos?:Array<XCompInfo>;
constructor(_infos?:Array<XCompInfo>) {
this.infos = _infos ?? undefined;
}
}
export let xcInfos:XCompInfos = AppStorageV2.connect<XCompInfos>(XCompInfos, () => new XCompInfos)!;
export let ctInfo: XCompInfo = AppStorageV2.connect<XCompInfo>(XCompInfo, () => new XCompInfo)!;
function XNRandomId(): string {
// 获取当前时间戳(毫秒)
const now = Date.now();
// 取时间戳的最后几位例如4位作为基础部分
const timePart = parseInt(now.toString().slice(-4));
// 生成剩余的4位随机数
const randomPart = Math.floor(Math.random() * 10000);
// 组合两部分并取模和偏移以确保在8位数范围内
// 例如: timePart (0-9999) + randomPart (0-9999) -> sum (0-19998)
// 需要将其映射到 [10000000, 99999999]
// 结果在 [0, 89999999]
const combined = (timePart * 10000 + randomPart) % 90000000;
// 调整范围到 [10000000, 99999999]
return (combined + 10000000).toString();
}
//初始化全局XComp管理
export function InitXCompNode(){
//数组为0表示初始化,Native侧这初始化主上下文,否则Native侧初始化共享上下文
const xcIs:XCompInfos = AppStorageV2.connect<XCompInfos>(XCompInfos, () => new XCompInfos)!;
xcIs.infos=new Array<XCompInfo>;
}
//创建XComp组件
export function CreateXCompNode(name:string){
const xcIs:XCompInfos = AppStorageV2.connect<XCompInfos>(XCompInfos, () => new XCompInfos)!;
let xInfo:XCompInfo=new XCompInfo;
xInfo.nId='n_'+XNRandomId();
xInfo.xId='x_'+XNRandomId();
xInfo.name=name+xInfo.xId;
xInfo.node=new NodeContent();
if((xcIs.infos as Array<XCompInfo>).length>0)
{
NativeOpenCAX.InitNativeNode(xInfo.nId,xInfo.xId,true,xInfo.node);
}else{
NativeOpenCAX.InitNativeNode(xInfo.nId,xInfo.xId,false,xInfo.node);
}
(xcIs.infos as Array<XCompInfo>).push(xInfo);
console.log(`ArkUI Node ID: ${xInfo.nId}`);
console.log(`ArkUI XComp ID: ${xInfo.xId}`);
}
//获取当前XComp组件
export function CurrentXCompIndex(id:number):XCompInfo{
const xcIs:XCompInfos = AppStorageV2.connect<XCompInfos>(XCompInfos, () => new XCompInfos)!;
return ctInfo=(xcIs.infos as Array<XCompInfo>)[id];
}
//执行命令
export function ExecuteCommand(event:TitleButton){
//事件处理
if(event?.eEvent=='Execute_LoadModel'){
NativeOpenCAX.loadModel("/data/storage/el2/base/haps/entry/files/example/2027.stp");
}
if(event?.eEvent=='Create_New'){
CreateXCompNode("");
}
//窗体打开
if(event?.eEvent=='Execute_CreateSubWindow'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.3,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
}else if(event?.eEvent=='Execute_ExitSubWindow'){
CloseSubWindow();
} else if(event?.eEvent=='Execute_CreateSubWindow_Options'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.6,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
} else if(event?.eEvent=='Execute_NewFileWindow'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.6,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
}else if(event?.eEvent=='Execute_SaveAsFileWindow'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.6,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
}else if(event?.eEvent=='Execute_ImportFileWindow'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.6,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
}else if(event?.eEvent=='Execute_ExportFileWindow'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.6,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
} else if(event?.eEvent=='Open_NewModel'){
const winInfo:WinInfo=new WinInfo(event.eName,event.ePage,mwInfo.width*0.6,mwInfo.height*0.9);
CreateAndShowSubWindow(winInfo);
} else if(event?.eEvent=='Execute_ExitSubWindow_Options'){
CloseSubWindow();
}
//视图切换
if(event?.eEvent=='CMD_VIEW_FRONT'){
NativeOpenCAX.switchView("CMD_VIEW_FRONT");
}else if(event?.eEvent=='CMD_VIEW_TOP'){
NativeOpenCAX.switchView("CMD_VIEW_TOP");
}else if(event?.eEvent=='CMD_VIEW_LEFT_SIDE'){
NativeOpenCAX.switchView("CMD_VIEW_LEFT_SIDE");
}else if(event?.eEvent=='CMD_VIEW_RIGHT_SIDE'){
NativeOpenCAX.switchView("CMD_VIEW_RIGHT_SIDE");
}else if(event?.eEvent=='CMD_VIEW_BOTTOM'){
NativeOpenCAX.switchView("CMD_VIEW_BOTTOM");
}else if(event?.eEvent=='CMD_VIEW_REAR'){
NativeOpenCAX.switchView("CMD_VIEW_REAR");
}else if(event?.eEvent=='CMD_VIEW_ISO'){
NativeOpenCAX.switchView("CMD_VIEW_ISO");
}else if(event?.eEvent=='CMD_VIEW_DIM'){
NativeOpenCAX.switchView("CMD_VIEW_DIM");
}
}