From 4be257a6618ce378b24dac6db9724a5aac70f1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Bar=C3=A1t?= Date: Wed, 28 Oct 2020 00:32:46 +0100 Subject: [PATCH] Add text surface provider support to lcd registration command --- src/GridOS/Core/GridOS.cs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/GridOS/Core/GridOS.cs b/src/GridOS/Core/GridOS.cs index 26ef8ca..613b999 100644 --- a/src/GridOS/Core/GridOS.cs +++ b/src/GridOS/Core/GridOS.cs @@ -142,13 +142,38 @@ private void TryExecuteCustomData(string customData) } } + // TODO: GridOS class shouldn't contain low-level logic, move this somewhere else. private void CommandHandler_AddLcd(CommandItem sender, string param) { - IMyTerminalBlock textSurface = _p.GridTerminalSystem.GetBlockWithName(param); - if ((textSurface == null) || !(textSurface is IMyTextSurface)) + int surfaceIndex = 0; + + { // Extract optional surface index + int delimiter = param.IndexOf(' '); + if (delimiter > -1) + { + if (int.TryParse( + param.Substring(delimiter + 1, param.Length - (delimiter + 1)), out surfaceIndex)) + { + surfaceIndex = Math.Max(0, + int.Parse(param.Substring(delimiter + 1, param.Length - (delimiter + 1))) + - 1); + } + + param = param.Substring(0, delimiter); + } + } + + IMyTerminalBlock target = _p.GridTerminalSystem.GetBlockWithName(param); + + if (target == null) return; - RegisterTextSurface(textSurface as IMyTextSurface); + if (target is IMyTextSurface) + RegisterTextSurface((IMyTextSurface)target); + + if (target is IMyTextSurfaceProvider) + RegisterTextSurface( + ((IMyTextSurfaceProvider)target).GetSurface(surfaceIndex)); } private void CommandHandler_DisableUpdates(CommandItem sender, string param)