CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/ChatScreen.cpp
Views: 1401
1
#include <ctype.h>
2
#include "ppsspp_config.h"
3
4
#include "Common/UI/Root.h"
5
#include "Common/UI/Context.h"
6
#include "Common/UI/View.h"
7
#include "Common/UI/ViewGroup.h"
8
#include "Common/UI/ScrollView.h"
9
#include "Common/UI/UI.h"
10
11
#include "Common/Data/Text/I18n.h"
12
#include "Common/Data/Encoding/Utf8.h"
13
#include "Common/System/Request.h"
14
#include "Core/Config.h"
15
#include "Core/System.h"
16
#include "Core/HLE/proAdhoc.h"
17
#include "UI/ChatScreen.h"
18
#include "UI/PopupScreens.h"
19
20
void ChatMenu::CreateContents(UI::ViewGroup *parent) {
21
using namespace UI;
22
auto n = GetI18NCategory(I18NCat::NETWORKING);
23
LinearLayout *outer = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT,400));
24
scroll_ = outer->Add(new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 1.0)));
25
LinearLayout *bottom = outer->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
26
27
chatButton_ = nullptr;
28
chatEdit_ = nullptr;
29
chatVert_ = nullptr;
30
31
if (System_GetPropertyInt(SYSPROP_DEVICE_TYPE) == DEVICE_TYPE_DESKTOP) {
32
// We have direct keyboard input.
33
chatEdit_ = bottom->Add(new TextEdit("", n->T("Chat message"), n->T("Chat Here"), new LinearLayoutParams(1.0)));
34
chatEdit_->OnEnter.Handle(this, &ChatMenu::OnSubmitMessage);
35
} else {
36
// If we have a native input box, like on Android, or at least we can do a popup text input with our UI...
37
chatButton_ = bottom->Add(new Button(n->T("Chat message"), new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
38
chatButton_->OnClick.Handle(this, &ChatMenu::OnAskForChatMessage);
39
}
40
41
if (g_Config.bEnableQuickChat) {
42
LinearLayout *quickChat = outer->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
43
quickChat->Add(new Button("1", new LinearLayoutParams(1.0)))->OnClick.Handle(this, &ChatMenu::OnQuickChat1);
44
quickChat->Add(new Button("2", new LinearLayoutParams(1.0)))->OnClick.Handle(this, &ChatMenu::OnQuickChat2);
45
quickChat->Add(new Button("3", new LinearLayoutParams(1.0)))->OnClick.Handle(this, &ChatMenu::OnQuickChat3);
46
quickChat->Add(new Button("4", new LinearLayoutParams(1.0)))->OnClick.Handle(this, &ChatMenu::OnQuickChat4);
47
quickChat->Add(new Button("5", new LinearLayoutParams(1.0)))->OnClick.Handle(this, &ChatMenu::OnQuickChat5);
48
}
49
chatVert_ = scroll_->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
50
chatVert_->SetSpacing(0);
51
parent->Add(outer);
52
}
53
54
void ChatMenu::CreateSubviews(const Bounds &screenBounds) {
55
using namespace UI;
56
57
float width = 550.0f;
58
59
switch (g_Config.iChatScreenPosition) {
60
// the chat screen size is still static 280x240 need a dynamic size based on device resolution
61
case 0:
62
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, 280, NONE, NONE, 240, true));
63
break;
64
case 1:
65
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, screenBounds.centerX(), NONE, NONE, 240, true));
66
break;
67
case 2:
68
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, NONE, NONE, 280, 240, true));
69
break;
70
case 3:
71
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, 280, 240, NONE, NONE, true));
72
break;
73
case 4:
74
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, screenBounds.centerX(), 240, NONE, NONE, true));
75
break;
76
case 5:
77
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, NONE, 240, 280, NONE, true));
78
break;
79
default:
80
box_ = nullptr;
81
break;
82
}
83
84
if (box_) {
85
Add(box_);
86
box_->SetBG(UI::Drawable(0x99303030));
87
box_->SetHasDropShadow(false);
88
89
auto n = GetI18NCategory(I18NCat::NETWORKING);
90
View *title = new PopupHeader(n->T("Chat"));
91
box_->Add(title);
92
93
CreateContents(box_);
94
}
95
96
UpdateChat();
97
}
98
99
UI::EventReturn ChatMenu::OnSubmitMessage(UI::EventParams &e) {
100
std::string chat = chatEdit_->GetText();
101
chatEdit_->SetText("");
102
chatEdit_->SetFocus();
103
sendChat(chat);
104
return UI::EVENT_DONE;
105
}
106
107
UI::EventReturn ChatMenu::OnAskForChatMessage(UI::EventParams &e) {
108
auto n = GetI18NCategory(I18NCat::NETWORKING);
109
110
using namespace UI;
111
112
if (System_GetPropertyBool(SYSPROP_HAS_TEXT_INPUT_DIALOG)) {
113
System_InputBoxGetString(token_, n->T("Chat"), "", false, [](const std::string &value, int) {
114
sendChat(value);
115
});
116
} else {
117
// We need to pop up a UI inputbox.
118
messageTemp_.clear();
119
TextEditPopupScreen *popupScreen = new TextEditPopupScreen(&messageTemp_, "", n->T("Chat message"), 256);
120
if (System_GetPropertyBool(SYSPROP_KEYBOARD_IS_SOFT)) {
121
popupScreen->SetAlignTop(true);
122
}
123
popupScreen->OnChange.Add([=](UI::EventParams &e) {
124
sendChat(messageTemp_);
125
return UI::EVENT_DONE;
126
});
127
popupScreen->SetPopupOrigin(chatButton_);
128
screenManager_->push(popupScreen);
129
}
130
return UI::EVENT_DONE;
131
}
132
133
UI::EventReturn ChatMenu::OnQuickChat1(UI::EventParams &e) {
134
sendChat(g_Config.sQuickChat0);
135
return UI::EVENT_DONE;
136
}
137
138
UI::EventReturn ChatMenu::OnQuickChat2(UI::EventParams &e) {
139
sendChat(g_Config.sQuickChat1);
140
return UI::EVENT_DONE;
141
}
142
143
UI::EventReturn ChatMenu::OnQuickChat3(UI::EventParams &e) {
144
sendChat(g_Config.sQuickChat2);
145
return UI::EVENT_DONE;
146
}
147
148
UI::EventReturn ChatMenu::OnQuickChat4(UI::EventParams &e) {
149
sendChat(g_Config.sQuickChat3);
150
return UI::EVENT_DONE;
151
}
152
153
UI::EventReturn ChatMenu::OnQuickChat5(UI::EventParams &e) {
154
sendChat(g_Config.sQuickChat4);
155
return UI::EVENT_DONE;
156
}
157
158
void ChatMenu::UpdateChat() {
159
using namespace UI;
160
if (chatVert_ != nullptr) {
161
chatVert_->Clear(); //read Access violation is proadhoc.cpp use NULL_->Clear() pointer?
162
std::vector<std::string> chatLog = getChatLog();
163
for (auto i : chatLog) {
164
uint32_t namecolor = 0x29B6F6;
165
uint32_t textcolor = 0xFFFFFF;
166
uint32_t infocolor = 0xFDD835;
167
168
std::string name = g_Config.sNickName;
169
std::string displayname = i.substr(0, i.find(':'));
170
171
if (name.substr(0, 8) == displayname) {
172
namecolor = 0xE53935;
173
}
174
175
if (i.length() <= displayname.length() || i[displayname.length()] != ':') {
176
TextView *v = chatVert_->Add(new TextView(i, ALIGN_LEFT | FLAG_WRAP_TEXT, true, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
177
v->SetTextColor(0xFF000000 | infocolor);
178
} else {
179
LinearLayout *line = chatVert_->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, FILL_PARENT)));
180
line->SetSpacing(0.0f);
181
TextView *nameView = line->Add(new TextView(displayname, ALIGN_LEFT, true, new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT, 0.0f)));
182
nameView->SetTextColor(0xFF000000 | namecolor);
183
184
std::string chattext = i.substr(displayname.length());
185
TextView *chatView = line->Add(new TextView(chattext, ALIGN_LEFT | FLAG_WRAP_TEXT, true, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f)));
186
chatView->SetTextColor(0xFF000000 | textcolor);
187
}
188
}
189
toBottom_ = true;
190
}
191
}
192
193
void ChatMenu::Update() {
194
AnchorLayout::Update();
195
if (scroll_ && toBottom_) {
196
toBottom_ = false;
197
scroll_->ScrollToBottom();
198
}
199
200
if (chatChangeID_ != GetChatChangeID()) {
201
chatChangeID_ = GetChatChangeID();
202
UpdateChat();
203
}
204
205
#if defined(USING_WIN_UI)
206
// Could remove the fullscreen check here, it works now.
207
auto n = GetI18NCategory(I18NCat::NETWORKING);
208
if (promptInput_ && g_Config.bBypassOSKWithKeyboard && !g_Config.UseFullScreen()) {
209
System_InputBoxGetString(token_, n->T("Chat"), n->T("Chat Here"), false, [](const std::string &value, int) {
210
sendChat(value);
211
});
212
promptInput_ = false;
213
}
214
#endif
215
}
216
217
bool ChatMenu::SubviewFocused(UI::View *view) {
218
if (!AnchorLayout::SubviewFocused(view))
219
return false;
220
221
promptInput_ = true;
222
return true;
223
}
224
225
void ChatMenu::Close() {
226
SetVisibility(UI::V_GONE);
227
}
228
229