Files
neoLegacy/Minecraft.Client/Common/UI/UIScene_InGameFriendsMenu.cpp
T

382 lines
12 KiB
C++

#include "stdafx.h"
#include "UI.h"
#include "UIScene_InGameFriendsMenu.h"
#include "UICustomStrings.h"
#include "../../Minecraft.h"
#if defined(_WINDOWS64) || defined(__linux__)
#include "../../Windows64/KeyboardMouseInput.h"
bool Win64_IsOnlineUsernameActive();
#endif
UIScene_InGameFriendsMenu::UIScene_InGameFriendsMenu(int iPad, void *initData, UILayer *parentLayer) : UIScene(iPad, parentLayer)
{
initialiseMovie();
m_bNeedsPopulate = true;
m_bIgnoreInput = false;
m_bPendingAddFriend = false;
m_nextRefreshMs = 0;
m_selection = eItem_AddFriend;
if (isOnline())
SocialOnlineService::RefreshFriends();
doHorizontalResizeCheck();
}
wstring UIScene_InGameFriendsMenu::getMoviePath()
{
return L"MultilistMenu";
}
bool UIScene_InGameFriendsMenu::isOnline()
{
#if defined(_WINDOWS64) || defined(__linux__)
return SocialOnlineService::IsLinked() && Win64_IsOnlineUsernameActive();
#else
return false;
#endif
}
void UIScene_InGameFriendsMenu::populateList()
{
bool alreadyRegistered = false;
for (size_t i = 0; i < m_controls.size(); ++i)
{
if (m_controls[i] == &m_multiList)
alreadyRegistered = true;
}
m_multiList.setupControl(this, m_rootPath, "MultiList");
if (!alreadyRegistered)
m_controls.push_back(&m_multiList);
m_multiList.clearList();
m_multiList.init(eControl_MultiList);
bool hasData = isOnline() && SocialOnlineService::HasFriendsData();
m_friends = hasData ? SocialOnlineService::GetFriends() : vector<SocialOnlineService::Person>();
m_incoming = hasData ? SocialOnlineService::GetIncomingRequests() : vector<SocialOnlineService::Person>();
m_pendingNames.clear();
for (const auto &person : m_incoming)
m_pendingNames.push_back(person.username);
m_multiList.AddNewLabel(L"Friends");
m_multiList.AddNewButton(L"Add Friend", eItem_AddFriend);
wstring pendingLabel = L"Pending Requests";
if (!m_pendingNames.empty())
pendingLabel += L" (" + std::to_wstring(m_pendingNames.size()) + L")";
m_multiList.AddNewButton(pendingLabel, eItem_PendingRequests);
bool selectionValid = m_selection == eItem_AddFriend || m_selection == eItem_PendingRequests;
if (!isOnline())
{
m_multiList.AddNewLabel(L"Online profile required");
}
else if (!hasData)
{
m_multiList.AddNewLabel(SocialOnlineService::IsConnecting() || SocialOnlineService::HasSession() ? L"Loading friends..." : L"Social server unreachable");
}
else
{
size_t onlineCount = 0;
for (const auto &person : m_friends)
{
if (!person.status.empty() && person.status != L"offline")
++onlineCount;
}
m_multiList.AddNewLabel(std::to_wstring(onlineCount) + L"/" + std::to_wstring(m_friends.size()) + L" online");
for (size_t i = 0; i < m_friends.size(); ++i)
{
int id = eItem_FriendBase + static_cast<int>(i);
m_multiList.AddNewButton(m_friends[i].username + L" - " + SocialOnlineService::StatusLabel(m_friends[i].status), id);
if (id == m_selection)
selectionValid = true;
}
}
if (!selectionValid)
m_selection = eItem_AddFriend;
IggyName funcDoVert = registerFastName(L"DoVerticalResizeCheck");
IggyName funcHideDesc = registerFastName(L"HideDescription");
IggyDataValue result;
IggyPlayerCallMethodRS(getMovie(), &result, m_rootPath, funcDoVert, 0, nullptr);
doHorizontalResizeCheck();
IggyPlayerCallMethodRS(getMovie(), &result, m_rootPath, funcHideDesc, 0, nullptr);
m_multiList.HighlightItem(m_selection);
updateTooltips();
}
void UIScene_InGameFriendsMenu::tick()
{
SocialOnlineService::Event serviceEvent;
while (SocialOnlineService::PopEvent(serviceEvent))
handleServiceEvent(serviceEvent);
if (isOnline() && SocialOnlineService::HasSession() && GetTickCount64() >= m_nextRefreshMs)
{
m_nextRefreshMs = GetTickCount64() + 20000;
SocialOnlineService::RefreshFriends();
}
if (m_bPendingAddFriend)
{
m_bPendingAddFriend = false;
sendFriendRequest(m_pendingFriendName);
}
if (m_bNeedsPopulate && hasMovie() && (bHasFocus || !m_bPopulatedOnce))
{
m_bNeedsPopulate = false;
m_bPopulatedOnce = true;
populateList();
}
UIScene::tick();
}
void UIScene_InGameFriendsMenu::handleServiceEvent(const SocialOnlineService::Event &e)
{
switch (e.type)
{
case SocialOnlineService::eEvent_FriendsUpdated:
case SocialOnlineService::eEvent_SessionReady:
m_bNeedsPopulate = true;
break;
case SocialOnlineService::eEvent_FriendRequestResult:
if (e.status == L"sent")
showMessage(L"Friend Request Sent", L"Friend request sent to " + e.name + L".");
else if (e.status == L"accepted")
showMessage(L"Friend Added", L"You and " + e.name + L" are now friends.");
else if (e.status == L"already_sent")
showMessage(L"Friend Request Sent", L"You have already sent " + e.name + L" a friend request.");
else if (e.status == L"already_friends")
showMessage(L"Already Friends", L"You are already friends with " + e.name + L".");
else if (e.status == L"not_accepting")
showMessage(L"Not Accepting Requests", e.name + L" is not accepting friend requests.");
else if (e.status == L"self")
showMessage(L"Invalid Friend", L"You cannot add yourself as a friend.");
else if (e.status == L"you_blocked")
showMessage(L"Player Blocked", L"You have blocked " + e.name + L". Unblock them with /friend unblock in Discord first.");
else
showMessage(L"Invalid Friend", L"No player with that username could be found.");
m_bNeedsPopulate = true;
break;
case SocialOnlineService::eEvent_InviteResult:
if (e.status == L"sent")
showMessage(L"Invite Sent", L"Invited " + e.name + L" to your world.");
else if (e.status == L"sent_offline")
showMessage(L"Invite Sent", e.name + L" is offline. They'll get a Discord message about your invite.");
else if (e.status == L"not_hosting")
showMessage(L"Can't Invite", L"Host a world with Online Game turned on to invite friends.");
else if (e.status == L"not_friends")
showMessage(L"Can't Invite", L"You can only invite your friends.");
else
showMessage(L"Can't Invite", L"The invite could not be sent. Try again in a moment.");
break;
case SocialOnlineService::eEvent_ActionFailed:
if (e.status == L"unreachable")
showMessage(L"Server Unreachable", L"Could not reach the social server. Try again in a moment.");
else
showMessage(L"Reconnecting", L"Your online session expired. Reconnecting, try again in a moment.");
break;
default:
break;
}
}
void UIScene_InGameFriendsMenu::showMessage(const wchar_t *title, const wstring &body)
{
UINT options[1];
options[0] = IDS_CONFIRM_OK;
ui.RequestAlertMessage(UICustomStrings::Get(title), UICustomStrings::Get(body.c_str()), options, 1, m_iPad);
}
void UIScene_InGameFriendsMenu::openAddFriendKeyboard()
{
#if defined(_WINDOWS64) || defined(__linux__)
UIKeyboardInitData kbData;
kbData.title = L"Friend's Username";
kbData.defaultText = L"";
kbData.maxChars = 16;
kbData.callback = &UIScene_InGameFriendsMenu::AddFriendKeyboardCallback;
kbData.lpParam = this;
kbData.pcMode = g_KBMInput.IsKBMActive();
m_bIgnoreInput = true;
ui.NavigateToScene(m_iPad, eUIScene_Keyboard, &kbData);
#endif
}
int UIScene_InGameFriendsMenu::AddFriendKeyboardCallback(LPVOID lpParam, const bool bRes)
{
UIScene_InGameFriendsMenu *pClass = static_cast<UIScene_InGameFriendsMenu *>(lpParam);
pClass->m_bIgnoreInput = false;
if (bRes)
{
uint16_t text[64];
ZeroMemory(text, sizeof(text));
#if defined(_WINDOWS64) || defined(__linux__)
Win64_GetKeyboardText(text, 64);
#endif
wstring name;
for (int i = 0; i < 63 && text[i]; ++i)
name += static_cast<wchar_t>(text[i]);
pClass->m_pendingFriendName = name;
pClass->m_bPendingAddFriend = true;
}
return 0;
}
void UIScene_InGameFriendsMenu::sendFriendRequest(const wstring &name)
{
bool valid = name.length() >= 3 && name.length() <= 16;
for (wchar_t c : name)
{
if (!((c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z') || (c >= L'0' && c <= L'9') || c == L'_'))
valid = false;
}
if (!valid)
{
ui.PlayUISFX(eSFX_CraftFail);
showMessage(L"Invalid Friend", L"No player with that username could be found.");
return;
}
SocialOnlineService::SendFriendRequest(name);
}
void UIScene_InGameFriendsMenu::FriendRemoved(void *param, int friendIndex)
{
UIScene_InGameFriendsMenu *pClass = static_cast<UIScene_InGameFriendsMenu *>(param);
if (friendIndex >= 0 && friendIndex < static_cast<int>(pClass->m_friends.size()))
SocialOnlineService::RemoveFriend(pClass->m_friends[friendIndex].onlineId);
pClass->m_bNeedsPopulate = true;
}
void UIScene_InGameFriendsMenu::FriendBlocked(void *param, int friendIndex)
{
UIScene_InGameFriendsMenu *pClass = static_cast<UIScene_InGameFriendsMenu *>(param);
if (friendIndex >= 0 && friendIndex < static_cast<int>(pClass->m_friends.size()))
SocialOnlineService::BlockUser(pClass->m_friends[friendIndex].onlineId);
pClass->m_bNeedsPopulate = true;
}
void UIScene_InGameFriendsMenu::FriendInvited(void *param, int friendIndex)
{
UIScene_InGameFriendsMenu *pClass = static_cast<UIScene_InGameFriendsMenu *>(param);
if (friendIndex >= 0 && friendIndex < static_cast<int>(pClass->m_friends.size()))
SocialOnlineService::SendInvite(pClass->m_friends[friendIndex].onlineId);
}
void UIScene_InGameFriendsMenu::RequestResponded(void *param, const wstring &name, bool accept)
{
UIScene_InGameFriendsMenu *pClass = static_cast<UIScene_InGameFriendsMenu *>(param);
for (const auto &person : pClass->m_incoming)
{
if (person.username == name)
{
SocialOnlineService::RespondToRequest(person.onlineId, accept);
break;
}
}
pClass->m_bNeedsPopulate = true;
}
void UIScene_InGameFriendsMenu::updateTooltips()
{
ui.SetTooltips(m_iPad, IDS_TOOLTIPS_SELECT, IDS_TOOLTIPS_BACK);
}
void UIScene_InGameFriendsMenu::updateComponents()
{
Minecraft *pMinecraft = Minecraft::GetInstance();
m_parentLayer->showComponent(m_iPad, eUIComponent_Panorama, pMinecraft == nullptr || pMinecraft->level == nullptr);
m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, false);
}
void UIScene_InGameFriendsMenu::handleReload()
{
UIScene::handleReload();
m_bNeedsPopulate = true;
}
void UIScene_InGameFriendsMenu::handleGainFocus(bool navBack)
{
UIScene::handleGainFocus(navBack);
if (navBack)
{
if (isOnline())
SocialOnlineService::RefreshFriends();
m_bNeedsPopulate = true;
}
}
void UIScene_InGameFriendsMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled)
{
if (m_bIgnoreInput)
return;
ui.AnimateKeyPress(iPad, key, repeat, pressed, released);
switch (key)
{
case ACTION_MENU_CANCEL:
if (pressed)
navigateBack();
handled = true;
break;
case ACTION_MENU_OK:
case ACTION_MENU_UP:
case ACTION_MENU_DOWN:
case ACTION_MENU_PAGEUP:
case ACTION_MENU_PAGEDOWN:
sendInputToMovie(key, repeat, pressed, released);
handled = true;
break;
}
}
void UIScene_InGameFriendsMenu::handleFocusChange(F64 controlId, F64 childId)
{
m_selection = static_cast<int>(childId);
}
void UIScene_InGameFriendsMenu::handlePress(F64 controlId, F64 childId)
{
if (m_bIgnoreInput)
return;
int id = static_cast<int>(childId);
m_selection = id;
ui.PlayUISFX(eSFX_Press);
if (id == eItem_AddFriend)
{
if (!isOnline())
{
showMessage(L"Online Profile Required", L"Link your Discord account and turn on your online profile in Social on the main menu to add friends.");
return;
}
openAddFriendKeyboard();
return;
}
if (id == eItem_PendingRequests)
{
if (!isOnline())
{
showMessage(L"Online Profile Required", L"Link your Discord account and turn on your online profile in Social on the main menu to see friend requests.");
return;
}
m_pendingData.requests = &m_pendingNames;
m_pendingData.onRespond = &UIScene_InGameFriendsMenu::RequestResponded;
m_pendingData.param = this;
ui.NavigateToScene(m_iPad, eUIScene_PendingRequestsMenu, &m_pendingData);
return;
}
int friendIndex = id - eItem_FriendBase;
if (friendIndex >= 0 && friendIndex < static_cast<int>(m_friends.size()))
{
m_friendMenuData.name = m_friends[friendIndex].username;
m_friendMenuData.onlineId = m_friends[friendIndex].onlineId;
m_friendMenuData.status = m_friends[friendIndex].status;
m_friendMenuData.friendOnline = false;
m_friendMenuData.friendInWorld = false;
m_friendMenuData.friendIndex = friendIndex;
m_friendMenuData.onRemove = &UIScene_InGameFriendsMenu::FriendRemoved;
m_friendMenuData.onBlock = &UIScene_InGameFriendsMenu::FriendBlocked;
m_friendMenuData.onInvite = &UIScene_InGameFriendsMenu::FriendInvited;
m_friendMenuData.param = this;
ui.NavigateToScene(m_iPad, eUIScene_FriendMenu, &m_friendMenuData);
}
}