Skip to content

Commit 0fa73b7

Browse files
committed
代码优化
Signed-off-by: 冰冰冰 <[email protected]>
1 parent be45f87 commit 0fa73b7

File tree

14 files changed

+308
-115
lines changed

14 files changed

+308
-115
lines changed

tests/cpp-tests/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { WorkerManager } from '../workers/WorkerManager'
2-
import { VideoPlayerInfo, VideoEvent } from '@ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayerMsg';
2+
import {
3+
VideoPlayerInfo,
4+
VideoEvent
5+
} from '@ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayerMsg';
36
import worker from '@ohos.worker';
47

58
@Component
6-
export struct CocosVideoPlayer {
9+
export struct CocosVideoPlayer {
710
@ObjectLink videoPlayerInfo: VideoPlayerInfo;
811
cocosWorker: worker.ThreadWorker = WorkerManager.getInstance().getWorker();
912

@@ -12,27 +15,39 @@ export struct CocosVideoPlayer {
1215
Video({
1316
src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile,
1417
controller: this.videoPlayerInfo.controller
15-
}).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y })
18+
})
1619
.width(this.videoPlayerInfo.w)
1720
.height(this.videoPlayerInfo.h)
1821
.visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None)
1922
.controls(false)
2023
.loop(this.videoPlayerInfo.isLoop)
2124
.objectFit(this.videoPlayerInfo.objectFit)
22-
.onPrepared(()=>{
23-
if(this.videoPlayerInfo.isPlay) {
25+
.onPrepared(() => {
26+
if (this.videoPlayerInfo.isPlay) {
2427
this.videoPlayerInfo.controller.start()
2528
}
2629
this.videoPlayerInfo.controller.requestFullscreen(this.videoPlayerInfo.isFullScreen)
2730
})
28-
.onStart(()=>{
29-
this.cocosWorker.postMessage({type: "onVideoCallBack", viewTag: this.videoPlayerInfo.viewTag, event: VideoEvent.PLAYING});
31+
.onStart(() => {
32+
this.cocosWorker.postMessage({
33+
type: "onVideoCallBack",
34+
viewTag: this.videoPlayerInfo.viewTag,
35+
event: VideoEvent.PLAYING
36+
});
3037
})
31-
.onPause(()=>{
32-
this.cocosWorker.postMessage({type: "onVideoCallBack", viewTag: this.videoPlayerInfo.viewTag, event: VideoEvent.PAUSED});
38+
.onPause(() => {
39+
this.cocosWorker.postMessage({
40+
type: "onVideoCallBack",
41+
viewTag: this.videoPlayerInfo.viewTag,
42+
event: VideoEvent.PAUSED
43+
});
3344
})
34-
.onFinish(()=>{
35-
this.cocosWorker.postMessage({type: "onVideoCallBack", viewTag: this.videoPlayerInfo.viewTag, event: VideoEvent.COMPLETED});
45+
.onFinish(() => {
46+
this.cocosWorker.postMessage({
47+
type: "onVideoCallBack",
48+
viewTag: this.videoPlayerInfo.viewTag,
49+
event: VideoEvent.COMPLETED
50+
});
3651
})
3752
}
3853
}

tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import deviceInfo from '@ohos.deviceInfo';
2+
13
import nativeRender from 'libnativerender.so'
24
import { ContextType } from '@ohos/libSysCapabilities'
35
import { TextInputInfo } from '@ohos/libSysCapabilities/src/main/ets/components/editbox/EditBoxMsg'
@@ -10,31 +12,27 @@ import { CocosEditBox } from '../components/CocosEditBox'
1012
import { CocosWebView } from '../components/CocosWebview'
1113
import { CocosVideoPlayer } from '../components/CocosVideoPlayer'
1214
import { TextInputDialog } from '../components/TextInputDialog'
13-
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"
14-
import { MessageEvents } from '@ohos.worker'
15-
import common from '@ohos.app.ability.common'
16-
import deviceInfo from '@ohos.deviceInfo';
17-
const nativePageLifecycle:nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE);
15+
import { GlobalContext, GlobalContextConstants } from "@ohos/libSysCapabilities"
16+
import promptAction from '@ohos.promptAction';
17+
import process from '@ohos.process';
18+
19+
const nativePageLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE);
1820

1921
@Entry
2022
@Component
2123
struct Index {
22-
2324
cocosWorker = WorkerManager.getInstance().getWorker();
2425
xcomponentController: XComponentController = new XComponentController();
25-
26+
processMgr = new process.ProcessManager();
2627
// EditBox
2728
@State editBoxArray: TextInputInfo[] = [];
2829
private editBoxIndexMap: Map<number, TextInputInfo> = new Map;
29-
3030
// WebView
3131
@State webViewArray: WebViewInfo[] = [];
3232
private webViewIndexMap: Map<number, number> = new Map;
33-
3433
// videoPlayer
3534
@State videoPlayerInfoArray: VideoPlayerInfo[] = [];
3635
private videoPlayerIndexMap: Map<number, VideoPlayerInfo> = new Map;
37-
3836
// textInputDialog
3937
showMessage: TextInputDialogEntity = new TextInputDialogEntity('');
4038
dialogController: CustomDialogController = new CustomDialogController({
@@ -69,7 +67,37 @@ struct Index {
6967

7068
onBackPress() {
7169
console.log('[LIFECYCLE-Page] onBackPress');
72-
this.cocosWorker.postMessage({ type: "exit" });
70+
try {
71+
promptAction.showDialog({
72+
title: "提示",
73+
message: "确认退出游戏吗",
74+
buttons: [
75+
{
76+
text: '取消',
77+
// 颜色可自定义
78+
color: '#000000'
79+
},
80+
{
81+
text: '确认',
82+
// 颜色可自定义
83+
color: '#000000'
84+
}
85+
],
86+
}).then(data => {
87+
console.info('showDialog success, click button: ' + data.index);
88+
if (data.index == 0) {
89+
console.info('showDialog click button cancel');
90+
return;
91+
} else {
92+
console.info('showDialog click button ok');
93+
this.processMgr.exit(0);
94+
}
95+
})
96+
} catch (error) {
97+
console.error(`showDialog args error code is ${error.code}, message is ${error.message}`);
98+
}
99+
;
100+
// If disable system exit needed, remove comment "return true"
73101
return true;
74102
}
75103

@@ -78,7 +106,7 @@ struct Index {
78106
}
79107

80108
build() {
81-
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
109+
Stack() {
82110
XComponent({
83111
id: 'xcomponentId',
84112
type: 'surface',
@@ -103,23 +131,26 @@ struct Index {
103131
)
104132
.onLoad((context) => {
105133
console.log('[cocos] XComponent.onLoad Callback');
106-
this.cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)});
134+
this.cocosWorker.postMessage({
135+
type: "abilityContextInit",
136+
data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)
137+
});
107138
this.cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" });
108139
this.cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage;
109140
})
110141
.onDestroy(() => {
111142
})
112-
ForEach(this.editBoxArray, (item:TextInputInfo) => {
113-
CocosEditBox({ textInputInfo: item});
114-
}, (item:TextInputInfo) => item.viewTag.toString())
143+
ForEach(this.editBoxArray, (item: TextInputInfo) => {
144+
CocosEditBox({ textInputInfo: item });
145+
}, (item: TextInputInfo) => item.viewTag.toString())
115146

116-
ForEach(this.webViewArray, (item:WebViewInfo) => {
147+
ForEach(this.webViewArray, (item: WebViewInfo) => {
117148
CocosWebView({ viewInfo: item })
118-
}, (item:WebViewInfo) => item.uniqueId.toString())
149+
}, (item: WebViewInfo) => item.uniqueId.toString())
119150

120-
ForEach(this.videoPlayerInfoArray, (item:VideoPlayerInfo) => {
151+
ForEach(this.videoPlayerInfoArray, (item: VideoPlayerInfo) => {
121152
CocosVideoPlayer({ videoPlayerInfo: item })
122-
}, (item:VideoPlayerInfo) => item.viewTag.toString())
153+
}, (item: VideoPlayerInfo) => item.viewTag.toString())
123154
}
124155
.width('100%')
125156
.height('100%')

tests/lua-tests/project/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default class MainAbility extends UIAbility {
4646
console.error('Failed to set the window, cause ' + JSON.stringify(err));
4747
});
4848
})
49-
49+
5050
windowStage.on("windowStageEvent", (data) => {
5151
let stageEventType: window.WindowStageEventType = data;
5252
switch (stageEventType) {

tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosEditBox.ets

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,75 @@ export struct CocosEditBox {
88
cocosWorker: worker.ThreadWorker = WorkerManager.getInstance().getWorker();
99

1010
build(){
11-
TextInput({text: this.textInputInfo.text, placeholder: this.textInputInfo.placeHolder})
12-
.id('TextInput' + this.textInputInfo.viewTag)
13-
.position({x: px2vp(this.textInputInfo.x), y: px2vp(this.textInputInfo.y)})
14-
.size({width: px2vp(this.textInputInfo.w), height: px2vp(this.textInputInfo.h)})
15-
.padding({top: px2vp(this.textInputInfo.paddingH), right: px2vp(this.textInputInfo.paddingW),
16-
bottom: px2vp(this.textInputInfo.paddingH), left: px2vp(this.textInputInfo.paddingW)})
17-
.borderRadius(0)
18-
.maxLength(this.textInputInfo.maxLength)
19-
.type(this.textInputInfo.inputMode)
20-
.fontFamily($rawfile(this.textInputInfo.fontPath))
21-
.fontSize(px2fp(this.textInputInfo.fontSize))
22-
.fontColor(`rgba(${this.textInputInfo.fontColor.r}, ${this.textInputInfo.fontColor.g}, ${this.textInputInfo.fontColor.b}, ${this.textInputInfo.fontColor.a})`)
23-
.placeholderFont({size: px2fp(this.textInputInfo.placeholderFontSize), family: $rawfile(this.textInputInfo.placeHolderFontPath)})
24-
.placeholderColor(`rgba(${this.textInputInfo.placeholderFontColor.r}, ${this.textInputInfo.placeholderFontColor.g}, ${this.textInputInfo.placeholderFontColor.b}, ${this.textInputInfo.placeholderFontColor.a})`)
25-
.backgroundColor(Color.Transparent)
26-
.visibility(this.textInputInfo.visible ? Visibility.Visible : Visibility.None)
27-
.showPasswordIcon(false)
28-
.onVisibleAreaChange([0.0, 1.0], () => {
29-
focusControl.requestFocus('TextInput' + this.textInputInfo.viewTag);
30-
})
31-
.onFocus(() => {
32-
this.cocosWorker.postMessage({type: "editBoxOnFocus", viewTag: this.textInputInfo.viewTag});
33-
})
34-
.onChange((value: string)=>{
35-
this.textInputInfo.text = value;
36-
this.cocosWorker.postMessage({type: "editBoxOnChange", viewTag: this.textInputInfo.viewTag, value: value});
37-
})
38-
.onBlur(()=> {
39-
this.textInputInfo.visible = false
40-
this.cocosWorker.postMessage({type: "editBoxOnEnter", viewTag: this.textInputInfo.viewTag, text: this.textInputInfo.text});
41-
})
42-
.onSubmit(()=>{
43-
this.cocosWorker.postMessage({type: "editBoxOnEnter", viewTag: this.textInputInfo.viewTag, text: this.textInputInfo.text});
44-
})
11+
if(this.textInputInfo.multiline){
12+
TextArea({text: this.textInputInfo.text, placeholder: this.textInputInfo.placeHolder})
13+
.id('TextArea'+this.textInputInfo.viewTag)
14+
.position({x: px2vp(this.textInputInfo.x), y: px2vp(this.textInputInfo.y)})
15+
.size({width: px2vp(this.textInputInfo.w), height: px2vp(this.textInputInfo.h)})
16+
.padding({top: px2vp(this.textInputInfo.paddingH), right: px2vp(this.textInputInfo.paddingW),
17+
bottom: px2vp(this.textInputInfo.paddingH), left: px2vp(this.textInputInfo.paddingW)})
18+
.borderRadius(0)
19+
.maxLength(this.textInputInfo.maxLength)
20+
.type(TextAreaType.NORMAL)
21+
.fontFamily($rawfile(this.textInputInfo.fontPath))
22+
.fontSize(px2fp(this.textInputInfo.fontSize))
23+
.fontColor(`rgba(${this.textInputInfo.fontColor.r}, ${this.textInputInfo.fontColor.g}, ${this.textInputInfo.fontColor.b}, ${this.textInputInfo.fontColor.a})`)
24+
.placeholderFont({size: px2fp(this.textInputInfo.placeholderFontSize), family: $rawfile(this.textInputInfo.placeHolderFontPath)})
25+
.placeholderColor(`rgba(${this.textInputInfo.placeholderFontColor.r}, ${this.textInputInfo.placeholderFontColor.g}, ${this.textInputInfo.placeholderFontColor.b}, ${this.textInputInfo.placeholderFontColor.a})`)
26+
.backgroundColor(Color.Transparent)
27+
.visibility(this.textInputInfo.visible ? Visibility.Visible : Visibility.None)
28+
.onVisibleAreaChange([0.0, 1.0], () => {
29+
focusControl.requestFocus('TextArea' + this.textInputInfo.viewTag);
30+
})
31+
.onFocus(() => {
32+
this.cocosWorker.postMessage({type: "editBoxOnFocus", viewTag: this.textInputInfo.viewTag});
33+
})
34+
.onChange((value: string)=>{
35+
this.textInputInfo.text = value;
36+
this.cocosWorker.postMessage({type: "editBoxOnChange", viewTag: this.textInputInfo.viewTag, value: value});
37+
})
38+
.onBlur(()=> {
39+
this.textInputInfo.visible = false
40+
this.cocosWorker.postMessage({type: "editBoxOnEnter", viewTag: this.textInputInfo.viewTag, text: this.textInputInfo.text});
41+
})
42+
.onSubmit(()=>{
43+
this.cocosWorker.postMessage({type: "editBoxOnEnter", viewTag: this.textInputInfo.viewTag, text: this.textInputInfo.text});
44+
})
45+
}else{
46+
TextInput({text: this.textInputInfo.text, placeholder: this.textInputInfo.placeHolder})
47+
.id('TextInput'+this.textInputInfo.viewTag)
48+
.position({x: px2vp(this.textInputInfo.x), y: px2vp(this.textInputInfo.y)})
49+
.size({width: px2vp(this.textInputInfo.w), height: px2vp(this.textInputInfo.h)})
50+
.padding({top: px2vp(this.textInputInfo.paddingH), right: px2vp(this.textInputInfo.paddingW),
51+
bottom: px2vp(this.textInputInfo.paddingH), left: px2vp(this.textInputInfo.paddingW)})
52+
.borderRadius(0)
53+
.maxLength(this.textInputInfo.maxLength)
54+
.type(this.textInputInfo.inputMode)
55+
.fontFamily($rawfile(this.textInputInfo.fontPath))
56+
.fontSize(px2fp(this.textInputInfo.fontSize))
57+
.fontColor(`rgba(${this.textInputInfo.fontColor.r}, ${this.textInputInfo.fontColor.g}, ${this.textInputInfo.fontColor.b}, ${this.textInputInfo.fontColor.a})`)
58+
.placeholderFont({size: px2fp(this.textInputInfo.placeholderFontSize), family: $rawfile(this.textInputInfo.placeHolderFontPath)})
59+
.placeholderColor(`rgba(${this.textInputInfo.placeholderFontColor.r}, ${this.textInputInfo.placeholderFontColor.g}, ${this.textInputInfo.placeholderFontColor.b}, ${this.textInputInfo.placeholderFontColor.a})`)
60+
.backgroundColor(Color.Transparent)
61+
.visibility(this.textInputInfo.visible ? Visibility.Visible : Visibility.None)
62+
.showPasswordIcon(false)
63+
.onVisibleAreaChange([0.0, 1.0], () => {
64+
focusControl.requestFocus('TextInput' + this.textInputInfo.viewTag);
65+
})
66+
.onFocus(() => {
67+
this.cocosWorker.postMessage({type: "editBoxOnFocus", viewTag: this.textInputInfo.viewTag});
68+
})
69+
.onChange((value: string)=>{
70+
this.textInputInfo.text = value;
71+
this.cocosWorker.postMessage({type: "editBoxOnChange", viewTag: this.textInputInfo.viewTag, value: value});
72+
})
73+
.onBlur(()=> {
74+
this.textInputInfo.visible = false
75+
this.cocosWorker.postMessage({type: "editBoxOnEnter", viewTag: this.textInputInfo.viewTag, text: this.textInputInfo.text});
76+
})
77+
.onSubmit(()=>{
78+
this.cocosWorker.postMessage({type: "editBoxOnEnter", viewTag: this.textInputInfo.viewTag, text: this.textInputInfo.text});
79+
})
80+
}
4581
}
46-
}
82+
}

tests/lua-tests/project/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { WorkerManager } from '../workers/WorkerManager'
2-
import { VideoPlayerInfo, VideoEvent } from '@ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayerMsg';
2+
import {
3+
VideoPlayerInfo,
4+
VideoEvent
5+
} from '@ohos/libSysCapabilities/src/main/ets/components/videoplayer/VideoPlayerMsg';
36
import worker from '@ohos.worker';
47

58
@Component
6-
export struct CocosVideoPlayer {
9+
export struct CocosVideoPlayer {
710
@ObjectLink videoPlayerInfo: VideoPlayerInfo;
811
cocosWorker: worker.ThreadWorker = WorkerManager.getInstance().getWorker();
912

@@ -12,27 +15,39 @@ export struct CocosVideoPlayer {
1215
Video({
1316
src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile,
1417
controller: this.videoPlayerInfo.controller
15-
}).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y })
18+
})
1619
.width(this.videoPlayerInfo.w)
1720
.height(this.videoPlayerInfo.h)
1821
.visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None)
1922
.controls(false)
2023
.loop(this.videoPlayerInfo.isLoop)
2124
.objectFit(this.videoPlayerInfo.objectFit)
22-
.onPrepared(()=>{
23-
if(this.videoPlayerInfo.isPlay) {
25+
.onPrepared(() => {
26+
if (this.videoPlayerInfo.isPlay) {
2427
this.videoPlayerInfo.controller.start()
2528
}
2629
this.videoPlayerInfo.controller.requestFullscreen(this.videoPlayerInfo.isFullScreen)
2730
})
28-
.onStart(()=>{
29-
this.cocosWorker.postMessage({type: "onVideoCallBack", viewTag: this.videoPlayerInfo.viewTag, event: VideoEvent.PLAYING});
31+
.onStart(() => {
32+
this.cocosWorker.postMessage({
33+
type: "onVideoCallBack",
34+
viewTag: this.videoPlayerInfo.viewTag,
35+
event: VideoEvent.PLAYING
36+
});
3037
})
31-
.onPause(()=>{
32-
this.cocosWorker.postMessage({type: "onVideoCallBack", viewTag: this.videoPlayerInfo.viewTag, event: VideoEvent.PAUSED});
38+
.onPause(() => {
39+
this.cocosWorker.postMessage({
40+
type: "onVideoCallBack",
41+
viewTag: this.videoPlayerInfo.viewTag,
42+
event: VideoEvent.PAUSED
43+
});
3344
})
34-
.onFinish(()=>{
35-
this.cocosWorker.postMessage({type: "onVideoCallBack", viewTag: this.videoPlayerInfo.viewTag, event: VideoEvent.COMPLETED});
45+
.onFinish(() => {
46+
this.cocosWorker.postMessage({
47+
type: "onVideoCallBack",
48+
viewTag: this.videoPlayerInfo.viewTag,
49+
event: VideoEvent.COMPLETED
50+
});
3651
})
3752
}
3853
}

0 commit comments

Comments
 (0)