Esempio n. 1
0
        public static IEnumerator Initialize(Transform parent)
        {
            CustomText tmpText = InitText(spacingChar, Color.clear, ChatConfig.instance.ChatScale, new Vector2(ChatConfig.instance.ChatWidth, 1), new Vector3(0, -100, 0), new Quaternion(0, 0, 0, 0), parent, TextAnchor.UpperLeft, false);

            yield return(null);

            imageSpacingWidth = tmpText.preferredWidth;
            //Plugin.Log($"Preferred width was {tmpText.preferredWidth.ToString()} with {tmpText.text.Length.ToString()} spaces");
            GameObject.Destroy(tmpText.gameObject);
        }
Esempio n. 2
0
        public static CustomText InitText(string text, Color textColor, float fontSize, Vector2 sizeDelta, Vector3 position, Quaternion rotation, Transform parent, TextAnchor textAlign, bool wrapText, Material mat = null)
        {
            GameObject newGameObj = new GameObject("CustomText");
            CustomText tmpText    = newGameObj.AddComponent <CustomText>();

            if (wrapText)
            {
                var mcs = newGameObj.AddComponent <ContentSizeManager>();
                mcs.transform.SetParent(tmpText.rectTransform, false);
                mcs.Init();
                var fitter = newGameObj.AddComponent <ContentSizeFitter>();
                fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
            }
            newGameObj.AddComponent <Shadow>();

            CanvasScaler scaler = newGameObj.AddComponent <CanvasScaler>();

            scaler.dynamicPixelsPerUnit = pixelsPerUnit;
            tmpText.rectTransform.SetParent(parent, false);
            tmpText.rectTransform.localPosition = position;
            tmpText.rectTransform.localRotation = rotation;
            tmpText.rectTransform.pivot         = new Vector2(0, 0);
            tmpText.rectTransform.sizeDelta     = sizeDelta;
            tmpText.supportRichText             = true;
            tmpText.font                 = LoadSystemFont(ChatConfig.instance.FontName);
            tmpText.text                 = text;
            tmpText.fontSize             = 10;
            tmpText.verticalOverflow     = VerticalWrapMode.Overflow;
            tmpText.alignment            = textAlign;
            tmpText.horizontalOverflow   = HorizontalWrapMode.Wrap;
            tmpText.color                = textColor;
            tmpText.material.renderQueue = 3001;
            //tmpText.resizeTextForBestFit = true;

            if (mat)
            {
                tmpText.material = mat;
            }

            return(tmpText);
        }
Esempio n. 3
0
        public static void OverlayImage(CustomText currentMessage, ImageInfo imageInfo)
        {
            CachedSpriteData cachedTextureData = imageInfo.cachedSprite;

            // If cachedTextureData is null, the emote will be overlayed at a later time once it's finished being cached
            if (cachedTextureData == null || (cachedTextureData.sprite == null && cachedTextureData.animInfo == null))
            {
                return;
            }

            bool animatedEmote      = cachedTextureData.animInfo != null;
            var  messageWithoutHtml = htmlRegex.Replace(currentMessage.text, (m) => m.Groups[1].Value);

            foreach (int i in EmojiUtilities.IndexOfAll(messageWithoutHtml, Char.ConvertFromUtf32(imageInfo.swapChar)))
            {
                CustomImage image = null, shadow = null;
                try
                {
                    if (i > 0 && i < currentMessage.text.Length)
                    {
                        image = ChatHandler.Instance.imagePool.Alloc();
                        image.cachedTextureData = cachedTextureData;
                        image.spriteIndex       = imageInfo.textureIndex;
                        image.imageType         = imageInfo.imageType;
                        image.sprite            = cachedTextureData.sprite;
                        image.preserveAspect    = false;
                        if (image.sprite)
                        {
                            image.sprite.texture.wrapMode = TextureWrapMode.Clamp;
                        }

                        image.rectTransform.SetParent(currentMessage.rectTransform, false);
                        image.rectTransform.localScale = new Vector3(0.064f, 0.064f, 0.064f);
                        image.rectTransform.sizeDelta  = new Vector2(cachedTextureData.width, cachedTextureData.height);

                        if (imageInfo.imageType == ImageType.Emoji || imageInfo.imageType == ImageType.Badge)
                        {
                            image.rectTransform.pivot = new Vector2(0, 0.21f);
                        }
                        else
                        {
                            image.rectTransform.pivot = new Vector2(0, 0.3f);
                        }

                        TextGenerator textGen = currentMessage.cachedTextGenerator;
                        Vector3       pos     = new Vector3(textGen.verts[i * 4].position.x, textGen.verts[i * 4].position.y);
                        image.rectTransform.position = currentMessage.gameObject.transform.TransformPoint(pos / pixelsPerUnit + new Vector3(0, 0, -0.1f));

                        if (animatedEmote)
                        {
                            cachedTextureData.animInfo?.animData?.IncRefs();
                            image.material = cachedTextureData.animInfo.imageMaterial;
                            if (ChatConfig.instance.DrawShadows && cachedTextureData.animInfo.shadowMaterial != null)
                            {
                                // Add a shadow to our animated image (the regular unity shadows won't work with this material)
                                shadow                          = ChatHandler.Instance.imagePool.Alloc();
                                shadow.material                 = cachedTextureData.animInfo.shadowMaterial;
                                shadow.sprite                   = null;
                                shadow.spriteIndex              = imageInfo.textureIndex;
                                shadow.imageType                = imageInfo.imageType;
                                shadow.rectTransform.pivot      = new Vector2(0, 0);
                                shadow.rectTransform.localScale = image.rectTransform.localScale;
                                shadow.rectTransform.SetParent(currentMessage.rectTransform, false);
                                shadow.rectTransform.position       = image.rectTransform.position;
                                shadow.rectTransform.localPosition += new Vector3(0.6f, -0.6f, 0.05f);
                                shadow.enabled = true;
                                currentMessage.emoteRenderers.Add(shadow);
                            }
                        }
                        else
                        {
                            image.material = noGlowMaterialUI;
                            if (ChatConfig.instance.DrawShadows)
                            {
                                image.shadow.enabled = true;
                            }
                        }
                        image.enabled = true;
                        currentMessage.emoteRenderers.Add(image);
                    }
                }
                catch (Exception e)
                {
                    if (image)
                    {
                        ChatHandler.Instance.imagePool.Free(image);
                    }

                    Plugin.Log($"Exception {e.ToString()} occured when trying to overlay emote at index {i.ToString()}!");
                }
            }
        }
        public static void OverlayImage(CustomText currentMessage, ImageInfo imageInfo)
        {
            CachedSpriteData cachedTextureData = ImageDownloader.CachedTextures.ContainsKey(imageInfo.textureIndex) ? ImageDownloader.CachedTextures[imageInfo.textureIndex] : null;

            // If cachedTextureData is null, the emote will be overlayed at a later time once it's finished being cached
            if (cachedTextureData == null || (cachedTextureData.sprite == null && cachedTextureData.animInfo == null))
            {
                return;
            }

            bool animatedEmote = cachedTextureData.animInfo != null;

            foreach (int i in EmojiUtilities.IndexOfAll(currentMessage.text, Char.ConvertFromUtf32(imageInfo.swapChar)))
            {
                CustomImage image = null, shadow = null;
                try
                {
                    if (i > 0 && i < currentMessage.text.Count())
                    {
                        image                     = ChatHandler.Instance.imagePool.Alloc();
                        image.spriteIndex         = imageInfo.textureIndex;
                        image.imageType           = imageInfo.imageType;
                        image.rectTransform.pivot = new Vector2(0, 0);
                        image.sprite              = cachedTextureData.sprite;
                        image.preserveAspect      = false;
                        if (image.sprite)
                        {
                            image.sprite.texture.wrapMode = TextureWrapMode.Clamp;
                        }

                        image.rectTransform.SetParent(currentMessage.rectTransform, false);

                        float aspectRatio = cachedTextureData.width / cachedTextureData.height;
                        if (aspectRatio > 1)
                        {
                            image.rectTransform.localScale = new Vector3(0.064f * aspectRatio, 0.064f, 0.064f);
                        }
                        else
                        {
                            image.rectTransform.localScale = new Vector3(0.064f, 0.064f, 0.064f);
                        }

                        TextGenerator textGen = currentMessage.cachedTextGenerator;
                        Vector3       pos     = new Vector3(textGen.verts[i * 4 + 3].position.x, textGen.verts[i * 4 + 3].position.y);
                        image.rectTransform.position       = currentMessage.gameObject.transform.TransformPoint(pos / pixelsPerUnit - new Vector3(cachedTextureData.width / pixelsPerUnit + 2.5f, cachedTextureData.height / pixelsPerUnit + 1f) + new Vector3(0, 0, -0.1f));
                        image.rectTransform.localPosition -= new Vector3(imageSpacingWidth / 2.3f, 0);

                        if (animatedEmote)
                        {
                            image.material = cachedTextureData.animInfo.imageMaterial;
                            //image.shadow.enabled = false;
                            if (ChatConfig.Instance.DrawShadows)
                            {
                                // Add a shadow to our animated image (the regular unity shadows won't work with this material)
                                shadow                          = ChatHandler.Instance.imagePool.Alloc();
                                shadow.material                 = cachedTextureData.animInfo.shadowMaterial;
                                shadow.sprite                   = null;
                                shadow.spriteIndex              = imageInfo.textureIndex;
                                shadow.imageType                = imageInfo.imageType;
                                shadow.rectTransform.pivot      = new Vector2(0, 0);
                                shadow.rectTransform.localScale = image.rectTransform.localScale;
                                shadow.rectTransform.SetParent(currentMessage.rectTransform, false);
                                shadow.rectTransform.position       = image.rectTransform.position;
                                shadow.rectTransform.localPosition += new Vector3(0.6f, -0.6f, 0.05f);

                                shadow.enabled = true;
                                currentMessage.emoteRenderers.Add(shadow);
                            }
                        }
                        else
                        {
                            image.material = Drawing.noGlowMaterialUI;
                            if (ChatConfig.Instance.DrawShadows)
                            {
                                image.shadow.enabled = true;
                            }
                        }
                        image.enabled = true;
                        currentMessage.emoteRenderers.Add(image);
                    }
                }
                catch (Exception e)
                {
                    if (image)
                    {
                        ChatHandler.Instance.imagePool.Free(image);
                    }

                    Plugin.Log($"Exception {e.ToString()} occured when trying to overlay emote at index {i.ToString()}!");
                }
            }
        }