Browse Source

Implement MVP CRF importer

tags/v0.0.1
NGnius (Graham) 3 years ago
parent
commit
f3edd8e66c
5 changed files with 102 additions and 7 deletions
  1. +3
    -1
      NPort/API/IImporter.cs
  2. +43
    -0
      NPort/API/ImportRegistry.cs
  3. +4
    -2
      NPort/API/SimpleImporter.cs
  4. +14
    -4
      NPort/Importers/RobocraftFactoryImporter.cs
  5. +38
    -0
      NPort/NPortPlugin.cs

+ 3
- 1
NPort/API/IImporter.cs View File

@@ -4,8 +4,10 @@ namespace NPort.API
{
string Name { get; }

string Help(string[] args);
string Help();

void Import(string[] args);

string[] ArgNames();
}
}

+ 43
- 0
NPort/API/ImportRegistry.cs View File

@@ -6,11 +6,38 @@ namespace NPort.API
{
private static List<IImporter> importers = new List<IImporter>();

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();
}
}
}
}

+ 4
- 2
NPort/API/SimpleImporter.cs View File

@@ -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);


+ 14
- 4
NPort/Importers/RobocraftFactoryImporter.cs View File

@@ -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;
}


+ 38
- 0
NPort/NPortPlugin.cs View File

@@ -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();
}
}
}

Loading…
Cancel
Save