mycj_demo/mycj/Assets/Game/Scripts/FB-Framework/FB-MVC/FBMVC.cs
2024-12-02 09:37:47 +08:00

121 lines
3.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Felix-BangFBMVC中间量
//   へ     /|
//  /7    ∠_/
//  / │    
//  Z _,    /`ヽ
// │     ヽ   /  〉
//  Y     `  /  /
// イ● 、 ●  ⊂⊃〈  /
// ()  へ    | \〈
//  >ー 、_  ィ  │
//  / へ   / ノ<|
//  ヽ_ノ  (_  │//
//  7       |
//  ―r ̄ ̄`ー―_
// DescribeMVC的中间类用于储存M、V、C分发事件
// Createtime2018/9/19
using System;
using System.Collections.Generic;
namespace FBFramework
{
public static class FBMVC
{
//用字典存储MVC
public static Dictionary<string, FBModel> Models = new Dictionary<string, FBModel>(); //名字---模型
public static Dictionary<string, FBView> Views = new Dictionary<string, FBView>(); //名字---视图
public static Dictionary<string, Type> CommandMap = new Dictionary<string, Type>(); //事件---控制器类型
#region
/// <summary>
/// 注册Model
/// </summary>
/// <param name="model">Model实例</param>
public static void RegisterModel(FBModel model)
{
Models[model.Name] = model;
}
/// <summary>
/// 注册View
/// </summary>
/// <param name="model">View实例</param>
public static void RegisterView(FBView view)
{
//防止重复
if (Views.ContainsKey(view.name))
Views.Remove(view.name);
view.RegisterEvents();
Views[view.name] = view;
}
/// <summary>
/// 注册控制器
/// </summary>
/// <param name="eventName">事件名称</param>
/// <param name="controllerType">控制器类型</param>
public static void RegisterController(string eventName,Type controllerType)
{
CommandMap[eventName] = controllerType;
}
#endregion
#region
public static T GetModel<T>()
where T:FBModel
{
foreach (FBModel m in Models.Values)
{
if (m is T)
return m as T;
}
return null;
}
public static T GetView<T>()
where T : FBView
{
foreach (FBView v in Views.Values)
{
if (v is T)
return v as T;
}
return null;
}
#endregion
/// <summary>
/// 发送事件
/// </summary>
/// <param name="eventName">事件名称</param>
/// <param name="data">携带信息(可空)</param>
public static void SendEvent(string eventName, object data = null)
{
//控制器一般对变化有逻辑性的处理,然后返回一个处理结果;
//View仅随变化实现相应的显示
//因此要先对控制器响应再View
//控制器响应
if (CommandMap.ContainsKey(eventName))
{
Type t = CommandMap[eventName];
FBController c = Activator.CreateInstance(t) as FBController;
c.Execute(data);
}
//视图响应
foreach (FBView v in Views.Values)
{
if (v.EventLists.Contains(eventName))
v.HandleEvent(eventName, data);
}
}
}
}