You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
265 lines
7.7 KiB
265 lines
7.7 KiB
4 years ago
|
using Assets.Scripts.Common.ChatSystem;
|
||
|
|
||
|
using AX.NetworkSystem;
|
||
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using SuperScrollView;
|
||
|
using AX.MessageSystem;
|
||
|
using Tween;
|
||
|
|
||
|
public class ChatPanel : UIView
|
||
|
{
|
||
|
//无限循环ListView
|
||
|
public LoopListView2 MyList;
|
||
|
//输入框数据
|
||
|
public InputField ChatInputField;
|
||
|
// 频道按钮
|
||
|
public GameObject ChannelButtons;
|
||
|
//
|
||
|
public CommonButton HideButton;
|
||
|
public CommonButton ShowButton;
|
||
|
//录音机
|
||
|
private AudioRecorder recorder;
|
||
|
|
||
|
private string fileName;
|
||
|
|
||
|
private TimeSpan time;
|
||
|
private DateTime startTime;
|
||
|
|
||
|
public override UIViewType ViewType
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return UIViewType.Normal;
|
||
|
}
|
||
|
}
|
||
|
public void ShowLongSize()
|
||
|
{
|
||
|
Show();
|
||
|
ChannelButtons.SetActive(false);
|
||
|
(transform as RectTransform).sizeDelta = new Vector2(1260, 226);
|
||
|
MyList.ResetListView();
|
||
|
UpdateUI();
|
||
|
}
|
||
|
public void ShowShortSize()
|
||
|
{
|
||
|
Show();
|
||
|
// 调整层次到最后
|
||
|
transform.SetAsFirstSibling();
|
||
|
HideButton.gameObject.SetActive(true);
|
||
|
ChannelButtons.SetActive(true);
|
||
|
(transform as RectTransform).sizeDelta = new Vector2(600, 315);
|
||
|
MyList.ResetListView();
|
||
|
UpdateUI();
|
||
|
//①提升战术素养 (<color=#a40e24>27</color>) ②提升战术协同 (<color=#f80c2e>25</color>) ③购买车辆 (<color=#fb435d>11</color>)
|
||
|
}
|
||
|
// 显示指挥中心聊天尺寸
|
||
|
public void ShowCommandCenterSize()
|
||
|
{
|
||
|
Show();
|
||
|
// 调整层次到最后
|
||
|
HideButton.gameObject.SetActive(true);
|
||
|
ChannelButtons.SetActive(true);
|
||
|
(transform as RectTransform).sizeDelta = new Vector2(600, 315);
|
||
|
MyList.ResetListView();
|
||
|
UpdateUI();
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 显示出警聊天尺寸
|
||
|
/// </summary>
|
||
|
public void ShowPoliceSize()
|
||
|
{
|
||
|
Show();
|
||
|
ChannelButtons.SetActive(false);
|
||
|
(transform as RectTransform).sizeDelta = new Vector2(810, 226);
|
||
|
MyList.ResetListView();
|
||
|
UpdateUI();
|
||
|
}
|
||
|
private void Awake()
|
||
|
{
|
||
|
ChatManager.Instance.InitPublicChatMsgs();
|
||
|
MyList.InitListView(ChatManager.Instance.GetMsgsCountFromCurrentPublicChannel(), SetPublicMsgData);
|
||
|
|
||
|
HideButton.OnClicked = HideButton_OnClicked;
|
||
|
ShowButton.OnClicked = ShowButton_OnClicked;
|
||
|
}
|
||
|
|
||
|
private void ShowButton_OnClicked(CommonButton obj)
|
||
|
{
|
||
|
ShowButton.gameObject.SetActive(false);
|
||
|
RectTransform rect = transform as RectTransform;
|
||
|
rect.TnUguiMove(new Vector3(86, 16, 0), 1)
|
||
|
.SetEase(Ease.InOutBack)
|
||
|
.SetCallBack(x => HideButton.gameObject.SetActive(true));
|
||
|
}
|
||
|
|
||
|
private void HideButton_OnClicked(CommonButton obj)
|
||
|
{
|
||
|
HideButton.gameObject.SetActive(false);
|
||
|
RectTransform rect = transform as RectTransform;
|
||
|
rect.TnUguiMove(new Vector3(-rect.sizeDelta.x, 16, 0), 1)
|
||
|
.SetEase(Ease.InBack)
|
||
|
.SetCallBack(x => ShowButton.gameObject.SetActive(true));
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
recorder = GetComponent<AudioRecorder>();
|
||
|
recorder.Stopped += OnRecorderStopped;
|
||
|
|
||
|
}
|
||
|
//更新Item
|
||
|
private LoopListViewItem2 SetPublicMsgData(LoopListView2 listView, int index)
|
||
|
{
|
||
|
// 获取数据
|
||
|
ChatMessage itemData = ChatManager.Instance.GetCurrentPublicChatMsgByIndex(index);
|
||
|
LoopListViewItem2 item = null;
|
||
|
// 判断数据的类型
|
||
|
if (itemData is ChatTextMessage)
|
||
|
{
|
||
|
ChatTextMessage msg = itemData as ChatTextMessage;
|
||
|
item = listView.NewListViewItem("ChatTextItem");
|
||
|
ChatTextItem itemScript = item.GetComponent<ChatTextItem>();
|
||
|
if (item.IsInitHandlerCalled == false)
|
||
|
{
|
||
|
item.IsInitHandlerCalled = true;
|
||
|
itemScript.Init();
|
||
|
}
|
||
|
itemScript.SetData(msg, index);
|
||
|
}
|
||
|
else if (itemData is ChatAudioMessage)
|
||
|
{
|
||
|
ChatAudioMessage msg = itemData as ChatAudioMessage;
|
||
|
item = listView.NewListViewItem("ChatAudioItem");
|
||
|
ChatAudioItem itemScript = item.GetComponent<ChatAudioItem>();
|
||
|
if (item.IsInitHandlerCalled == false)
|
||
|
{
|
||
|
item.IsInitHandlerCalled = true;
|
||
|
itemScript.Init();
|
||
|
}
|
||
|
itemScript.SetData(msg, index);
|
||
|
}
|
||
|
return item;
|
||
|
}
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
MessageDispatcher.AddListener(MessageName.RefreshPublicChatMsgs.ToString(), RefreshPublicChatMsgs);
|
||
|
}
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
MessageDispatcher.RemoveListener(MessageName.RefreshPublicChatMsgs.ToString(), RefreshPublicChatMsgs);
|
||
|
}
|
||
|
|
||
|
private void RefreshPublicChatMsgs(IMessage obj)
|
||
|
{
|
||
|
UpdateUI();
|
||
|
}
|
||
|
|
||
|
public override void OnDestroy()
|
||
|
{
|
||
|
base.OnDestroy();
|
||
|
recorder.Stopped -= OnRecorderStopped;
|
||
|
#if !UNITY_EDITOR
|
||
|
//销毁聊天数据
|
||
|
ChatManager.Instance.ClearPublicChatMsgs();
|
||
|
#endif
|
||
|
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 创建音频文件
|
||
|
/// </summary>
|
||
|
/// <param name="segment"></param>
|
||
|
private void OnRecorderStopped(ArraySegment<byte> segment)
|
||
|
{
|
||
|
//生成文件名
|
||
|
fileName = string.Format("{0}.opus", Guid.NewGuid().ToString("N"));
|
||
|
string userPath = ChatManager.Instance.UserPath;
|
||
|
if (!Directory.Exists(userPath))
|
||
|
{
|
||
|
Directory.CreateDirectory(userPath);
|
||
|
}
|
||
|
string filePath = Path.Combine(userPath, fileName);
|
||
|
using (var filestream = new FileStream(filePath, FileMode.CreateNew))
|
||
|
{
|
||
|
filestream.Write(segment.Array, segment.Offset, segment.Count);
|
||
|
}
|
||
|
// 结束记录语音时间
|
||
|
time = DateTime.Now - startTime;
|
||
|
ChatManager.Instance.SendPublicAudioChatMessage(fileName, time);
|
||
|
// 输入框获取焦点
|
||
|
ChatInputField.ActivateInputField();
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
if (Input.GetKeyDown(KeyCode.Return))
|
||
|
{
|
||
|
InputFieldEnter();
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 更新UI
|
||
|
/// </summary>
|
||
|
public void UpdateUI()
|
||
|
{
|
||
|
int count = ChatManager.Instance.GetMsgsCountFromCurrentPublicChannel();
|
||
|
MyList.SetListItemCount(count, false);
|
||
|
MyList.MovePanelToItemIndex(count, 0);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 移动到指定位置
|
||
|
/// </summary>
|
||
|
/// <param name="index"></param>
|
||
|
public void MoveTo(int index)
|
||
|
{
|
||
|
MyList.MovePanelToItemIndex(index, 0);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 发送文本消息
|
||
|
/// </summary>
|
||
|
public void InputFieldEnter()
|
||
|
{
|
||
|
string inputText = ChatInputField.text;
|
||
|
ChatInputField.text = null;
|
||
|
if (!string.IsNullOrEmpty(inputText))
|
||
|
{
|
||
|
//向服务器发送公聊文本消息
|
||
|
ChatManager.Instance.SendPublicTextChatMessage(inputText);
|
||
|
}
|
||
|
// 输入框获取焦点
|
||
|
ChatInputField.ActivateInputField();
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 弹起语音按钮
|
||
|
/// </summary>
|
||
|
public void AudioFieldEnterUp()
|
||
|
{
|
||
|
recorder.StopRecorder();
|
||
|
//// 结束记录语音时间
|
||
|
//time = DateTime.Now - startTime;
|
||
|
//ChatManager.Instance.SendPublicAudioChatMessage(fileName, time);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 按下语音按钮
|
||
|
/// </summary>
|
||
|
public void AudioFieldEnterDown()
|
||
|
{
|
||
|
|
||
|
if (recorder.Ready)
|
||
|
{
|
||
|
startTime = DateTime.Now;
|
||
|
// 开始记录语音事件
|
||
|
recorder.StartRecorder();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
LoadPromptWin.Instance.LoadTextPromptWindow("没有可用设备", 1f);
|
||
|
}
|
||
|
}
|
||
|
}
|