public BehaviourGraphAddNodeDropdown(BehaviourManifest manifest, Action <object> onSelectNode) : base(new AdvancedDropdownState()) { minimumSize = new Vector2(270, 308); this.manifest = manifest; this.onSelectNode = onSelectNode; }
public void Work() { var manifest = BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain); var sourceObject = new RootModel() { FirstValue = "Lol", AChild = new ChildModel() { }, BChild = null, Children = new ChildModel[] { new ChildModel(), null }, MoreChildren = new Dictionary <string, ChildModel>() { ["alpha"] = new ChildModel() { }, ["beta"] = null } }; var editorSession = new EditorSession(manifest, sourceObject, new JsonSerializer()); DrawTree(editorSession.Root); editorSession.Root["BChild"].SetValue(new ChildModel()); editorSession.Root["BChild"].ApplyModifiedProperties(); DrawTree(editorSession.Root); }
public EditorSession(BehaviourManifest manifest, JObject instance, string type) { Manifest = manifest; Root = new EditorField(this, instance, "root", new FieldInformation() { Type = type }); }
public EditorSession(BehaviourManifest manifest, object instance) { Manifest = manifest; Root = new EditorField(this, JObject.FromObject(instance), "root", new FieldInformation() { Type = instance.GetType().FullName }); }
public void SetTarget(BehaviourManifest manifest) { if (Manifest == manifest) { return; } Manifest = manifest; Reload(); }
public void Work() { var types = TypeManifest.Construct( new Type[] { typeof(int), typeof(bool), typeof(string), }, new Type[] { typeof(RootModel), typeof(ChildModel) } ); var manifest = new BehaviourManifest() { Nodes = null, Types = types }; var sourceObject = new RootModel() { FirstValue = "Lol", AChild = new ChildModel() { }, BChild = null, Children = new ChildModel[] { new ChildModel(), null }, MoreChildren = new Dictionary <string, ChildModel>() { ["alpha"] = new ChildModel() { }, ["beta"] = null } }; var editorSession = new EditorSession(manifest, sourceObject, new JsonSerializer()); DrawTree(editorSession.Root); editorSession.Root["BChild"].SetValue(new ChildModel()); editorSession.Root["BChild"].ApplyModifiedProperties(); DrawTree(editorSession.Root); }
public EditorSession(BehaviourManifest manifest, JObject instance, string type, JsonSerializer jsonSerializer) { Manifest = manifest; JsonSerializer = jsonSerializer; Instance = instance; var rootField = new FieldInformation() { Type = type }; Root = new EditorField(this, instance, "root", rootField); }
public void WorkWithGenerics() { var manifest = BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain); var generic = new GenericModel() { Data = JObject.FromObject(new ChildModel()), Type = "ChildModel" }; var editorSession = new EditorSession(manifest, generic, new JsonSerializer()); DrawTree(editorSession.Root); }
public EditorSessionFrame(IResource resource) { Resource = resource; JObject editorTarget; using (var editorTargetData = Resource.LoadStream()) using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } var manifest = BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain); string typeName = null; if (Resource.Tags.Contains("type-building")) { typeName = "BuildingTemplate"; } else if (Resource.Tags.Contains("type-resource")) { typeName = "ResourceTemplate"; } else if (Resource.Tags.Contains("type-buildingpack")) { typeName = "BuildingPackTemplate"; } else if (Resource.Tags.Contains("gamerules")) { typeName = "GameRulesTemplate"; } else { typeName = "ProceduralItemTemplate"; } EditorSession = new EditorSession(manifest, editorTarget, typeName, serializer); EditorSession.OnChanged += () => { HasUnsavedChanges = true; }; var feature = EditorSession.Root.GetOrCreateFeature <FramedEditorSessionFeature>(); feature.Frame = this; }
public static TypeInformation GetTypeInformation(BehaviourManifest manifest, string type) { if (manifest.Types.JsonTypes.TryGetValue(type, out var jsonType)) { return(jsonType); } if (manifest.Types.ObjectTypes.TryGetValue(type, out var objectType)) { return(objectType); } if (manifest.Nodes.Nodes.TryGetValue(type, out var nodeType)) { return(nodeType); } return(null); }
public EditorSession(BehaviourManifest manifest, JObject instance, string type, JsonSerializer jsonSerializer) { Manifest = manifest; JsonSerializer = jsonSerializer; Instance = instance; features = new List <object>(); var typeInformation = Manifest.GetTypeInformation(type); if (typeInformation == null) { throw new InvalidOperationException($"Failed to find type for {type}"); } Root = new EditorObject(this, typeInformation, instance); }
public EditorSession(BehaviourManifest manifest, object instance, JsonSerializer jsonSerializer) { Manifest = manifest; JsonSerializer = jsonSerializer; var rootJson = JObject.FromObject(instance, JsonSerializer); string type = instance.GetType().FullName; Instance = rootJson; var rootField = new FieldInformation() { Type = type }; Root = new EditorField(this, rootJson, "root", rootField); }
public EditorSession(BehaviourManifest manifest, object instance, JsonSerializer jsonSerializer) { Manifest = manifest; JsonSerializer = jsonSerializer; features = new List <object>(); var rootJson = JObject.FromObject(instance, JsonSerializer); string type = instance.GetType().Name; var typeInformation = Manifest.GetTypeInformation(type); if (typeInformation == null) { throw new InvalidOperationException($"Failed to find type for {type}"); } Instance = rootJson; Root = new EditorObject(this, typeInformation, rootJson); }
public void Work() { var manifest = BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain); Console.Write(manifest.ToString()); var sourceObject = new RootModel() { FirstValue = "Lol", AChild = new ChildModel() { }, BChild = null, Children = new ChildModel[] { new ChildModel(), null }, MoreChildren = new Dictionary <string, ChildModel>() { ["alpha"] = new ChildModel() { }, ["beta"] = null } }; var editorSession = new EditorSession(manifest, sourceObject, new JsonSerializer()); DrawTree(editorSession.Root); var newChild = new ChildModel() { ChildFirstValue = -10, ChildSecondValue = true }; (editorSession.Root.Fields["AChild"].Value as EditorObject).PopulateObject(newChild); // (editorSession.Root.Fields["AChild"].Value as EditorEditableValue).ApplyModifiedProperties(); TestContext.Error.WriteLine("==========="); DrawTree(editorSession.Root); }
private void DrawAssetSelection() { CurrentPackage = (ProjectImport)EditorGUILayout.ObjectField(CurrentPackage, typeof(ProjectImport), true); if (CurrentPackage != null) { var explorer = CurrentPackage.Explorer; foreach (var resource in explorer.Resources) { if (!resource.Name.EndsWith(".bhvr")) { continue; } if (GUILayout.Button(resource.ToString())) { CurrentResource = resource; JObject editorTarget; using (var editorTargetData = CurrentResource.LoadStream()) using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } var nodes = NodeManifest.Construct(new Type[] { typeof(AddNode), typeof(RollNode), typeof(OutputValueNode) }); var types = TypeManifest.ConstructBaseTypes(); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types, }; Debug.Log(editorTarget); var graphEditor = new EditorSession(manifest, editorTarget, "SerializedGraph", Serializer); View.BeginSession(graphEditor); } } } }
public void Start() { IntEventField left = new IntEventField(1); IntEventField right = new IntEventField(3); left += (int newValue) => { Console.WriteLine("New Value: " + newValue); }; left.Value += 5; Console.WriteLine($"{left.Value} + {right.Value} = " + (left + right).Calculate()); IntEventField one = new IntEventField(1); IntEventField test = new IntEventField(left + right + one); left.Value++; right.Value++; Console.WriteLine($"{left.Value} + {right.Value} + {one.Value} = " + test.Value); var nodes = NodeManifest.Construct(new Type[] { typeof(StatsNode), typeof(RollNode) }); var types = TypeManifest.ConstructBaseTypes(); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types }; File.WriteAllText("Content/RPGCoreMath.bmfst", manifest.ToString()); Console.WriteLine("Importing Graph..."); var proj = ProjectExplorer.Load("Content/Core"); Console.WriteLine(proj.Name); Console.WriteLine("\t\"" + proj.Name + "\""); foreach (var asset in proj.Assets) { Console.WriteLine("\t" + asset.Archive.Name); foreach (var resource in asset.Resources) { Console.WriteLine("\t\t" + resource); } } proj.Export("Content/Core.bpkg"); Console.WriteLine("Exported package..."); var exportedPackage = PackageExplorer.Load("Content/Core.bpkg"); foreach (var asset in exportedPackage.Assets) { Console.WriteLine(asset.Root); foreach (var resource in asset.Resources) { Console.WriteLine("\t" + resource.ToString()); } } var packageItem = JsonConvert.DeserializeObject <PackageBehaviour> (File.ReadAllText("Content/Core/Longsword/Main.bhvr")); Console.WriteLine("Imported: " + packageItem.Name); var unpackedGraph = packageItem.Unpack(); Console.WriteLine("Running Simulation..."); var player = new Actor(); IBehaviour instancedItem = unpackedGraph.Setup(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } instancedItem.Remove(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } }
public void Start() { var nodes = NodeManifest.Construct( new Type[] { typeof(AddNode), typeof(RollNode), typeof(OutputValueNode), typeof(ItemInputNode), typeof(GetStatNode), typeof(IterateNode), } ); var types = TypeManifest.ConstructBaseTypes(); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types }; File.WriteAllText("Content/RPGCoreMath.bmfst", manifest.ToString()); Console.WriteLine("Importing Graph..."); var proj = ProjectExplorer.Load("Content/Tutorial"); Console.WriteLine(proj.Name); Console.WriteLine("\t\"" + proj.Name + "\""); foreach (var resource in ((IPackageExplorer)proj).Resources) { Console.WriteLine("\t" + resource.FullName); } var editorTargetResource = proj.Resources["Tutorial Gamerules/Main.bhvr"]; var editorTargetData = editorTargetResource.LoadStream(); JObject editorTarget; var serializer = new JsonSerializer(); using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } var editor = new EditorSession(manifest, editorTarget, "SerializedGraph", serializer); foreach (var node in editor.Root["Nodes"]) { var nodeData = node["Data"]; foreach (var field in nodeData) { Console.WriteLine($"{field}"); if (field.Name == "MaxValue") { field.SetValue(field.GetValue <int> () + 10); field.ApplyModifiedProperties(); field.SetValue(field.GetValue <int> ()); field.ApplyModifiedProperties(); } else if (field.Name == "ValueB") { Console.WriteLine(field.GetValue <LocalPropertyId> ()); } } } using (var file = editorTargetResource.WriteStream()) using (var jsonWriter = new JsonTextWriter(file) { Formatting = Formatting.Indented }) { serializer.Serialize(jsonWriter, editorTarget); } Console.WriteLine(new DirectoryInfo("Content/Temp").FullName); var consoleRenderer = new BuildConsoleRenderer(); var buildPipeline = new BuildPipeline() { Exporters = new List <ResourceExporter> () { new BhvrExporter() }, BuildActions = new List <IBuildAction> () { consoleRenderer } }; consoleRenderer.DrawProgressBar(32); proj.Export(buildPipeline, "Content/Temp"); Console.WriteLine("Exported package..."); var exportedPackage = PackageExplorer.Load("Content/Temp/Core.bpkg"); var fireballAsset = exportedPackage.Resources["Fireball/Main.bhvr"]; var data = fireballAsset.LoadStream(); SerializedGraph packageItem; using (var sr = new StreamReader(data)) using (var reader = new JsonTextReader(sr)) { packageItem = serializer.Deserialize <SerializedGraph> (reader); } Console.WriteLine("Imported: " + packageItem.Name); var unpackedGraph = packageItem.Unpack(); Console.WriteLine("Running Simulation..."); var player = new DemoPlayer(); IGraphInstance instancedItem = unpackedGraph.Create(); instancedItem.Setup(); instancedItem.SetInput(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 10; } instancedItem.Remove(); var packedInstance = ((GraphInstance)instancedItem).Pack(); string serializedGraph = packedInstance.AsJson(); Console.WriteLine(serializedGraph); var deserialized = JsonConvert.DeserializeObject <SerializedGraphInstance> (serializedGraph); var unpackedInstance = deserialized.Unpack(unpackedGraph); unpackedInstance.SetInput(player); unpackedInstance.Setup(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } unpackedInstance.Remove(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } }
public EditorSessionFrame(IResource resource) { Resource = resource; JObject editorTarget; using (var editorTargetData = Resource.LoadStream()) using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } var nodes = NodeManifest.Construct(new Type[] { typeof(AddNode), typeof(RollNode), typeof(OutputValueNode), typeof(ItemInputNode), typeof(ActivatableItemNode), typeof(IterateNode), typeof(GetStatNode), }); var types = TypeManifest.Construct( new Type[] { typeof(bool), typeof(string), typeof(int), typeof(byte), typeof(long), typeof(short), typeof(uint), typeof(ulong), typeof(ushort), typeof(sbyte), typeof(char), typeof(float), typeof(double), typeof(decimal), typeof(InputSocket), typeof(LocalId), }, new Type[] { typeof(SerializedGraph), typeof(SerializedNode), typeof(PackageNodeEditor), typeof(PackageNodePosition), typeof(ExtraData), typeof(ResourceTemplate), typeof(BuildingTemplate), } ); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types, }; string typeName = null; if (Resource.Tags.Contains("type-building")) { typeName = "BuildingTemplate"; } else if (Resource.Tags.Contains("type-resource")) { typeName = "ResourceTemplate"; } EditorSession = new EditorSession(manifest, editorTarget, typeName, Serializer); }
private void HandleInput() { if (currentEvent.type == EventType.MouseUp) { switch (View.CurrentMode) { case BehaviourEditorView.ControlMode.NodeDragging: currentEvent.Use(); foreach (string selectedNode in View.Selection) { var nodesField = View.EditorObject.Fields["Nodes"]; var selectedNodeData = (nodesField.Value as EditorDictionary).KeyValuePairs[selectedNode].Value as EditorObject; var editor = selectedNodeData.Fields["Editor"].Value as EditorObject; var editorPosition = editor.Fields["Position"].Value as EditorObject; var posX = editorPosition.Fields["x"].Value as EditorValue; posX.ApplyModifiedProperties(); var posY = editorPosition.Fields["y"].Value as EditorValue; posY.ApplyModifiedProperties(); } View.CurrentMode = BehaviourEditorView.ControlMode.None; break; case BehaviourEditorView.ControlMode.ViewDragging: View.CurrentMode = BehaviourEditorView.ControlMode.None; break; case BehaviourEditorView.ControlMode.CreatingConnection: View.CurrentMode = BehaviourEditorView.ControlMode.None; break; } Window.Repaint(); } else if (currentEvent.type == EventType.KeyDown) { if (currentEvent.keyCode == KeyCode.Space) { var position = new PackageNodePosition() { x = (int)currentEvent.mousePosition.x, y = (int)currentEvent.mousePosition.y }; var window = new BehaviourGraphAddNodeDropdown(BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain), (newNodeObject) => { string newNodeType = (string)newNodeObject; var graphEditorNodesField = View.EditorObject.Fields["Nodes"]; var graphEditorNodes = graphEditorNodesField.Value as EditorDictionary; string newId = LocalId.NewId().ToString(); graphEditorNodes.Add(newId); var newNode = graphEditorNodes.KeyValuePairs[newId]; var nodeData = new JObject(); /*newNode.SetValue(new SerializedNode() * { * Type = newNodeType, * Data = nodeData, * Editor = new PackageNodeEditor() * { * Position = position * } * }); * newNode.ApplyModifiedProperties(); */ Window.Repaint(); }); window.Show(new Rect(currentEvent.mousePosition.x, currentEvent.mousePosition.y, 0, 0)); } else if (currentEvent.keyCode == KeyCode.Delete) { var graphEditorNodesField = View.EditorObject.Fields["Nodes"]; var graphEditorNodes = graphEditorNodesField.Value as EditorDictionary; foreach (string node in View.Selection) { graphEditorNodes.Remove(node); } } } else if (currentEvent.type == EventType.MouseDrag) { switch (View.CurrentMode) { case BehaviourEditorView.ControlMode.NodeDragging: foreach (string selectedNode in View.Selection) { var nodesField = View.EditorObject.Fields["Nodes"]; var selectedNodeData = (nodesField.Value as EditorDictionary).KeyValuePairs[selectedNode].Value as EditorObject; var editor = selectedNodeData.Fields["Editor"].Value as EditorObject; var editorPosition = editor.Fields["Position"].Value as EditorObject; var posX = editorPosition.Fields["x"].Value as EditorValue; posX.SetValue(posX.GetValue <int>() + ((int)currentEvent.delta.x)); var posY = editorPosition.Fields["y"].Value as EditorValue; posY.SetValue(posY.GetValue <int>() + ((int)currentEvent.delta.y)); } break; case BehaviourEditorView.ControlMode.ViewDragging: View.PanPosition += currentEvent.delta; break; } Window.Repaint(); } else if (currentEvent.type == EventType.MouseDown) { if (Position.Contains(currentEvent.mousePosition)) { GUI.UnfocusWindow(); GUI.FocusControl(""); View.CurrentMode = BehaviourEditorView.ControlMode.ViewDragging; currentEvent.Use(); Window.Repaint(); } } }
public void Start() { var nodes = NodeManifest.Construct(new Type[] { typeof(StatsNode), typeof(RollNode) }); var types = TypeManifest.ConstructBaseTypes(); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types }; File.WriteAllText("Content/RPGCoreMath.bmfst", manifest.ToString()); Console.WriteLine("Importing Graph..."); var proj = ProjectExplorer.Load("Content/Tutorial"); Console.WriteLine(proj.Name); Console.WriteLine("\t\"" + proj.Name + "\""); foreach (var asset in proj.Assets) { Console.WriteLine("\t" + asset.Archive.Name); foreach (var resource in asset.ProjectResources) { Console.WriteLine("\t\t" + resource); } } proj.Export("Content/Temp"); Console.WriteLine("Exported package..."); var exportedPackage = PackageExplorer.Load("Content/Temp/Core.bpkg"); foreach (var asset in exportedPackage.Assets) { Console.WriteLine(asset.Root); foreach (var resource in asset.PackageResources) { Console.WriteLine("\t" + resource.ToString()); } } var fireballAsset = exportedPackage.Assets["Fireball"]; var data = fireballAsset.GetResource("Main.bhvr").LoadStream(); SerializedGraph packageItem; using (var sr = new StreamReader(data)) using (var reader = new JsonTextReader(sr)) { var serializer = new JsonSerializer(); packageItem = serializer.Deserialize <SerializedGraph>(reader); } Console.WriteLine("Imported: " + packageItem.Name); var unpackedGraph = packageItem.Unpack(); Console.WriteLine("Running Simulation..."); var player = new Actor(); IBehaviour instancedItem = unpackedGraph.Setup(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } instancedItem.Remove(); var packedInstance = ((GraphInstance)instancedItem).Pack(); string serializedGraph = packedInstance.AsJson(); Console.WriteLine(serializedGraph); var deserialized = JsonConvert.DeserializeObject <SerializedGraphInstance>(serializedGraph); var unpackedInstance = deserialized.Unpack(unpackedGraph); unpackedInstance.Setup(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } unpackedInstance.Remove(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } }
private void DrawAssetSelection() { CurrentPackage = (ProjectImport)EditorGUILayout.ObjectField(CurrentPackage, typeof(ProjectImport), true, GUILayout.Width(180)); if (CurrentPackage == null) { return; } var explorer = CurrentPackage.Explorer; foreach (var resource in explorer.Resources) { if (!resource.Name.EndsWith(".bhvr")) { continue; } if (GUILayout.Button(resource.ToString())) { CurrentResource = resource; JObject editorTarget; using (var editorTargetData = CurrentResource.LoadStream()) using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } var nodes = NodeManifest.Construct(new Type[] { typeof(AddNode), typeof(RollNode), typeof(OutputValueNode), typeof(ItemInputNode), typeof(ActivatableItemNode), typeof(IterateNode), typeof(GetStatNode), }); var types = TypeManifest.Construct( new Type[] { typeof(bool), typeof(string), typeof(int), typeof(byte), typeof(long), typeof(short), typeof(uint), typeof(ulong), typeof(ushort), typeof(sbyte), typeof(char), typeof(float), typeof(double), typeof(decimal), typeof(InputSocket), typeof(LocalId), }, new Type[] { typeof(SerializedGraph), typeof(SerializedNode), typeof(PackageNodeEditor), typeof(PackageNodePosition), typeof(ExtraData) } ); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types, }; Debug.Log(editorTarget); var graphEditor = new EditorSession(manifest, editorTarget, "SerializedGraph", Serializer); View.BeginSession(graphEditor, graphEditor.Root); } } }
public ManifestInspectFrame(BehaviourManifest manifest) { Manifest = manifest; }
private void BuildProject(List <TreeViewItem> collection, IExplorer explorer, int depth, ref int id) { var newItem = new ContentTreeViewItem { displayName = explorer.Definition.Properties.Name, id = id++, depth = depth, icon = ContentEditorResources.Instance.ProjectIcon, item = explorer }; idToItemMapping[newItem.id] = newItem; collection.Add(newItem); collection.Add(new ContentTreeViewItem { displayName = "Dependancies", id = id++, depth = depth + 1, icon = ContentEditorResources.Instance.DependanciesIcon, item = null }); collection.Add(new ContentTreeViewItem { displayName = "Manifests", id = id++, depth = depth + 2, icon = ContentEditorResources.Instance.ManifestDependancyIcon, item = null }); var manifestItem = BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain); var manifestTreeViewItem = new ContentTreeViewItem { displayName = "RPGCore 1.0.0", id = id++, depth = depth + 3, icon = ContentEditorResources.Instance.ManifestDependancyIcon, item = manifestItem }; idToItemMapping[manifestTreeViewItem.id] = manifestTreeViewItem; collection.Add(manifestTreeViewItem); collection.Add(new ContentTreeViewItem { displayName = "Projects", id = id++, depth = depth + 2, icon = ContentEditorResources.Instance.ProjectDependancyIcon, item = null }); BuildDirectory(collection, explorer.RootDirectory, depth + 1, ref id); }
public void Start() { var nodes = NodeManifest.Construct(new Type[] { typeof(AddNode), typeof(RollNode) }); var types = TypeManifest.ConstructBaseTypes(); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types }; File.WriteAllText("Content/RPGCoreMath.bmfst", manifest.ToString()); Console.WriteLine("Importing Graph..."); var proj = ProjectExplorer.Load("Content/Tutorial"); Console.WriteLine(proj.Name); Console.WriteLine("\t\"" + proj.Name + "\""); foreach (var resource in ((IPackageExplorer)proj).Resources) { Console.WriteLine("\t" + resource.FullName); } var editorTargetResource = proj.Resources["Tutorial Gamerules/Main.bhvr"]; var editorTargetData = editorTargetResource.LoadStream(); JObject editorTarget; var serializer = new JsonSerializer(); using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } //var editNode = editorTarget.Nodes.First (); var editor = new EditorSession(manifest, editorTarget, "SerializedGraph"); /*foreach (var field in editor.Root) * { * Console.WriteLine ($"{field.Name}: {field.Json} ({field.Field.Type})"); * if (field.Name == "MaxValue") * { * var newObject = JToken.FromObject (field.Json.ToObject<int> () + 10); * field.Json.Replace (newObject); * } * }*/ using (var file = editorTargetResource.WriteStream()) { serializer.Serialize(new JsonTextWriter(file) { Formatting = Formatting.Indented }, editorTarget); } Console.WriteLine(new DirectoryInfo("Content/Temp").FullName); var consoleRenderer = new BuildConsoleRenderer(); var buildPipeline = new BuildPipeline() { Exporters = new List <ResourceExporter> () { new BhvrExporter() }, BuildActions = new List <IBuildAction> () { consoleRenderer } }; consoleRenderer.DrawProgressBar(32); proj.Export(buildPipeline, "Content/Temp"); Console.WriteLine("Exported package..."); var exportedPackage = PackageExplorer.Load("Content/Temp/Core.bpkg"); var fireballAsset = exportedPackage.Resources["Fireball/Main.bhvr"]; var data = fireballAsset.LoadStream(); SerializedGraph packageItem; using (var sr = new StreamReader(data)) using (var reader = new JsonTextReader(sr)) { packageItem = serializer.Deserialize <SerializedGraph> (reader); } Console.WriteLine("Imported: " + packageItem.Name); var unpackedGraph = packageItem.Unpack(); Console.WriteLine("Running Simulation..."); var player = new Actor(); IBehaviour instancedItem = unpackedGraph.Setup(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } instancedItem.Remove(); var packedInstance = ((GraphInstance)instancedItem).Pack(); string serializedGraph = packedInstance.AsJson(); Console.WriteLine(serializedGraph); var deserialized = JsonConvert.DeserializeObject <SerializedGraphInstance> (serializedGraph); var unpackedInstance = deserialized.Unpack(unpackedGraph); unpackedInstance.Setup(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } unpackedInstance.Remove(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } }
public static void Run() { var manifest = BehaviourManifest.CreateFromAppDomain(AppDomain.CurrentDomain); File.WriteAllText("Content/RPGCoreMath.bmfst", manifest.ToString()); Console.WriteLine("Importing Graph..."); var importPipeline = ImportPipeline.Create().Build(); var proj = ProjectExplorer.Load("Content/Core", importPipeline); Console.WriteLine(proj.Definition.Properties.Name); Console.WriteLine($"\t\"{proj.Definition.Properties.Name}\""); foreach (var resource in ((IExplorer)proj).Resources) { Console.WriteLine($"\t{resource.FullName}"); } var editorTargetResource = proj.Resources["Fireball/Main.json"]; var editorTargetData = editorTargetResource.Content.LoadStream(); JObject editorTarget; var serializer = new JsonSerializer(); using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); } var editor = new EditorSession(manifest, editorTarget, "SerializedGraph", serializer); foreach (var node in (editor.Root.Fields["Nodes"].Value as EditorObject).Fields.Values) { var nodeData = (node.Value as EditorObject).Fields["Data"]; foreach (var field in (nodeData.Value as EditorObject).Fields.Values) { var editableValue = field.Value as EditorValue; Console.WriteLine($"{field}"); if (field.Field.Name == "MaxValue") { editableValue.SetValue(editableValue.GetValue <int>() + 10); editableValue.ApplyModifiedProperties(); editableValue.SetValue(editableValue.GetValue <int>()); editableValue.ApplyModifiedProperties(); } else if (field.Field.Name == "ValueB") { Console.WriteLine(editableValue.GetValue <LocalPropertyId>()); } } } using (var file = editorTargetResource.Content.OpenWrite()) using (var sr = new StreamWriter(file)) using (var jsonWriter = new JsonTextWriter(sr) { Formatting = Formatting.Indented }) { serializer.Serialize(jsonWriter, editorTarget); } Console.WriteLine(new DirectoryInfo("Content/Temp").FullName); var consoleRenderer = new BuildConsoleRenderer(); var buildPipeline = new BuildPipeline(); buildPipeline.Exporters.Add(new BhvrExporter()); buildPipeline.BuildActions.Add(consoleRenderer); consoleRenderer.DrawProgressBar(32); proj.ExportZippedToDirectory(buildPipeline, "Content/Temp"); Console.WriteLine("Exported package..."); var exportedPackage = PackageExplorer.LoadFromFileAsync("Content/Temp/Core.bpkg").Result; var fireballAsset = exportedPackage.Resources["Fireball/Main.json"]; var data = fireballAsset.Content.LoadStream(); SerializedGraph packageItem; using (var sr = new StreamReader(data)) using (var reader = new JsonTextReader(sr)) { packageItem = serializer.Deserialize <SerializedGraph>(reader); } Console.WriteLine("Imported: " + fireballAsset.Name); var unpackedGraph = packageItem.Unpack(); Console.WriteLine("Running Simulation..."); var player = new DemoPlayer(); IGraphInstance instancedItem = unpackedGraph.Create(); instancedItem.Setup(); instancedItem.SetInput(player); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 10; } instancedItem.Remove(); var settings = new JsonSerializerSettings(); settings.Converters.Add(new LocalIdJsonConverter()); settings.Converters.Add(new SerializedGraphInstanceProxyConverter(null)); string serializedGraph = JsonConvert.SerializeObject(instancedItem, settings); // var packedInstance = ((GraphInstance)instancedItem).Pack (); // string serializedGraph = packedInstance.AsJson (); Console.WriteLine(serializedGraph); var deserialized = JsonConvert.DeserializeObject <SerializedGraphInstance>(serializedGraph); var unpackedInstance = deserialized.Unpack(unpackedGraph); unpackedInstance.SetInput(player); unpackedInstance.Setup(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } unpackedInstance.Remove(); for (int i = 0; i < 5; i++) { Thread.Sleep(100); player.Health.Value -= 20; } }
public void OnGUI() { screenRect = new Rect(0, EditorGUIUtility.singleLineHeight + 1, position.width, position.height - (EditorGUIUtility.singleLineHeight + 1)); currentEvent = Event.current; // HandleDragAndDrop (screenRect); DrawBackground(screenRect, dragging_Position); DrawTopBar(); CurrentPackage = (ProjectImport)EditorGUILayout.ObjectField(CurrentPackage, typeof(ProjectImport), true); var explorer = CurrentPackage.Explorer; foreach (var resource in explorer.Resources) { if (!resource.Name.EndsWith(".bhvr")) { continue; } if (GUILayout.Button(resource.ToString())) { CurrentResource = resource; HasCurrentResource = true; HasEditor = false; } } if (HasCurrentResource && CurrentResource != null) { if (HasEditor == false) { Debug.Log(CurrentResource); var editorTargetData = CurrentResource.LoadStream(); using (var sr = new StreamReader(editorTargetData)) using (var reader = new JsonTextReader(sr)) { editorTarget = JObject.Load(reader); // editorTarget = serializer.Deserialize(reader); } var nodes = NodeManifest.Construct(new Type[] { typeof(AddNode), typeof(RollNode) }); var types = TypeManifest.ConstructBaseTypes(); var manifest = new BehaviourManifest() { Nodes = nodes, Types = types, }; Debug.Log(editorTarget); graphEditor = new EditorSession(manifest, editorTarget, "SerializedGraph"); HasEditor = true; } if (GUILayout.Button("Save")) { using (var file = CurrentResource.WriteStream()) { serializer.Serialize(new JsonTextWriter(file) { Formatting = Formatting.Indented }, editorTarget); } } foreach (var node in graphEditor.Root["Nodes"]) { var nodeEditor = node["Editor"]; var nodeEditorPosition = nodeEditor["Position"]; var nodeRect = new Rect( dragging_Position.x + nodeEditorPosition["x"].GetValue <int>(), dragging_Position.y + nodeEditorPosition["y"].GetValue <int>(), 200, 160 ); bool startDrag = false; if (Event.current.type == EventType.Repaint) { BehaviourGraphResources.Instance.NodeStyle.Draw(nodeRect, false, node.Name == selectedNode, false, false); } GUILayout.BeginArea(nodeRect); var nodeData = node.Json["Data"]; var nodeType = node["Type"]; object fieldObject; if (!node.ViewBag.TryGetValue("Generic", out fieldObject)) { var fieldInformation = new FieldInformation(); fieldInformation.Type = nodeType.Json.ToObject <string>(); fieldObject = new EditorField(graphEditor, nodeData, node.Name, fieldInformation); node.ViewBag["Generic"] = fieldObject; } var field = (EditorField)fieldObject; foreach (var childField in field) { DrawField(childField); } GUILayout.EndArea(); if (Event.current.type == EventType.MouseDown) { if (nodeRect.Contains(Event.current.mousePosition)) { selectedNode = node.Name; dragging_IsDragging = true; dragging_NodeDragging = true; GUI.UnfocusWindow(); GUI.FocusControl(""); Event.current.Use(); } } } foreach (var node in graphEditor.Root["Nodes"]) { var nodeEditor = node["Editor"]; var nodeEditorPosition = nodeEditor["Position"]; var nodePositionX = nodeEditorPosition["x"].GetValue <int>() + dragging_Position.x; var nodePositionY = nodeEditorPosition["y"].GetValue <int>() + dragging_Position.y; var nodeData = node.Json["Data"]; var nodeType = node["Type"]; object fieldObject; if (!node.ViewBag.TryGetValue("Generic", out fieldObject)) { var fieldInformation = new FieldInformation(); fieldInformation.Type = nodeType.Json.ToObject <string>(); fieldObject = new EditorField(graphEditor, nodeData, node.Name, fieldInformation); node.ViewBag["Generic"] = fieldObject; } var field = (EditorField)fieldObject; foreach (var childField in field) { if (childField.Field.Type == "InputSocket") { object renderPosObject; var renderPos = new Rect();; if (childField.ViewBag.TryGetValue("Position", out renderPosObject)) { renderPos = (Rect)renderPosObject; } else { Debug.LogError(childField.Name + " has no position"); } renderPos.x += nodePositionX; renderPos.y += nodePositionY; // EditorGUI.DrawRect(renderPos, Color.red); Vector3 start = new Vector3(renderPos.x, renderPos.y); Vector3 end = new Vector3(renderPos.x - 100, renderPos.y - 100); Vector3 startDir = new Vector3(-1, 0); Vector3 endDir = new Vector3(1, 0); DrawConnection(start, end, startDir, endDir); } } } } HandleInput(); }