|
@@ -5,179 +5,182 @@ using UnityEngine.UI;
|
|
|
using Sirenix.OdinInspector;
|
|
|
using KairoEngine.UI.InteractionHandler;
|
|
|
|
|
|
-[HideMonoScript]
|
|
|
-public class EncyclopediaUi : MonoBehaviour
|
|
|
+namespace KairoEngine.UI.Encyclopedia
|
|
|
{
|
|
|
- public EncyclopediaController controller;
|
|
|
- public GameObject menuGameObject;
|
|
|
- public GameObject subMenuGameObject;
|
|
|
- public GameObject viewsGameObject;
|
|
|
- public Transform menuContainer;
|
|
|
- public Transform subMenuContainer;
|
|
|
- public bool createOnStart = true;
|
|
|
- public bool showDebug = false;
|
|
|
- public List<EncyclopediaUiView> views;
|
|
|
- private List<SelectedButton> menuButtonSelection;
|
|
|
- private List<SelectedButton> subMenuButtonSelection;
|
|
|
- private int menuIndex = -1;
|
|
|
- private int subMenuIndex = -1;
|
|
|
- private int currentMenuIndex = -1;
|
|
|
- private int currentSubMenuIndex = -1;
|
|
|
-
|
|
|
- private void Start()
|
|
|
+ [HideMonoScript]
|
|
|
+ public class EncyclopediaUi : MonoBehaviour
|
|
|
{
|
|
|
- if(menuGameObject == null) Debug.LogError("Missing Menu GameObject in EncyclopediaUi", this.gameObject);
|
|
|
- if(subMenuGameObject == null) Debug.LogError("Missing submenu GameObject in EncyclopediaUi", this.gameObject);
|
|
|
- if(viewsGameObject == null) Debug.LogError("Missing views GameObject in EncyclopediaUi", this.gameObject);
|
|
|
- if(createOnStart) Populate();
|
|
|
- }
|
|
|
+ public EncyclopediaController controller;
|
|
|
+ public GameObject menuGameObject;
|
|
|
+ public GameObject subMenuGameObject;
|
|
|
+ public GameObject viewsGameObject;
|
|
|
+ public Transform menuContainer;
|
|
|
+ public Transform subMenuContainer;
|
|
|
+ public bool createOnStart = true;
|
|
|
+ public bool showDebug = false;
|
|
|
+ public List<EncyclopediaUiView> views;
|
|
|
+ private List<SelectedButton> menuButtonSelection;
|
|
|
+ private List<SelectedButton> subMenuButtonSelection;
|
|
|
+ private int menuIndex = -1;
|
|
|
+ private int subMenuIndex = -1;
|
|
|
+ private int currentMenuIndex = -1;
|
|
|
+ private int currentSubMenuIndex = -1;
|
|
|
|
|
|
- private void Update()
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
+ private void Start()
|
|
|
+ {
|
|
|
+ if(menuGameObject == null) Debug.LogError("Missing Menu GameObject in EncyclopediaUi", this.gameObject);
|
|
|
+ if(subMenuGameObject == null) Debug.LogError("Missing submenu GameObject in EncyclopediaUi", this.gameObject);
|
|
|
+ if(viewsGameObject == null) Debug.LogError("Missing views GameObject in EncyclopediaUi", this.gameObject);
|
|
|
+ if(createOnStart) Populate();
|
|
|
+ }
|
|
|
|
|
|
- [Button("Show Submenu"), ButtonGroup("EditorActions")]
|
|
|
- public void ShowSubmenu() => subMenuGameObject.SetActive(true);
|
|
|
+ private void Update()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- [Button("Hide Submenu"), ButtonGroup("EditorActions")]
|
|
|
- public void HideSubmenu() => subMenuGameObject.SetActive(false);
|
|
|
+ [Button("Show Submenu"), ButtonGroup("EditorActions")]
|
|
|
+ public void ShowSubmenu() => subMenuGameObject.SetActive(true);
|
|
|
|
|
|
- public void Populate()
|
|
|
- {
|
|
|
- if(showDebug) Debug.Log("Creating Encyclopedia UI Element");
|
|
|
- menuIndex = controller.menuIndex;
|
|
|
- subMenuIndex = controller.subMenuIndex;
|
|
|
- currentMenuIndex = controller.menuIndex;
|
|
|
- currentSubMenuIndex = controller.subMenuIndex;
|
|
|
- CreateMenu();
|
|
|
- CreateSubMenu();
|
|
|
- ShowArticle();
|
|
|
- }
|
|
|
+ [Button("Hide Submenu"), ButtonGroup("EditorActions")]
|
|
|
+ public void HideSubmenu() => subMenuGameObject.SetActive(false);
|
|
|
|
|
|
- public void CreateMenu()
|
|
|
- {
|
|
|
- if(controller == null || menuContainer == null) return;
|
|
|
- menuButtonSelection = new List<SelectedButton>();
|
|
|
- Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
|
|
|
- int counter = 0;
|
|
|
- //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} menu buttons");
|
|
|
- foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
|
|
|
- foreach (EncyclopediaCategory category in controller.categories)
|
|
|
+ public void Populate()
|
|
|
{
|
|
|
- var obj = Instantiate(controller.categoryButtomPrefab, new Vector3(), Quaternion.identity, menuContainer.transform);
|
|
|
- var btnData = obj.GetComponent<ButtonData>();
|
|
|
- if(btnData != null) btnData.Setup(category.title, category.icon);
|
|
|
- var handler = obj.GetComponent<ClickHandler>();
|
|
|
- if(handler != null) handler.Setup(category.id, (IClickHandler)controller);
|
|
|
- var selectedButton = obj.GetComponent<SelectedButton>();
|
|
|
- if(selectedButton != null) menuButtonSelection.Add(selectedButton);
|
|
|
- counter += 1;
|
|
|
+ if(showDebug) Debug.Log("Creating Encyclopedia UI Element");
|
|
|
+ menuIndex = controller.menuIndex;
|
|
|
+ subMenuIndex = controller.subMenuIndex;
|
|
|
+ currentMenuIndex = controller.menuIndex;
|
|
|
+ currentSubMenuIndex = controller.subMenuIndex;
|
|
|
+ CreateMenu();
|
|
|
+ CreateSubMenu();
|
|
|
+ ShowArticle();
|
|
|
}
|
|
|
- if(menuIndex < menuButtonSelection.Count)
|
|
|
+
|
|
|
+ public void CreateMenu()
|
|
|
{
|
|
|
- //menuIndex = controller.menuIndex;
|
|
|
- menuButtonSelection[menuIndex].Select();
|
|
|
+ if(controller == null || menuContainer == null) return;
|
|
|
+ menuButtonSelection = new List<SelectedButton>();
|
|
|
+ Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
|
|
|
+ int counter = 0;
|
|
|
+ //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} menu buttons");
|
|
|
+ foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
|
|
|
+ foreach (EncyclopediaCategory category in controller.categories)
|
|
|
+ {
|
|
|
+ var obj = Instantiate(controller.categoryButtomPrefab, new Vector3(), Quaternion.identity, menuContainer.transform);
|
|
|
+ var btnData = obj.GetComponent<ButtonData>();
|
|
|
+ if(btnData != null) btnData.Setup(category.title, category.icon);
|
|
|
+ var handler = obj.GetComponent<ClickHandler>();
|
|
|
+ if(handler != null) handler.Setup(category.id, (IClickHandler)controller);
|
|
|
+ var selectedButton = obj.GetComponent<SelectedButton>();
|
|
|
+ if(selectedButton != null) menuButtonSelection.Add(selectedButton);
|
|
|
+ var categoryDataViews = obj.GetComponentsInChildren<IEncyclopediaCategoryDataView>(true);
|
|
|
+ if(categoryDataViews != null) foreach (var view in categoryDataViews) view.Setup(category);
|
|
|
+ counter += 1;
|
|
|
+ }
|
|
|
+ if(menuIndex < menuButtonSelection.Count)
|
|
|
+ {
|
|
|
+ //menuIndex = controller.menuIndex;
|
|
|
+ menuButtonSelection[menuIndex].Select();
|
|
|
+ }
|
|
|
+ //if(showDebug) Debug.Log($"Created {counter} menu buttons");
|
|
|
}
|
|
|
- //if(showDebug) Debug.Log($"Created {counter} menu buttons");
|
|
|
- }
|
|
|
|
|
|
- public void CreateSubMenu()
|
|
|
- {
|
|
|
- if(controller == null || subMenuContainer == null) return;
|
|
|
- subMenuButtonSelection = new List<SelectedButton>();
|
|
|
- Button[] oldButtons = subMenuContainer.GetComponentsInChildren<Button>();
|
|
|
- int counter = 0;
|
|
|
- var category = controller.categories[controller.menuIndex];
|
|
|
- category.UpdateArticles();
|
|
|
- //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} sub menu buttons");
|
|
|
- foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
|
|
|
- foreach (EncyclopediaArticle article in category.GetArticles())
|
|
|
+ public void CreateSubMenu()
|
|
|
{
|
|
|
- var obj = Instantiate(category.buttonPrefab, new Vector3(), Quaternion.identity, subMenuContainer.transform);
|
|
|
- var btnData = obj.GetComponent<ButtonData>();
|
|
|
- Sprite icon = null;
|
|
|
- if(article.icon != null) icon = article.icon;
|
|
|
- else icon = category.icon;
|
|
|
- if(btnData != null) btnData.Setup(article.title, icon);
|
|
|
- var handler = obj.GetComponent<ClickHandler>();
|
|
|
- if(handler != null) handler.Setup(article.id, (IClickHandler)controller);
|
|
|
- var selectedButton = obj.GetComponent<SelectedButton>();
|
|
|
- if(selectedButton != null) subMenuButtonSelection.Add(selectedButton);
|
|
|
- counter += 1;
|
|
|
+ if(controller == null || subMenuContainer == null) return;
|
|
|
+ subMenuButtonSelection = new List<SelectedButton>();
|
|
|
+ Button[] oldButtons = subMenuContainer.GetComponentsInChildren<Button>();
|
|
|
+ int counter = 0;
|
|
|
+ var category = controller.categories[controller.menuIndex];
|
|
|
+ category.UpdateArticles();
|
|
|
+ //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} sub menu buttons");
|
|
|
+ foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
|
|
|
+ foreach (EncyclopediaArticle article in category.GetArticles())
|
|
|
+ {
|
|
|
+ var obj = Instantiate(category.buttonPrefab, new Vector3(), Quaternion.identity, subMenuContainer.transform);
|
|
|
+ var btnData = obj.GetComponent<ButtonData>();
|
|
|
+ Sprite icon = null;
|
|
|
+ if(article.icon != null) icon = article.icon;
|
|
|
+ else icon = category.icon;
|
|
|
+ if(btnData != null) btnData.Setup(article.title, icon);
|
|
|
+ var handler = obj.GetComponent<ClickHandler>();
|
|
|
+ if(handler != null) handler.Setup(article.id, (IClickHandler)controller);
|
|
|
+ var selectedButton = obj.GetComponent<SelectedButton>();
|
|
|
+ if(selectedButton != null) subMenuButtonSelection.Add(selectedButton);
|
|
|
+ var contentDataViews = obj.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
|
|
|
+ foreach (var item in contentDataViews) item.Setup(article);
|
|
|
+ counter += 1;
|
|
|
+ }
|
|
|
+ if(controller.subMenuIndex < subMenuButtonSelection.Count && menuIndex == currentMenuIndex)
|
|
|
+ {
|
|
|
+ //subMenuIndex = controller.subMenuIndex;
|
|
|
+ subMenuButtonSelection[subMenuIndex].Select();
|
|
|
+ }
|
|
|
+ //if(showDebug) Debug.Log($"Created {counter} sub menu buttons");
|
|
|
}
|
|
|
- if(controller.subMenuIndex < subMenuButtonSelection.Count && menuIndex == currentMenuIndex)
|
|
|
+
|
|
|
+ public void ShowArticle()
|
|
|
{
|
|
|
- //subMenuIndex = controller.subMenuIndex;
|
|
|
+ if(controller == null) return;
|
|
|
+ var category = controller.categories[menuIndex];
|
|
|
+ var articles = category.GetArticles();
|
|
|
+ var article = articles[subMenuIndex];
|
|
|
+ var layout = GetView(category.layoutName);
|
|
|
+ if(layout == null) return;
|
|
|
+ HideViews();
|
|
|
+ layout.view.SetActive(true);
|
|
|
+ var contentDataViews = layout.view.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
|
|
|
+ foreach (var item in contentDataViews) item.Setup(article);
|
|
|
+ foreach (var btn in subMenuButtonSelection) btn.Deselect();
|
|
|
subMenuButtonSelection[subMenuIndex].Select();
|
|
|
+ currentMenuIndex = menuIndex;
|
|
|
+ currentSubMenuIndex = subMenuIndex;
|
|
|
}
|
|
|
- //if(showDebug) Debug.Log($"Created {counter} sub menu buttons");
|
|
|
- }
|
|
|
|
|
|
- public void ShowArticle()
|
|
|
- {
|
|
|
- if(controller == null) return;
|
|
|
- var category = controller.categories[menuIndex];
|
|
|
- var articles = category.GetArticles();
|
|
|
- var article = articles[subMenuIndex];
|
|
|
- var layout = GetView(category.layoutName);
|
|
|
- if(layout == null) return;
|
|
|
- HideViews();
|
|
|
- layout.view.SetActive(true);
|
|
|
- var contentDataViews = layout.view.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
|
|
|
- if(showDebug) Debug.Log($"Showing article content in layout {category.layoutName} and found {contentDataViews.Length} dataViewComponents");
|
|
|
- foreach (var item in contentDataViews)
|
|
|
+ public void Navigate()
|
|
|
{
|
|
|
- item.Setup(article);
|
|
|
+ if(controller == null) return;
|
|
|
+ // Navigated to different category
|
|
|
+ if(menuIndex != controller.menuIndex)
|
|
|
+ {
|
|
|
+ menuIndex = controller.menuIndex;
|
|
|
+ CreateMenu();
|
|
|
+ CreateSubMenu();
|
|
|
+ }
|
|
|
+ else if (currentMenuIndex != menuIndex && currentSubMenuIndex == subMenuIndex)
|
|
|
+ {
|
|
|
+ if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
|
|
|
+ else subMenuIndex = controller.subMenuIndex;
|
|
|
+ ShowArticle();
|
|
|
+ }
|
|
|
+ if(subMenuIndex != controller.subMenuIndex)
|
|
|
+ {
|
|
|
+ if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
|
|
|
+ else subMenuIndex = controller.subMenuIndex;
|
|
|
+ ShowArticle();
|
|
|
+ }
|
|
|
}
|
|
|
- foreach (var btn in subMenuButtonSelection) btn.Deselect();
|
|
|
- subMenuButtonSelection[subMenuIndex].Select();
|
|
|
- currentMenuIndex = menuIndex;
|
|
|
- currentSubMenuIndex = subMenuIndex;
|
|
|
- }
|
|
|
|
|
|
- public void Navigate()
|
|
|
- {
|
|
|
- if(controller == null) return;
|
|
|
- // Navigated to different category
|
|
|
- if(menuIndex != controller.menuIndex)
|
|
|
+ public EncyclopediaUiView GetView(string title)
|
|
|
{
|
|
|
- menuIndex = controller.menuIndex;
|
|
|
- CreateMenu();
|
|
|
- CreateSubMenu();
|
|
|
+ for (int i = 0; i < views.Count; i++)
|
|
|
+ {
|
|
|
+ if(views[i].title == title) return views[i];
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
- else if (currentMenuIndex != menuIndex && currentSubMenuIndex == subMenuIndex)
|
|
|
+
|
|
|
+ public void HideViews()
|
|
|
{
|
|
|
- if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
|
|
|
- else subMenuIndex = controller.subMenuIndex;
|
|
|
- ShowArticle();
|
|
|
+ foreach (var layout in views) layout.view.SetActive(false);
|
|
|
}
|
|
|
- if(subMenuIndex != controller.subMenuIndex)
|
|
|
- {
|
|
|
- if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
|
|
|
- else subMenuIndex = controller.subMenuIndex;
|
|
|
- ShowArticle();
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- public EncyclopediaUiView GetView(string title)
|
|
|
- {
|
|
|
- for (int i = 0; i < views.Count; i++)
|
|
|
+ [System.Serializable]
|
|
|
+ public class EncyclopediaUiView
|
|
|
{
|
|
|
- if(views[i].title == title) return views[i];
|
|
|
+ [HorizontalGroup("Line", 0.4f), HideLabel] public string title;
|
|
|
+ [HorizontalGroup("Line", 0.6f), HideLabel] public GameObject view;
|
|
|
}
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public void HideViews()
|
|
|
- {
|
|
|
- foreach (var layout in views) layout.view.SetActive(false);
|
|
|
- }
|
|
|
-
|
|
|
- [System.Serializable]
|
|
|
- public class EncyclopediaUiView
|
|
|
- {
|
|
|
- [HorizontalGroup("Line", 0.4f), HideLabel] public string title;
|
|
|
- [HorizontalGroup("Line", 0.6f), HideLabel] public GameObject view;
|
|
|
}
|
|
|
-}
|
|
|
+}
|