MessageViewPresenter.ts
5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Presenter } from "../../../common/classbase/PresenterBase";
import { MessageView, MessageEntryItemProps, MessageViewType } from "../view/type/MessageView";
import { GameModelManager } from "../../model/GameModelManager";
import { ReadonlyPlot, SentenceType, SentenceTextContent, SentenceMediaContent } from "../../../avg/model/PlotModel";
import { DeepReadonly, shallowEqual } from "simba-utils";
import { richNodesToCocosString } from "../../../avg/utils/RichTextUtils";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { ChatListViewPresenter } from "./ChatListViewPresenter";
import { RegPresenter } from "../PresenterCCViewFactory";
import { MessageSceneModel } from "../../model/MessageSceneModel";
import { PlotManager } from "../../../avg/PlotManager";
import { PlotSceneType } from "../../Enums";
import { PhoneCallViewPresenter } from "./PhoneCallViewPresenter";
function getSentenceText(content: DeepReadonly<SentenceTextContent> | DeepReadonly<SentenceMediaContent>) {
let text = ""
switch (content.type) {
case SentenceType.TEXT:
text = richNodesToCocosString(content.value);
break;
case SentenceType.AUDIO:
text = "[Voice]";
break;
case SentenceType.IMAGE:
text = "[Image]";
break;
case SentenceType.VIDEO:
text = "[Video]";
break;
}
return text;
}
function getPlotString(plot?: ReadonlyPlot) {
if (plot) {
let text = "";
let sentence:any = plot.sentences[0];
if (sentence.content) {
if (sentence.content.type === SentenceType.SELECT) {
let select = PlotManager.getPlotSelection(plot.id);
if (select !== undefined) {
let option = sentence.content.value[select];
if (option.content) {
text = getSentenceText(option.content);
} else {
text = option.summary;
}
}
} else {
text = getSentenceText(sentence.content);
}
}
return text;
}
return "";
}
@RegPresenter(MessageViewType)
export default class MessageViewPresenter extends Presenter<undefined, MessageView> {
static uuid = "MessageViewPresenter";
private _scenesActive: { [key: number]: boolean } = {};
private _currPlotHasMsg = false;
onOpen() {
super.onOpen(undefined);
this.checkScenesActive();
this.setViewProps();
this._disposable.add(GameModelManager.MessageSceneChanged.on(() => {
if (!this._currPlotHasMsg && !this._view.isHidden && !this._isInBackground) {
this.checkScenesActive();
this.setViewProps();
}
}));
this._disposable.add(PlotManager.PlotStartEvent.on(() => {
if (this._currPlotHasMsg && !this._view.isHidden && !this._isInBackground) {
this.checkScenesActive();
this.setViewProps();
}
}));
this.checkPhoneCall();
}
onShow() {
super.onShow();
this.checkScenesActive();
this.setViewProps();
this.checkPhoneCall();
}
checkPhoneCall() {
if (PlotManager.getCurrentPlots()[0].plotSceneType === PlotSceneType.PhoneCall) {
UIManager.pushPresenter(PhoneCallViewPresenter, undefined);
}
}
onEnterForeground() {
super.onEnterForeground();
this.checkScenesActive();
this.setViewProps();
this.checkPhoneCall();
}
private checkScenesActive = () => {
this._currPlotHasMsg = false;
this._scenesActive = GameModelManager.getMessageScenesModel().reduce((pv, v) => (pv[v.id] = this.checkSceneActive(v), pv), {});
}
private checkSceneActive = (model: MessageSceneModel) => {
let ret = PlotManager.getCurrentPlots().findIndex(v => v.plotSceneType === PlotSceneType.Message && v.plotSceneTypeId === model.id) >= 0;
if (ret) this._currPlotHasMsg = true;
return ret;
}
private setViewProps() {
this._view.setProps({ // 暂每条只有一个头像,群聊显示单独头像,以后可能群聊显示多人头像
items: GameModelManager.getMessageScenesModel().map(
v => {
return {
key: v.id + "", icons: [GameModelManager.getRoleData(v.config.roles[0])!.getHeadIcon(true)],
title: GameModelManager.getConfigLanguageTxt(v.config.title), lastMsg: getPlotString(v.lastPlot),
redDot: this._scenesActive[v.id]
}
}),
onItemClick: this.onItemClick
});
}
private onItemClick = (key: string) => {
console.log("item click", key);
let sceneModel = GameModelManager.getMessageSceneModel(parseInt(key));
if (!sceneModel) { console.error("message scene model not found ", key); return; }
UIManager.pushPresenter(ChatListViewPresenter, sceneModel);
}
}