diff --git a/CUDLR/Examples/GameObjectExamples.cs b/CUDLR/Examples/GameObjectExamples.cs index fa328d6..d5a8a98 100644 --- a/CUDLR/Examples/GameObjectExamples.cs +++ b/CUDLR/Examples/GameObjectExamples.cs @@ -2,36 +2,46 @@ using System.Collections.Generic; using System.Reflection; using System.Net; +using System; /** * Example console commands for getting information about GameObjects */ public static class GameObjectCommands { - [CUDLR.Command("object list", "lists all the game objects in the scene")] + [CUDLR.Command("object list", "lists all the game objects in the scene", "object")] public static void ListGameObjects() { - UnityEngine.Object[] objects = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)); - foreach (UnityEngine.Object obj in objects) { - CUDLR.Console.Log(obj.name); + Action deepList = (Transform xform, int indentLevel) => { + string listing = "- " + xform.name; + CUDLR.Console.Log(listing.PadLeft(listing.Length + indentLevel * 2)); + + int childCount = xform.childCount; + for (int child = 0; child < childCount; child++) + deepList(xform.GetChild(child), indentLevel + 1); + }; + + foreach (Transform xform in GameObject.FindObjectsOfType(typeof(Transform))) { + if (xform.parent == null) + deepList(xform, 0); } } - [CUDLR.Command("object print", "lists properties of the object")] + [CUDLR.Command("object print", "lists properties of the object", "object")] public static void PrintGameObject(string[] args) { if (args.Length < 1) { - CUDLR.Console.Log( "expected : object print " ); + CUDLR.Console.Log("expected: object print "); return; } GameObject obj = GameObject.Find( args[0] ); if (obj == null) { - CUDLR.Console.Log("GameObject not found : "+args[0]); + CUDLR.Console.Log("GameObject not found: "+args[0]); } else { - CUDLR.Console.Log("Game Object : "+obj.name); + CUDLR.Console.Log("Game Object: "+obj.name); foreach (Component component in obj.GetComponents(typeof(Component))) { - CUDLR.Console.Log(" Component : "+component.GetType()); + CUDLR.Console.Log(" + Component: "+component.GetType()); foreach (FieldInfo f in component.GetType().GetFields()) { - CUDLR.Console.Log(" "+f.Name+" : "+f.GetValue(component)); + CUDLR.Console.Log(" - "+f.Name+": "+f.GetValue(component)); } } } @@ -47,7 +57,7 @@ public static void PrintGameObject(string[] args) { // FIXME need main thread support public static class GameObjectRoutes { - + [CUDLR.Route("^/object/list.json$")] public static bool ListGameObjects(HttpListenerContext context) { string json = "["; diff --git a/CUDLR/Scripts/Attributes.cs b/CUDLR/Scripts/Attributes.cs index 89a699b..c267ba3 100644 --- a/CUDLR/Scripts/Attributes.cs +++ b/CUDLR/Scripts/Attributes.cs @@ -10,15 +10,17 @@ public class CommandAttribute : Attribute public delegate void CallbackSimple(); public delegate void Callback(string[] args); - public CommandAttribute(string cmd, string help, bool runOnMainThread = true) + public CommandAttribute(string cmd, string help, string cat, bool runOnMainThread = true) { m_command = cmd; m_help = help; + m_cat = cat; m_runOnMainThread = runOnMainThread; } public string m_command; public string m_help; + public string m_cat; public bool m_runOnMainThread; public Callback m_callback; } diff --git a/CUDLR/Scripts/Console.cs b/CUDLR/Scripts/Console.cs index fb98cff..12038ce 100644 --- a/CUDLR/Scripts/Console.cs +++ b/CUDLR/Scripts/Console.cs @@ -15,7 +15,7 @@ struct QueuedCommand { public class Console { - // Max number of lines in the console output + // Max number of lines in the buffer const int MAX_LINES = 100; // Maximum number of commands stored in the history @@ -71,15 +71,27 @@ public static void Run(string str) { } } - /* Clear all output from console */ - [Command("clear", "clears console output", false)] - public static void Clear() { - Instance.m_output.Clear(); - } - /* Print a list of all console commands */ - [Command("help", "prints commands", false)] + [Command("help", "prints commands", "basic", false)] public static void Help() { + if (string.IsNullOrEmpty(Instance.m_help)) { + var commands = Instance.m_commands.ToList(); + commands.Sort(delegate(CommandAttribute a, CommandAttribute b) { + return a.m_cat.CompareTo(b.m_cat); + }); + + string help = ""; + string cat = null; + foreach (var cmd in commands) { + if (cmd.m_cat != cat) { + help += "\n\n Category: " + cmd.m_cat; + cat = cmd.m_cat; + } + help += string.Format("\n {0} : {1}", cmd.m_command, cmd.m_help); + } + Instance.m_help = help; + } + Log( string.Format("Commands:{0}", Instance.m_help)); } @@ -96,7 +108,7 @@ public static void LogCommand(string cmd) { /* Logs string to output */ public static void Log(string str) { Instance.m_output.Add(str); - if (Instance.m_output.Count > MAX_LINES) + if (Instance.m_output.Count > MAX_LINES) Instance.m_output.RemoveAt(0); } @@ -110,20 +122,23 @@ public static void LogCallback (string logString, string stackTrace, LogType typ /* Returns the output */ public static string Output() { - return string.Join("\n", Instance.m_output.ToArray()); + string output = string.Join("\n", Instance.m_output.ToArray()); + if (!string.IsNullOrEmpty(output)) + output += "\n"; + Instance.m_output.Clear(); + return output; } /* Register a new console command */ - public static void RegisterCommand(string command, string desc, CommandAttribute.Callback callback, bool runOnMainThread = true) { + public static void RegisterCommand(string command, string desc, string cat, CommandAttribute.Callback callback, bool runOnMainThread = true) { if (command == null || command.Length == 0) { throw new Exception("Command String cannot be empty"); } - CommandAttribute cmd = new CommandAttribute(command, desc, runOnMainThread); + CommandAttribute cmd = new CommandAttribute(command, desc, cat, runOnMainThread); cmd.m_callback = callback; Instance.m_commands.Add(cmd); - Instance.m_help += string.Format("\n{0} : {1}", command, desc); } private void RegisterAttributes() { @@ -157,10 +172,9 @@ private void RegisterAttributes() { Debug.LogError(string.Format("Method {0}.{1} needs a valid command name.", type, method.Name)); continue; } - + cmd.m_callback = cb; m_commands.Add(cmd); - m_help += string.Format("\n{0} : {1}", cmd.m_command, cmd.m_help); } } } @@ -174,7 +188,7 @@ public static string PreviousCommand(int index) { /* Update history with a new command */ private void RecordCommand(string command) { m_history.Insert(0, command); - if (m_history.Count > MAX_HISTORY) + if (m_history.Count > MAX_HISTORY) m_history.RemoveAt(m_history.Count - 1); } @@ -230,6 +244,22 @@ public CommandTree() { m_subcommands = new Dictionary(); } + public List ToList() { + HashSet set = new HashSet(); + _toList(set, this); + return set.ToList(); + } + + private void _toList(HashSet set, CommandTree tree) { + if (tree.m_command != null) { + set.Add(tree.m_command); + return; + } + var subcmds = tree.m_subcommands.Values.ToArray(); + foreach (CommandTree childTree in subcmds) + tree._toList(set, childTree); + } + public void Add(CommandAttribute cmd) { _add(cmd.m_command.ToLower().Split(' '), 0, cmd); } @@ -334,4 +364,4 @@ private void RunCommand(string[] args) { } } } -} \ No newline at end of file +} diff --git a/StreamingAssets/CUDLR/console.css b/StreamingAssets/CUDLR/console.css index b3f7e92..02d8dc5 100644 --- a/StreamingAssets/CUDLR/console.css +++ b/StreamingAssets/CUDLR/console.css @@ -1,16 +1,19 @@ -textarea {resize:none;} +textarea { + resize:none; +} body.console { background-color:black; } + textarea.console { width:100%; background-color:#383838; color:#F0F0F0; font-size:14px; - font-family:;"Courier New", Courier, monospace; - + font-family:"Courier New", Courier, monospace; } + #output { height:500px; } diff --git a/StreamingAssets/CUDLR/index.html b/StreamingAssets/CUDLR/index.html index 235b764..7768e08 100644 --- a/StreamingAssets/CUDLR/index.html +++ b/StreamingAssets/CUDLR/index.html @@ -32,12 +32,18 @@ // Check if we are scrolled to the bottom to force scrolling on update var output = $('#output'); shouldScroll = (output[0].scrollHeight - output.scrollTop()) == output.innerHeight(); - output.val(String(data)); + var current = output.val(); + output.val(current + String(data)); if (callback) callback(); if (shouldScroll) scrollBottom(); }); } + function resetOutput() { + $('#output').val(""); + scrollBottom(); + } + function resetInput() { commandIndex = -1; $("#input").val(""); @@ -52,7 +58,7 @@ } function updateCommand(index) { - // Check if we are at the defualt index and clear the input + // Check if we are at the default index and clear the input if (index < 0) { resetInput(); return; @@ -82,6 +88,7 @@ +