Add missile launcher and missile controller projects
[SEScripts.git] / SECommon / Output.cs
1 using Sandbox.Game.EntityComponents;
2 using Sandbox.ModAPI.Ingame;
3 using Sandbox.ModAPI.Interfaces;
4
5 using SpaceEngineers.Game.ModAPI.Ingame;
6
7 using System;
8 using System.Collections;
9 using System.Collections.Generic;
10 using System.Collections.Immutable;
11 using System.Linq;
12 using System.Text;
13
14 using VRage;
15 using VRage.Collections;
16 using VRage.Game;
17 using VRage.Game.Components;
18 using VRage.Game.GUI.TextPanel;
19 using VRage.Game.ModAPI.Ingame;
20 using VRage.Game.ModAPI.Ingame.Utilities;
21 using VRage.Game.ObjectBuilders.Definitions;
22
23 using VRageMath;
24
25 namespace IngameScript
26 {
27 class Output
28 {
29 IList<IMyTextSurface> outputs;
30 int maxNbLines;
31
32 public Output(IList<IMyTextSurface> surfaces, int maxNbLines = 10)
33 {
34 foreach (var s in surfaces)
35 {
36 s.ContentType = ContentType.TEXT_AND_IMAGE;
37 s.WriteText("");
38 }
39
40 this.outputs = surfaces;
41 this.maxNbLines = maxNbLines;
42 }
43
44 public Output(IMyCockpit cockpit, int maxNbLines = 10)
45 {
46 this.outputs = new List<IMyTextSurface>();
47 for (int n = 0; n < cockpit.SurfaceCount; n++)
48 {
49 var surface = cockpit.GetSurface(n);
50 surface.ContentType = ContentType.TEXT_AND_IMAGE;
51 surface.WriteText("");
52 this.outputs.Add(surface);
53 }
54 this.maxNbLines = maxNbLines;
55 }
56
57 public Output(IMyTextSurface surface, int maxNbLines = 10)
58 : this(new List<IMyTextSurface> { surface }, maxNbLines)
59 { }
60
61 public void Print(string text, int outputNumber = 0)
62 {
63 if (this.outputs.Count() <= outputNumber)
64 {
65 throw new Exception($"Output number {outputNumber} doesn't exist (number of output: {this.outputs.Count()}");
66 }
67 else
68 {
69 var output = this.outputs[outputNumber];
70 var currentText = output.GetText();
71 var lines = currentText.Split('\n');
72 if (lines.Count() >= this.maxNbLines)
73 {
74 output.WriteText(lines.Skip(lines.Count() - this.maxNbLines + 1).Append(text).Aggregate((a, b) => a + Environment.NewLine + b));
75 }
76 else if (lines.Count() == 0)
77 {
78 output.WriteText(text);
79 }
80 else
81 {
82 output.WriteText(Environment.NewLine + text, true);
83 }
84 }
85 }
86
87 public void PrintError(string text, int outputNumber = 0)
88 {
89 this.Print($"Error: {text}", outputNumber);
90 }
91
92
93 public void Display(string text, int outputNumber = 0)
94 {
95 if (this.outputs.Count() <= outputNumber)
96 {
97 throw new Exception($"Output number {outputNumber} doesn't exist (number of output: {this.outputs.Count()}");
98 }
99 else
100 {
101 this.outputs[outputNumber].WriteText(text);
102 }
103 }
104 }
105 }