public override void Draw(GameTime gameTime) { int width = Game.GraphicsDevice.PresentationParameters.BackBufferWidth; int height = Game.GraphicsDevice.PresentationParameters.BackBufferHeight; if (show) { spriteBatch.Begin(); spriteBatch.Draw(background, new Rectangle(0, height / 2, width, height), Color.White); String text; if (false && drawCursor && cursorOffset >= inputBuffer.Length) { text = inputBuffer.ToString() + "_"; } else if (false && drawCursor && cursorOffset > 0 && inputBuffer[cursorOffset] != '\n') { char temp = inputBuffer[cursorOffset]; inputBuffer[cursorOffset] = '_'; text = inputBuffer.ToString(); inputBuffer[cursorOffset] = temp; } else { text = inputBuffer.ToString(); } string cursorText = new String((from i in Enumerable.Range(0, text.Length + 1) select i == cursorOffset ? '_' : (i >= 0 && i < inputBuffer.Length && inputBuffer[i] == '\n' ? '\n' : ' ')).ToArray()); if (drawCursor) { spriteBatch.DrawString(font, cursorText, new Vector2(CURSOR_START_X, CURSOR_START_Y), Color.Blue); } spriteBatch.DrawString(font, text, new Vector2(CURSOR_START_X, CURSOR_START_Y), Color.Black); if (lastResult.HasValue) { EvalResult result = lastResult.Value; spriteBatch.DrawString(font, result.Message, new Vector2(19, 19), Color.Black); spriteBatch.DrawString(font, result.Message, new Vector2(20, 20), Color.White); } spriteBatch.DrawString(font, "You have to define two functions 'visible' and 'material',\n" + "both accepting a Point3D as parameter. 'visible' must return for a point if it\n" + "is visible, material returns the material index (see spritesheet).\n" + "Press F5 to evaluate the script and let the program create the cube geometry\n" + "which is defined by those two functions. Press CTRL + C to close the console.", new Vector2(20, 40), Color.White); spriteBatch.End(); } base.Draw(gameTime); }
public EvalResult Evaluate(String input) { String prelude = "import clr\n" + "clr.AddReference('Mathcraft')\n" + "from Mathcraft import *\n" + "clr.AddReference('System.Core')\n" + "from System import Func\n"; input = prelude + input; String suffix = "\n" + "wrapper.visible_func = Func[Point3D,bool](visible)\n" + "wrapper.material_func = Func[Point3D,int](material)\n"; if (input.Contains("def visible") && input.Contains("def material")) { input += suffix; } EvalResult result = new EvalResult(); Point3D size = new Point3D(20, 20, 20); Point3D pos = new Point3D(-50, -50, -200); Func <Point3D, bool> visFunc = null; Func <Point3D, int> matFunc = null; var scope = engine.CreateScope(); scope.SetVariable("wrapper", wrapper); scope.SetVariable("pos", pos); scope.SetVariable("size", size); scope.SetVariable("visibility", visFunc); scope.SetVariable("material", matFunc); ScriptSource source = engine.CreateScriptSourceFromString(input, SourceCodeKind.Statements); CompiledCode compiled = null; wrapper.material_func = null; wrapper.visible_func = null; try { compiled = source.Compile(); } catch (Exception ex) { result.Success = false; result.Message = ex.Message; return(result); } // Executes in the scope of Python compiled.Execute(scope); bool gotPos = scope.TryGetVariable <Point3D>("pos", out pos); bool gotSize = scope.TryGetVariable <Point3D>("size", out size); if (wrapper.visible_func != null && wrapper.material_func != null) { var param = new GeometryParameters(pos, size, wrapper.visible_func, wrapper.material_func); result.GeometryParameters = param; var gm = game.Services.GetService(typeof(GeometryManager)) as GeometryManager; gm.Clear(); try { gm.AddGeometry(param); } catch (Exception ex) { result.Message = ex.Message; result.Success = false; } } result.Message = "OK"; result.Success = true; return(result); }