215 lines
7.2 KiB
C#
Raw Blame History

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using TMPro;
using System.Collections.Generic;
using System;
namespace QFramework.Example
{
public class UIKnowledgeData : UIPanelData
{
public XMLTool.Knowledge knowledge;
}
public partial class UIKnowledge : UIPanel
{
ResLoader loader;
Transform content;
Transform subContent;
Dictionary<string, Sprite> sprites = new Dictionary<string, Sprite>();
IAction curAction;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIKnowledgeData ?? new UIKnowledgeData();
// please add init code here
loader = ResLoader.Allocate();
CloseBtn.onClick.AddListener(() =>
{
//Hide();
Hide();
TypeEventSystem.Global.Send<OnModuleQuit>();
UIKit.ClosePanel<UIRightTop>();
UIKit.OpenPanelAsync<UIModuleSelect>().ToAction().StartGlobal();
});
}
protected override void OnOpen(IUIData uiData = null)
{
if (Global.Instance.curModule.knowledge != null && Global.Instance.curModule.knowledge.items != null)
{
LeftContent.RemoveAllChildren();
GlobalComs.RemoveAllChildren();
string bg = Global.Instance.curModule.knowledge.bgImage;
LoadBgImage(bg, () =>
{
SetRightContentPos("");
RefreshComponents(Global.Instance.curModule.knowledge.components, GlobalComs);
});
foreach (var item in Global.Instance.curModule.knowledge.items)
{
ItemFactory(item);
}
}
}
public void ItemFactory(XMLTool.Knowledge.Item itemData, Transform parent = null)
{
Transform content = parent == null ? LeftContent : parent;
GameObject leftObj = GameObject.Instantiate(LeftItem.gameObject, content);
Transform subContent = leftObj.transform.Find("SubContent");
Toggle toggle = leftObj.transform.Find("Toggle").GetComponent<Toggle>();
TextMeshProUGUI label = toggle.transform.Find("Name").GetComponent<TextMeshProUGUI>();
label.text = itemData.title;
toggle.onValueChanged.AddListener(isOn =>
{
if (isOn)
{
LoadBgImage(itemData.bgImage, () =>
{
SetRightContentPos(itemData.setPos);
});
ItemComs.RemoveAllChildren();
RefreshComponents(itemData.components, ItemComs);
}
if (subContent.childCount > 0)
{
subContent.gameObject.SetActive(isOn);
if (isOn)
{
subContent.GetChild(0).GetComponentInChildren<Toggle>().isOn = true;
}
}
else
{
label.color = isOn == true ? Color.blue : Color.black;
}
});
if (itemData.subs != null && itemData.subs.Count > 0)
{
foreach (var sub in itemData.subs)
{
ItemFactory(sub, subContent);
}
}
else
{
toggle.group = LeftContent.GetComponent<ToggleGroup>();
}
}
/// <summary>
/// ˢ<>µ<EFBFBD>ǰҳ<C7B0><D2B3><EFBFBD>ĹҼ<C4B9>
/// </summary>
public void RefreshComponents(List<XMLTool.Knowledge.Item.Component> coms, Transform content)
{
int numIndex = 0;
if (coms != null && coms.Count > 0)
{
foreach (var ComData in coms)
{
switch (ComData.type)
{
case "Button":
GameObject btn = GameObject.Instantiate(BtnPrefab.gameObject, content);
btn.name ="Btn" +numIndex;
btn.GetComponent<RectTransform>().sizeDelta = Utility.GetVector2FromStrArray(ComData.size);
btn.transform.localPosition = Utility.GetVector2FromStrArray(ComData.pos);
btn.GetComponent<Button>().onClick.AddListener(() =>
{
if (curAction != null)
{
curAction.Deinit();
curAction = null;
}
curAction = ActionHelper.GetActionAndSub(ComData.action);
curAction.StartGlobal(() =>
{
curAction = null;
});
});
numIndex++;
break;
}
}
}
}
public void LoadBgImage(string bg, Action callback = null)
{
if (string.IsNullOrEmpty(bg) == false)
{
if (sprites.ContainsKey(bg))
{
RightContent.sprite = sprites[bg];
RightContent.SetNativeSize();
callback?.Invoke();
}
else
{
var localImageUrl = Global.imagePath + bg;
loader.Add2Load(localImageUrl.ToNetImageResName(), (success, res) =>
{
if (success)
{
Sprite sprite = Utility.GetSprite(res.Asset.As<Texture2D>());
if (sprites.ContainsKey(bg) == false)
{
sprites.Add(bg, sprite);
}
}
});
TypeEventSystem.Global.Send<OnLoadingShow>();
loader.LoadAsync(() =>
{
callback?.Invoke();
RightContent.sprite = sprites[bg];
RightContent.SetNativeSize();
TypeEventSystem.Global.Send<OnLoadingHide>();
});
}
}
else
{
callback?.Invoke();
}
}
public void SetRightContentPos(string setPos)
{
Vector2 pos = default;
if (string.IsNullOrEmpty(setPos) == false)
{
pos = Utility.GetVector2FromStrArray(setPos);
}
RightContent.rectTransform.anchoredPosition = pos;
}
protected override void OnShow()
{
}
protected override void OnHide()
{
AudioKit.StopVoice();
}
protected override void OnClose()
{
AudioKit.StopVoice();
sprites.Clear();
loader.ReleaseAllRes();
}
}
}