Skip to content

Commit

Permalink
[multiplayer] Make multiplayer respect back btn
Browse files Browse the repository at this point in the history
  • Loading branch information
itszn committed May 18, 2021
1 parent 22824b0 commit eef36f8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Main/include/MultiplayerScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MultiplayerScreen : public IAsyncLoadableApplicationTickable
void OnKeyReleased(SDL_Scancode code) override;
void MousePressed(MouseButton button);


void OnSuspend() override;
void OnRestore() override;
bool AsyncLoad() override;
Expand Down Expand Up @@ -149,6 +150,7 @@ class MultiplayerScreen : public IAsyncLoadableApplicationTickable
void m_render(float deltaTime);

void m_joinRoomWithToken();
bool m_returnToMainList();

void OnSearchStatusUpdated(String status);

Expand Down
107 changes: 71 additions & 36 deletions Main/src/MultiplayerScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,51 @@ void MultiplayerScreen::MousePressed(MouseButton button)
lua_settop(m_lua, 0);
}

bool MultiplayerScreen::m_returnToMainList()
{
if (m_screenState == MultiplayerScreenState::ROOM_LIST)
return false;

// Exiting from name setup
if (m_userName == "")
{
m_suspended = true;
g_application->RemoveTickable(this);
return true;
}

if (m_screenState == MultiplayerScreenState::IN_ROOM)
{
nlohmann::json packet;
packet["topic"] = "room.leave";
m_tcp.SendJSON(packet);
}
else
{
nlohmann::json packet;
packet["topic"] = "server.rooms";
m_tcp.SendJSON(packet);
}

if (m_textInput->active)
m_textInput->SetActive(false);

if (m_screenState == MultiplayerScreenState::IN_ROOM) {
m_chatOverlay->AddMessage("You left the lobby", 207, 178, 41);
}

m_chatOverlay->EnableOpeningChat();

m_screenState = MultiplayerScreenState::ROOM_LIST;
m_stopPreview();
lua_pushstring(m_lua, "roomList");
lua_setglobal(m_lua, "screenState");
g_application->DiscordPresenceMulti("", 0, 0, "");
m_roomId = "";
m_hasSelectedMap = false;
return true;
}

void MultiplayerScreen::Tick(float deltaTime)
{
// Tick the tcp socket even if we are suspended
Expand Down Expand Up @@ -1065,43 +1110,23 @@ void MultiplayerScreen::OnKeyPressed(SDL_Scancode code)
}
else if (code == SDL_SCANCODE_ESCAPE)
{
if (m_screenState != MultiplayerScreenState::ROOM_LIST)
{

// Exiting from name setup
if (m_userName == "")
{
m_suspended = true;
g_application->RemoveTickable(this);
return;
}
int backScancode = g_gameConfig.GetInt(GameConfigKeys::Key_Back);
// SDL_Keycode k = SDL_GetKeyFromScancode(g_gameConfig.GetInt(GameConfigKeys::Key_Back));

if (m_screenState == MultiplayerScreenState::IN_ROOM)
{
nlohmann::json packet;
packet["topic"] = "room.leave";
m_tcp.SendJSON(packet);
}
else
if (g_gameConfig.GetEnum<Enum_InputDevice>(GameConfigKeys::ButtonInputDevice) != InputDevice::Keyboard
|| backScancode != SDL_SCANCODE_ESCAPE)
{
switch (m_screenState)
{
nlohmann::json packet;
packet["topic"] = "server.rooms";
m_tcp.SendJSON(packet);
// Escape works for text editing
case MultiplayerScreenState::JOIN_PASSWORD:
case MultiplayerScreenState::NEW_ROOM_NAME:
case MultiplayerScreenState::NEW_ROOM_PASSWORD:
case MultiplayerScreenState::SET_USERNAME:
m_returnToMainList();
default:
break;
}

if (m_textInput->active)
m_textInput->SetActive(false);

m_chatOverlay->AddMessage("You left the lobby", 207, 178, 41);
m_chatOverlay->EnableOpeningChat();

m_screenState = MultiplayerScreenState::ROOM_LIST;
m_stopPreview();
lua_pushstring(m_lua, "roomList");
lua_setglobal(m_lua, "screenState");
g_application->DiscordPresenceMulti("", 0, 0, "");
m_roomId = "";
m_hasSelectedMap = false;
}
}
else if (code == SDL_SCANCODE_RETURN)
Expand Down Expand Up @@ -1147,8 +1172,15 @@ void MultiplayerScreen::m_OnButtonPressed(Input::Button buttonCode)
if (IsSuspended() || m_settDiag.IsActive())
return;

if (g_gameConfig.GetEnum<Enum_InputDevice>(GameConfigKeys::ButtonInputDevice) == InputDevice::Keyboard && (m_textInput->active || m_chatOverlay->IsOpen()))
return;
if (g_gameConfig.GetEnum<Enum_InputDevice>(GameConfigKeys::ButtonInputDevice) == InputDevice::Keyboard)
{
if (m_chatOverlay->IsOpen())
return;

// Allow Button Back while text editing
if (m_textInput->active && buttonCode != Input::Button::Back)
return;
}

m_timeSinceButtonPressed[buttonCode] = 0;

Expand All @@ -1170,6 +1202,9 @@ void MultiplayerScreen::m_OnButtonPressed(Input::Button buttonCode)
return;
}
break;
case Input::Button::Back:
if (m_returnToMainList())
return;
default:
break;
}
Expand Down
43 changes: 27 additions & 16 deletions bin/skins/Default/scripts/multiplayerscreen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,13 @@ end
-- Toggle hard gauage
function toggle_hard()
Tcp.SendLine(json.encode({topic="user.hard.toggle"}))
hard_mode = not hard_mode
end

-- Toggle hard gauage
function toggle_mirror()
Tcp.SendLine(json.encode({topic="user.mirror.toggle"}))
mirror_mode = not mirror_mode
end

function new_room()
Expand Down Expand Up @@ -835,6 +837,23 @@ function join_room(room)
end
end

function go_back()
if screenState == "roomList" then
did_exit = true;
mpScreen.Exit();
return
end

-- Reset room data
screenState = "roomList" -- have to update here
selected_room = nil;
rooms = {};
selected_song = nil
selected_song_index = 1;
jacket = 0;
end


-- Handle button presses to advance the UI
button_released = function(button)
if button == game.BUTTON_STA then
Expand Down Expand Up @@ -872,26 +891,18 @@ button_released = function(button)
if button == game.BUTTON_FXR then
toggle_mirror();
end
end

-- Handle the escape key around the UI
function key_pressed(key)
if key == 27 then --escape pressed
if button == game.BUTTON_BCK then
go_back();
end
if button == game.BUTTON_BTA then
if screenState == "roomList" then
did_exit = true;
mpScreen.Exit();
return
new_room()
end

-- Reset room data
screenState = "roomList" -- have to update here
selected_room = nil;
rooms = {};
selected_song = nil
selected_song_index = 1;
jacket = 0;
end
end

-- Handle the keys around the UI
function key_pressed(key)
end

-- Handle mouse clicks in the UI
Expand Down

0 comments on commit eef36f8

Please sign in to comment.