Example #1
0
        private static UnityWebRequest MakeGetLessonsRequest(string address, string lessonId)
        {
            if (!string.IsNullOrEmpty(lessonId))
            {
                address += "?lessonId=" + lessonId;
            }

            var req = UnityWebRequest.Post(address, new WWWForm());

            req.SetRequestHeader("Content-Type", "application/json");
            req.SetRequestHeader("Authorization", "Bearer " + UnityConnectProxy.GetAccessToken());
            req.method = "GET";
            return(req);
        }
        /// <summary>
        /// Opens an Url in the browser.
        /// Links to Unity's websites will open only if the user is logged in.
        /// </summary>
        /// <param name="url"></param>
        public static void OpenUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return;
            }
            string urlWithoutHttpPrefix = RemoveHttpProtocolPrefix(url);

            if (IsUnityUrlRequiringAuthentication(urlWithoutHttpPrefix) && UnityConnectProxy.loggedIn)
            {
                UnityConnectProxy.OpenAuthorizedURLInWebBrowser(url);
                return;
            }
            Application.OpenURL(url);
        }
Example #3
0
        private static void GetTutorial(string lessonId, Action <List <TutorialProgressStatus> > action)
        {
            var userId  = UnityConnectProxy.GetUserId();
            var getLink = @"/v1/users/" + userId + @"/lessons";
            var address = HostAddress + getLink;
            var req     = MakeGetLessonsRequest(address, lessonId);

            SendWebRequest(req, (UnityWebRequest r) =>
            {
                if (!IsRequestSuccess(r))
                {
                    return;
                }
                var lessonResponses = TutorialProgressStatus.ParseResponses(r.downloadHandler.text);
                action(lessonResponses);
            });
        }
Example #4
0
            public void OpenUrl()
            {
                if (string.IsNullOrEmpty(url))
                {
                    return;
                }

                if (authorizedUrl && UnityConnectProxy.loggedIn)
                {
                    UnityConnectProxy.OpenAuthorizedURLInWebBrowser(url);
                }
                else
                {
                    Application.OpenURL(url);
                }

                AnalyticsHelper.SendExternalReferenceEvent(url, heading, linkText, tutorial?.lessonId);
            }
Example #5
0
        public static void LogTutorialStatusUpdate(string lessonId, string lessonStatus)
        {
            var userId  = UnityConnectProxy.GetUserId();
            var getLink = @"/v1/users/" + userId + @"/lessons";
            var address = HostAddress + getLink;

            var jsonData = RegisterLessonRequest.GetJSONString(lessonStatus, userId, lessonId);
            var req      = UnityWebRequest.Post(address, jsonData);
            var data     = System.Text.Encoding.UTF8.GetBytes(jsonData);

            req.uploadHandler = new UploadHandlerRaw(data);

            req.SetRequestHeader("Content-Type", "application/json");
            req.SetRequestHeader("Authorization", "Bearer " + UnityConnectProxy.GetAccessToken());

            SendWebRequest(req, r =>
            {
                if (!IsRequestSuccess(r))
                {
                    return;
                }
            });
        }
Example #6
0
        /// <summary>
        /// Transforms HTML tags to word element labels with different styles to enable rich text.
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="targetContainer">
        /// The following need to set for the container's style:
        /// flex-direction: row;
        /// flex-wrap: wrap;
        /// </param>
        public static void RichTextToVisualElements(string htmlText, VisualElement targetContainer)
        {
            bool   addError  = false;
            string errorText = "";

            try
            {
                XDocument.Parse("<content>" + htmlText + "</content>");
            }
            catch (Exception e)
            {
                targetContainer.Clear();
                errorText = e.Message;
                htmlText  = ShowContentWithError(htmlText);
                addError  = true;
            }

            // TODO should translation be a responsibility of the caller of this function instead?
            htmlText = Localization.Tr(htmlText);

            targetContainer.Clear();
            bool   boldOn          = false; // <b> sets this on </b> sets off
            bool   italicOn        = false; // <i> </i>
            bool   linkOn          = false;
            string linkURL         = "";
            bool   firstLine       = true;
            bool   lastLineHadText = false;

            // start streaming text per word to elements while retaining current style for each word block
            string[] lines = htmlText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

            foreach (string line in lines)
            {
                string[] words = line.Split(new[] { " " }, StringSplitOptions.None);

                if (!firstLine && !lastLineHadText)
                {
                    AddParagraphToElement(targetContainer);
                }
                if (!firstLine && lastLineHadText)
                {
                    AddLinebreakToElement(targetContainer);
                    //AddParagraphToElement(targetContainer);
                    lastLineHadText = false;
                }

                foreach (string word in words)
                {
                    if (word == "" || word == " " || word == "   ")
                    {
                        continue;
                    }
                    lastLineHadText = true;
                    string strippedWord = word;
                    bool   removeBold   = false;
                    bool   removeItalic = false;
                    bool   addParagraph = false;
                    bool   removeLink   = false;

                    if (strippedWord.Contains("<b>"))
                    {
                        strippedWord = strippedWord.Replace("<b>", "");
                        boldOn       = true;
                    }
                    if (strippedWord.Contains("<i>"))
                    {
                        strippedWord = strippedWord.Replace("<i>", "");
                        italicOn     = true;
                    }
                    if (strippedWord.Contains("<a"))
                    {
                        strippedWord = strippedWord.Replace("<a", "");
                        linkOn       = true;
                    }
                    if (linkOn && strippedWord.Contains("href="))
                    {
                        strippedWord = strippedWord.Replace("href=", "");
                        int linkFrom = strippedWord.IndexOf("\"", StringComparison.Ordinal) + 1;
                        int linkTo   = strippedWord.LastIndexOf("\"", StringComparison.Ordinal);
                        linkURL      = strippedWord.Substring(linkFrom, linkTo - linkFrom);
                        strippedWord = strippedWord.Substring(linkTo + 2, (strippedWord.Length - 2) - linkTo);
                        strippedWord.Replace("\">", "");
                    }
                    if (strippedWord.Contains("</a>"))
                    {
                        strippedWord = strippedWord.Replace("</a>", "");
                        // TODO </a>text -> also text part is still blue. Parse - for now we can take care when authoring.
                        removeLink = true;
                    }
                    if (strippedWord.Contains("<br/>"))
                    {
                        strippedWord = strippedWord.Replace("<br/>", "");
                        addParagraph = true;
                    }
                    if (strippedWord.Contains("</b>"))
                    {
                        strippedWord = strippedWord.Replace("</b>", "");
                        removeBold   = true;
                    }
                    if (strippedWord.Contains("</i>"))
                    {
                        strippedWord = strippedWord.Replace("</i>", "");
                        removeItalic = true;
                    }
                    if (boldOn)
                    {
                        Label wordLabel = new Label(strippedWord);
                        wordLabel.style.unityFontStyleAndWeight = new StyleEnum <FontStyle>(FontStyle.Bold);
                        targetContainer.Add(wordLabel);
                    }
                    else if (italicOn)
                    {
                        Label wordLabel = new Label(strippedWord);
                        wordLabel.style.unityFontStyleAndWeight = new StyleEnum <FontStyle>(FontStyle.Italic);
                        targetContainer.Add(wordLabel);
                    }
                    else if (addParagraph)
                    {
                        AddParagraphToElement(targetContainer);
                    }
                    else if (linkOn && !string.IsNullOrEmpty(linkURL))
                    {
                        var label = new HyperlinkLabel
                        {
                            text    = strippedWord,
                            tooltip = linkURL
                        };
                        label.RegisterCallback <MouseUpEvent, string>(
                            (evt, linkurl) =>
                        {
                            // Supporting only hyperlinks to Unity's websites.
                            // The user needs be be logged in in order the hyperlink to work.
                            UnityConnectProxy.OpenAuthorizedURLInWebBrowser(linkurl);
                        },
                            linkURL
                            );

                        targetContainer.Add(label);
                    }
                    else
                    {
                        Label newlabel = new Label(strippedWord);
                        targetContainer.Add(newlabel);
                    }
                    if (removeBold)
                    {
                        boldOn = false;
                    }
                    if (removeItalic)
                    {
                        italicOn = false;
                    }
                    if (removeLink)
                    {
                        linkOn  = false;
                        linkURL = "";
                    }
                }
                firstLine = false;
            }

            if (addError)
            {
                var label = new ParseErrorLabel()
                {
                    text    = Localization.Tr("PARSE ERROR"),
                    tooltip = Localization.Tr("Click here to see more information in the console.")
                };
                label.RegisterCallback <MouseUpEvent>((e) => Debug.LogError(errorText));
                targetContainer.Add(label);
            }
        }
Example #7
0
        /// <summary>
        /// Transforms HTML tags to word element labels with different styles to enable rich text.
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="targetContainer">
        /// The following need to set for the container's style:
        /// flex-direction: row;
        /// flex-wrap: wrap;
        /// </param>
        public static void RichTextToVisualElements(string htmlText, VisualElement targetContainer)
        {
            // TODO should translation be a responsibility of the caller of this function instead
            htmlText = Localization.Tr(htmlText);
            VisualElementStyleSheetSet style = targetContainer.styleSheets;

            targetContainer.Clear();
            bool   boldOn          = false; // <b> sets this on </b> sets off
            bool   italicOn        = false; // <i> </i>
            bool   linkOn          = false;
            string linkURL         = "";
            bool   firstLine       = true;
            bool   lastLineHadText = false;

            // start streaming text per word to elements while retaining current style for each word block
            string[] lines = htmlText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

            foreach (string line in lines)
            {
                string[] words = line.Split(new[] { " " }, StringSplitOptions.None);

                if (!firstLine && !lastLineHadText)
                {
                    AddParagraphToElement(targetContainer);
                }
                if (!firstLine && lastLineHadText)
                {
                    AddLinebreakToElement(targetContainer);
                    //AddParagraphToElement(targetContainer);
                    lastLineHadText = false;
                }

                foreach (string word in words)
                {
                    if (word == "" || word == " " || word == "   ")
                    {
                        continue;
                    }
                    lastLineHadText = true;
                    string strippedWord = word;
                    bool   removeBold   = false;
                    bool   removeItalic = false;
                    bool   addParagraph = false;
                    bool   removeLink   = false;

                    if (strippedWord.Contains("<b>"))
                    {
                        strippedWord = strippedWord.Replace("<b>", "");
                        boldOn       = true;
                    }
                    if (strippedWord.Contains("<i>"))
                    {
                        strippedWord = strippedWord.Replace("<i>", "");
                        italicOn     = true;
                    }
                    if (strippedWord.Contains("<a"))
                    {
                        strippedWord = strippedWord.Replace("<a", "");
                        linkOn       = true;
                    }
                    if (linkOn && strippedWord.Contains("href="))
                    {
                        strippedWord = strippedWord.Replace("href=", "");
                        int linkFrom = strippedWord.IndexOf("\"", StringComparison.Ordinal) + 1;
                        int linkTo   = strippedWord.LastIndexOf("\"", StringComparison.Ordinal);
                        // TODO handle invalid values
                        linkURL      = strippedWord.Substring(linkFrom, linkTo - linkFrom);
                        strippedWord = strippedWord.Substring(linkTo + 2, (strippedWord.Length - 2) - linkTo);
                        strippedWord.Replace("\">", "");
                    }
                    if (strippedWord.Contains("</a>"))
                    {
                        strippedWord = strippedWord.Replace("</a>", "");
                        // TODO </a>text -> also text part is still blue. Parse - for now we can take care when authoring.
                        removeLink = true;
                    }
                    if (strippedWord.Contains("<br/>"))
                    {
                        strippedWord = strippedWord.Replace("<br/>", "");
                        addParagraph = true;
                    }
                    if (strippedWord.Contains("</b>"))
                    {
                        strippedWord = strippedWord.Replace("</b>", "");
                        removeBold   = true;
                    }
                    if (strippedWord.Contains("</i>"))
                    {
                        strippedWord = strippedWord.Replace("</i>", "");
                        removeItalic = true;
                    }
                    if (boldOn)
                    {
                        Label wordLabel = new Label(strippedWord);
                        wordLabel.style.color = Color.black;
                        wordLabel.style.unityFontStyleAndWeight = new StyleEnum <FontStyle>(FontStyle.Bold);
                        targetContainer.Add(wordLabel);
                    }
                    else if (italicOn)
                    {
                        Label wordLabel = new Label(strippedWord);
                        wordLabel.style.color = Color.black;
                        wordLabel.style.unityFontStyleAndWeight = new StyleEnum <FontStyle>(FontStyle.Italic);
                        targetContainer.Add(wordLabel);
                    }
                    else if (addParagraph)
                    {
                        AddParagraphToElement(targetContainer);
                    }
                    else if (linkOn && !string.IsNullOrEmpty(linkURL))
                    {
                        Label newLabel = new Label(strippedWord);

                        newLabel.style.color             = Color.blue;
                        newLabel.style.borderBottomWidth = 1f;
                        newLabel.style.borderBottomColor = Color.blue;
                        newLabel.tooltip = linkURL;
                        newLabel.RegisterCallback <MouseUpEvent, string>(
                            (evt, linkurl) =>
                        {
                            // Supporting only hyperlinks to Unity's websites.
                            // The user needs be be logged in in order the hyperlink to work.
                            UnityConnectProxy.OpenAuthorizedURLInWebBrowser(linkurl);
                        },
                            linkURL
                            );

                        targetContainer.Add(newLabel);
                    }
                    else
                    {
                        Label newlabel = new Label(strippedWord);
                        newlabel.style.color = Color.black;
                        targetContainer.Add(newlabel);
                    }
                    if (removeBold)
                    {
                        boldOn = false;
                    }
                    if (removeItalic)
                    {
                        italicOn = false;
                    }
                    if (removeLink)
                    {
                        linkOn  = false;
                        linkURL = "";
                    }
                }
                firstLine = false;
            }
        }