Advertisement
klassekatze

SpriteHUDLCD

May 25th, 2023
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.99 KB | None | 0 0
  1. /*
  2.  * R e a d m e
  3.  * -----------
  4.  *
  5.  * In this file you can include any instructions or other comments you want to have injected onto the
  6.  * top of your final script. You can safely delete this file if you do not want any such comments.
  7.  */
  8.  
  9. // This file contains your actual script.
  10. //
  11. // You can either keep all your code here, or you can create separate
  12. // code files to make your program easier to navigate while coding.
  13. //
  14. // In order to add a new utility class, right-click on your project,
  15. // select 'New' then 'Add Item...'. Now find the 'Space Engineers'
  16. // category under 'Visual C# Items' on the left hand side, and select
  17. // 'Utility Class' in the main area. Name it in the box below, and
  18. // press OK. This utility class will be merged in with your code when
  19. // deploying your final script.
  20. //
  21. // You can also simply create a new utility class manually, you don't
  22. // have to use the template if you don't want to. Just do so the first
  23. // time to see what a utility class looks like.
  24. //
  25. // Go to:
  26. // https://github.com/malware-dev/MDK-SE/wiki/Quick-Introduction-to-Space-Engineers-Ingame-Scripts
  27. //
  28. // to learn more about ingame scripts.
  29.  
  30. public Program()
  31. {
  32.     // The constructor, called only once every session and
  33.     // always before any other method is called. Use it to
  34.     // initialize your script.
  35.     //
  36.     // The constructor is optional and can be removed if not
  37.     // needed.
  38.     //
  39.     // It's recommended to set Runtime.UpdateFrequency
  40.     // here, which will allow your script to run itself without a
  41.     // timer block.
  42.     IMyTextSurface s = (IMyTextSurface)GridTerminalSystem.GetBlockWithName("LCD");
  43.     SpriteHUDLCD x = new SpriteHUDLCD(s);
  44.     //x.s = s;
  45.     x.setLCD("this is<color=lightgreen> a test<color=red> of color");
  46. }
  47.  
  48. public void Save()
  49. {
  50.     // Called when the program needs to save its state. Use
  51.     // this method to save your state to the Storage field
  52.     // or some other means.
  53.     //
  54.     // This method is optional and can be removed if not
  55.     // needed.
  56. }
  57.  
  58. public void Main(string argument, UpdateType updateSource)
  59. {
  60.     // The main entry point of the script, invoked every time
  61.     // one of the programmable block's Run actions are invoked,
  62.     // or the script updates itself. The updateSource argument
  63.     // describes where the update came from. Be aware that the
  64.     // updateSource is a  bitfield  and might contain more than
  65.     // one update type.
  66.     //
  67.     // The method itself is required, but the arguments above
  68.     // can be removed if not needed.
  69. }
  70.  
  71. public class SpriteHUDLCD
  72. {
  73.     static Dictionary<string, Color> ColorList = new Dictionary<string, Color>
  74.     {
  75.         { "aliceblue", Color.AliceBlue },
  76.         { "antiquewhite", Color.AntiqueWhite },
  77.         { "aqua", Color.Aqua },
  78.         { "aquamarine", Color.Aquamarine },
  79.         { "azure", Color.Azure },
  80.         { "beige", Color.Beige },
  81.         { "bisque", Color.Bisque },
  82.         { "black", Color.Black },
  83.         { "blanchedalmond", Color.BlanchedAlmond },
  84.         { "blue", Color.Blue },
  85.         { "blueviolet", Color.BlueViolet },
  86.         { "brown", Color.Brown },
  87.         { "burlywood", Color.BurlyWood },
  88.         { "badetblue", Color.CadetBlue },
  89.         { "chartreuse", Color.Chartreuse },
  90.         { "chocolate", Color.Chocolate },
  91.         { "coral", Color.Coral },
  92.         { "cornflowerblue", Color.CornflowerBlue },
  93.         { "cornsilk", Color.Cornsilk },
  94.         { "crimson", Color.Crimson },
  95.         { "cyan", Color.Cyan },
  96.         { "darkblue", Color.DarkBlue },
  97.         { "darkcyan", Color.DarkCyan },
  98.         { "darkgoldenrod", Color.DarkGoldenrod },
  99.         { "darkgray", Color.DarkGray },
  100.         { "darkgreen", Color.DarkGreen },
  101.         { "darkkhaki", Color.DarkKhaki },
  102.         { "darkmagenta", Color.DarkMagenta },
  103.         { "darkoliveGreen", Color.DarkOliveGreen },
  104.         { "darkorange", Color.DarkOrange },
  105.         { "darkorchid", Color.DarkOrchid },
  106.         { "darkred", Color.DarkRed },
  107.         { "darksalmon", Color.DarkSalmon },
  108.         { "darkseagreen", Color.DarkSeaGreen },
  109.         { "darkslateblue", Color.DarkSlateBlue },
  110.         { "darkslategray", Color.DarkSlateGray },
  111.         { "darkturquoise", Color.DarkTurquoise },
  112.         { "darkviolet", Color.DarkViolet },
  113.         { "deeppink", Color.DeepPink },
  114.         { "deepskyblue", Color.DeepSkyBlue },
  115.         { "dimgray", Color.DimGray },
  116.         { "dodgerblue", Color.DodgerBlue },
  117.         { "firebrick", Color.Firebrick },
  118.         { "floralwhite", Color.FloralWhite },
  119.         { "forestgreen", Color.ForestGreen },
  120.         { "fuchsia", Color.Fuchsia },
  121.         { "gainsboro", Color.Gainsboro },
  122.         { "ghostwhite", Color.GhostWhite },
  123.         { "gold", Color.Gold },
  124.         { "goldenrod", Color.Goldenrod },
  125.         { "gray", Color.Gray },
  126.         { "green", Color.Green },
  127.         { "greenyellow", Color.GreenYellow },
  128.         { "doneydew", Color.Honeydew },
  129.         { "hotpink", Color.HotPink },
  130.         { "indianred", Color.IndianRed },
  131.         { "indigo", Color.Indigo },
  132.         { "ivory", Color.Ivory },
  133.         { "khaki", Color.Khaki },
  134.         { "lavender", Color.Lavender },
  135.         { "lavenderblush", Color.LavenderBlush },
  136.         { "lawngreen", Color.LawnGreen },
  137.         { "lemonchiffon", Color.LemonChiffon },
  138.         { "lightblue", Color.LightBlue },
  139.         { "lightcoral", Color.LightCoral },
  140.         { "lightcyan", Color.LightCyan },
  141.         { "lightgoldenrodyellow", Color.LightGoldenrodYellow },
  142.         { "lightgray", Color.LightGray },
  143.         { "lightgreen", Color.LightGreen },
  144.         { "lightpink", Color.LightPink },
  145.         { "lightsalmon", Color.LightSalmon },
  146.         { "lightseagreen", Color.LightSeaGreen },
  147.         { "lightskyblue", Color.LightSkyBlue },
  148.         { "lightslategray", Color.LightSlateGray },
  149.         { "lightsteelblue", Color.LightSteelBlue },
  150.         { "lightyellow", Color.LightYellow },
  151.         { "lime", Color.Lime },
  152.         { "limegreen", Color.LimeGreen },
  153.         { "linen", Color.Linen },
  154.         { "magenta", Color.Magenta },
  155.         { "maroon", Color.Maroon },
  156.         { "mediumaquamarine", Color.MediumAquamarine },
  157.         { "mediumblue", Color.MediumBlue },
  158.         { "mediumorchid", Color.MediumOrchid },
  159.         { "mediumpurple", Color.MediumPurple },
  160.         { "mediumseagreen", Color.MediumSeaGreen },
  161.         { "mediumslateblue", Color.MediumSlateBlue },
  162.         { "mediumspringgreen", Color.MediumSpringGreen },
  163.         { "mediumturquoise", Color.MediumTurquoise },
  164.         { "mediumvioletred", Color.MediumVioletRed },
  165.         { "midnightblue", Color.MidnightBlue },
  166.         { "mintcream", Color.MintCream },
  167.         { "mistyrose", Color.MistyRose },
  168.         { "moccasin", Color.Moccasin },
  169.         { "navajowhite", Color.NavajoWhite },
  170.         { "navy", Color.Navy },
  171.         { "oldlace", Color.OldLace },
  172.         { "olive", Color.Olive },
  173.         { "olivedrab", Color.OliveDrab },
  174.         { "orange", Color.Orange },
  175.         { "orangered", Color.OrangeRed },
  176.         { "orchid", Color.Orchid },
  177.         { "palegoldenrod", Color.PaleGoldenrod },
  178.         { "palegreen", Color.PaleGreen },
  179.         { "paleturquoise", Color.PaleTurquoise },
  180.         { "palevioletred", Color.PaleVioletRed },
  181.         { "papayawhip", Color.PapayaWhip },
  182.         { "peachpuff", Color.PeachPuff },
  183.         { "peru", Color.Peru },
  184.         { "pink", Color.Pink },
  185.         { "plum", Color.Plum },
  186.         { "powderblue", Color.PowderBlue },
  187.         { "purple", Color.Purple },
  188.         { "red", Color.Red },
  189.         { "rosybrown", Color.RosyBrown },
  190.         { "royalblue", Color.RoyalBlue },
  191.         { "saddlebrown", Color.SaddleBrown },
  192.         { "salmon", Color.Salmon },
  193.         { "sandybrown", Color.SandyBrown },
  194.         { "seagreen", Color.SeaGreen },
  195.         { "seashell", Color.SeaShell },
  196.         { "sienna", Color.Sienna },
  197.         { "silver", Color.Silver },
  198.         { "skyblue", Color.SkyBlue },
  199.         { "slateblue", Color.SlateBlue },
  200.         { "slategray", Color.SlateGray },
  201.         { "snow", Color.Snow },
  202.         { "springgreen", Color.SpringGreen },
  203.         { "steelblue", Color.SteelBlue },
  204.         { "tan", Color.Tan },
  205.         { "teal", Color.Teal },
  206.         { "thistle", Color.Thistle },
  207.         { "tomato", Color.Tomato },
  208.         { "turquoise", Color.Turquoise },
  209.         { "violet", Color.Violet },
  210.         { "wheat", Color.Wheat },
  211.         { "white", Color.White },
  212.         { "whitesmoke", Color.WhiteSmoke },
  213.         { "yellow", Color.Yellow },
  214.         { "yellowgreen", Color.YellowGreen }
  215.     };
  216.     public IMyTextSurface s = null;
  217.  
  218.     public SpriteHUDLCD(IMyTextSurface s)
  219.     {
  220.         this.s = s;
  221.     }
  222.  
  223.     public void setLCD(string text)
  224.     {
  225.         s.WriteText(text);
  226.         List<object> tok = new List<object>();
  227.         string[] tokens = text.Split(new string[] { "<color=" }, StringSplitOptions.None);
  228.         for(int i = 0; i < tokens.Length; i++)
  229.         {
  230.             var t = tokens[i];
  231.             foreach(var kvp in ColorList)
  232.             {
  233.                 if(t.StartsWith(kvp.Key+">"))
  234.                 {
  235.                     t = t.Substring(kvp.Key.Length + 1);
  236.                     tok.Add(kvp.Value);
  237.                     break;
  238.                 }
  239.             }
  240.             tok.Add(t);
  241.         }
  242.  
  243.         s.ContentType = ContentType.SCRIPT;
  244.         s.Script = "";
  245.         RectangleF _viewport;
  246.         _viewport = new RectangleF(
  247. (s.TextureSize - s.SurfaceSize) / 2f,
  248. s.SurfaceSize
  249.     );
  250.  
  251.         using (var frame = s.DrawFrame())
  252.         {
  253.             var zpos = new Vector2(0, 0) + _viewport.Position + new Vector2(s.TextPadding / 100 * s.SurfaceSize.X, s.TextPadding / 100 * s.SurfaceSize.Y);
  254.             var position = zpos;
  255.             Color cColor = Color.White;
  256.             foreach(var t in tok)
  257.             {
  258.                 if (t is Color) cColor = (Color)t;
  259.                 else if(t is string) writeText((string)t, frame, ref position, zpos, s.FontSize,cColor);
  260.             }
  261.  
  262.             //writeText("012", frame, ref position, zpos, s.FontSize, Color.Blue);
  263.             //writeText("345", frame, ref position, zpos, s.FontSize, Color.White);
  264.             //writeText("678\n12", frame, ref position, zpos, s.FontSize, Color.Red);
  265.             //writeText("345", frame, ref position, zpos, s.FontSize, Color.Green);
  266.         }
  267.     }
  268.  
  269.     public void writeText(string text, MySpriteDrawFrame frame, ref Vector2 pos, Vector2 zpos, float textSize, Color color)
  270.     {
  271.         string[] lines = text.Split('\n');
  272.         for(int l = 0; l < lines.Length; l++)
  273.         {
  274.             var line = lines[l];
  275.             if (line.Length > 0)
  276.             {
  277.                 MySprite sprite = MySprite.CreateText(line, "Monospace", color, textSize, TextAlignment.LEFT);
  278.                 sprite.Position = pos;
  279.                 frame.Add(sprite);
  280.             }
  281.             if (l < lines.Length - 1)
  282.             {
  283.                 pos.X = zpos.X;
  284.                 pos.Y += 28 * textSize;
  285.             }else pos.X += 20 * textSize * line.Length;
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement