From ca5f5a137b17eacfdf08533e0f7aae97bc12dcb0 Mon Sep 17 00:00:00 2001 From: alexiscatnip Date: Sun, 16 Jan 2022 10:56:41 +0800 Subject: [PATCH] Inital commit for the new UI page "avatar info" --- .../UI/AvatarInfo/AvatarInfoPresenter.cs | 123 ++++++++++++++++++ .../UI/AvatarInfo/AvatarInfoPresenter.cs.meta | 3 + Assets/Raindrop/UI/views/RawImageView.cs | 10 +- 3 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs create mode 100644 Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs.meta diff --git a/Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs b/Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs new file mode 100644 index 0000000..a82b8bd --- /dev/null +++ b/Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs @@ -0,0 +1,123 @@ +using OpenMetaverse; +using OpenMetaverse.StructuredData; +using Raindrop; +using Raindrop.Netcom; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using UnityEngine; +using System; +using FMOD; +using OpenMetaverse.Assets; +using Raindrop.Services; +using Settings = Raindrop.Settings; +using UnityEngine.UI; +using UniRx; +using TMPro; +using static Raindrop.LoginUtils; +using Avatar = OpenMetaverse.Avatar; +using Logger = OpenMetaverse.Logger; + + +namespace Raindrop.Presenters +{ + // show the avatar card and all the information. + + public class AvatarInfoPresenter : MonoBehaviour + { + private RaindropInstance instance { get { return ServiceLocator.ServiceLocator.Instance.Get(); } } + private RaindropNetcom netcom { get { return instance.Netcom; } } + private UIService uimanager => ServiceLocator.ServiceLocator.Instance.Get(); + + #region UI elements - the 'view' in MVP + public RawImageView avatarImg; + public TMP_Text aviName; + #endregion + + #region internal data representation + + // public Avatar avatarShown; + public UUID aviID; + public UUID aviImgID { get; set; } + #endregion + + + void Start() + { + + AddNetcomEvents(); + } + + private void UpdateAviName(string s) + { + aviName.text = s; + } + + + + private void AddNetcomEvents() + { + netcom.ClientConnected += NetcomOnClientConnected; + } + + private void NetcomOnClientConnected(object sender, EventArgs e) + { + instance.Client.Avatars.AvatarPropertiesReply += new EventHandler(Avatars_AvatarPropertiesReply); + } + + private void Avatars_AvatarPropertiesReply(object sender, AvatarPropertiesReplyEventArgs e) + { + if (e.AvatarID != aviID) return; + + UnityMainThreadDispatcher.Instance().Enqueue(() => + { + Avatars_AvatarPropertiesReply(sender, e); + }); + + UpdateAgentInfos(e); + } + + //write UI. please do this on main thread. + private void UpdateAgentInfos(AvatarPropertiesReplyEventArgs e) + { + // ID. + this.aviID = e.AvatarID; + + //Image UUID and image + this.aviImgID = e.Properties.ProfileImage; + instance.Client.Assets.RequestImage(e.AvatarID, ImageReady_Callback); + + //UI - set name + this.aviName.text = instance.Names.Get(aviID); + } + + private void ImageReady_Callback(TextureRequestState state, AssetTexture assettexture) + { + UnityMainThreadDispatcher.Instance().Enqueue(() => + { + var ok = assettexture.Decode(); //call openjpg to decode j2k->managedimage + if (ok) + { + var img = assettexture.Image; + Texture2D t2d = img.ExportTex2D(); + + this.avatarImg.setRawImage(t2d); + } + else + { + Logger.Log("decoding texture failed!", Helpers.LogLevel.Error); + } + }); + } + + + private void RemoveNetcomEvents() + { + netcom.ClientLoggingIn -= new EventHandler(NetcomOnClientConnected); + } + + + + } + +} \ No newline at end of file diff --git a/Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs.meta b/Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs.meta new file mode 100644 index 0000000..662bb8a --- /dev/null +++ b/Assets/Raindrop/UI/AvatarInfo/AvatarInfoPresenter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 602e6aee4d42474ba7008e70186f3139 +timeCreated: 1642264242 \ No newline at end of file diff --git a/Assets/Raindrop/UI/views/RawImageView.cs b/Assets/Raindrop/UI/views/RawImageView.cs index bdebc59..6cee6cf 100644 --- a/Assets/Raindrop/UI/views/RawImageView.cs +++ b/Assets/Raindrop/UI/views/RawImageView.cs @@ -12,13 +12,17 @@ public class RawImageView : MonoBehaviour public void setRawImage(Texture2D img) { //hack: delete old texture before loading new one - + unloadRawImage(); + + this.GetComponent().texture = img; + } + public void unloadRawImage() + { if (this.GetComponent().texture != null) { Object.Destroy(this.GetComponent().texture); } - this.GetComponent().texture = img; - + this.GetComponent().texture = Texture2D.grayTexture; } }