Skip to content

Commit

Permalink
优化与清理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
will258012 committed Aug 29, 2024
1 parent 5f906d1 commit f2bc8d5
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 125 deletions.
4 changes: 2 additions & 2 deletions FPSCamera/Code/Cam/Controller/FPSCamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void EnableCam()
CamInfoPanel.Instance.EnableCamInfoPanel();
if (ModSettings.HideGameUI)
{
UIManager.ToggleUI(false);
StartCoroutine(UIManager.ToggleUI(false));
}
GameCamController.Instance.Initialize();
//StartCoroutine(ShadowsManager.ToggleShadowsOptimization(true));
Expand Down Expand Up @@ -72,8 +72,8 @@ private void AfterTransition()
{
if (ModSettings.HideGameUI)
{
UIManager.ToggleUI(true);
GameCamController.Instance.MainCamera.rect = GameCamController.Instance._cachedRect;
StartCoroutine(UIManager.ToggleUI(true));
}
GameCamController.Instance.Restore();
}
Expand Down
2 changes: 1 addition & 1 deletion FPSCamera/Code/Cam/Controller/GameCamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Restore()
if (_camDoF != null) _camDoF.enabled = _IsDoFEnabled;
if (_camTiltEffect != null) _camTiltEffect.enabled = _IsTiltEffectEnabled;

_mainCamera.fieldOfView= _cachedfieldOfView;
_mainCamera.fieldOfView = _cachedfieldOfView;
_mainCamera.farClipPlane = _cachednearClipPlane;

_controller.enabled = true;
Expand Down
4 changes: 3 additions & 1 deletion FPSCamera/Code/Game/UIManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ColossalFramework.UI;
using FPSCamera.Utils;
using System.Collections;
using UnityEngine;
using Log = AlgernonCommons.Logging;
using ToggleItManager = ToggleIt.Managers.ToggleManager;
Expand All @@ -18,7 +19,7 @@ private class UIState

private static UIState savedState;

public static void ToggleUI(bool visibility)
public static IEnumerator ToggleUI(bool visibility)
{
if (ModSupport.FoundToggleIt)
{
Expand All @@ -37,6 +38,7 @@ public static void ToggleUI(bool visibility)
{
SetUIVisibilityDirectly(visibility);
}
yield break;
}

private static void SaveState()
Expand Down
4 changes: 2 additions & 2 deletions FPSCamera/Code/Settings/Tabs/CameraOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace FPSCamera.Settings.Tabs
{
public sealed partial class CameraOptions
public sealed class CameraOptions
{
// Layout constants.
private const float Margin = 5f;
Expand Down Expand Up @@ -122,7 +122,7 @@ internal CameraOptions(UITabstrip tabStrip, int tabIndex)
rotateKeyFactor_Slider.eventValueChanged += (_, value) => ModSettings.RotateKeyFactor = value;
currentY += rotateKeyFactor_Slider.height + SliderMargin;

maxPitchDeg_Slider = UISliders.AddPlainSliderWithValue(scrollPanel, LeftMargin, currentY, Translations.Translate("SETTINGS_MAXPITSHDEG"), 10f, 90f, 1f, ModSettings.MaxPitchDeg, new UISliders.SliderValueFormat(valueMultiplier: 1, roundToNearest: 1f, numberFormat: "N0", suffix: "°"));
maxPitchDeg_Slider = UISliders.AddPlainSliderWithValue(scrollPanel, LeftMargin, currentY, Translations.Translate("SETTINGS_MAXPITSHDEG"), 10f, 89f, 1f, ModSettings.MaxPitchDeg, new UISliders.SliderValueFormat(valueMultiplier: 1, roundToNearest: 1f, numberFormat: "N0", suffix: "°"));
maxPitchDeg_Slider.tooltip = Translations.Translate("SETTINGS_MAXPITSHDEG_DETAIL");
maxPitchDeg_Slider.eventValueChanged += (_, value) => ModSettings.MaxPitchDeg = value;
currentY += maxPitchDeg_Slider.height + SliderMargin;
Expand Down
3 changes: 1 addition & 2 deletions FPSCamera/Code/Settings/Tabs/GeneralOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

namespace FPSCamera.Settings.Tabs
{

internal class GeneralOptions
public sealed class GeneralOptions
{
// Layout constants.
private const float LeftMargin = 24f;
Expand Down
9 changes: 4 additions & 5 deletions FPSCamera/Code/UI/CamInfoPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using FPSCamera.Settings;
using FPSCamera.Utils;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class CamInfoPanel : MonoBehaviour
Expand Down Expand Up @@ -42,7 +41,7 @@ private void Awake()
enabled = false;
}

protected void LateUpdate()
private void Update()
{
var cam = FPSCamController.Instance.FPSCam;
if (cam.IsVaild())
Expand Down Expand Up @@ -126,12 +125,12 @@ private void OnGUI()
var columnRect = rect; columnRect.width = fieldWidth;
DrawInfoFields(_leftInfos, style, columnRect, infoMargin);
columnRect.x += fieldWidth + margin; columnRect.width = textWidth;
DrawListInRows(_leftInfos.Select(info => info.Value), style, columnRect, infoMargin);
DrawListInRows(_leftInfos.Values, style, columnRect, infoMargin);

rect.x += blockWidth * 3f;
style.alignment = TextAnchor.MiddleRight;
columnRect = rect; columnRect.width = textWidth;
DrawListInRows(_rightInfos.Select(info => info.Value), style, columnRect, infoMargin);
DrawListInRows(_rightInfos.Values, style, columnRect, infoMargin);
columnRect.x += textWidth + margin; columnRect.width = fieldWidth;
DrawInfoFields(_rightInfos, style, columnRect, infoMargin);

Expand All @@ -155,7 +154,7 @@ private void DrawInfoFields(Dictionary<string, string> infos, GUIStyle style, Re
style.alignment = TextAnchor.MiddleCenter;
style.fontSize = (int)(oFontSize * _fieldFontSizeRatio);

DrawListInRows(infos.Select(info => info.Key), style, rect, margin);
DrawListInRows(infos.Keys, style, rect, margin);

style.normal.background = null;
style.alignment = oAlign; style.fontSize = oFontSize;
Expand Down
156 changes: 61 additions & 95 deletions FPSCamera/Code/UI/FollowButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,49 @@
using ColossalFramework.UI;
using FPSCamera.Cam.Controller;
using FPSCamera.Utils;
using System;
using UnityEngine;

namespace FPSCamera.UI
{
public class FollowButtons : MonoBehaviour
{
private void Awake()
{
citizenVehicleInfo_Button = Initialize(ref citizenVehicleInfo_Panel);
cityServiceVehicleInfo_Button = Initialize(ref cityServiceVehicleInfo_Panel);
publicTransportVehicleInfo_Button = Initialize(ref publicTransportVehicleInfo_Panel);
citizenInfo_Button = Initialize(ref citizenInfo_Panel);
touristInfo_Button = Initialize(ref touristInfo_Panel);
}
private void Update()
{
UpdateButtonVisibility(citizenVehicleInfo_Panel, citizenVehicleInfo_Button,
id => id.Type != InstanceType.ParkedVehicle);
UpdateButtonVisibility(cityServiceVehicleInfo_Panel, cityServiceVehicleInfo_Button);
UpdateButtonVisibility(publicTransportVehicleInfo_Panel, publicTransportVehicleInfo_Button);
UpdateButtonVisibility(citizenInfo_Panel, citizenInfo_Button);
UpdateButtonVisibility(touristInfo_Panel, touristInfo_Button);
}
private void OnDestroy()
{
Destroy(citizenVehicleCameraButton);
Destroy(cityServiceVehicleCameraButton);
Destroy(publicTransportCameraButton);
Destroy(citizenCameraButton);
Destroy(touristCameraButton);
Destroy(citizenVehicleInfo_Button);
Destroy(cityServiceVehicleInfo_Button);
Destroy(publicTransportVehicleInfo_Button);
Destroy(citizenInfo_Button);
Destroy(touristInfo_Button);
}

private void Awake()
private UIButton Initialize<T>(ref T panel) where T : WorldInfoPanel
{
var citizenVehicleInfoPanel = UIView.library.Get<CitizenVehicleWorldInfoPanel>(typeof(CitizenVehicleWorldInfoPanel).Name);
citizenVehicleCameraButton = CreateCameraButton
(
citizenVehicleInfoPanel.component,
(_, param) =>
{
var instance = PrivateField.GetValue<InstanceID>(citizenVehicleInfoPanel, "m_InstanceID");
FPSCamController.Instance.StartFollowing(instance);
}
);
citizenVehicleCameraButton.parent.eventVisibilityChanged += (_, isShow) =>
{
if (isShow == true)
{
var instance = PrivateField.GetValue<InstanceID>(citizenVehicleInfoPanel, "m_InstanceID");
if (instance.Type == InstanceType.ParkedVehicle) citizenVehicleCameraButton.isVisible = false;
}
};
var cityServiceVehicleInfoPanel = UIView.library.Get<CityServiceVehicleWorldInfoPanel>(typeof(CityServiceVehicleWorldInfoPanel).Name);
cityServiceVehicleCameraButton = CreateCameraButton
(
cityServiceVehicleInfoPanel.component,
(_, param) =>
{
if (!(PrivateField.GetValue<InstanceID>(cityServiceVehicleInfoPanel, "m_InstanceID") is var instance))
{
cityServiceVehicleCameraButton.isVisible = false;
return;
}
FPSCamController.Instance.StartFollowing(instance);
}
);

var publicTransportVehicleInfoPanel = UIView.library.Get<PublicTransportVehicleWorldInfoPanel>(typeof(PublicTransportVehicleWorldInfoPanel).Name);
publicTransportCameraButton = CreateCameraButton
(
publicTransportVehicleInfoPanel.component,
(_, param) =>
{
if (!(PrivateField.GetValue<InstanceID>(publicTransportVehicleInfoPanel, "m_InstanceID") is var instance))
{
publicTransportCameraButton.isVisible = false;
return;
}
FPSCamController.Instance.StartFollowing(instance);
}
);

var citizenInfoPanel = UIView.library.Get<CitizenWorldInfoPanel>(typeof(CitizenWorldInfoPanel).Name);
citizenCameraButton = CreateCameraButton
(
citizenInfoPanel.component,
(_, param) =>
{
if (!(PrivateField.GetValue<InstanceID>(citizenInfoPanel, "m_InstanceID") is var instance))
{
citizenCameraButton.isVisible = false;
return;
}
FPSCamController.Instance.StartFollowing(instance);
}
);

var touristInfoPanel = UIView.library.Get<TouristWorldInfoPanel>(typeof(TouristWorldInfoPanel).Name);
touristCameraButton = CreateCameraButton
(
touristInfoPanel.component,
(_, param) =>
{
if (!(PrivateField.GetValue<InstanceID>(touristInfoPanel, "m_InstanceID") is var instance))
{
touristCameraButton.isVisible = false;
return;
}
FPSCamController.Instance.StartFollowing(instance);
}
);
panel = UIView.library.Get<T>(typeof(T).Name);
return CreateCameraButton(panel);
}

UIButton CreateCameraButton(UIComponent parentComponent, MouseEventHandler handler)
private UIButton CreateCameraButton<T>(T panel) where T : WorldInfoPanel
{
//var button = UIView.GetAView().AddUIComponent(typeof(UIButton)) as UIButton;
var button = parentComponent.AddUIComponent<UIButton>();
button.name = parentComponent.name + "_StartFollow";
var button = panel.component.AddUIComponent<UIButton>();
button.name = panel.component.name + "_StartFollow";
button.tooltip = Translations.Translate("FOLLOWBTN_TOOLTIP");
button.size = new Vector2(40f, 40f);
button.scaleFactor = .8f;
Expand All @@ -116,17 +58,41 @@ UIButton CreateCameraButton(UIComponent parentComponent, MouseEventHandler handl
button.hoveredTextColor = new Color32(255, 255, 255, 255);
button.focusedTextColor = new Color32(255, 255, 255, 255);
button.pressedTextColor = new Color32(30, 30, 44, 255);
button.eventClick += handler;
button.AlignTo(parentComponent, UIAlignAnchor.BottomRight);
button.relativePosition = new Vector3(parentComponent.width - 10f, parentComponent.height - 30f);
button.eventClick += (_, p) =>
{
FPSCamController.Instance.StartFollowing(GetPanelInstanceID(panel));
panel.component.isVisible = false;
};
button.AlignTo(panel.component, UIAlignAnchor.BottomRight);
button.relativePosition = new Vector3(button.relativePosition.x - 4f, button.relativePosition.y - 20f);

return button;
}
private void UpdateButtonVisibility<T>(T panel, UIButton button, Func<InstanceID, bool> filter = null) where T : WorldInfoPanel
{
if (panel.component.isVisible)
{
var instanceID = GetPanelInstanceID(panel);
button.isVisible = instanceID != default && (filter?.Invoke(instanceID) ?? true);
}
}

private InstanceID GetPanelInstanceID<T>(T panel) where T : WorldInfoPanel => PrivateField.GetValue<InstanceID>(panel, "m_InstanceID");

private CitizenVehicleWorldInfoPanel citizenVehicleInfo_Panel;
private UIButton citizenVehicleInfo_Button;

private CityServiceVehicleWorldInfoPanel cityServiceVehicleInfo_Panel;
private UIButton cityServiceVehicleInfo_Button;

private PublicTransportVehicleWorldInfoPanel publicTransportVehicleInfo_Panel;
private UIButton publicTransportVehicleInfo_Button;

private CitizenWorldInfoPanel citizenInfo_Panel;
private UIButton citizenInfo_Button;

private UIButton citizenVehicleCameraButton;
private UIButton cityServiceVehicleCameraButton;
private UIButton publicTransportCameraButton;
private UIButton citizenCameraButton;
private UIButton touristCameraButton;
private TouristWorldInfoPanel touristInfo_Panel;
private UIButton touristInfo_Button;
}

}
14 changes: 9 additions & 5 deletions FPSCamera/Code/UI/MainPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private void Awake()
Panel.backgroundSprite = "ScrollbarTrack";
Panel.width = 400f;
Panel.height = 1000f;
Panel.isVisible = false;
AddSettings();
Panel.eventVisibilityChanged += (_, isVisible) =>
{
if (!isVisible)
Expand All @@ -37,12 +37,14 @@ private void Awake()
AddSettings();
}
};
Panel.isVisible = false;

if (ModSupport.FoundUUI)
{
UUISupport.UUIRegister();
return;
}

#endregion

#region Main Button
Expand Down Expand Up @@ -125,8 +127,6 @@ private void AddSettings()
fov_Slider.eventValueChanged += (_, value) => ModSettings.CamFieldOfView = value;
currentY += fov_Slider.height + SliderMargin;

//_settings.Add(_mainPanel.Add<ToggleSetting>(props.Swap(Config.instance.SmoothTransition)));

string[] groundClipingItems = new[]
{
Translations.Translate("SETTINGS_GROUNDCLIPING_NONE"),
Expand Down Expand Up @@ -163,7 +163,11 @@ private void AddSettings()

var walkThruBtn = UIButtons.AddButton(Panel, Margin, currentY, Translations.Translate("WALKTHRUBTN_TEXT"), 200f, 40f);

walkThruBtn.eventClick += (_, m) => FPSCamController.Instance.StartWalkThruCam();
walkThruBtn.eventClick += (_, m) =>
{
FPSCamController.Instance.StartWalkThruCam();
Panel.isVisible = false;
};
Panel.height = currentY + walkThruBtn.height + Margin;
}
else
Expand All @@ -175,7 +179,7 @@ private void Close()
{
foreach (var component in Panel.components)
{
Destroy(component.gameObject);
Destroy(component);
}
}
public bool OnEsc()
Expand Down
Loading

0 comments on commit f2bc8d5

Please sign in to comment.