/// <summary> /// Generates the source code in C# for the specified scripting that represents scene scripting. /// </summary> /// <param name="globalScripting">The scripting to generate the source code for.</param> /// <param name="textWriter">The <see cref="TextWriter"/> for writing the source code of the scene.</param> public void GenerateGlobalSceneActor(ScriptingComponent globalScripting, TextWriter textWriter) { actor = null; writer = textWriter; writer.WriteLine("class GlobalSceneActor : Actor"); writer.WriteLine("{"); writer.WriteLine("public override void InitializeActor()"); writer.WriteLine("{"); writer.WriteLine("Active = true;"); GenerateActorVariables(globalScripting); GenerateActorEvents(globalScripting); writer.WriteLine("base.InitializeActor();"); writer.WriteLine("}"); writer.WriteLine("public override void InitializeComponents()"); writer.WriteLine("{"); GenerateActorScripting(globalScripting); writer.WriteLine("base.InitializeComponents();"); writer.WriteLine("}"); writer.WriteLine("public override Actor Create()"); writer.WriteLine("{"); writer.WriteLine("throw new NotImplementedException();"); writer.WriteLine("}"); writer.WriteLine("}"); }
internal void Generate(string outputDirectory) { if (!Directory.Exists(outputDir)) { Directory.CreateDirectory(outputDirectory); } outputDir = outputDirectory; scriptingComponent = new ScriptingComponent(null); scriptingScreen = new ScriptingScreen(); scriptingScreen.State = scriptingComponent.StateMachines[0].StartingState; scriptingScreen.Width = screenRect.Width; scriptingScreen.Height = screenRect.Height; image = new Bitmap(screenRect.Width, screenRect.Height); // create xml dcument doc = new XmlDocument(); XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("nodes")); // generate nodes info and images GenerateNodes(ScriptingNodes.Root, root); // save xml document doc.Save(Path.Combine(outputDir, "nodes.xml")); }
/// <summary> /// Generates the source code for initializing actor events from the specified scripting. /// </summary> /// <param name="scripting">The scripting to get actor events from.</param> private void GenerateActorEvents(ScriptingComponent scripting) { // init events foreach (Event scriptEvent in scripting.EventsOut) { writer.WriteLine(@"events.Add(""{0}"", new EventWrapper());", scriptEvent.Name); } }
/// <summary> /// Deserialize object. /// </summary> /// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/> to retrieve data.</param> /// <param name="ctxt">The source (see <see cref="System.Runtime.Serialization.StreamingContext"/>) for this deserialization.</param> private Scene(SerializationInfo info, StreamingContext ctxt) { _name = info.GetString("Name"); _paths = (ObservableIndexedList <Path>)info.GetValue("Paths", typeof(ObservableIndexedList <Path>)); _layers = (ObservableIndexedList <Layer>)info.GetValue("Layers", typeof(ObservableIndexedList <Layer>)); _selectedLayer = (Layer)info.GetValue("SelectedLayer", typeof(Layer)); _globalScript = (ScriptingComponent)info.GetValue("GlobalScript", typeof(ScriptingComponent)); }
/// <summary> /// Generates the source code for initializing state machines from the specified scripting. /// </summary> /// <param name="scripting">Scripting to get state machines from.</param> private void GenerateStateMachines(ScriptingComponent scripting) { Debug.Assert(scripting.StateMachines.Count != 0, "One state machine is required."); writer.WriteLine("stateMachines = new State[{0}];", scripting.StateMachines.Count); Dictionary <State, string> states = new Dictionary <State, string>(); // init states for (int i = 0; i < scripting.StateMachines.Count; ++i) { Debug.Assert(scripting.StateMachines[i].States.Count != 0, "One state is required."); for (int j = 0; j < scripting.StateMachines[i].States.Count; ++j) { State state = scripting.StateMachines[i].States[j]; states[state] = String.Format("stateMachine{0}state{1}", i, j); writer.WriteLine("State {0} = new State(this, {1});", states[state], i); } writer.WriteLine("stateMachines[{0}] = {1};", i, scripting.StateMachines[i].StartingState != null ? states[scripting.StateMachines[i].StartingState] : states[scripting.StateMachines[i].States[0]]); } // create connections for (int i = 0; i < scripting.StateMachines.Count; ++i) { for (int j = 0; j < scripting.StateMachines[i].States.Count; ++j) { State state = scripting.StateMachines[i].States[j]; foreach (Transition transition in state.Transitions) { Debug.Assert(state == transition.StateFrom, "Incorrect format of transition."); if (transition.Event != null && transition.StateTo != null) { writer.WriteLine(@"{0}.AddTransition(""{1}"", {2});", states[state], transition.Event.Name, states[transition.StateTo]); } } } } // create script nodes for (int i = 0; i < scripting.StateMachines.Count; ++i) { for (int j = 0; j < scripting.StateMachines[i].States.Count; ++j) { State state = scripting.StateMachines[i].States[j]; writer.WriteLine("{"); GenerateScriptNodes(state, states[state]); writer.WriteLine("}"); } } }
/// <summary> /// Generates the source code for initializing actor variables from the specified scripting. /// </summary> /// <param name="scripting">The scripting to get actor variables from.</param> private void GenerateActorVariables(ScriptingComponent scripting) { // init variables foreach (NamedVariable variable in scripting.Variables) { writer.WriteLine(@"variables.Add(""{0}"", new Variable<{1}>());", variable.Name, GetVariableGameEngineType(variable.VariableType)); } }
/// <summary> /// Initializes a new instance of the <see cref="Actor"/> class. /// </summary> /// <param name="drawableAsset">The drawable asset for the actor.</param> /// <param name="position">The position of the actor.</param> public Actor(DrawableAsset drawableAsset, Vector2 position) { _name = String.Format("Actor {0}", Id); _drawableAsset = drawableAsset; _position = position; _physics = new PhysicsComponent(); _scripting = new ScriptingComponent(this); _type = Project.Singleton.ActorTypes.Root; _children = new ChildrenList(this); DrawableAssetVisible = true; }
private void GenerateActorScripting(ScriptingComponent scripting) { // set local variables values foreach (NamedVariable variable in scripting.Variables) { writer.WriteLine(@"GetVariable<{0}>(""{1}"").Value = {2};", GetVariableGameEngineType(variable.VariableType), variable.Name, GetVariableValue(variable.Value)); } // generate state machines GenerateStateMachines(scripting); }
/// <summary> /// Initializes a new instance of the <see cref="Actor"/> class from the specified <see cref="Actor"/> instance. /// </summary> /// <param name="actor">The actor.</param> private Actor(Actor actor) { _drawableAsset = actor.DrawableAsset; _position = actor.Position; _name = actor.Name; _angle = actor.Angle; _scale = actor.ScaleFactor; _physics = actor.Physics.Clone(); _scripting = actor.Scripting.Clone(this); _type = actor.Type; _children = new ChildrenList(this); DrawableAssetVisible = actor.DrawableAssetVisible; }
/// <summary> /// Initializes a new instance of the <see cref="Scene"/> class. /// Creates the default layer for the scene. /// </summary> public Scene() { _globalScript = new Scripting.ScriptingComponent(null); _layers = new ObservableIndexedList <Layer>(); _paths = new ObservableIndexedList <Path>(); // default layer _selectedLayer = new Layer(this) { Name = "Default" }; _layers.Add(_selectedLayer); }
/// <inheritdoc /> protected Actor(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt) { Layer = (Layer)info.GetValue("Layer", typeof(Layer)); _parent = (Actor)info.GetValue("Parent", typeof(Actor)); _children = (ChildrenList)info.GetValue("Children", typeof(ChildrenList)); _position = (Vector2)info.GetValue("Position", typeof(Vector2)); _angle = info.GetSingle("Angle"); _scale = (Vector2)info.GetValue("ScaleFactor", typeof(Vector2)); _type = (ActorType)info.GetValue("ActorType", typeof(ActorType)); _drawableAsset = (DrawableAsset)info.GetValue("DrawableAsset", typeof(DrawableAsset)); _physics = (PhysicsComponent)info.GetValue("Physics", typeof(PhysicsComponent)); _scripting = (ScriptingComponent)info.GetValue("Scripting", typeof(ScriptingComponent)); _shapes = (ObservableList <Shape>)info.GetValue("Shapes", typeof(ObservableList <Shape>)); DrawableAssetVisible = info.GetBoolean("DrawableAssetVisible"); }
/// <summary> /// Gets the path from the specified scripting. /// </summary> /// <param name="scripting">The scripting where to start.</param> /// <param name="path">Container to store items on the path.</param> protected void GetPathFromScripting(ScriptingComponent scripting, List <ItemOnPath> path) { if (scripting.Actor != null && scripting.Scene == null) { GetPathFromActor(scripting.Actor, path); } else if (scripting.Actor == null && scripting.Scene != null) { path.Add(new ItemOnPath("Scene Scripting", null, scripting)); path.Add(CreateItemOnPath(scripting.Scene)); } else { Debug.Assert(true, "Not possible combination"); } }
/// <summary> /// Deletes the specified named variable from the specified scripting. /// </summary> /// <param name="namedVariable">The named variable to delete.</param> /// <param name="scripting">The scripting.</param> /// <param name="items">Stores items to delete.</param> public static void DeleteFromScripting(NamedVariable namedVariable, ScriptingComponent scripting, HashSet <ItemForDeletion> items) { // all state machines foreach (StateMachine stateMachine in scripting.StateMachines) { // all states foreach (State state in stateMachine.States) { foreach (BaseNode baseNode in state.Nodes) { Variable variable = baseNode as Variable; if (variable != null && variable.NamedVariable == namedVariable) { items.Add(new ScriptVariableOfNamedVariableForDeletion(variable)); } } } } }
/// <summary> /// Deletes the specified value of the specified variable type from the specified scripting. /// </summary> /// <param name="variableType">Type of the variable.</param> /// <param name="value">The value.</param> /// <param name="scripting">The scripting.</param> /// <param name="items">Stores items to delete.</param> public static void DeleteFromScripting(VariableType variableType, object value, ScriptingComponent scripting, HashSet <ItemForDeletion> items) { // all variables foreach (NamedVariable variable in scripting.Variables) { // used in variable if (variable.VariableType == variableType && variable.Value.GetValue() == value) { items.Add(new ScriptNamedVariableForDeletion(variable)); } } // all state machines foreach (StateMachine stateMachine in scripting.StateMachines) { // all states foreach (State state in stateMachine.States) { foreach (BaseNode baseNode in state.Nodes) { // all script nodes if (baseNode is Node) { Node node = baseNode as Node; foreach (NodeSocket nodeSocket in node.Sockets) { // all variable node sockets if (nodeSocket is VariableNodeSocket) { VariableNodeSocket variableNodeSocket = nodeSocket as VariableNodeSocket; // used in variable node socket if (variableNodeSocket.VariableType == variableType && variableNodeSocket.Value.GetValue() == value) { items.Add(new ScriptVariableNodeSocketForDeletion(variableNodeSocket)); } } } } // all script variables else if (baseNode is Variable) { Variable variable = baseNode as Variable; // used in script variable if (variable.NamedVariable == null && variable.VariableType == variableType && variable.Value.GetValue() == value) { items.Add(new ScriptVariableForDeletion(variable)); } } } } } }