using Thousandto.Cfg.Data;
using Thousandto.Code.Center;
using Thousandto.Code.Global;
using Thousandto.Plugins.Common;
using System;
using System.Collections.Generic;
using System.Text;
using Thousandto.Core.Base;
using UnityEngine;
using FLogger = UnityEngine.Gonbest.MagicCube.FLogger;
namespace Thousandto.Code.Logic
{
///
/// 主窗体的功能按钮处理系统
///
public class MainFunctionSystem
{
#region//私有变量
//整理后的功能按钮的字典数据
private Dictionary> _dataByPos = new Dictionary>();
//记录提醒信息
private Dictionary _dataByID = new Dictionary();
//缓存开启的功能ID,在某些地图中功能开启效果不展示,进入新的地图才展示
private List _cacheOpenFuncList = new List();
#endregion
#region//静态属性
//主窗体是否创建完成
public static bool MainFormIsCreated { get; set; }
//头顶界面是否显示
public static bool HUDFormIsShowed { get; set; }
//主菜单是否显示
public static bool MainMenuIsShowed { get; set; }
//引导界面是否显示
public static bool GuideFormIsShowed { get; set; }
//主窗体是否正在显示
public static bool IsShowingMainForm { get; set; }
#endregion
#region//静态构造函数
static MainFunctionSystem()
{
MainFormIsCreated = false;
HUDFormIsShowed = false;
MainMenuIsShowed = false;
GuideFormIsShowed = false;
}
#endregion
#region//公共接口: 根据功能ID打开相应功能的
//通过功能ID的枚举码,来打开相应功能
public void DoFunctionCallBack(int code, object param = null)
{
var funcData = GameCenter.MainFunctionSystem.GetFunctionInfo(code);
if (funcData == null)
{
return;
}
if (!funcData.IsVisible)
{
ShowNotOpenTips(funcData);
return;
}
//功能打开全部交给lua处理
GameCenter.LuaSystem.Adaptor.DoFunctionCallBack(code, param);
}
//显示功能未开启提示
public void ShowNotOpenTips(int code)
{
GameCenter.LuaSystem.Adaptor.ShowFuncNotOpenTips(code);
}
public void ShowNotOpenTips(MainFunctionInfo funcData)
{
if (funcData == null)
return;
ShowNotOpenTips(funcData.ID);
}
#endregion
#region //公共接口: 初始化, 功能列表获取,功能设置
//初始化
public void Initialize()
{
MainMenuIsShowed = false;
//遍历功能配置表,填充到字典中
var e = DeclareFunctionStart.CacheData.GetEnumerator();
try
{
while (e.MoveNext())
{
var cfg = e.Current.Value;
var info = new MainFunctionInfo(cfg);
_dataByID.Add((int)info.ID, info);
List list = null;
if (_dataByPos.TryGetValue(cfg.FunctionPosType, out list))
{
list.Add(info);
}
else
{
list = new List();
list.Add(info);
_dataByPos.Add(cfg.FunctionPosType, list);
}
}
}
finally
{
e.Dispose();
}
//遍历字典,链接父子关系
var e2 = _dataByID.GetEnumerator();
try
{
while (e2.MoveNext())
{
var curValue = e2.Current.Value;
if (curValue.Cfg.ParentId != curValue.Cfg.FunctionId)
{
MainFunctionInfo parent = null;
if (_dataByID.TryGetValue(curValue.Cfg.ParentId, out parent))
{
curValue.SetParent(parent);
parent.AddChild(curValue);
}
}
}
}
finally
{
e2.Dispose();
}
//遍历字典数据,对其中的链表进行排序
var e1 = _dataByPos.GetEnumerator();
try
{
while (e1.MoveNext())
{
e1.Current.Value.Sort(Comparison);
}
}
finally
{
e1.Dispose();
}
_cacheOpenFuncList.Clear();
SetFunctionEffectByAlert(FunctionStartIdCode.RealmStifle, true);
//仙人留步功能设置为false,防止登录时关闭弹出此界面
SetFunctionEnabled(FunctionStartIdCode.ExitRewardTips, false);
}
//卸载化
public void Uninitialize()
{
_dataByID.Clear();
_dataByPos.Clear();
_cacheOpenFuncList.Clear();
}
//获取功能信息
public MainFunctionInfo GetFunctionInfo(int id)
{
MainFunctionInfo result = null;
_dataByID.TryGetValue(id, out result);
return result;
}
//获取功能列表 -- 根据位置类型
public List GetFunctionList(MainFunctionPosType posType)
{
if (_dataByPos.ContainsKey((int)posType))
{
return _dataByPos[(int)posType];
}
return new List();
}
//设置功能效果是否显示
public void SetFunctionEffect(int code, bool isShow)
{
var info = GetFunctionInfo(code);
if (info != null)
{
info.SetEffectEnabled(isShow);
}
}
//设置功能效果是否根据红点显示
public void SetFunctionEffectByAlert(int code, bool value)
{
var info = GetFunctionInfo(code);
if (info != null)
{
info.SetEffectByAlert(value);
}
}
//设置功能副标题
public void SetFunctionLittleName(int code, string name)
{
var info = GetFunctionInfo(code);
if (info != null)
{
info.SetLittleName(name);
}
}
//设置功能是否显示在主界面上
public void SetFunctionVisible(int code, bool isShow)
{
var info = GetFunctionInfo(code);
if (info != null)
{
info.SetIsVisible(isShow);
}
}
//设置功能是否开启
public void SetFunctionEnabled(int code, bool enable)
{
var info = GetFunctionInfo(code);
if (info != null)
{
info.SetEnabled(enable);
}
}
//设置警告标记
public void SetAlertFlag(int id, bool alertFlag)
{
var info = GetFunctionInfo(id);
if (info != null)
{
info.IsShowRedPoint = alertFlag;
}
}
//获取提醒标记
public bool GetAlertFlag(int id)
{
var info = GetFunctionInfo(id);
if (info != null)
{
return info.IsShowRedPoint;
}
return false;
}
//功能是否开放
public bool FunctionIsEnabled(int id)
{
var info = GetFunctionInfo(id);
if (info != null)
{
return info.IsEnable;
}
return false;
}
//功能是否开放
public bool FunctionIsVisible(int id)
{
var info = GetFunctionInfo(id);
if (info != null)
{
return info.IsVisible;
}
return false;
}
//功能是否在跨服中不可使用
public bool FunctionIsCanNotUseInCross(int id)
{
var info = GetFunctionInfo(id);
if (info != null)
{
return info.IsCanNotUseInCross;
}
return false;
}
//功能是否在跨服中开放
public bool FuncationIsEnabledInCorss(int id)
{
var info = GetFunctionInfo(id);
if (info != null)
return info.FuncationInCross;
return false;
}
//进入场景,缓存的功能处理
public void OnEnterScene()
{
if (GameCenter.MapLogicSwitch.ShowNewFunction && _cacheOpenFuncList.Count > 0)
{
for (int i = 0; i < _cacheOpenFuncList.Count; ++i)
{
//新功能开启界面
GameCenter.BlockingUpPromptSystem.AddNewFunction(1, _cacheOpenFuncList[i]);
//新功能开启引导检测
GameCenter.GuideSystem.Check(GuideTriggerType.SystemOpen, _cacheOpenFuncList[i]);
}
_cacheOpenFuncList.Clear();
}
}
#endregion
#region//网络服务器接口处理
//功能是否开启的初始化的消息处理
public void GS2U_ResFuncOpenList(MSG_Backend.ResFuncOpenList result)
{
var msgTmpDic = new Dictionary(result.funcOpenList.Count);
for (int i = 0; i < result.funcOpenList.Count; ++i)
{
msgTmpDic[result.funcOpenList[i].id] = result.funcOpenList[i].state;
}
var iter = _dataByID.GetEnumerator();
try
{
while (iter.MoveNext())
{
var info = iter.Current.Value;
bool open = true;
bool getAward = false;
var state = 0;
if (msgTmpDic.TryGetValue(info.Cfg.FunctionId, out state))
{
open = state != 0;
getAward = state == 2;
}
info.SetEnabled(open);
info.SetGetAward(getAward);
if (open)
{
//功能开启处理
OnFunctionOpened(info.ID, false);
}
}
}
finally
{
iter.Dispose();
}
GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_SYNC_FUNCTION_INFO);
}
//功能开启状态切换的消息处理
public void ResSwitchFunction(MSG_Backend.ResSwitchFunction result)
{
var list = result.switchFuncList;
list.Sort((a, b) => { return b.id - a.id; });
for (int i = 0; i < list.Count; ++i)
{
var msgInfo = list[i];
var info = GetFunctionInfo(msgInfo.id);
if (info != null)
{
info.SetEnabled(msgInfo.state != 0);
info.SetGetAward(msgInfo.state == 2);
if (msgInfo.state != 0)
{
if (result.isnew)
{
if (!GameCenter.MapLogicSwitch.ShowNewFunction)
{
_cacheOpenFuncList.Add(msgInfo.id);
}
else
{
//新功能开启界面
GameCenter.BlockingUpPromptSystem.AddNewFunction(1, msgInfo.id);
//新功能开启引导检测
GameCenter.GuideSystem.Check(GuideTriggerType.SystemOpen, msgInfo.id);
}
}
//功能开启处理
OnFunctionOpened(info.ID, result.isnew);
}
}
}
GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_SYNC_FUNCTION_INFO);
}
//领取奖励状态更新
public void GS2U_ResFuncOpenReward(MSG_Backend.ResFuncOpenReward result)
{
//领取成功
var cfg = DeclareFunctionOpenTips.Get(result.id);
if (cfg != null)
{
var info = GetFunctionInfo(cfg.FunctionId);
if (info != null)
{
info.SetGetAward(true);
GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_SYNC_FUNCTION_INFO);
}
}
}
#endregion
#region//私有方法
//两个功能配置的比较方法---用于list的排序
private int Comparison(MainFunctionInfo x, MainFunctionInfo y)
{
return x.SortNum - y.SortNum;
}
//处理功能开启事件
public void OnFunctionOpened(int idCode, bool isNew)
{
//功能开启事件全部交给lua处理
GameCenter.LuaSystem.Adaptor.OnFunctionOpened(idCode, isNew);
}
#endregion
}
}