Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Base Class:
- protected virtual void CreateTerminalControls<T>()
- {
- if (m_ControlsInited.Contains(typeof(T)))
- return;
- m_ControlsInited.Add(typeof(T));
- MyAPIGateway.TerminalControls.CustomControlGetter -= TerminalControls_CustomControlGetter;
- MyAPIGateway.TerminalControls.CustomActionGetter -= TerminalControls_CustomActionGetter;
- MyAPIGateway.TerminalControls.CustomControlGetter += TerminalControls_CustomControlGetter;
- MyAPIGateway.TerminalControls.CustomActionGetter += TerminalControls_CustomActionGetter;
- // Separator
- if (sep == null)
- {
- sep = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlSeparator, IMyTerminalBlock>(string.Empty);
- sep.Visible = (b) => b.BlockDefinition.SubtypeId.Contains("FTL");
- }
- MyAPIGateway.TerminalControls.AddControl<T>(sep);
- var gpsAction = MyAPIGateway.TerminalControls.CreateProperty<Vector3D?, T>("Phoenix.FTL.OneTimeDestination");
- if (gpsAction != null)
- {
- StringBuilder actionname = new StringBuilder();
- gpsAction.Enabled = (b) => b.IsFTL() && b.IsWorking;
- gpsAction.Setter = (b, v) => MessageUtils.SendMessageToServer(new MessageGPS() { FTLId = b.EntityId, Destination = (v.HasValue ? new SerializableVector3D?(v.Value) : null) });
- gpsAction.Getter = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpTargetGPS != null ? new Vector3D?(b.GameLogic.GetAs<FTLBase>().Data.jumpTargetGPS.Coords) : null;
- MyAPIGateway.TerminalControls.AddControl<T>(gpsAction);
- }
- var jumpStateProperty = MyAPIGateway.TerminalControls.CreateProperty<string, T>("Phoenix.FTL.State");
- if (jumpStateProperty != null)
- {
- StringBuilder actionname = new StringBuilder();
- jumpStateProperty.Enabled = (b) => b.IsFTL() && b.IsWorking;
- jumpStateProperty.Setter = (b, v) => { }; // No setter, but define one, otherwise crash
- jumpStateProperty.Getter = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpState.ToString();
- MyAPIGateway.TerminalControls.AddControl<T>(jumpStateProperty);
- }
- var timeProperty = MyAPIGateway.TerminalControls.CreateProperty<long, T>("Phoenix.FTL.TimeRemaining");
- if (timeProperty != null)
- {
- StringBuilder actionname = new StringBuilder();
- timeProperty.Enabled = (b) => b.IsFTL() && b.IsWorking;
- timeProperty.Setter = (b, v) => { }; // No setter, but define one, otherwise crash
- timeProperty.Getter = (b) =>
- {
- if (b.GameLogic.GetAs<FTLBase>().Data.jumpState == JumpState.Spooling)
- return (long)Math.Max((long)0, Math.Round((b.GameLogic.GetAs<FTLBase>().Data.jumpTime - DateTime.Now).TotalSeconds));
- else if(b.GameLogic.GetAs<FTLBase>().Data.jumpState == JumpState.Cooldown)
- return (long)Math.Max((long)0, Math.Round((b.GameLogic.GetAs<FTLBase>().Data.resetTime - DateTime.Now).TotalSeconds));
- else
- return 0;
- };
- MyAPIGateway.TerminalControls.AddControl<T>(timeProperty);
- }
- var jumpDistanceProperty = MyAPIGateway.TerminalControls.CreateProperty<float, T>("Phoenix.FTL.JumpDistanceInMeters");
- if (jumpDistanceProperty != null)
- {
- StringBuilder actionname = new StringBuilder();
- jumpDistanceProperty.Enabled = (b) => b.IsFTL() && b.IsWorking;
- jumpDistanceProperty.Getter = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpDistance;
- jumpDistanceProperty.Setter = (b, v) => b.GameLogic.GetAs<FTLBase>().SendDistanceChange(v);
- MyAPIGateway.TerminalControls.AddControl<T>(jumpDistanceProperty);
- }
- }
- // Gyro derived class:
- protected override void CreateTerminalControls<T>()
- {
- if (m_ControlsInited.Contains(typeof(T)))
- return; // This must be first!
- base.CreateTerminalControls<T>(); // This must be second!
- // Do rest of init
- // Create controls first, so they can be referenced below
- var distance = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlSlider, T>("JumpDistance"); // Use for compatibility with stock drive
- var selectedTarget = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlListbox, T>("Phoenix.FTL.SelectedTarget");
- var remButton = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlButton, T>("Phoenix.FTL.RemoveBtn");
- var addButton = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlButton, T>("Phoenix.FTL.SelectBtn");
- var gpsList = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlListbox, T>("Phoenix.FTL.GpsList");
- var jumpButton = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlButton, T>("Phoenix.FTL.Jump");
- IMyTerminalAction jumpAction = MyAPIGateway.TerminalControls.CreateAction<IMyTerminalBlock>("Jump"); // Use for compatibility with stock drive
- jumpButton.Title = MyStringId.GetOrCompute("BlockActionTitle_Jump");
- jumpButton.Visible = (b) => b.IsFTL();
- jumpButton.Enabled = (b) => b.IsWorking;
- jumpButton.Action = (b) => b.GameLogic.GetAs<FTLBase>().RequestJump();
- MyAPIGateway.TerminalControls.AddControl<T>(jumpButton);
- Action updateDelegate = () =>
- {
- distance.UpdateVisual();
- addButton.UpdateVisual();
- remButton.UpdateVisual();
- selectedTarget.UpdateVisual();
- };
- StringBuilder actionname = new StringBuilder();
- actionname.Append("Jump");
- jumpAction.Name = actionname;
- jumpAction.Icon = @"Textures\GUI\Icons\Actions\Toggle.dds";
- jumpAction.ValidForGroups = false;
- jumpAction.Enabled = (b) => b.IsWorking;
- jumpAction.Action = (b) => jumpButton.Action(b);
- MyAPIGateway.TerminalControls.AddAction<T>(jumpAction);
- distance.Title = MyStringId.GetOrCompute("BlockPropertyTitle_JumpDistance");
- distance.SetLimits(0f, 100f);
- distance.Getter = (b) => (b.GameLogic.GetAs<FTLBase>().Data.jumpDistance / b.GameLogic.GetAs<FTLGyro>().ComputeMaxDistance()) * 100f;
- distance.Setter = (b, v) => b.GameLogic.GetAs<FTLGyro>().SendDistanceChange((v / 100f) * b.GameLogic.GetAs<FTLGyro>().ComputeMaxDistance());
- distance.Writer = (b, v) =>
- {
- v.AppendFormat("{0:P0} (", distance.Getter(b) / 100f);
- MyValueFormatter.AppendDistanceInBestUnit(b.GameLogic.GetAs<FTLBase>().Data.jumpDistance, v);
- v.Append(")");
- };
- distance.Visible = (b) => b.IsFTL();
- distance.Enabled = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpTargetGPS == null;
- MyAPIGateway.TerminalControls.AddControl<IMyGyro>(distance);
- selectedTarget.Title = MyStringId.GetOrCompute("BlockPropertyTitle_DestinationGPS");
- selectedTarget.ListContent = (b, list1, list2) => b.GameLogic.GetAs<FTLGyro>().FillSelectedTarget(b, list1, list2);
- selectedTarget.Visible = (b) => b.IsFTL();
- selectedTarget.VisibleRowsCount = 1;
- MyAPIGateway.TerminalControls.AddControl<IMyGyro>(selectedTarget);
- remButton.Title = MyStringId.GetOrCompute("RemoveProjectionButton");
- remButton.Visible = (b) => b.IsFTL();
- remButton.Enabled = (b) => b.GameLogic.GetAs<FTLGyro>().CanRemove();
- remButton.Action = (b) =>
- {
- b.GameLogic.GetAs<FTLGyro>().RemoveSelected();
- updateDelegate();
- };
- MyAPIGateway.TerminalControls.AddControl<IMyGyro>(remButton);
- addButton.Title = MyStringId.GetOrCompute("SelectBlueprint");
- addButton.Visible = (b) => b.IsFTL();
- addButton.Enabled = (b) => b.GameLogic.GetAs<FTLGyro>().CanSelect();
- addButton.Action = (b) =>
- {
- b.GameLogic.GetAs<FTLGyro>().SelectTarget();
- updateDelegate();
- };
- MyAPIGateway.TerminalControls.AddControl<IMyGyro>(addButton);
- gpsList.Title = MyStringId.GetOrCompute("BlockPropertyTitle_GpsLocations");
- gpsList.ListContent = (b, list1, list2) => b.GameLogic.GetAs<FTLGyro>().FillGpsList(b, list1, list2);
- gpsList.ItemSelected = (b, v) =>
- {
- b.GameLogic.GetAs<FTLGyro>().SelectGps(v);
- updateDelegate();
- };
- gpsList.Visible = (b) => b.IsFTL();
- gpsList.VisibleRowsCount = 8;
- MyAPIGateway.TerminalControls.AddControl<IMyGyro>(gpsList);
- }
- // JumpDrive derived class
- protected override void CreateTerminalControls<T>()
- {
- if (m_ControlsInited.Contains(typeof(T)))
- return; // This must be first!
- base.CreateTerminalControls<T>();
- // Change the Jump button to do my own stuff
- List<IMyTerminalControl> controls;
- List<IMyTerminalAction> actions;
- MyAPIGateway.TerminalControls.GetControls<T>(out controls);
- MyAPIGateway.TerminalControls.GetActions<T>(out actions);
- var jumpControl = controls.FirstOrDefault((x) => x.Id == "Jump") as IMyTerminalControlButton;
- if (jumpControl != null)
- {
- jumpControl.Visible = (b) => b.IsFTL();
- var oldAction = jumpControl.Action;
- jumpControl.Action = (b) =>
- {
- if (b.IsFTL())
- b.GameLogic.GetAs<FTLBase>().RequestJump();
- else
- oldAction(b);
- };
- }
- var jumpAction = actions.FirstOrDefault((x) => x.Id == "Jump") as IMyTerminalAction;
- if (jumpAction != null)
- {
- var oldAction = jumpAction.Action;
- var oldToolbars = jumpAction.InvalidToolbarTypes;
- jumpAction.InvalidToolbarTypes = null;
- jumpAction.Action = (b) =>
- {
- if (b.IsFTL())
- b.GameLogic.GetAs<FTLBase>().RequestJump();
- else
- oldAction(b);
- };
- }
- // Keep track of slider control, to change limits
- var distanceControl = controls.FirstOrDefault((x) => x.Id == "JumpDistance") as IMyTerminalControlSlider;
- if (distanceControl != null)
- {
- var oldWriter = distanceControl.Writer;
- distanceControl.Writer = (b,t) =>
- {
- if (b.IsFTL())
- {
- t.AppendFormat("{0:P0} (", distanceControl.Getter(b) / 100f);
- MyValueFormatter.AppendDistanceInBestUnit(b.GameLogic.GetAs<FTLJumpDrive>().ComputeMaxDistance(), t);
- t.Append(")");
- }
- else
- oldWriter(b, t);
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment