diff --git a/NPort/API/IImporter.cs b/NPort/API/IImporter.cs index 57b90cc..2bdb73c 100644 --- a/NPort/API/IImporter.cs +++ b/NPort/API/IImporter.cs @@ -4,8 +4,10 @@ namespace NPort.API { string Name { get; } - string Help(string[] args); + string Help(); void Import(string[] args); + + string[] ArgNames(); } } \ No newline at end of file diff --git a/NPort/API/ImportRegistry.cs b/NPort/API/ImportRegistry.cs index 568cce4..00cc16c 100644 --- a/NPort/API/ImportRegistry.cs +++ b/NPort/API/ImportRegistry.cs @@ -6,11 +6,38 @@ namespace NPort.API { private static List importers = new List(); + private static string[] _namesCache = null; + + public static string[] Names + { + get + { + if (_namesCache == null) RebuildCache(); + return _namesCache; + } + } + + private static string[][] _argsCache = null; + + private static string[] _helpCache = null; + public static void AddImporter(IImporter i) { importers.Add(i); } + public static string Help(int index) + { + if (_helpCache == null) RebuildCache(); + return _helpCache[index]; + } + + public static string[] Args(int index) + { + if (_argsCache == null) RebuildCache(); + return _argsCache[index]; + } + internal static void Call(string[] args, string name = null) { if (name == null) name = args[0]; @@ -23,5 +50,21 @@ namespace NPort.API } } } + + // Rebuild cache. + // Public just in case, but should probably not be invoked outside of NPort + public static void RebuildCache() + { + _namesCache = new string[importers.Count]; + _argsCache = new string[importers.Count][]; + _helpCache = new string[importers.Count]; + for (int i = 0; i < importers.Count; i++) + { + var importer = importers[i]; + _namesCache[i] = importer.Name; + _argsCache[i] = importer.ArgNames(); + _helpCache[i] = importer.Help(); + } + } } } \ No newline at end of file diff --git a/NPort/API/SimpleImporter.cs b/NPort/API/SimpleImporter.cs index d42ee55..c7ea643 100644 --- a/NPort/API/SimpleImporter.cs +++ b/NPort/API/SimpleImporter.cs @@ -7,7 +7,7 @@ namespace NPort.API public abstract class SimpleImporter : IImporter { public abstract string Name { get; } - public abstract string Help(string[] args); + public abstract string Help(); public void Import(string[] args) { @@ -22,13 +22,15 @@ namespace NPort.API } } + public abstract string[] ArgNames(); + protected void PlaceBlock(BlockIDs type, float3 position, float3 rotation, BlockColor colour, BlockMaterial material) { // TODO bulk place (cache and place when Import(...) completes?) Block placedBlock = Block.PlaceNew(type, position); placedBlock.Color = colour; placedBlock.Rotation = rotation; - placedBlock.Material = material; + //placedBlock.Material = material; } public abstract bool SimpleImport(string[] args); diff --git a/NPort/Importers/RobocraftFactoryImporter.cs b/NPort/Importers/RobocraftFactoryImporter.cs index e5b2610..4faccb4 100644 --- a/NPort/Importers/RobocraftFactoryImporter.cs +++ b/NPort/Importers/RobocraftFactoryImporter.cs @@ -9,13 +9,23 @@ namespace NPort.Importers { public class RobocraftFactoryImporter : API.SimpleImporter { + + private const float BAY_CENTER = 48f/2f; // BAY is actually 49 block cube, but TB snapping causes 24.5 to be rounded to 25 (which is off by one) public override string Name { get; } = "CRF"; - public override string Help(string[] args) + public override string Help() { return "Import a robot from Robocraft's Community Robot Factory"; } + public override string[] ArgNames() + { + return new [] + { + "Search" + }; + } + public override bool SimpleImport(string[] args) { if (args.Length < 2) @@ -37,10 +47,10 @@ namespace NPort.Importers foreach (var cube in cubes) { float3 rotation = TranslateBlockRotation(cube.orientation); - float3 position = new float3(cube.x, cube.y, cube.z); + float3 position = new float3(cube.x - BAY_CENTER, cube.y - 8f , cube.z - BAY_CENTER); BlockColor colour = QuantizeToBlockColor(cube.colour); - PlaceBlock(BlockIDs.Cube, position, rotation, colour, BlockMaterial.SteelBodywork); - break; + PlaceBlock(BlockIDs.Cube, position * 0.2f, rotation, colour, BlockMaterial.SteelBodywork); + //break; } return true; } diff --git a/NPort/NPortPlugin.cs b/NPort/NPortPlugin.cs index 102d461..231b0e1 100644 --- a/NPort/NPortPlugin.cs +++ b/NPort/NPortPlugin.cs @@ -46,12 +46,50 @@ namespace NPort API.ImportRegistry.AddImporter(new Importers.RobocraftFactoryImporter()); } + private int _selectedItem = 0; + + private string[] _argCache = { }; + + private string[] _argNames = { }; + public override void OnGUI() { +#if DEBUG + // testing if (GUILayout.Button("Get PIG!!! OINK!")) { API.ImportRegistry.Call(new []{"CRF", "PIG",}); } +#endif + // import menu + GUILayout.BeginVertical(); + // display all available importers in a toolbar + _selectedItem = GUILayout.Toolbar(_selectedItem, API.ImportRegistry.Names); + if (_selectedItem >= 0) + { + // get arguments for selected importer + _argNames = API.ImportRegistry.Args(_selectedItem); + // grow/shrink _argCache when needed + if (_argNames.Length + 1 != _argCache.Length) + { + _argCache = new string[_argNames.Length + 1]; + // like CLI arguments (first item is name of program, others are program arguments) + _argCache[0] = API.ImportRegistry.Names[_selectedItem]; + } + // display each argument with input box + for (int i = 0; i < _argNames.Length; i++) + { + GUILayout.BeginHorizontal(); + GUILayout.Label(_argNames[i]); + _argCache[i + 1] = GUILayout.TextField(_argCache[i + 1], GUILayout.MinWidth(128)); + GUILayout.EndHorizontal(); + } + if (GUILayout.Button("Submit")) + { + API.ImportRegistry.Call(_argCache); + } + } + GUILayout.EndVertical(); } } }