Exemple #1
0
 //Setting
 public static bool SetBrush(string name, HeightBrush brush, CSScript script = null)
 {
     if (name != null && name.Length > 0)
     {
         if (script == null)
         {
             script        = new CSScript();
             script.Source = "double Sample(int x, int y, double intensity, int left, int right, int top, int bottom)\r\n{\r\n\t//Your code goes here\r\n}\r\ndouble Blend(double baseValue, double newValue)\r\n{\r\n\t//Your code goes here\r\n}";
         }
         BrushScript bs;
         if (!brushes.TryGetValue(name, out bs))
         {
             bs = new BrushScript()
             {
                 brush = brush, script = script
             }
         }
         ;
         else
         {
             bs.brush = brush;
         }
         brushes[name] = bs;
         if (brushSet != null)
         {
             brushSet(name, bs.brush);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemple #2
0
 private void FountainDocument_BrushRemoved(string name, HeightBrush brush)
 {
     if (name == brushName)
     {
         Close();
     }
 }
Exemple #3
0
        private static BrushScript LoadBrush(Stream stream)
        {
            byte[] buffer = new byte[4];

            stream.Read(buffer, 0, sizeof(int));
            int width = BitConverter.ToInt32(buffer, 0);

            stream.Read(buffer, 0, sizeof(int));
            int height = BitConverter.ToInt32(buffer, 0);

            stream.Read(buffer, 0, sizeof(float));
            float power = BitConverter.ToSingle(buffer, 0);

            stream.Read(buffer, 0, sizeof(int));
            int precision = BitConverter.ToInt32(buffer, 0);

            CSScript script = new CSScript();

            script.Source = LoadText(stream);
            HeightBrush.SampleFunction sample;
            HeightBrush.BlendFunction  blend;
            string errors;

            HeightBrush.CompileFunctions(script, out sample, out blend, out errors);

            BrushScript bs = new BrushScript()
            {
                brush = new HeightBrush(width, height, power, precision, sample, blend), script = script
            };

            return(bs);
        }
Exemple #4
0
        private void compileButton_Click(object sender, EventArgs e)
        {
            script.Source = scriptBox.Text;
            HeightBrush.SampleFunction sample;
            HeightBrush.BlendFunction  blend;
            string errors;

            switch (HeightBrush.CompileFunctions(script, out sample, out blend, out errors))
            {
            case HeightBrush.CompileResult.WrongSampleSignature:
                MessageBox.Show("The method signature for the \"Sample\" function should be:\n\nfloat Sample(int x, int y, float intensity, int left, int right, int top int bottom)", "Script Error");
                break;

            case HeightBrush.CompileResult.MissingSampleFunction:
                MessageBox.Show("The \"Sample\" function is missing from your script.");
                break;

            case HeightBrush.CompileResult.WrongBlendSignature:
                MessageBox.Show("The method signature for the \"Blend\" function should be:\n\nfloat Blend(float baseValue, float newValue)", "Script Error");
                break;

            case HeightBrush.CompileResult.MissingBlendFunction:
                MessageBox.Show("The \"Blend\" function is missing from your script.");
                break;

            case HeightBrush.CompileResult.SyntaxError:
                MessageBox.Show("There was a compilation error in your script:\r\n" + errors, "Syntax Error");
                break;

            case HeightBrush.CompileResult.Success:
                brush.Sample = sample;
                brush.Blend  = blend;
                break;
            }
        }
Exemple #5
0
        public BrushDialog(string brushName, Form owner)
        {
            Owner = owner;
            if (brushName != null && brushName.Length > 0)
            {
                CenterToParent();
                InitializeComponent();

                if (Document.ContainsBrush(this.brushName = brushName))
                {
                    brush  = Document.GetBrush(brushName);
                    script = Document.GetBrushScript(brushName);
                }
                else
                {
                    Document.SetBrush(brushName, brush = new HeightBrush(64, 64, 1.0f, 8));
                    script = Document.GetBrushScript(brushName);
                }

                Text               = "Brush - " + brushName;
                widthBox.Value     = brush.Width;
                heightBox.Value    = brush.Height;
                powerBox.Value     = (decimal)brush.Power;
                precisionBox.Value = brush.Precision;
                scriptBox.Text     = script.Source;

                Document.BrushRemoved += FountainDocument_BrushRemoved;
                Document.Loaded       += FountainDocument_Loaded;
                Document.Cleared      += FountainDocument_Cleared;
            }
            else
            {
                throw new Exception("The supplied name was empty or null.");
            }
        }
Exemple #6
0
        public BrushDialog(string brushName)
        {
            CenterToParent();
            InitializeComponent();
            if (Document.ContainsBrush(this.brushName = brushName))
            {
                brush  = Document.GetBrush(brushName);
                script = Document.GetBrushScript(brushName);

                Text               = "Brush - " + brushName;
                widthBox.Value     = brush.Width;
                heightBox.Value    = brush.Height;
                powerBox.Value     = (decimal)brush.Power;
                precisionBox.Value = brush.Precision;
                scriptBox.Text     = script.Source;

                Document.BrushRemoved += FountainDocument_BrushRemoved;
                Document.Loaded       += FountainDocument_Loaded;
                Document.Cleared      += FountainDocument_Cleared;
            }
            else
            {
                throw new Exception("The given brush name does not correspond to any brush in the current document.");
            }
        }
Exemple #7
0
 //Setting
 public static bool SetBrush(string name, HeightBrush brush, CSScript script = null)
 {
     if (name != null)
     {
         if (script == null)
         {
             script = new CSScript();
         }
         BrushScript bs;
         if (!brushes.TryGetValue(name, out bs))
         {
             bs = new BrushScript()
             {
                 brush = brush, script = script
             }
         }
         ;
         else
         {
             bs.brush = brush;
         }
         brushes[name] = bs;
         if (brushSet != null)
         {
             brushSet(name, bs.brush);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemple #8
0
        void InitBrushes()
        {
            heightBrush = new HeightBrush();
            grassBrush  = new GrassBrush();
            noneBrush   = new NoneBrush();

            currentBrushStrategy = heightBrush;
        }
Exemple #9
0
    private void Awake()
    {
        heightBrush   = new HeightBrush(HexEditMesh);
        materialBrush = new MaterialBrush(HexEditMesh);
        sceneObjBrush = new SceneObjBrush(HexEditMesh);
        edgeBrush     = new EdgeBrush(HexEditMesh);
        waterBrush    = new WaterBrush(HexEditMesh);
        Init();

        EditorSceneManager.sceneOpening -= CloseScene;
        EditorSceneManager.sceneOpening += CloseScene;
    }
Exemple #10
0
 private void Document_BrushSet(string name, HeightBrush brush)
 {
     leftBrushNameBox.Items.Clear();
     rightBrushNameBox.Items.Clear();
     foreach (string s in Document.BrushNames)
     {
         leftBrushNameBox.Items.Add(s);
         rightBrushNameBox.Items.Add(s);
     }
     leftBrushNameBox.SelectedItem  = Document.LeftBrushName;
     rightBrushNameBox.SelectedItem = Document.RightBrushName;
 }
Exemple #11
0
 private void RefreshInfo()
 {
     if (Document.ContainsBrush(BrushName))
     {
         //Fields
         brush  = Document.GetBrush(BrushName);
         script = Document.GetBrushScript(BrushName);
         //Control Values
         Text               = "Brush - " + BrushName;
         widthBox.Value     = brush.Width;
         heightBox.Value    = brush.Height;
         powerBox.Value     = (decimal)brush.Power;
         precisionBox.Value = brush.Precision;
         scriptBox.Text     = script.Source;
         //Enable Controls
         deleteButton.Enabled  = true;
         widthBox.Enabled      = true;
         heightBox.Enabled     = true;
         powerBox.Enabled      = true;
         precisionBox.Enabled  = true;
         scriptBox.Enabled     = true;
         compileButton.Enabled = true;
     }
     else
     {
         //Fields
         brush  = null;
         script = null;
         //Control Values
         Text               = "Brushes";
         widthBox.Value     = 0;
         heightBox.Value    = 0;
         powerBox.Value     = 1;
         precisionBox.Value = 1;
         scriptBox.Text     = null;
         //Disable Controls
         deleteButton.Enabled  = false;
         widthBox.Enabled      = false;
         heightBox.Enabled     = false;
         powerBox.Enabled      = false;
         precisionBox.Enabled  = false;
         scriptBox.Enabled     = false;
         compileButton.Enabled = false;
     }
 }
Exemple #12
0
        private void Application_Idle(object sender, EventArgs e)
        {
            Vector2 pointOnRenderArea = Numerics.ToVector(renderArea.PointToClient(Control.MousePosition));
            Vector2 pointOnRender     = renderArea.ClientToImage(pointOnRenderArea);

            if (renderArea.Focused && Document.SelectedRender != null)
            {
                #region Panning
                if (MouseButtons == MouseButtons.Middle)
                {
                    renderArea.ImageOffset += (pointOnRenderArea - lastPointOnRenderArea) / renderArea.ImageScale;
                }
                #endregion
                #region Brush
                HeightBrush activeBrush = null;
                if (MouseButtons == MouseButtons.Left)
                {
                    activeBrush = Document.LeftBrush;
                }
                else if (MouseButtons == MouseButtons.Right)
                {
                    activeBrush = Document.RightBrush;
                }

                if (activeBrush != null)
                {
                    HeightRender   render   = Document.SelectedRender;
                    PhotonGradient gradient = Document.SelectedGradient;
                    IEnumerable <HeightRender.Effect> effects;
                    if (paintEffectsBox.Checked)
                    {
                        effects = Document.SelectedEffects;
                    }
                    else
                    {
                        effects = null;
                    }
                    #region Process Steps
                    Vector2 brushDelta   = lastPointOnRender - pointOnRender;
                    float   strokeLength = (float)brushDelta.Length;
                    float   steps        = strokeLength / activeBrush.Precision + 1;
                    Vector2 brushStep    = brushDelta * (1.0f / steps);
                    for (int i = 0; i < steps; i++)
                    {
                        Vector2 brushPosition = pointOnRender + brushStep * i;
                        #region Process Brush Paint
                        try
                        {
                            FieldSelection brushArea;
                            activeBrush.Paint(render.HeightField, (int)brushPosition.X, (int)brushPosition.Y, strokeLength, out brushArea);
                            foreach (FieldSelection fs in brushArea.SubSelectionsOf(render.HeightField))
                            {
                                if (!fs.IsEmpty)
                                {
                                    render.UpdateArea(fs.Left, fs.Top, fs.Width, fs.Height, gradient, effects);
                                    Point start = Numerics.ToPoint(renderArea.ImageToClient(new Vector2(fs.Left, fs.Top)));
                                    Point end   = Numerics.ToPoint(renderArea.ImageToClient(new Vector2(fs.Right, fs.Bottom)));
                                    renderArea.Invalidate(new Rectangle(Numerics.Max(start.X, 0), Numerics.Max(start.Y, 0), Numerics.Max(end.X, 0), Numerics.Max(end.Y, 0)));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "There was a runtime error with your brush script.");
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
            }

            lastPointOnRenderArea = pointOnRenderArea;
            lastPointOnRender     = pointOnRender;
        }
Exemple #13
0
 private void Document_BrushSet(string name, HeightBrush brush)
 {
     BrushName = name;
 }
Exemple #14
0
 private void Document_BrushRemoved(string name, HeightBrush brush)
 {
     RefreshNameList();
     RefreshInfo();
 }
Exemple #15
0
        private void Application_Idle(object sender, EventArgs e)
        {
            Vector2 pointOnRenderArea = Numerics.ToVector(renderArea.PointToClient(Control.MousePosition));
            Vector2 pointOnRender     = renderArea.ClientToImage(pointOnRenderArea);

            if (renderArea.Focused && Document.SelectedRender != null)
            {
                #region Undo
                if (undoing)
                {
                    Undo();
                }
                #endregion
                #region Redo
                if (redoing)
                {
                    Redo();
                }
                #endregion
                #region Panning
                if (MouseButtons == MouseButtons.Middle)
                {
                    renderArea.ImageOffset += (pointOnRenderArea - lastPointOnRenderArea) / renderArea.ImageScale;
                }
                #endregion
                #region Brush
                //Select the appropriate brush based on the active mouse buttons.
                HeightBrush activeBrush = null;
                if (MouseButtons == MouseButtons.Left)
                {
                    activeBrush = Document.LeftBrush;
                }
                else if (MouseButtons == MouseButtons.Right)
                {
                    activeBrush = Document.RightBrush;
                }
                //If a mouse button was in use; paint.
                if (activeBrush != null)
                {
                    HeightRender   render   = Document.SelectedRender;
                    PhotonGradient gradient = Document.SelectedGradient;
                    IEnumerable <HeightRender.Effect> effects;
                    if (paintEffectsBox.Checked)
                    {
                        effects = Document.SelectedEffects;
                    }
                    else
                    {
                        effects = null;                     //If we aren't painting with effects then we don't care about supplying them to the update.
                    }
                    #region Process Steps
                    //The brush stroke is broken up into steps that are each a given length (in pixels). The length is denoted by the brush precision.
                    Vector2 brushDelta   = lastPointOnRender - pointOnRender;
                    float   strokeLength = (float)brushDelta.Length;
                    float   steps        = strokeLength / activeBrush.Precision + 1;
                    Vector2 brushStep    = brushDelta * (1.0f / steps);
                    for (int i = (int)steps - 1; i >= 0; i--)                    //Work backwards so the undo and redo functionality makes sense.
                    {
                        //Calculate the current brush position, based on the starting point, the step vector and the current step index.
                        Vector2 brushPosition = pointOnRender + brushStep * i;
                        #region Process Brush Paint
                        try
                        {
                            //Perform the actual brush operation and capture the selected area it affects.
                            FieldSelection brushArea;
                            float[]        previousData;
                            activeBrush.Paint(render.HeightField, (int)brushPosition.X, (int)brushPosition.Y, strokeLength, out brushArea, out previousData);
                            //Add this paint event to the undo queue.
                            undoQueue.Enqueue(new BrushAction(brushArea, previousData));
                            redoQueue.Clear();
                            //Break up the brush area into multiple parts if they intersect the edges of the image.
                            foreach (FieldSelection fs in brushArea.SubSelectionsOf(render.HeightField))
                            {
                                //Omit any parts that have a width or height of zero.
                                if (!fs.IsEmpty)
                                {
                                    //Update the corresponding part of the render.
                                    render.UpdateArea(fs.Left, fs.Top, fs.Width, fs.Height, gradient, effects);
                                    //Invalidate the corresponding part of the image panel so that it redraws itself in realtime.
                                    Point start = Numerics.ToPoint(renderArea.ImageToClient(new Vector2(fs.Left, fs.Top)));
                                    Point end   = Numerics.ToPoint(renderArea.ImageToClient(new Vector2(fs.Right, fs.Bottom)));
                                    renderArea.Invalidate(new Rectangle(Numerics.Max(start.X, 0), Numerics.Max(start.Y, 0), Numerics.Max(end.X, 0), Numerics.Max(end.Y, 0)));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            //This usually happens when activeBrush.Paint is called, because it executes the user's brush script. Shows the script error message as a message box.
                            MessageBox.Show(ex.Message, "There was a runtime error with your brush script.");
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
            }
            else
            {
                undoing = redoing = false;
            }

            lastPointOnRenderArea = pointOnRenderArea;
            lastPointOnRender     = pointOnRender;
        }
Exemple #16
0
        public async Task SaveAsync(string path)
        {
            var editor = await TerrainEditor.OpenEmptyAsync(new TerrainSettings
            {
                Size          = Size,
                DefaultHeight = DefaultHeight
            });

            editor.Load();

            var heightBrush = new HeightBrush(editor);

            if (Heights != default)
            {
                foreach (var height in Heights)
                {
                    heightBrush.Size = height.Size;

                    heightBrush.Power = height.Power;

                    heightBrush.Apply(height.Position);
                }
            }

            if (Colors != default)
            {
                var colorBrush = new ColorBrush(editor);

                foreach (var color in Colors)
                {
                    colorBrush.Size = color.Size;

                    colorBrush.Color = color.Color;

                    colorBrush.Apply(color.Position);
                }
            }

            editor.Apply();

            if (File.Exists(LightMap))
            {
                Console.WriteLine($"Applying light map.");

                var data = await File.ReadAllBytesAsync(LightMap);

                foreach (var chunk in editor.Source.Chunks)
                {
                    chunk.Lightmap.Data = data;
                }
            }

            if (File.Exists(BlendMap))
            {
                Console.WriteLine($"Applying blend map.");

                var data = await File.ReadAllBytesAsync(BlendMap);

                foreach (var chunk in editor.Source.Chunks)
                {
                    chunk.Blendmap.Data = data;
                }
            }

            await editor.SaveAsync(Path.Combine(path, FileName));
        }