Phoenix84

ftl controls

Jun 7th, 2016 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.81 KB | None | 0 0
  1. //Base Class:
  2.  
  3.         protected virtual void CreateTerminalControls<T>()
  4.         {
  5.             if (m_ControlsInited.Contains(typeof(T)))
  6.                 return;
  7.  
  8.             m_ControlsInited.Add(typeof(T));
  9.  
  10.             MyAPIGateway.TerminalControls.CustomControlGetter -= TerminalControls_CustomControlGetter;
  11.             MyAPIGateway.TerminalControls.CustomActionGetter -= TerminalControls_CustomActionGetter;
  12.  
  13.             MyAPIGateway.TerminalControls.CustomControlGetter += TerminalControls_CustomControlGetter;
  14.             MyAPIGateway.TerminalControls.CustomActionGetter += TerminalControls_CustomActionGetter;
  15.  
  16.             // Separator
  17.             if (sep == null)
  18.             {
  19.                 sep = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlSeparator, IMyTerminalBlock>(string.Empty);
  20.                 sep.Visible = (b) => b.BlockDefinition.SubtypeId.Contains("FTL");
  21.             }
  22.             MyAPIGateway.TerminalControls.AddControl<T>(sep);
  23.  
  24.             var gpsAction = MyAPIGateway.TerminalControls.CreateProperty<Vector3D?, T>("Phoenix.FTL.OneTimeDestination");
  25.             if (gpsAction != null)
  26.             {
  27.                 StringBuilder actionname = new StringBuilder();
  28.                 gpsAction.Enabled = (b) => b.IsFTL() && b.IsWorking;
  29.                 gpsAction.Setter = (b, v) => MessageUtils.SendMessageToServer(new MessageGPS() { FTLId = b.EntityId, Destination = (v.HasValue ? new SerializableVector3D?(v.Value) : null) });
  30.                 gpsAction.Getter = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpTargetGPS != null ? new Vector3D?(b.GameLogic.GetAs<FTLBase>().Data.jumpTargetGPS.Coords) : null;
  31.                 MyAPIGateway.TerminalControls.AddControl<T>(gpsAction);
  32.             }
  33.  
  34.             var jumpStateProperty = MyAPIGateway.TerminalControls.CreateProperty<string, T>("Phoenix.FTL.State");
  35.             if (jumpStateProperty != null)
  36.             {
  37.                 StringBuilder actionname = new StringBuilder();
  38.                 jumpStateProperty.Enabled = (b) => b.IsFTL() && b.IsWorking;
  39.                 jumpStateProperty.Setter = (b, v) => { };               // No setter, but define one, otherwise crash
  40.                 jumpStateProperty.Getter = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpState.ToString();
  41.                 MyAPIGateway.TerminalControls.AddControl<T>(jumpStateProperty);
  42.             }
  43.  
  44.             var timeProperty = MyAPIGateway.TerminalControls.CreateProperty<long, T>("Phoenix.FTL.TimeRemaining");
  45.             if (timeProperty != null)
  46.             {
  47.                 StringBuilder actionname = new StringBuilder();
  48.                 timeProperty.Enabled = (b) => b.IsFTL() && b.IsWorking;
  49.                 timeProperty.Setter = (b, v) => { };                    // No setter, but define one, otherwise crash
  50.                 timeProperty.Getter = (b) =>
  51.                     {
  52.                         if (b.GameLogic.GetAs<FTLBase>().Data.jumpState == JumpState.Spooling)
  53.                             return (long)Math.Max((long)0, Math.Round((b.GameLogic.GetAs<FTLBase>().Data.jumpTime - DateTime.Now).TotalSeconds));
  54.                         else if(b.GameLogic.GetAs<FTLBase>().Data.jumpState == JumpState.Cooldown)
  55.                             return (long)Math.Max((long)0, Math.Round((b.GameLogic.GetAs<FTLBase>().Data.resetTime - DateTime.Now).TotalSeconds));
  56.                         else
  57.                             return 0;
  58.                     };
  59.                 MyAPIGateway.TerminalControls.AddControl<T>(timeProperty);
  60.             }
  61.  
  62.             var jumpDistanceProperty = MyAPIGateway.TerminalControls.CreateProperty<float, T>("Phoenix.FTL.JumpDistanceInMeters");
  63.             if (jumpDistanceProperty != null)
  64.             {
  65.                 StringBuilder actionname = new StringBuilder();
  66.                 jumpDistanceProperty.Enabled = (b) => b.IsFTL() && b.IsWorking;
  67.                 jumpDistanceProperty.Getter = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpDistance;
  68.                 jumpDistanceProperty.Setter = (b, v) => b.GameLogic.GetAs<FTLBase>().SendDistanceChange(v);
  69.                 MyAPIGateway.TerminalControls.AddControl<T>(jumpDistanceProperty);
  70.             }
  71.         }
  72.  
  73.  
  74. // Gyro derived class:
  75.         protected override void CreateTerminalControls<T>()
  76.         {
  77.             if (m_ControlsInited.Contains(typeof(T)))
  78.                 return;                         // This must be first!
  79.  
  80.             base.CreateTerminalControls<T>();      // This must be second!
  81.  
  82.             // Do rest of init
  83.             // Create controls first, so they can be referenced below
  84.             var distance = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlSlider, T>("JumpDistance");    // Use for compatibility with stock drive
  85.             var selectedTarget = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlListbox, T>("Phoenix.FTL.SelectedTarget");
  86.             var remButton = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlButton, T>("Phoenix.FTL.RemoveBtn");
  87.             var addButton = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlButton, T>("Phoenix.FTL.SelectBtn");
  88.             var gpsList = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlListbox, T>("Phoenix.FTL.GpsList");
  89.             var jumpButton = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlButton, T>("Phoenix.FTL.Jump");
  90.             IMyTerminalAction jumpAction = MyAPIGateway.TerminalControls.CreateAction<IMyTerminalBlock>("Jump");        // Use for compatibility with stock drive
  91.  
  92.             jumpButton.Title = MyStringId.GetOrCompute("BlockActionTitle_Jump");
  93.             jumpButton.Visible = (b) => b.IsFTL();
  94.             jumpButton.Enabled = (b) => b.IsWorking;
  95.             jumpButton.Action = (b) => b.GameLogic.GetAs<FTLBase>().RequestJump();
  96.             MyAPIGateway.TerminalControls.AddControl<T>(jumpButton);
  97.  
  98.             Action updateDelegate = () =>
  99.             {
  100.                 distance.UpdateVisual();
  101.                 addButton.UpdateVisual();
  102.                 remButton.UpdateVisual();
  103.                 selectedTarget.UpdateVisual();
  104.             };
  105.  
  106.             StringBuilder actionname = new StringBuilder();
  107.             actionname.Append("Jump");
  108.             jumpAction.Name = actionname;
  109.             jumpAction.Icon = @"Textures\GUI\Icons\Actions\Toggle.dds";
  110.             jumpAction.ValidForGroups = false;
  111.             jumpAction.Enabled = (b) => b.IsWorking;
  112.             jumpAction.Action = (b) => jumpButton.Action(b);
  113.             MyAPIGateway.TerminalControls.AddAction<T>(jumpAction);
  114.  
  115.             distance.Title = MyStringId.GetOrCompute("BlockPropertyTitle_JumpDistance");
  116.             distance.SetLimits(0f, 100f);
  117.             distance.Getter = (b) => (b.GameLogic.GetAs<FTLBase>().Data.jumpDistance / b.GameLogic.GetAs<FTLGyro>().ComputeMaxDistance()) * 100f;
  118.             distance.Setter = (b, v) => b.GameLogic.GetAs<FTLGyro>().SendDistanceChange((v / 100f) * b.GameLogic.GetAs<FTLGyro>().ComputeMaxDistance());
  119.             distance.Writer = (b, v) =>
  120.             {
  121.                 v.AppendFormat("{0:P0} (", distance.Getter(b) / 100f);
  122.                 MyValueFormatter.AppendDistanceInBestUnit(b.GameLogic.GetAs<FTLBase>().Data.jumpDistance, v);
  123.                 v.Append(")");
  124.             };
  125.             distance.Visible = (b) => b.IsFTL();
  126.             distance.Enabled = (b) => b.GameLogic.GetAs<FTLBase>().Data.jumpTargetGPS == null;
  127.             MyAPIGateway.TerminalControls.AddControl<IMyGyro>(distance);
  128.  
  129.             selectedTarget.Title = MyStringId.GetOrCompute("BlockPropertyTitle_DestinationGPS");
  130.             selectedTarget.ListContent = (b, list1, list2) => b.GameLogic.GetAs<FTLGyro>().FillSelectedTarget(b, list1, list2);
  131.             selectedTarget.Visible = (b) => b.IsFTL();
  132.             selectedTarget.VisibleRowsCount = 1;
  133.             MyAPIGateway.TerminalControls.AddControl<IMyGyro>(selectedTarget);
  134.  
  135.             remButton.Title = MyStringId.GetOrCompute("RemoveProjectionButton");
  136.             remButton.Visible = (b) => b.IsFTL();
  137.             remButton.Enabled = (b) => b.GameLogic.GetAs<FTLGyro>().CanRemove();
  138.             remButton.Action = (b) =>
  139.                 {
  140.                     b.GameLogic.GetAs<FTLGyro>().RemoveSelected();
  141.                     updateDelegate();
  142.                 };
  143.             MyAPIGateway.TerminalControls.AddControl<IMyGyro>(remButton);
  144.  
  145.             addButton.Title = MyStringId.GetOrCompute("SelectBlueprint");
  146.             addButton.Visible = (b) => b.IsFTL();
  147.             addButton.Enabled = (b) => b.GameLogic.GetAs<FTLGyro>().CanSelect();
  148.             addButton.Action = (b) =>
  149.                 {
  150.                     b.GameLogic.GetAs<FTLGyro>().SelectTarget();
  151.                     updateDelegate();
  152.                 };
  153.             MyAPIGateway.TerminalControls.AddControl<IMyGyro>(addButton);
  154.  
  155.             gpsList.Title = MyStringId.GetOrCompute("BlockPropertyTitle_GpsLocations");
  156.             gpsList.ListContent = (b, list1, list2) => b.GameLogic.GetAs<FTLGyro>().FillGpsList(b, list1, list2);
  157.             gpsList.ItemSelected = (b, v) =>
  158.                 {
  159.                     b.GameLogic.GetAs<FTLGyro>().SelectGps(v);
  160.                     updateDelegate();
  161.                 };
  162.             gpsList.Visible = (b) => b.IsFTL();
  163.             gpsList.VisibleRowsCount = 8;
  164.             MyAPIGateway.TerminalControls.AddControl<IMyGyro>(gpsList);
  165.         }
  166.  
  167. // JumpDrive derived class
  168.         protected override void CreateTerminalControls<T>()
  169.         {
  170.             if (m_ControlsInited.Contains(typeof(T)))
  171.                 return;                         // This must be first!
  172.  
  173.             base.CreateTerminalControls<T>();
  174.  
  175.             // Change the Jump button to do my own stuff
  176.             List<IMyTerminalControl> controls;
  177.             List<IMyTerminalAction> actions;
  178.             MyAPIGateway.TerminalControls.GetControls<T>(out controls);
  179.             MyAPIGateway.TerminalControls.GetActions<T>(out actions);
  180.  
  181.             var jumpControl = controls.FirstOrDefault((x) => x.Id == "Jump") as IMyTerminalControlButton;
  182.             if (jumpControl != null)
  183.             {
  184.                 jumpControl.Visible = (b) => b.IsFTL();
  185.                 var oldAction = jumpControl.Action;
  186.                 jumpControl.Action = (b) =>
  187.                 {
  188.                     if (b.IsFTL())
  189.                         b.GameLogic.GetAs<FTLBase>().RequestJump();
  190.                     else
  191.                         oldAction(b);
  192.                 };
  193.             }
  194.  
  195.             var jumpAction = actions.FirstOrDefault((x) => x.Id == "Jump") as IMyTerminalAction;
  196.  
  197.             if (jumpAction != null)
  198.             {
  199.                 var oldAction = jumpAction.Action;
  200.                 var oldToolbars = jumpAction.InvalidToolbarTypes;
  201.                 jumpAction.InvalidToolbarTypes = null;
  202.                 jumpAction.Action = (b) =>
  203.                     {
  204.                         if (b.IsFTL())
  205.                             b.GameLogic.GetAs<FTLBase>().RequestJump();
  206.                         else
  207.                             oldAction(b);
  208.                     };
  209.             }
  210.  
  211.             // Keep track of slider control, to change limits
  212.             var distanceControl = controls.FirstOrDefault((x) => x.Id == "JumpDistance") as IMyTerminalControlSlider;
  213.             if (distanceControl != null)
  214.             {
  215.                 var oldWriter = distanceControl.Writer;
  216.                 distanceControl.Writer = (b,t) =>
  217.                 {
  218.                     if (b.IsFTL())
  219.                     {
  220.                         t.AppendFormat("{0:P0} (", distanceControl.Getter(b) / 100f);
  221.                         MyValueFormatter.AppendDistanceInBestUnit(b.GameLogic.GetAs<FTLJumpDrive>().ComputeMaxDistance(), t);
  222.                         t.Append(")");
  223.                     }
  224.                     else
  225.                         oldWriter(b, t);
  226.                 };
  227.             }
  228.         }
Advertisement
Add Comment
Please, Sign In to add comment