Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/ChatScreen.cpp
5655 views
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
for (int i = 0; i < 5; i++) {
44
std::string name = std::to_string(i + 1);
45
quickChat->Add(new Button(name, new LinearLayoutParams(1.0)))->OnClick.Add([i](UI::EventParams &e) {
46
sendChat(g_Config.sQuickChat[i]);
47
});
48
}
49
}
50
chatVert_ = scroll_->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
51
chatVert_->SetSpacing(0);
52
parent->Add(outer);
53
}
54
55
void ChatMenu::CreateSubviews(const Bounds &screenBounds) {
56
using namespace UI;
57
58
float width = 550.0f;
59
60
switch (g_Config.iChatScreenPosition) {
61
// the chat screen size is still static 280x240 need a dynamic size based on device resolution
62
case 0:
63
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, 280, NONE, NONE, 240, Centering::Both));
64
break;
65
case 1:
66
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, screenBounds.centerX(), NONE, NONE, 240, Centering::Both));
67
break;
68
case 2:
69
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, NONE, NONE, 280, 240, Centering::Both));
70
break;
71
case 3:
72
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, 280, 240, NONE, NONE, Centering::Both));
73
break;
74
case 4:
75
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, screenBounds.centerX(), 240, NONE, NONE, Centering::Both));
76
break;
77
case 5:
78
box_ = new LinearLayout(ORIENT_VERTICAL, new AnchorLayoutParams(width, WRAP_CONTENT, NONE, 240, 280, NONE, Centering::Both));
79
break;
80
default:
81
box_ = nullptr;
82
break;
83
}
84
85
if (box_) {
86
Add(box_);
87
box_->SetBG(UI::Drawable(0x99303030));
88
box_->SetHasDropShadow(false);
89
90
auto n = GetI18NCategory(I18NCat::NETWORKING);
91
View *title = new PopupHeader(n->T("Chat"));
92
box_->Add(title);
93
94
CreateContents(box_);
95
}
96
97
UpdateChat();
98
}
99
100
void ChatMenu::OnSubmitMessage(UI::EventParams &e) {
101
std::string chat = chatEdit_->GetText();
102
chatEdit_->SetText("");
103
chatEdit_->SetFocus();
104
sendChat(chat);
105
}
106
107
void 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
});
126
popupScreen->SetPopupOrigin(chatButton_);
127
screenManager_->push(popupScreen);
128
}
129
}
130
131
void ChatMenu::UpdateChat() {
132
using namespace UI;
133
if (chatVert_ != nullptr) {
134
chatVert_->Clear(); //read Access violation is proadhoc.cpp use NULL_->Clear() pointer?
135
std::vector<std::string> chatLog = getChatLog();
136
for (auto i : chatLog) {
137
uint32_t namecolor = 0x29B6F6;
138
uint32_t textcolor = 0xFFFFFF;
139
uint32_t infocolor = 0xFDD835;
140
141
std::string name = g_Config.sNickName;
142
std::string displayname = i.substr(0, i.find(':'));
143
144
if (name.substr(0, 8) == displayname) {
145
namecolor = 0xE53935;
146
}
147
148
if (i.length() <= displayname.length() || i[displayname.length()] != ':') {
149
TextView *v = chatVert_->Add(new TextView(i, ALIGN_LEFT | FLAG_WRAP_TEXT, true, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
150
v->SetTextColor(0xFF000000 | infocolor);
151
} else {
152
LinearLayout *line = chatVert_->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, FILL_PARENT)));
153
line->SetSpacing(0.0f);
154
TextView *nameView = line->Add(new TextView(displayname, ALIGN_LEFT, true, new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT, 0.0f)));
155
nameView->SetTextColor(0xFF000000 | namecolor);
156
157
std::string chattext = i.substr(displayname.length());
158
TextView *chatView = line->Add(new TextView(chattext, ALIGN_LEFT | FLAG_WRAP_TEXT, true, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f)));
159
chatView->SetTextColor(0xFF000000 | textcolor);
160
}
161
}
162
toBottom_ = true;
163
}
164
}
165
166
void ChatMenu::Update() {
167
AnchorLayout::Update();
168
if (scroll_ && toBottom_) {
169
toBottom_ = false;
170
scroll_->ScrollToBottom();
171
}
172
173
if (chatChangeID_ != GetChatChangeID()) {
174
chatChangeID_ = GetChatChangeID();
175
UpdateChat();
176
}
177
178
#if defined(USING_WIN_UI)
179
// Could remove the fullscreen check here, it works now.
180
auto n = GetI18NCategory(I18NCat::NETWORKING);
181
if (promptInput_ && g_Config.bBypassOSKWithKeyboard && !g_Config.bFullScreen) {
182
System_InputBoxGetString(token_, n->T("Chat"), n->T("Chat Here"), false, [](const std::string &value, int) {
183
sendChat(value);
184
});
185
promptInput_ = false;
186
}
187
#endif
188
}
189
190
bool ChatMenu::SubviewFocused(UI::View *view) {
191
if (!AnchorLayout::SubviewFocused(view))
192
return false;
193
194
promptInput_ = true;
195
return true;
196
}
197
198
void ChatMenu::Close() {
199
SetVisibility(UI::V_GONE);
200
}
201
202