Native3D/n3d/src/main/ets/pages/Index.ets
2025-11-24 23:14:38 +08:00

101 lines
3.1 KiB
Plaintext

import { hilog } from '@kit.PerformanceAnalysisKit';
import Native3D from 'libn3d.so';
const DOMAIN = 0x0000;
class MyXComponentController extends XComponentController{
onSurfaceCreated(surfaceId: string): void {
console.info(`onSurfaceCreated surfaceId: ${surfaceId}`);
Native3D.SetSurfaceId(BigInt(surfaceId));
}
onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void {
console.info(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`);
// 在onSurfaceChanged中调用ChangeSurface绘制内容
Native3D.ChangeSurface(BigInt(surfaceId), rect.surfaceWidth, rect.surfaceHeight);
}
onSurfaceDestroyed(surfaceId: string): void {
console.info(`onSurfaceDestroyed surfaceId: ${surfaceId}`);
Native3D.DestroySurface(BigInt(surfaceId));
}
}
@Entry
@Component
struct Index {
@State objStatus: string = "当前状态:";
@State currentStatus: string = "Null";
@State msgHead: string = 'Native3D Demo';
@State btnRun:string ='绘制图形';
@State btnChn:string ='改变颜色';
ComCtrl: XComponentController = new MyXComponentController();
build() {
Column() {
Row() {
Text(this.msgHead)
.fontSize(18)
.fontWeight(FontWeight.Bold)
}.height('10%')
Column({ space: 10 }){
XComponent({
type: XComponentType.SURFACE,
controller: this.ComCtrl
}).height('80%')
Row(){
Text(this.objStatus)
.fontSize('24fp')
.fontWeight(500)
.height('20%')
Text(this.currentStatus)
.fontSize('24fp')
.fontWeight(500)
.height('20%')
}.height('20%')
}.height('80%')
Column() {
Row(){
Text('渲染后端:')
Checkbox()
Text('OpenGL ES')
Checkbox()
Text('Vulkan')
}
Row() {
Button(this.btnRun)
.fontSize(22)
.fontWeight(FontWeight.Bold)
.onClick(() => {
let surfaceId = this.ComCtrl.getXComponentSurfaceId();
Native3D.DrawPattern(BigInt(surfaceId));
let hasDraw: boolean = false;
if (Native3D.GetXComponentStatus(BigInt(surfaceId))) {
hasDraw = Native3D.GetXComponentStatus(BigInt(surfaceId)).hasDraw;
}
if (hasDraw) {
this.currentStatus = "绘制图形";
}
})
Button(this.btnChn)
.fontSize(22)
.fontWeight(FontWeight.Bold)
.onClick(() => {
let surfaceId = this.ComCtrl.getXComponentSurfaceId();
Native3D.ChangeColor(BigInt(surfaceId));
let hasChangeColor: boolean = false;
if (Native3D.GetXComponentStatus(BigInt(surfaceId))) {
hasChangeColor = Native3D.GetXComponentStatus(BigInt(surfaceId)).hasChangeColor;
}
if (hasChangeColor) {
this.currentStatus = "改变颜色";
}
})
}
}.height('10%')
}
}
}