172 lines
3.8 KiB
C#
172 lines
3.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
[NetworkSettings(channel = 0, sendInterval = 1)]
|
|
public class clientGameMenu : NetworkBehaviour {
|
|
public GameObject next;
|
|
public NetworkManager network;
|
|
|
|
|
|
private TweetAnimator tweetAnimator;
|
|
public TweetContainer tweetContainer;
|
|
|
|
|
|
//Letölti az új tweetet
|
|
private void Update ()
|
|
{
|
|
if(isClient)
|
|
{
|
|
if (tweetAnimator == null)
|
|
tweetAnimator = new TweetAnimator(0.5f, 7.5f, tweetContainer);
|
|
tweetAnimator.Animate(Time.deltaTime);
|
|
}
|
|
}
|
|
|
|
[ClientRpc(channel = 0)]
|
|
public void RpcSetNewTweet (Tweet newTweet)
|
|
{
|
|
if (tweetAnimator == null)
|
|
tweetAnimator = new TweetAnimator(0.5f, 7.5f, tweetContainer);
|
|
tweetAnimator.SetNewTweet(newTweet);
|
|
}
|
|
|
|
/*public void goBack()
|
|
{
|
|
network.StopClient();
|
|
gameObject.SetActive(false);
|
|
next.SetActive(true);
|
|
next.GetComponent<clientMenu>().errorField.text = "No errors so far.";
|
|
}*/
|
|
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct Tweet
|
|
{
|
|
public TweetAccount account;
|
|
public string message;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct TweetAccount
|
|
{
|
|
public string realName;
|
|
public string name;
|
|
public int picID;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct TweetContainer
|
|
{
|
|
public GameObject tweetBox;
|
|
public Text realName;
|
|
public Text message;
|
|
public RawImage image;
|
|
public List<Texture> pics;
|
|
}
|
|
|
|
//Betölti az új tweetet
|
|
public class TweetAnimator
|
|
{
|
|
private Tweet tweet;
|
|
private TweetContainer tweetContainer;
|
|
|
|
private float elapsedTime;
|
|
|
|
private float animationTimeout;
|
|
private float tweetTimeout;
|
|
|
|
private bool hasNewTweet = false;
|
|
|
|
public float opacity = 0.95f;
|
|
|
|
private enum FadeState
|
|
{
|
|
notVisible,
|
|
fadingIn,
|
|
fullyVisible,
|
|
fadingOut
|
|
}
|
|
|
|
FadeState fadeState = FadeState.notVisible;
|
|
|
|
public TweetAnimator (float animationTimeout, float tweetTimeout, TweetContainer tweetContainer)
|
|
{
|
|
this.animationTimeout = animationTimeout;
|
|
this.tweetTimeout = tweetTimeout;
|
|
this.tweetContainer = tweetContainer;
|
|
}
|
|
|
|
public void Animate (float deltaTime)
|
|
{
|
|
elapsedTime += deltaTime;
|
|
|
|
float q = elapsedTime / animationTimeout;
|
|
|
|
if (q > 1)
|
|
q = 1;
|
|
|
|
switch (fadeState)
|
|
{
|
|
case FadeState.notVisible:
|
|
tweetContainer.tweetBox.GetComponent<CanvasGroup>().alpha = 0;
|
|
if (hasNewTweet)
|
|
{
|
|
elapsedTime = 0;
|
|
changeText();
|
|
hasNewTweet = false;
|
|
fadeState = FadeState.fadingIn;
|
|
}
|
|
break;
|
|
case FadeState.fadingIn:
|
|
tweetContainer.tweetBox.GetComponent<CanvasGroup>().alpha = q * opacity;
|
|
if (q >= 1)
|
|
{
|
|
elapsedTime = 0;
|
|
fadeState = FadeState.fullyVisible;
|
|
}
|
|
break;
|
|
case FadeState.fullyVisible:
|
|
if (elapsedTime > tweetTimeout)
|
|
{
|
|
elapsedTime = 0;
|
|
fadeState = FadeState.fadingOut;
|
|
}
|
|
break;
|
|
case FadeState.fadingOut:
|
|
tweetContainer.tweetBox.GetComponent<CanvasGroup>().alpha = (1 - q) * opacity;
|
|
if (q >= 1)
|
|
fadeState = FadeState.notVisible;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void SetNewTweet(Tweet newTweet)
|
|
{
|
|
tweet = newTweet;
|
|
if (fadeState == FadeState.fadingIn || fadeState == FadeState.fullyVisible)
|
|
fadeState = FadeState.fadingOut;
|
|
hasNewTweet = true;
|
|
}
|
|
|
|
private void changeText()
|
|
{
|
|
tweetContainer.realName.text = tweet.account.realName + " <color=blue>@" + tweet.account.name + "</color>";
|
|
|
|
|
|
string formattedMessage = Regex.Replace(tweet.message, "#[a-zA-Z]+", LocalReplaceMatchCase);
|
|
tweetContainer.message.text = formattedMessage;
|
|
tweetContainer.image.texture = tweetContainer.pics[tweet.account.picID];
|
|
}
|
|
|
|
private string LocalReplaceMatchCase (Match matchExpression)
|
|
{
|
|
return "<color=blue>" + matchExpression.Value + "</color>";
|
|
}
|
|
|
|
}
|