Exemple #1
0
        /// <summary>
        /// Default behavior - create scene selected in the combo-box.
        /// </summary>
        public IRayScene SceneByComboBox()
        {
            DefaultRayScene sc        = new DefaultRayScene();
            string          sceneName = (string)comboScene.Items[selectedScene];

            object                 initFunction;
            InitSceneDelegate      isd  = null;
            InitSceneParamDelegate ispd = null;

            sceneRepository.TryGetValue(sceneName, out initFunction);
            isd  = initFunction as InitSceneDelegate;
            ispd = initFunction as InitSceneParamDelegate;
            if (isd == null &&
                ispd == null)
            {
                return(Scenes.DefaultScene(sc));
            }

            if (isd != null)
            {
                isd(sc);
            }
            else
            {
                ispd?.Invoke(sc, textParam.Text);
            }

            SetText($"Rendering '{sceneName}'..");
            return(sc);
        }
Exemple #2
0
        /// <summary>
        /// Default behavior - create scene selected in the combo-box.
        /// </summary>
        public IRayScene SceneByComboBox()
        {
            DefaultRayScene sc        = new DefaultRayScene();
            string          sceneName = (string)comboScene.Items[selectedScene];

            object                 initFunction;
            InitSceneDelegate      isd  = null;
            InitSceneParamDelegate ispd = null;

            sceneRepository.TryGetValue(sceneName, out initFunction);
            isd  = initFunction as InitSceneDelegate;
            ispd = initFunction as InitSceneParamDelegate;
            if (isd == null &&
                ispd == null)
            {
                isd = Scenes.staticRepository["Sphere on the plane"] as InitSceneDelegate;
            }

            if (isd != null)
            {
                isd(sc);
            }
            else
            {
                ispd?.Invoke(sc, textParam.Text);
            }

            return(sc);
        }
        /// <summary>
        /// Compute a scene based on general description object 'definition' (one of delegate functions or CSscript file-name).
        /// </summary>
        /// <param name="name">Readable short scene name.</param>
        /// <param name="definition">Scene definition object.</param>
        /// <param name="par">Text parameter (from form's text field..).</param>
        /// <param name="message">Message function</param>
        /// <returns>New initialized instance of a IRayScene object.</returns>
        public static IRayScene SceneFromObject(DefaultRayScene sc, string name, object definition, string par,
                                                InitSceneDelegate defaultScene, StringDelegate message = null, Dictionary <string, object> outPar = null)
        {
            InitSceneDelegate      isd  = definition as InitSceneDelegate;
            InitSceneParamDelegate ispd = definition as InitSceneParamDelegate;
            string scriptFileName       = definition as string;
            string scriptSource         = null;

            if (!string.IsNullOrEmpty(scriptFileName) &&
                File.Exists(scriptFileName))
            {
                try
                {
                    scriptSource = File.ReadAllText(scriptFileName);
                }
                catch (IOException)
                {
                    Console.WriteLine($"Warning: I/O error in scene read: '{scriptFileName}'");
                    scriptSource = null;
                }
                catch (UnauthorizedAccessException)
                {
                    Console.WriteLine($"Warning: access error in scene read: '{scriptFileName}'");
                    scriptSource = null;
                }

                if (!string.IsNullOrEmpty(scriptSource))
                {
                    message?.Invoke($"Compiling and running scene script '{name}' ({++count})..");

                    // interpret the CS-script defining the scene:
                    var assemblyNames = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

                    List <Assembly> assemblies = new List <Assembly>();
                    assemblies.Add(Assembly.GetExecutingAssembly());
                    foreach (var assemblyName in assemblyNames)
                    {
                        assemblies.Add(Assembly.Load(assemblyName));
                    }

                    List <string> imports = new List <string>();
                    imports.Add("System.Collections.Generic");
                    imports.Add("OpenTK");
                    imports.Add("Rendering");
                    imports.Add("Utilities");

                    bool    ok      = true;
                    Globals globals = new Globals {
                        sceneName = name, scene = sc, param = par, outParam = outPar ?? new Dictionary <string, object>()
                    };
                    try
                    {
                        var task = CSharpScript.RunAsync(scriptSource, globals: globals, options: ScriptOptions.Default.WithReferences(assemblies).AddImports(imports));
                        Task.WaitAll(task);
                    }
                    catch (CompilationErrorException e)
                    {
                        MessageBox.Show($"Error compiling scene script: {e.Message}, using default scene", "CSscript Error");
                        ok = false;
                    }

                    if (ok)
                    {
                        message?.Invoke($"Script '{name}' finished ok, rendering..");
                        return(globals.scene);
                    }
                }

                message?.Invoke("Using default scene..");
                defaultScene(sc);
                return(sc);
            }

            if (isd != null)
            {
                isd(sc);
            }
            else
            {
                ispd?.Invoke(sc, par);
            }

            message?.Invoke($"Rendering '{name}' ({++count})..");
            return(sc);
        }