示例#1
0
        private void LoadPage(IDocumentationPage <T> page, VisualElement rootOverride = null)
        {
            VisualElement root = rootOverride ?? contentRoot;

            SetCurrentDefaultRoot(root);
            root.Clear();

            //Constant header
            window.DrawConstantHeader();

            //Documentation
            page.DrawDocumentation(window);

            //Additions
            if (additions.TryGetValue(page, out var additionsList))
            {
                foreach (var addition in additionsList)
                {
                    addition.DrawDocumentation(window);
                }
            }

            page.DrawDocumentationAfterAdditions(window);

            //Button Links
            if (injectedButtonLinks.TryGetValue(page, out var buttonsBelow))
            {
                VisualElement injectedButtonContainer = new VisualElement();
                injectedButtonContainer.AddToClassList("injected-button-container");
                AddToRoot(injectedButtonContainer);
                SetCurrentDefaultRoot(injectedButtonContainer);

                foreach (DocumentationPage <T> .ButtonInjection button in buttonsBelow)
                {
                    DocumentationPage <T> pageOfOrigin = button.PageOfOrigin;
                    window.AddFullWidthButton(pageOfOrigin.Title, pageOfOrigin.Color, () => GoToPage(pageOfOrigin.GetType().FullName));
                }
            }

            currentPageStateName = page.GetType().FullName;
        }
示例#2
0
        public override bool InitialiseContent()
        {
            if (!InitialisePages())
            {
                return(false);
            }
            if (EditorPrefs.HasKey(stateEditorPrefsKey))
            {
                string page = EditorPrefs.GetString(stateEditorPrefsKey);
                if (!LoadPage(page))
                {
                    Home();
                }
            }
            else
            {
                Home();
            }

            return(true);

            bool InitialisePages()
            {
                Type windowType = window.GetType();
                //Find all the documentation and additions to it
                Type iDocGenericType = typeof(IDocumentation <>).MakeGenericType(windowType);
                IEnumerable <IDocumentation <T> >     documentation          = GetExtensionsOfTypeIE <IDocumentation <T> >(iDocGenericType);
                List <DocumentationPage <T> >         documentationPages     = new List <DocumentationPage <T> >();
                List <DocumentationPageAddition <T> > documentationAdditions = new List <DocumentationPageAddition <T> >();

                foreach (IDocumentation <T> iDoc in documentation)
                {
                    switch (iDoc)
                    {
                    case DocumentationPageAddition <T> pageAddition:
                        documentationAdditions.Add(pageAddition);
                        break;

                    case DocumentationPage <T> page:
                        if (page.InjectedButtonLinks?[0].ParentType == windowType)
                        {
                            if (pageRoot != null)
                            {
                                Debug.LogError($"Multiple pages are assigned to be the root for window. {this.pageRoot} & {pageRoot}.");
                                continue;
                            }

                            pageRoot = page;
                        }
                        else
                        {
                            documentationPages.Add(page);
                        }

                        pages.Add(page.GetType().FullName, page);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException($"{iDoc.GetType()} is not present in the DocumentationContent.InitialiseContent page switch.");
                    }

                    iDoc.Initialise(window);
                }

                if (pageRoot == null)
                {
                    Debug.LogError("No root page defined.");
                    return(false);
                }

                //fill the additions dictionary
                foreach (DocumentationPageAddition <T> docAddition in documentationAdditions)
                {
                    Type pageToAddToType = docAddition.PageToAddToType;
                    GetDocumentationPage <DocumentationPageAddition <T> >(pageToAddToType,
                                                                          "does not provide a page to add to.",
                                                                          $"{docAddition.GetType().FullName}'s provided PageToAddToType",
                                                                          additions,
                                                                          docAddition);
                }

                foreach (var additionList in additions.Values)
                {
                    additionList.Sort((a, b) => a.Order.CompareTo(b.Order));
                }


                //Discover the injected buttons
                foreach (DocumentationPage <T> iDoc in documentationPages)
                {
                    DocumentationPage <T> .ButtonInjection[] injectedButtons = iDoc.InjectedButtonLinks;
                    if (injectedButtons == null)
                    {
                        continue;
                    }
                    //For all the buttons in we're injecting
                    foreach (DocumentationPage <T> .ButtonInjection buttonInjection in injectedButtons)
                    {
                        buttonInjection.PageOfOrigin = iDoc;
                        //We find the relevant page and add our injected button to its dictionary entry
                        GetDocumentationPage <DocumentationPage <T> .ButtonInjection>(buttonInjection.ParentType,
                                                                                      "does not provide a page to add to.",
                                                                                      $"{iDoc.GetType().FullName}'s intended location for button injection {buttonInjection.ParentType.FullName}",
                                                                                      injectedButtonLinks, buttonInjection);
                    }
                }

                //Sort the injected buttons
                foreach (List <DocumentationPage <T> .ButtonInjection> buttonInjections in injectedButtonLinks.Values)
                {
                    buttonInjections.Sort((a, b) => a.Order.CompareTo(b.Order));
                }

                void GetDocumentationPage <TType>(Type query, string queryNullError, string queryNotIDocError, Dictionary <IDocumentationPage <T>, List <TType> > dictionary, TType add)
                {
                    if (query == null)
                    {
                        Debug.LogError($"{query.FullName} {queryNullError}");
                        return;
                    }

                    string key = query.FullName;

                    if (!pages.TryGetValue(key, out IDocumentationPage <T> pageToAddTo))
                    {
                        if (!query.IsSubclassOf(typeof(IDocumentation <T>)))
                        {
                            Debug.LogError($"{queryNotIDocError} ({key}) is not a Documentation Page {GetType().FullName}.");
                        }
                        return;
                    }

                    if (!dictionary.TryGetValue(pageToAddTo, out List <TType> addList))
                    {
                        addList = new List <TType>();
                        dictionary.Add(pageToAddTo, addList);
                    }

                    addList.Add(add);
                }

                return(true);
            }
        }