Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
GameWidget.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
3using Game;
5
6public class GameWidget : CanvasWidget {
7 public List<Camera> m_cameras = new();
8 public Dictionary<Camera, Func<GameWidget, bool>> m_isCameraEnable = new();
9
11
12 public ViewWidget ViewWidget { get; set; }
13
14 public ContainerWidget GuiWidget { get; set; }
15
16 public int GameWidgetIndex { get; set; }
17
19
20 public PlayerData PlayerData { get; set; }
21
22 public ReadOnlyList<Camera> Cameras => new(m_cameras);
23
25 get => m_activeCamera;
26 set {
27 if (value == null
28 || value.GameWidget != this) {
29 throw new InvalidOperationException("Invalid camera.");
30 }
31 if (!IsCameraAllowed(value)) {
32 value = FindCamera<FppCamera>();
33 }
34 if (value != m_activeCamera) {
35 Camera activeCamera = m_activeCamera;
36 m_activeCamera = value;
37 m_activeCamera.Activate(activeCamera);
38 }
39 }
40 }
41
42 public ComponentCreature Target { get; set; }
43
44 public GameWidget(PlayerData playerData, int gameViewIndex) {
45 PlayerData = playerData;
46 GameWidgetIndex = gameViewIndex;
48 LoadContents(this, ContentManager.Get<XElement>("Widgets/GameWidget"));
49 ViewWidget = Children.Find<ViewWidget>("View");
50 GuiWidget = Children.Find<ContainerWidget>("Gui");
51 AddCamera(new FppCamera(this));
52 AddCamera(new DeathCamera(this));
53 AddCamera(new IntroCamera(this));
54 AddCamera(new TppCamera(this));
55 AddCamera(new OrbitCamera(this));
56 AddCamera(new FixedCamera(this));
57 AddCamera(new LoadingCamera(this));
59 "ManageCameras",
60 modLoader => {
61 modLoader.ManageCameras(this);
62 return false;
63 }
64 );
65 List<KeyValuePair<string, int>> list = ModSettingsManager.CombinedCameraManageSettings.OrderBy(x => x.Value).ToList();
66 int num = 0;
67 foreach (KeyValuePair<string, int> item in list) {
68 string name = item.Key;
69 int value = item.Value;
70 if (value >= 0) { //刷新列表时重新按顺序分配值,避免出现空缺
72 num++;
73 }
74 }
76 }
77
78 public T FindCamera<T>(bool throwOnError = true) where T : Camera {
79 T val = (T)m_cameras.FirstOrDefault(c => c is T);
80 if (val != null
81 || !throwOnError) {
82 return val;
83 }
84 throw new InvalidOperationException($"Camera with type \"{typeof(T).Name}\" not found.");
85 }
86
87 public Camera FindCamera(Type type, bool throwOnError = true) {
88 Camera val = m_cameras.FirstOrDefault(c => c.GetType() == type);
89 if (val != null
90 || !throwOnError) {
91 return val;
92 }
93 throw new InvalidOperationException($"Camera with type \"{type.Name}\" not found.");
94 }
95
102 public Camera FindCamera(Type type, out bool isEnable, bool throwOnError = true) {
103 isEnable = true;
104 Camera result = FindCamera(type, throwOnError);
105 if (m_isCameraEnable.TryGetValue(result, out Func<GameWidget, bool> func)) {
106 isEnable = func?.Invoke(this) ?? true;
107 }
108 return result;
109 }
110
116 public void AddCamera(Camera camera, Func<GameWidget, bool> isEnable = null) {
117 if (camera == null) {
118 return;
119 }
120 m_cameras.Add(camera);
121 if (isEnable != null) {
122 m_isCameraEnable.Add(camera, isEnable);
123 }
124 }
125
126 public bool IsEntityTarget(Entity entity) {
127 if (Target != null) {
128 return Target.Entity == entity;
129 }
130 return false;
131 }
132
133 public bool IsEntityFirstPersonTarget(Entity entity) {
134 if (IsEntityTarget(entity)) {
135 return ActiveCamera is FppCamera;
136 }
137 return false;
138 }
139
140 public override void Update() {
141 WidgetInputDevice widgetInputDevice = DetermineInputDevices();
142 if (WidgetsHierarchyInput == null
143 || WidgetsHierarchyInput.Devices != widgetInputDevice) {
144 WidgetsHierarchyInput = new WidgetInput(widgetInputDevice);
145 }
146 if ((widgetInputDevice & WidgetInputDevice.MultiMice) != 0
147 && (widgetInputDevice & WidgetInputDevice.Mouse) == 0) {
148 WidgetsHierarchyInput.UseSoftMouseCursor = true;
149 }
150 else {
151 WidgetsHierarchyInput.UseSoftMouseCursor = false;
152 }
153 if (GuiWidget.ParentWidget == null) {
155 }
156 }
157
159 bool flag = false;
160 foreach (PlayerData playersDatum in PlayerData.SubsystemPlayers.PlayersData) {
161 if ((playersDatum.InputDevice & WidgetInputDevice.MultiMice) != 0) {
162 flag = true;
163 }
164 }
165 WidgetInputDevice widgetInputDevice = WidgetInputDevice.None;
166 foreach (WidgetInputDevice allInputDevice in PlayerScreen.AllInputDevices) {
167 if (!flag
168 || allInputDevice != (WidgetInputDevice.Keyboard | WidgetInputDevice.Mouse)) {
169 widgetInputDevice |= allInputDevice;
170 }
171 }
172 if (PlayerData.SubsystemPlayers.PlayersData.Count > 0
173 && PlayerData == PlayerData.SubsystemPlayers.PlayersData[0]) {
174 WidgetInputDevice widgetInputDevice2 = WidgetInputDevice.None;
175 foreach (PlayerData playersDatum2 in PlayerData.SubsystemPlayers.PlayersData) {
176 if (playersDatum2 != PlayerData) {
177 widgetInputDevice2 |= playersDatum2.InputDevice;
178 }
179 }
180 return (widgetInputDevice & ~widgetInputDevice2) | WidgetInputDevice.Touch | PlayerData.InputDevice;
181 }
182 WidgetInputDevice widgetInputDevice3 = WidgetInputDevice.None;
183 foreach (PlayerData playersDatum3 in PlayerData.SubsystemPlayers.PlayersData) {
184 if (playersDatum3 == PlayerData) {
185 break;
186 }
187 widgetInputDevice3 |= playersDatum3.InputDevice;
188 }
189 return (PlayerData.InputDevice & ~widgetInputDevice3) | WidgetInputDevice.Touch;
190 }
191
192 public bool IsCameraAllowed(Camera camera) {
193 if (camera is LoadingCamera) {
194 return false;
195 }
196 return true;
197 }
198}
readonly WidgetsList Children
static object Get(Type type, string name)
static Dictionary< string, int > CombinedCameraManageSettings
SubsystemGameWidgets SubsystemGameWidgets
WidgetInputDevice InputDevice
static ReadOnlyList< WidgetInputDevice > AllInputDevices
static void SetCameraManageSetting(string keyName, int value)
仅用于修改现有相机配置,添加相机配置请使用ModLoader.GetCameraList
WidgetInput WidgetsHierarchyInput
static void UpdateWidgetsHierarchy(Widget rootWidget)
virtual void LoadContents(object eventsTarget, XElement node)
ReadOnlyList< Camera > Cameras
WidgetInputDevice DetermineInputDevices()
Dictionary< Camera, Func< GameWidget, bool > > m_isCameraEnable
ViewWidget ViewWidget
bool IsEntityFirstPersonTarget(Entity entity)
Camera m_activeCamera
ContainerWidget GuiWidget
SubsystemGameWidgets SubsystemGameWidgets
bool IsEntityTarget(Entity entity)
List< Camera > m_cameras
T FindCamera< T >(bool throwOnError=true)
PlayerData PlayerData
bool IsCameraAllowed(Camera camera)
GameWidget(PlayerData playerData, int gameViewIndex)
void AddCamera(Camera camera, Func< GameWidget, bool > isEnable=null)
此方法建议在ModLoader.ManageCameras接口中使用,避免重复添加。若无需结合条件判断摄像机是否可用(比如调试视角仅创造模式可用),则isEnable可传null
override void Update()
Camera ActiveCamera
Camera FindCamera(Type type, bool throwOnError=true)
ComponentCreature Target
Camera FindCamera(Type type, out bool isEnable, bool throwOnError=true)
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook