Skip to content

Commit

Permalink
Mute Unmute -> turn off local audio source
Browse files Browse the repository at this point in the history
This shout the local audio source of the corresponding remote player
  • Loading branch information
sbanca committed Jan 20, 2025
1 parent ab58699 commit 3c1bd2a
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 2,090 deletions.
2,276 changes: 241 additions & 2,035 deletions Assets/Prefabs/PopUps/PopUpWindow_RoomOptions.prefab

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions Assets/Scripts/GUI/MultiplayerRoomOptionsPanelButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2023 The Open Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using UnityEngine;

namespace TiltBrush
{
public class MultiplayerRoomOptionsPanelButton : OptionButton
{
[SerializeField] private bool m_CommandIgnored = false;
[HideInInspector] public int playerId;

override protected void OnButtonPressed()
{

MultiplayerRoomOptionsPopUpWindow popup = m_Manager.GetComponent<MultiplayerRoomOptionsPopUpWindow>();

// For some circumstances on mobile, we want to ignore the command, but still
// notify the popup that we were pressed. Which happens below.
if (!m_CommandIgnored)
{
if (m_RequiresPopup & m_Command == SketchControlsScript.GlobalCommands.MuteUserInMultiplayer)
{

}

base.OnButtonPressed();
}


Debug.Assert(popup != null);
popup.OnMultiplayerRoomOptionsPopUpWindowButtonPressed(this);
}
}
} // namespace TiltBrush
11 changes: 11 additions & 0 deletions Assets/Scripts/GUI/MultiplayerRoomOptionsPanelButton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions Assets/Scripts/GUI/MultiplayerRoomOptionsPopUpWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
using OpenBrush.Multiplayer;
using System;
using System.Collections.Generic;
using OpenBrush;

namespace TiltBrush
{

public class MultiplayerRoomOptionsPopUpWindow : PopUpWindow
{
public GameObject m_playerGuiPrefab;
Expand Down Expand Up @@ -145,8 +145,20 @@ private void ClearGuiPrefabsList()
m_instantiatedGuiPrefabs.Clear();
}


#region player list
public void OnMultiplayerRoomOptionsPopUpWindowButtonPressed(MultiplayerRoomOptionsPanelButton button)
{
switch (button.m_Command)
{
case SketchControlsScript.GlobalCommands.Null:
break;
case SketchControlsScript.GlobalCommands.MuteUserInMultiplayer:
MultiplayerAudioSourcesManager.m_Instance.MuteAudioSourcesForPlayer(button.playerId);
break;
case SketchControlsScript.GlobalCommands.UnmuteUserInMultiplayer:
MultiplayerAudioSourcesManager.m_Instance.UnmuteAudioSourcesForPlayer(button.playerId);
break;
}
}

#if UNITY_EDITOR
void OnDrawGizmos()
Expand All @@ -170,7 +182,6 @@ void OnDrawGizmos()

}
#endif
#endregion


}
Expand Down
45 changes: 33 additions & 12 deletions Assets/Scripts/GUI/PlayerListItemPrefab.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
// Copyright 2023 The Open Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OpenBrush.Multiplayer;
using TMPro;
using UnityEngine;

public class PlayerListItemPrefab : MonoBehaviour
namespace TiltBrush
{
public TextMeshPro PlayerId;
public TextMeshPro NickName;
public RemotePlayer remotePlayer;

public void SetRemotePlayer(RemotePlayer Player)
public class PlayerListItemPrefab : MonoBehaviour
{
remotePlayer = Player;
SetPlayerId();
SetNickname();
}
public TextMeshPro PlayerIdObject;
public TextMeshPro NickNameObject;
public MultiplayerRoomOptionsPanelButton MuteButton;
public RemotePlayer remotePlayer;

public void SetNickname() { NickName.text = remotePlayer.Nickname; }
public void SetPlayerId() { PlayerId.text = remotePlayer.PlayerId.ToString(); }
public void SetRemotePlayer(RemotePlayer Player)
{
remotePlayer = Player;
SetPlayerId();
SetNickname();
}

public void SetNickname() { NickNameObject.text = remotePlayer.Nickname; }
public void SetPlayerId()
{
PlayerIdObject.text = remotePlayer.PlayerId.ToString();
MuteButton.playerId = remotePlayer.PlayerId;
}
}
}
105 changes: 67 additions & 38 deletions Assets/Scripts/Multiplayer/MultiplayerAudioSourcesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,90 @@
// limitations under the License.

using System.Collections.Generic;
using System.Linq;
using TiltBrush;
using UnityEngine;

public class MultiplayerAudioSourcesManager : MonoBehaviour
namespace OpenBrush
{
public static MultiplayerAudioSourcesManager m_Instance;
private List<AudioSource> sources;
private float _previousScale;

private void Awake()
public class MultiplayerAudioSourcesManager : MonoBehaviour
{
sources = new List<AudioSource>();
public static MultiplayerAudioSourcesManager m_Instance;
private Dictionary<int, AudioSource> sources; // Key: playerId, Value: AudioSource
private float _previousScale;

if (m_Instance == null) m_Instance = this;
else Debug.LogWarning("Multiple instances of MultiplayerAudioSourcesManager detected!");
}
private void Awake()
{
sources = new Dictionary<int, AudioSource>();

public void AddAudioSource(AudioSource source)
{
sources.Append(source);
}
if (m_Instance == null) m_Instance = this;
else Debug.LogWarning("Multiple instances of MultiplayerAudioSourcesManager detected!");

void Update()
{
float currentScale = App.Scene.Pose.scale;
}

if (!Mathf.Approximately(currentScale, _previousScale))
public void AddAudioSource(int playerId, AudioSource source)
{
_previousScale = currentScale;
UpdateAudioSources(currentScale);
if (source != null)
sources[playerId] = source;
}
}

private void UpdateAudioSources(float sceneScale)
{
// Loop backward to remove invalid AudioSources
for (int i = sources.Count - 1; i >= 0; i--)
void Update()
{
var source = sources[i];
if (source != null)
float currentScale = App.Scene.Pose.scale;

if (!Mathf.Approximately(currentScale, _previousScale))
{
float adjustedMaxDistance = CalculateMaxDistance(sceneScale);
source.maxDistance = adjustedMaxDistance;
_previousScale = currentScale;
UpdateAudioSources(currentScale);
}
else sources.RemoveAt(i);
}
}

private float CalculateMaxDistance(float sceneScale)
{
// This is based on OpenBrush default scene max radius
// - At scale 0.1, the mountains diameter is 200 (close range).
// - At scale 1.0,the mountains diameter is 20000 (far range).
return Mathf.Lerp(200f, 20000f, Mathf.Clamp01(sceneScale));
}
private void UpdateAudioSources(float sceneScale)
{
// Loop backward to remove invalid AudioSources
foreach (var kvp in new List<KeyValuePair<int, AudioSource>>(sources))
{
var source = kvp.Value;
if (source != null)
{
float adjustedMaxDistance = CalculateMaxDistance(sceneScale);
source.maxDistance = adjustedMaxDistance;
}
else sources.Remove(kvp.Key); // Remove invalid AudioSource by playerId
}
}

private float CalculateMaxDistance(float sceneScale)
{
// This is based on OpenBrush default scene max radius
// - At scale 0.1, the mountains diameter is 200 (close range).
// - At scale 1.0, the mountains diameter is 20000 (far range).
return Mathf.Lerp(200f, 20000f, Mathf.Clamp01(sceneScale));
}

public void MuteAudioSources()
{
foreach (var source in sources.Values)
if (source != null) source.mute = true;
}

public void UnmuteAudioSources()
{
foreach (var source in sources.Values)
if (source != null) source.mute = false;
}

public void MuteAudioSourcesForPlayer(int playerId)
{ AudioSourcesMuteStateForPlayer(playerId, true); }

public void UnmuteAudioSourcesForPlayer(int playerId)
{ AudioSourcesMuteStateForPlayer(playerId, false); }

public void AudioSourcesMuteStateForPlayer(int playerId, bool state)
{
sources.TryGetValue(playerId, out AudioSource source);
if (source == null) return;
source.mute = state;
}
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/Multiplayer/MultiplayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public void OnRemoteVoiceConnected(int id, GameObject voicePrefab)
Debug.LogWarning($"VoicePrefab with ID {id} lack AudioSource :S ");
return;
}
MultiplayerAudioSourcesManager.m_Instance.AddAudioSource(audioSource);
MultiplayerAudioSourcesManager.m_Instance.AddAudioSource(id, audioSource);
}

public void SendLargeDataToPlayer(int playerId, byte[] Data, int percentage)
Expand Down
4 changes: 4 additions & 0 deletions Assets/Scripts/SketchControlsScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public enum GlobalCommands
MultiplayerDisconnect = 1008,
EditMultiplayerNickName = 1009,
OpenRoomSettings = 1010,
MuteUserInMultiplayer = 1011,
UnmuteUserInMultiplayer = 1012,

RenameSketch = 5200,
OpenLayerOptionsPopup = 5201,
Expand Down Expand Up @@ -4871,6 +4873,8 @@ public void IssueGlobalCommand(GlobalCommands rEnum, int iParam1 = -1,
case GlobalCommands.MultiplayerLeaveRoom: break; // Intentionally blank.
case GlobalCommands.MultiplayerConnect: break; // Intentionally blank.
case GlobalCommands.MultiplayerDisconnect: break; // Intentionally blank.
case GlobalCommands.MuteUserInMultiplayer: break; // Intentionally blank.
case GlobalCommands.UnmuteUserInMultiplayer: break; // Intentionally blank.
case GlobalCommands.WhatIsNew: break;// Intentionally blank.
default:
Debug.LogError($"Unrecognized command {rEnum}");
Expand Down

0 comments on commit 3c1bd2a

Please sign in to comment.