/// <summary> /// Updates invite panel IMGUI. /// </summary> private void UpdateInvitePanel() { if (!IsInvitePanelVisible()) { GUI.color = Color.white; GUI.Label(new Rect(0, Screen.height - 100, 200.0f, 20.0f), "[ESCAPE] - Invite friend"); return; } const float invitePanelHeight = 400.0f; const float invitePanelWidth = 300.0f; const float rowHeight = 20.0f; Rect invitePanelRect = new Rect(Screen.width - invitePanelWidth - 10.0f, Screen.height / 2 - invitePanelHeight / 2, invitePanelWidth, 20.0f); // Draw header GUI.color = new Color(1.0f, 0.5f, 0.0f, 0.8f); IMGUIUtils.DrawPlainColorRect(invitePanelRect); GUI.color = Color.white; invitePanelRect.x += 2.0f; GUI.Label(invitePanelRect, "Invite friend"); invitePanelRect.x -= 2.0f; // Draw contents invitePanelRect.y += 21.0f; invitePanelRect.height = invitePanelHeight; GUI.color = new Color(0.0f, 0.0f, 0.0f, 0.8f); IMGUIUtils.DrawPlainColorRect(invitePanelRect); GUI.color = new Color(1.0f, 0.5f, 0.0f, 0.8f); int onlineFriendsCount = onlineFriends.Count; invitePanelRect.height -= 2.0f; if (onlineFriendsCount == 0) { GUI.color = Color.white; var previousAlignment = GUI.skin.label.alignment; GUI.skin.label.alignment = TextAnchor.MiddleCenter; bool playerIsOffline = Steamworks.SteamFriends.GetPersonaState() == Steamworks.EPersonaState.k_EPersonaStateOffline; if (playerIsOffline) { GUI.Label(invitePanelRect, "You cannot invite friends while in steam offline mode.\n\nSwitch back your steam status to online to be able to invite players."); } else { GUI.Label(invitePanelRect, "You don't have any friends online."); } GUI.skin.label.alignment = previousAlignment; return; } friendsScrollViewPos = GUI.BeginScrollView(invitePanelRect, friendsScrollViewPos, new Rect(0, 0, invitePanelWidth - 20.0f, 20.0f * onlineFriendsCount)); int firstVisibleFriendId = (int)(friendsScrollViewPos.y / rowHeight); int maxVisibleFriends = (int)(invitePanelHeight / rowHeight); int lastIndex = firstVisibleFriendId + maxVisibleFriends + 1; if (lastIndex > onlineFriendsCount) { lastIndex = onlineFriendsCount; } for (int i = firstVisibleFriendId; i < lastIndex; ++i) { FriendEntry friend = onlineFriends[i]; if (friend.playingMSC) { GUI.color = Color.green; } else { GUI.color = Color.white; } Rect friendRect = new Rect(2, 1 + rowHeight * i, 200.0f, rowHeight); GUI.Label(friendRect, friend.name); friendRect.x += 180.0f; friendRect.width = 100.0f; Steamworks.CSteamID friendSteamId = friend.steamId; if (invitedFriendSteamId == friendSteamId) { GUI.Label(friendRect, String.Format("INVITED! ({0:F1}s)", inviteCooldown)); continue; } if (inviteCooldown > 0.0f) { continue; } if (GUI.Button(friendRect, "Invite")) { if (netManager.InviteToMyLobby(friendSteamId)) { invitedFriendSteamId = friendSteamId; inviteCooldown = INVITE_COOLDOWN; } else { UI.MPGUI.Instance.ShowMessageBox("Failed to invite friend due to steam error."); } } } GUI.EndScrollView(); }
/// <summary> /// Draw network statistics. /// </summary> public void Draw() { GUI.color = Color.white; const int WINDOW_WIDTH = 300; const int WINDOW_HEIGHT = 600; Rect statsWindowRect = new Rect(Screen.width - WINDOW_WIDTH - 10, Screen.height - WINDOW_HEIGHT - 10, WINDOW_WIDTH, WINDOW_HEIGHT); GUI.Window(666, statsWindowRect, (int window) => { // Draw traffic graph title. var rct = new Rect(10, 20, 200, 25); GUI.Label(rct, $"Traffic graph (last {HISTORY_SIZE} frames):"); rct.y += 25; var graphRect = new Rect(rct.x, rct.y, WINDOW_WIDTH - 20, 100); // Draw graph background. GUI.color = new Color(0.0f, 0.0f, 0.0f, 0.35f); IMGUIUtils.DrawPlainColorRect(graphRect); // Draw the graph itself. graphRect.x += statsWindowRect.x; graphRect.y += statsWindowRect.y; DrawGraph(graphRect); GUI.color = Color.white; rct.y += 5; rct.x += 5; IMGUIUtils.DrawSmallLabel($"{FormatBytes(maxBytesSentInHistory)} sent/frame", rct, Color.red, true); rct.y += 12; IMGUIUtils.DrawSmallLabel( $"{FormatBytes(maxBytesReceivedInHistory)} recv/frame", rct, Color.green, true); rct.y -= 12 - 5; rct.x -= 5; rct.y += graphRect.height; rct.height = 20; // Draw separator GUI.color = Color.black; IMGUIUtils.DrawPlainColorRect(new Rect(0, rct.y, WINDOW_WIDTH, 2)); rct.y += 2; // Draw stats background GUI.color = new Color(0.0f, 0.0f, 0.0f, 0.5f); IMGUIUtils.DrawPlainColorRect( new Rect(0, rct.y, WINDOW_WIDTH, WINDOW_HEIGHT - rct.y)); // Draw statistics DrawStatHelper(ref rct, "packetsSendTotal", packetsSendTotal); DrawStatHelper(ref rct, "packetsReceivedTotal", packetsReceivedTotal); DrawStatHelper(ref rct, "packetsSendLastFrame", packetsSendLastFrame, 1000); DrawStatHelper( ref rct, "packetsReceivedLastFrame", packetsReceivedLastFrame, 1000); DrawStatHelper( ref rct, "packetsSendCurrentFrame", packetsSendCurrentFrame, 1000); DrawStatHelper(ref rct, "packetsReceivedCurrentFrame", packetsReceivedCurrentFrame, 1000); DrawStatHelper(ref rct, "bytesSendTotal", bytesSentTotal, -1, true); DrawStatHelper(ref rct, "bytesReceivedTotal", bytesReceivedTotal, -1, true); DrawStatHelper( ref rct, "bytesSendLastFrame", bytesSentLastFrame, 1000, true); DrawStatHelper( ref rct, "bytesReceivedLastFrame", bytesReceivedLastFrame, 1000, true); DrawStatHelper( ref rct, "bytesSendCurrentFrame", bytesSentCurrentFrame, 1000, true); DrawStatHelper(ref rct, "bytesReceivedCurrentFrame", bytesReceivedCurrentFrame, 1000, true); // Draw separator rct.y += 2; GUI.color = Color.black; IMGUIUtils.DrawPlainColorRect(new Rect(0, rct.y, WINDOW_WIDTH, 2)); rct.y += 2; // Draw P2P session state. DrawTextHelper(ref rct, "Steam session state:", ""); Steamworks.P2PSessionState_t sessionState = new Steamworks.P2PSessionState_t(); if (netManager.GetP2PSessionState(out sessionState)) { DrawTextHelper( ref rct, "Is Connecting", sessionState.m_bConnecting.ToString()); DrawTextHelper(ref rct, "Is connection active", sessionState.m_bConnectionActive == 0 ? "no" : "yes"); DrawTextHelper(ref rct, "Using relay?", sessionState.m_bConnectionActive == 0 ? "no" : "yes"); DrawTextHelper(ref rct, "Session error", Utils.P2PSessionErrorToString( (Steamworks.EP2PSessionError)sessionState.m_eP2PSessionError)); DrawTextHelper(ref rct, "Bytes queued for send", FormatBytes(sessionState.m_nBytesQueuedForSend)); DrawTextHelper(ref rct, "Packets queued for send", sessionState.m_nPacketsQueuedForSend.ToString()); uint uip = sessionState.m_nRemoteIP; string ip = string.Format("{0}.{1}.{2}.{3}", (uip >> 24) & 0xff, (uip >> 16) & 0xff, (uip >> 8) & 0xff, uip & 0xff); DrawTextHelper(ref rct, "Remote ip", ip); DrawTextHelper( ref rct, "Remote port", sessionState.m_nRemotePort.ToString()); } else { DrawTextHelper(ref rct, "Session inactive.", ""); } }, "Network statistics"); }