|
@@ -6,7 +6,7 @@ using Sirenix.OdinInspector;
|
|
|
using KairoEngine.UI.InteractionHandler;
|
|
|
|
|
|
[HideMonoScript]
|
|
|
-public class EncyclopediaUi : MonoBehaviour, IClickHandler
|
|
|
+public class EncyclopediaUi : MonoBehaviour
|
|
|
{
|
|
|
public EncyclopediaController controller;
|
|
|
public GameObject menuGameObject;
|
|
@@ -14,14 +14,22 @@ public class EncyclopediaUi : MonoBehaviour, IClickHandler
|
|
|
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()
|
|
|
{
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
private void Update()
|
|
@@ -36,27 +44,131 @@ public class EncyclopediaUi : MonoBehaviour, IClickHandler
|
|
|
public void HideSubmenu() => subMenuGameObject.SetActive(false);
|
|
|
|
|
|
public void Populate()
|
|
|
+ {
|
|
|
+ menuIndex = controller.menuIndex;
|
|
|
+ subMenuIndex = controller.subMenuIndex;
|
|
|
+ currentMenuIndex = controller.menuIndex;
|
|
|
+ currentSubMenuIndex = controller.subMenuIndex;
|
|
|
+ CreateMenu();
|
|
|
+ CreateSubMenu();
|
|
|
+ ShowArticle();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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} buttons");
|
|
|
+ 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(category.buttonPrefab, new Vector3(), Quaternion.identity, menuContainer.transform);
|
|
|
+ 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.layoutName, this);
|
|
|
+ if(handler != null) handler.Setup(category.id, (IClickHandler)controller);
|
|
|
+ var selectedButton = obj.GetComponent<SelectedButton>();
|
|
|
+ if(selectedButton != null) menuButtonSelection.Add(selectedButton);
|
|
|
+ counter += 1;
|
|
|
+ }
|
|
|
+ if(menuIndex < menuButtonSelection.Count)
|
|
|
+ {
|
|
|
+
|
|
|
+ menuButtonSelection[menuIndex].Select();
|
|
|
+ }
|
|
|
+ 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())
|
|
|
+ {
|
|
|
+ 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(showDebug) Debug.Log($"Created {counter} buttons");
|
|
|
+ if(controller.subMenuIndex < subMenuButtonSelection.Count && menuIndex == currentMenuIndex)
|
|
|
+ {
|
|
|
+
|
|
|
+ subMenuButtonSelection[subMenuIndex].Select();
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ foreach (var item in layout.view.GetComponentsInChildren<EncyclopediaArticleUi>(true))
|
|
|
+ {
|
|
|
+ item.Setup(article);
|
|
|
+ }
|
|
|
+ foreach (var btn in subMenuButtonSelection) btn.Deselect();
|
|
|
+ subMenuButtonSelection[subMenuIndex].Select();
|
|
|
+ currentMenuIndex = menuIndex;
|
|
|
+ currentSubMenuIndex = subMenuIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Navigate()
|
|
|
+ {
|
|
|
+ if(controller == null) return;
|
|
|
+
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public EncyclopediaUiView GetView(string title)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < views.Count; i++)
|
|
|
+ {
|
|
|
+ if(views[i].title == title) return views[i];
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- public void OnClick(string title)
|
|
|
+ public void HideViews()
|
|
|
{
|
|
|
- throw new System.NotImplementedException();
|
|
|
+ foreach (var layout in views) layout.view.SetActive(false);
|
|
|
}
|
|
|
|
|
|
[System.Serializable]
|