From 4ac6ee4bf353e5518cfae954fe10aa625d05e803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 2 Oct 2024 00:59:57 +0200 Subject: [PATCH] Chat: Cleanup the selection of chat input method, allow in-ui popup (what iOS needs). --- UI/ChatScreen.cpp | 54 +++++++++++++++++++++++++++++++++++------------ UI/ChatScreen.h | 11 +++++++--- UI/EmuScreen.cpp | 7 +++++- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/UI/ChatScreen.cpp b/UI/ChatScreen.cpp index 3445767c772c..b71df0177869 100644 --- a/UI/ChatScreen.cpp +++ b/UI/ChatScreen.cpp @@ -15,6 +15,7 @@ #include "Core/System.h" #include "Core/HLE/proAdhoc.h" #include "UI/ChatScreen.h" +#include "UI/PopupScreens.h" void ChatMenu::CreateContents(UI::ViewGroup *parent) { using namespace UI; @@ -22,14 +23,20 @@ void ChatMenu::CreateContents(UI::ViewGroup *parent) { LinearLayout *outer = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT,400)); scroll_ = outer->Add(new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 1.0))); LinearLayout *bottom = outer->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT))); -#if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) || defined(SDL) - chatEdit_ = bottom->Add(new TextEdit("", n->T("Chat message"), n->T("Chat Here"), new LinearLayoutParams(1.0))); - chatEdit_->OnEnter.Handle(this, &ChatMenu::OnSubmit); -#elif PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(IOS) - bottom->Add(new Button(n->T("Chat Here"),new LayoutParams(FILL_PARENT, WRAP_CONTENT)))->OnClick.Handle(this, &ChatMenu::OnSubmit); - bottom->Add(new Button(n->T("Send")))->OnClick.Handle(this, &ChatMenu::OnSubmit); -#endif + chatButton_ = nullptr; + chatEdit_ = nullptr; + chatVert_ = nullptr; + + if (System_GetPropertyInt(SYSPROP_DEVICE_TYPE) == DEVICE_TYPE_DESKTOP) { + // We have direct keyboard input. + chatEdit_ = bottom->Add(new TextEdit("", n->T("Chat message"), n->T("Chat Here"), new LinearLayoutParams(1.0))); + chatEdit_->OnEnter.Handle(this, &ChatMenu::OnSubmitMessage); + } else { + // If we have a native input box, like on Android, or at least we can do a popup text input with our UI... + chatButton_ = bottom->Add(new Button(n->T("Chat message"), new LayoutParams(FILL_PARENT, WRAP_CONTENT))); + chatButton_->OnClick.Handle(this, &ChatMenu::OnAskForChatMessage); + } if (g_Config.bEnableQuickChat) { LinearLayout *quickChat = outer->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT))); @@ -89,18 +96,37 @@ void ChatMenu::CreateSubviews(const Bounds &screenBounds) { UpdateChat(); } -UI::EventReturn ChatMenu::OnSubmit(UI::EventParams &e) { -#if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) || (defined(SDL) && !PPSSPP_PLATFORM(SWITCH)) +UI::EventReturn ChatMenu::OnSubmitMessage(UI::EventParams &e) { std::string chat = chatEdit_->GetText(); chatEdit_->SetText(""); chatEdit_->SetFocus(); sendChat(chat); -#elif PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(SWITCH) || PPSSPP_PLATFORM(IOS) + return UI::EVENT_DONE; +} + +UI::EventReturn ChatMenu::OnAskForChatMessage(UI::EventParams &e) { auto n = GetI18NCategory(I18NCat::NETWORKING); - System_InputBoxGetString(token_, n->T("Chat"), "", false, [](const std::string &value, int) { - sendChat(value); - }); -#endif + + using namespace UI; + + if (System_GetPropertyBool(SYSPROP_HAS_TEXT_INPUT_DIALOG)) { + System_InputBoxGetString(token_, n->T("Chat"), "", false, [](const std::string &value, int) { + sendChat(value); + }); + } else { + // We need to pop up a UI inputbox. + messageTemp_.clear(); + TextEditPopupScreen *popupScreen = new TextEditPopupScreen(&messageTemp_, "", n->T("Chat message"), 256); + if (System_GetPropertyBool(SYSPROP_KEYBOARD_IS_SOFT)) { + popupScreen->SetAlignTop(true); + } + popupScreen->OnChange.Add([=](UI::EventParams &e) { + sendChat(messageTemp_); + return UI::EVENT_DONE; + }); + popupScreen->SetPopupOrigin(chatButton_); + screenManager_->push(popupScreen); + } return UI::EVENT_DONE; } diff --git a/UI/ChatScreen.h b/UI/ChatScreen.h index 5c2bee05324e..26e0d924f136 100644 --- a/UI/ChatScreen.h +++ b/UI/ChatScreen.h @@ -5,8 +5,8 @@ class ChatMenu : public UI::AnchorLayout { public: - ChatMenu(int token, const Bounds &screenBounds, UI::LayoutParams *lp = nullptr) - : UI::AnchorLayout(lp), token_(token) { + ChatMenu(int token, const Bounds &screenBounds, ScreenManager *screenManager, UI::LayoutParams *lp = nullptr) + : UI::AnchorLayout(lp), screenManager_(screenManager), token_(token) { CreateSubviews(screenBounds); } void Update() override; @@ -25,7 +25,9 @@ class ChatMenu : public UI::AnchorLayout { void CreateContents(UI::ViewGroup *parent); void UpdateChat(); - UI::EventReturn OnSubmit(UI::EventParams &e); + UI::EventReturn OnAskForChatMessage(UI::EventParams &e); + + UI::EventReturn OnSubmitMessage(UI::EventParams &e); UI::EventReturn OnQuickChat1(UI::EventParams &e); UI::EventReturn OnQuickChat2(UI::EventParams &e); UI::EventReturn OnQuickChat3(UI::EventParams &e); @@ -38,9 +40,12 @@ class ChatMenu : public UI::AnchorLayout { UI::ScrollView *scroll_ = nullptr; UI::LinearLayout *chatVert_ = nullptr; UI::ViewGroup *box_ = nullptr; + ScreenManager *screenManager_; int chatChangeID_ = 0; bool toBottom_ = true; bool promptInput_ = false; int token_; + std::string messageTemp_; + UI::Button *chatButton_ = nullptr; }; diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index cafc6b72930b..90a624bd20c3 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -468,6 +468,11 @@ EmuScreen::~EmuScreen() { } void EmuScreen::dialogFinished(const Screen *dialog, DialogResult result) { + if (std::string_view(dialog->tag()) == "TextEditPopup") { + // Chat message finished. + return; + } + // TODO: improve the way with which we got commands from PauseMenu. // DR_CANCEL/DR_BACK means clicked on "continue", DR_OK means clicked on "back to menu", // DR_YES means a message sent to PauseMenu by System_PostUIMessage. @@ -1055,7 +1060,7 @@ void EmuScreen::CreateViews() { root_->Add(btn)->OnClick.Handle(this, &EmuScreen::OnChat); chatButton_ = btn; } - chatMenu_ = root_->Add(new ChatMenu(GetRequesterToken(), screenManager()->getUIContext()->GetBounds(), new LayoutParams(FILL_PARENT, FILL_PARENT))); + chatMenu_ = root_->Add(new ChatMenu(GetRequesterToken(), screenManager()->getUIContext()->GetBounds(), screenManager(), new LayoutParams(FILL_PARENT, FILL_PARENT))); chatMenu_->SetVisibility(UI::V_GONE); } else { chatButton_ = nullptr;