Skip to content

Commit

Permalink
Added docking port cameras.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Wheeler committed Jun 1, 2020
1 parent edfd0cd commit 1b486a9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
107 changes: 94 additions & 13 deletions Assets/Scripts/Mod.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Assets.Scripts.Flight.UI;
using Assets.Scripts.Craft;
using Assets.Scripts.Craft.Parts.Modifiers;
using Assets.Scripts.Flight.GameView.Cameras;
using Assets.Scripts.Flight.GameView.UI.Inspector;
using Assets.Scripts.UI;
using ModApi;
using ModApi.Common;
using ModApi.Craft.Parts;
using ModApi.Mods;
using ModApi.Scenes.Events;
using ModApi.Ui;
Expand Down Expand Up @@ -47,15 +50,49 @@ protected override void OnModInitialized() {
UserInterfaceIds.Flight.FlightSceneUI,
OnBuildFlightSceneUI
);

Game.Instance.SceneManager.SceneLoaded += SceneManagerOnSceneLoaded;
}

private void SceneManagerOnSceneLoaded(
Object sender,
SceneEventArgs e) {
if (Game.InFlightScene) {
// Add docking port cameras
var craft = Game.Instance.FlightScene.CraftNode.CraftScript;

foreach (var partData in craft.Data.Assembly.Parts.Where(p => !p.IsDestroyed)) {
var part = partData.PartScript;
if (part.GetModifier<DockingPortScript>() != null) {
var existingScript = part.GetModifier<CameraVantageScript>();
if (existingScript == null) {
// Automatically add a CameraVantage Part Modifier to all DockingPort parts
var cameraData =
PartModifierData.CreateFromDefaultXml<CameraVantageData>(part.Data);

cameraData.ViewMode = ViewMode.FirstPerson;

var cameraVantageScript = (CameraVantageScript)cameraData.CreateScript();
var cameraAlignment =
cameraVantageScript.gameObject.AddComponent<DockingPortCameraAlignment>();

cameraAlignment.Initialize(cameraVantageScript, part);
} else if (existingScript.gameObject.GetComponent<DockingPortCameraAlignment>() == null) {
var cameraAlignment =
existingScript.gameObject.AddComponent<DockingPortCameraAlignment>();

cameraAlignment.Initialize(existingScript, part);
}
}
}
}
}

private static readonly XNamespace XmlNs = XmlLayoutConstants.XmlNamespace;
private const String CameraPanelButtonId = "toggle-camera-panel-button";
private const String ToggleDockingHudButtonId = "toggle-docking-hud-button";

private void OnBuildFlightSceneViewPanel(BuildUserInterfaceXmlRequest request) {
Debug.Log("OnBuildFlightSceneViewPanel");

var cameraPanelButton =
request.XmlDocument.Descendants(XmlNs + "ContentButton")
.FirstOrDefault(n => n.Attribute("id")?.Value == CameraPanelButtonId);
Expand Down Expand Up @@ -114,24 +151,68 @@ private static IXmlElement FindChildElementById(
private void OnToggleHudButtonClicked() {
if (this.hudScript == null) {
if (Game.InFlightScene) {
hudScript = Game.Instance.UserInterface.BuildUserInterfaceFromResource<HeadsUpDisplayScript>(
"CrewDragonHUD/HeadsUpDisplay",
(script, controller) => script.OnLayoutRebuilt(controller));

Game.Instance.SceneManager.SceneTransitionStarted += SceneManagerOnSceneTransitionStarted;
hudScript =
Game.Instance.UserInterface.BuildUserInterfaceFromResource<HeadsUpDisplayScript>(
"CrewDragonHUD/HeadsUpDisplay",
(script, controller) => script.OnLayoutRebuilt(controller)
);

Game.Instance.SceneManager.SceneTransitionStarted +=
SceneManagerOnSceneTransitionStarted;
}
} else {
this.hudScript.Close();
this.hudScript = null;
Game.Instance.SceneManager.SceneTransitionStarted -= SceneManagerOnSceneTransitionStarted;
Game.Instance.SceneManager.SceneTransitionStarted -=
SceneManagerOnSceneTransitionStarted;
}
}

private void SceneManagerOnSceneTransitionStarted(object sender, SceneTransitionEventArgs e) {
private void SceneManagerOnSceneTransitionStarted(Object sender, SceneTransitionEventArgs e) {
if (this.hudScript != null) {
this.hudScript.Close();
this.hudScript = null;
Game.Instance.SceneManager.SceneTransitionStarted -= SceneManagerOnSceneTransitionStarted;
Game.Instance.SceneManager.SceneTransitionStarted -=
SceneManagerOnSceneTransitionStarted;
}
}
}

internal class DockingPortCameraAlignment : MonoBehaviour {
private static readonly PropertyInfo InteractiveCameraController_TouchingProperty =
typeof(InteractiveCameraController).GetProperty("Touching", BindingFlags.Instance | BindingFlags.NonPublic);

private CameraVantageScript cameraVantage;
private IPartScript dockingPort;

public void Initialize(
CameraVantageScript cameraScript,
IPartScript dockingPortScript) {
this.cameraVantage = cameraScript;
this.dockingPort = dockingPortScript;
this.firstUpdate = true;
}

private static readonly Vector2 Center = new Vector2(-90, 180);
private bool firstUpdate;

public void Update() {
if (this.cameraVantage.CameraController is FirstPersonCameraController controller) {
if (this.firstUpdate) {
controller.DeltaRotation = Center;
this.firstUpdate = false;
return;
}

var touching = (Boolean)InteractiveCameraController_TouchingProperty.GetValue(controller);
if (!touching && controller.DeltaRotation != Center) {
var rot = controller.DeltaRotation;
controller.DeltaRotation =
new Vector2(
rot.x > -90 ? Math.Max(-90, rot.x - Time.unscaledDeltaTime * 20) : Math.Min(-90, rot.x + Time.unscaledDeltaTime * 20),
rot.y > 180 ? Math.Max(180, rot.y - Time.unscaledDeltaTime * 40) : Math.Min(180, rot.y + Time.unscaledDeltaTime * 40)
);
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Assets/Scripts/UI/HeadsUpDisplayScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ private static Single ToOffsetAngle(Single angle) {
return angle > 180 ? angle - 360 : angle;
}

private static (IPartScript part, Vector3 localPosition) FindClosestDockingPort(ICraftScript craft, Vector3 targetLocalPosition) {
private static (IPartScript part, Vector3 localPosition) FindClosestDockingPort(
ICraftScript craft,
Vector3 targetLocalPosition) {

IPartScript closestPort = null;
var closestDistance = 0d;
Vector3 closestLocalPosition = default(Vector3);
Expand Down

0 comments on commit 1b486a9

Please sign in to comment.