Exemplo n.º 1
0
        private void saveFile()
        {
            Debug.Assert(m_filePath != string.Empty);

            ParticleDefiniton pd = new ParticleDefiniton();

            pd.Editor = new EditorSettings();
            pd.Editor.BackgroundColour = colourToInt(m_sfmlControl.BackgroundColour);
            pd.Editor.EmitterPosition  = new Point((int)m_defaultPos.X, (int)m_defaultPos.Y);
            pd.Editor.EnableMovement   = enableMovementToolStripMenuItem.Checked;

            pd.BlendMode              = m_particleSystem.blendMode.ToString();
            pd.Colour                 = colourToInt(m_particleSystem.colour);
            pd.Delay                  = (float)numericUpDownStartDelay.Value;
            pd.Duration               = (float)numericUpDownDuration.Value;
            pd.EmitRate               = (float)numericUpDownEmitRate.Value;
            pd.InitialVelocity        = new Point((int)m_particleSystem.initialVelocity.X, (int)m_particleSystem.initialVelocity.Y);
            pd.Lifetime               = m_particleSystem.particleLifetime;
            pd.ParticleSize           = new Size((int)numericUpDownSizeX.Value, (int)numericUpDownSizeY.Value);
            pd.RandomInitialPositions = new List <Point>();
            if (m_particleSystem.randomInitialPositions != null)
            {
                foreach (var v in m_particleSystem.randomInitialPositions)
                {
                    pd.RandomInitialPositions.Add(new Point((int)v.X, (int)v.Y));
                }
            }

            pd.RandomInitialVelocities = new List <Point>();
            if (m_particleSystem.randomInitialVelocities != null)
            {
                foreach (var v in m_particleSystem.randomInitialVelocities)
                {
                    pd.RandomInitialVelocities.Add(new Point((int)v.X, (int)v.Y));
                }
            }
            pd.Affectors = new List <AffectorDefinition>();
            foreach (var a in m_particleSystem.Affectors)
            {
                AffectorDefinition ad = new AffectorDefinition();
                ad.Type = a.type().ToString();
                ad.Data = new List <float>();
                switch (a.type())
                {
                case AffectorType.Colour:
                    ColourAffector ca = (ColourAffector)a;
                    ad.Data.Add(colourToInt(ca.StartColour));
                    ad.Data.Add(colourToInt(ca.EndColour));
                    ad.Data.Add(ca.Duration);
                    break;

                case AffectorType.Force:
                    var force = ((ForceAffector)a).Force;
                    ad.Data.Add(force.X);
                    ad.Data.Add(force.Y);
                    break;

                case AffectorType.Rotation:
                    ad.Data.Add(((RotationAffector)a).Rotation);
                    break;

                case AffectorType.Scale:
                    var scale = ((ScaleAffector)a).Scale;
                    ad.Data.Add(scale.X);
                    ad.Data.Add(scale.Y);
                    break;

                case AffectorType.Velocity:
                    var vscale = ((VelocityAffector)a).Scale;
                    ad.Data.Add(vscale.X);
                    ad.Data.Add(vscale.Y);
                    break;
                }
                pd.Affectors.Add(ad);
            }

            pd.ReleaseCount = (byte)numericUpDownReleaseCount.Value;
            pd.Texture      = textBoxTexturePath.Text;

            try
            {
                JsonSerializer srlz = new JsonSerializer();
                srlz.NullValueHandling = NullValueHandling.Ignore;
                srlz.Formatting        = Formatting.Indented;

                using (StreamWriter sw = new StreamWriter(m_filePath))
                    using (JsonWriter jw = new JsonTextWriter(sw))
                    {
                        srlz.Serialize(jw, pd);
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Output failed");
            }
        }
Exemplo n.º 2
0
        private void loadFile()
        {
            Debug.Assert(m_filePath != string.Empty);

            try
            {
                ParticleDefiniton pd;
                JsonSerializer    srlz = new JsonSerializer();
                srlz.NullValueHandling = NullValueHandling.Ignore;

                using (StreamReader sr = new StreamReader(m_filePath))
                    using (JsonReader jr = new JsonTextReader(sr))
                    {
                        pd = srlz.Deserialize <ParticleDefiniton>(jr);
                    }

                m_sfmlControl.BackgroundColour          = intToColour(pd.Editor.BackgroundColour);
                enableMovementToolStripMenuItem.Checked = pd.Editor.EnableMovement;
                m_defaultPos = new Vector2f(pd.Editor.EmitterPosition.X, pd.Editor.EmitterPosition.Y);
                m_particleSystem.position = m_defaultPos;

                switch (pd.BlendMode)
                {
                case "Add":
                    m_particleSystem.blendMode = BlendMode.Add;
                    break;

                case "Alpha":
                    m_particleSystem.blendMode = BlendMode.Alpha;
                    break;

                case "Multiply":
                    m_particleSystem.blendMode = BlendMode.Multiply;
                    break;

                case "None":
                    m_particleSystem.blendMode = BlendMode.None;
                    break;
                }
                comboBoxBlendMode.SelectedItem = m_particleSystem.blendMode;

                m_particleSystem.colour = intToColour(pd.Colour);
                panelColour.BackColor   = System.Drawing.Color.FromArgb(m_particleSystem.colour.A, m_particleSystem.colour.R, m_particleSystem.colour.G, m_particleSystem.colour.B);

                numericUpDownStartDelay.Value = (decimal)pd.Delay;
                numericUpDownDuration.Value   = (decimal)pd.Duration;
                numericUpDownEmitRate.Value   = (decimal)pd.EmitRate;
                numericUpDownInitVelX.Value   = pd.InitialVelocity.X;
                numericUpDownInitVelY.Value   = pd.InitialVelocity.Y;
                numericUpDownLifetime.Value   = (decimal)pd.Lifetime;
                numericUpDownSizeX.Value      = pd.ParticleSize.Width;
                numericUpDownSizeY.Value      = pd.ParticleSize.Height;

                m_particleSystem.randomInitialPositions = null;
                listBoxSpawnPoints.Items.Clear();
                if (pd.RandomInitialPositions.Count > 0)
                {
                    List <Vector2f> randPositions = new List <Vector2f>();
                    foreach (var p in pd.RandomInitialPositions)
                    {
                        randPositions.Add(new Vector2f(p.X, p.Y));
                    }
                    m_particleSystem.randomInitialPositions = randPositions;
                    updateListbox(listBoxSpawnPoints.Items, randPositions);
                }

                m_particleSystem.randomInitialVelocities = null;
                listBoxSpawnVelocities.Items.Clear();
                if (pd.RandomInitialVelocities.Count > 0)
                {
                    List <Vector2f> randVelocities = new List <Vector2f>();
                    foreach (var v in pd.RandomInitialVelocities)
                    {
                        randVelocities.Add(new Vector2f(v.X, v.Y));
                    }
                    m_particleSystem.randomInitialVelocities = randVelocities;
                    updateListbox(listBoxSpawnVelocities.Items, randVelocities);
                }

                listBoxAffectors.Items.Clear();
                m_particleSystem.Affectors.Clear();
                if (pd.Affectors.Count > 0)
                {
                    foreach (var ad in pd.Affectors)
                    {
                        if (ad.Type == AffectorType.Colour.ToString())
                        {
                            SFML.Graphics.Color start = intToColour((int)ad.Data[0]);
                            SFML.Graphics.Color end   = intToColour((int)ad.Data[1]);
                            ColourAffector      ca    = new ColourAffector(start, end, ad.Data[2]);
                            m_particleSystem.Affectors.Add(ca);
                            m_particleSystem.colour = start;
                        }
                        else if (ad.Type == AffectorType.Force.ToString())
                        {
                            ForceAffector fa = new ForceAffector(new Vector2f(ad.Data[0], ad.Data[1]));
                            m_particleSystem.Affectors.Add(fa);
                        }
                        else if (ad.Type == AffectorType.Rotation.ToString())
                        {
                            RotationAffector ra = new RotationAffector(ad.Data[0]);
                            m_particleSystem.Affectors.Add(ra);
                        }
                        else if (ad.Type == AffectorType.Scale.ToString())
                        {
                            ScaleAffector sa = new ScaleAffector(new Vector2f(ad.Data[0], ad.Data[1]));
                            m_particleSystem.Affectors.Add(sa);
                        }
                        else if (ad.Type == AffectorType.Velocity.ToString())
                        {
                            VelocityAffector va = new VelocityAffector(new Vector2f(ad.Data[0], ad.Data[1]));
                            m_particleSystem.addAffector(va);
                        }
                        listBoxAffectors.Items.Add(ad.Type);
                    }
                }

                numericUpDownReleaseCount.Value = pd.ReleaseCount;
                if (!string.IsNullOrEmpty(pd.Texture))
                {
                    string path = pd.Texture;
                    if (!File.Exists(path))
                    {
                        //try reconstructing from known asset paths
                        path = path.Replace('/', '\\');
                        bool loaded = false;
                        foreach (string str in m_AssetPaths)
                        {
                            string temp = str + "\\" + path;
                            if (File.Exists(temp))
                            {
                                m_texture = new Texture(temp);
                                m_particleSystem.texture        = m_texture;
                                textBoxTexturePath.Text         = pd.Texture;
                                panelTexPreview.BackgroundImage = new Bitmap(temp);
                                loaded = true;
                                break;
                            }
                        }
                        if (!loaded)
                        {
                            //last ditch, try working dir
                            string temp = Path.GetFileName(pd.Texture);
                            if (File.Exists(temp))
                            {
                                m_texture = new Texture(temp);
                                m_particleSystem.texture        = m_texture;
                                textBoxTexturePath.Text         = pd.Texture;
                                panelTexPreview.BackgroundImage = new Bitmap(temp);
                            }
                        }
                    }
                    else
                    {
                        m_texture = new Texture(pd.Texture);
                        m_particleSystem.texture        = m_texture;
                        textBoxTexturePath.Text         = pd.Texture;
                        panelTexPreview.BackgroundImage = new Bitmap(pd.Texture);
                    }
                    fitPreviewImage();
                }
                else
                {
                    m_texture = null;
                    m_particleSystem.texture        = null;
                    textBoxTexturePath.Text         = string.Empty;
                    panelTexPreview.BackgroundImage = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Failed to open particle file");
                MessageBox.Show("make sure asset paths are valid\n(Options->Add Asset Folder)");
            }
        }
Exemplo n.º 3
0
        private void loadFile()
        {
            Debug.Assert(m_filePath != string.Empty);

            try
            {
                ParticleDefiniton pd;
                JsonSerializer srlz = new JsonSerializer();
                srlz.NullValueHandling = NullValueHandling.Ignore;

                using (StreamReader sr = new StreamReader(m_filePath))
                using (JsonReader jr = new JsonTextReader(sr))
                {
                    pd = srlz.Deserialize<ParticleDefiniton>(jr);
                }

                m_sfmlControl.BackgroundColour = intToColour(pd.Editor.BackgroundColour);
                enableMovementToolStripMenuItem.Checked = pd.Editor.EnableMovement;
                m_defaultPos = new Vector2f(pd.Editor.EmitterPosition.X, pd.Editor.EmitterPosition.Y);
                m_particleSystem.position = m_defaultPos;

                switch(pd.BlendMode)
                {
                    case "Add":
                        m_particleSystem.blendMode = BlendMode.Add;
                        break;
                    case "Alpha":
                        m_particleSystem.blendMode = BlendMode.Alpha;
                        break;
                    case "Multiply":
                        m_particleSystem.blendMode = BlendMode.Multiply;
                        break;
                    case "None":
                        m_particleSystem.blendMode = BlendMode.None;
                        break;
                }
                comboBoxBlendMode.SelectedItem = m_particleSystem.blendMode;

                m_particleSystem.colour = intToColour(pd.Colour);
                panelColour.BackColor = System.Drawing.Color.FromArgb(m_particleSystem.colour.A, m_particleSystem.colour.R, m_particleSystem.colour.G, m_particleSystem.colour.B);

                numericUpDownStartDelay.Value = (decimal)pd.Delay;
                numericUpDownDuration.Value = (decimal)pd.Duration;
                numericUpDownEmitRate.Value = (decimal)pd.EmitRate;
                numericUpDownInitVelX.Value = pd.InitialVelocity.X;
                numericUpDownInitVelY.Value = pd.InitialVelocity.Y;
                numericUpDownLifetime.Value = (decimal)pd.Lifetime;
                numericUpDownSizeX.Value = pd.ParticleSize.Width;
                numericUpDownSizeY.Value = pd.ParticleSize.Height;

                m_particleSystem.randomInitialPositions = null;
                listBoxSpawnPoints.Items.Clear();
                if (pd.RandomInitialPositions.Count > 0)
                {
                    List<Vector2f> randPositions = new List<Vector2f>();
                    foreach(var p in pd.RandomInitialPositions)
                    {
                        randPositions.Add(new Vector2f(p.X, p.Y));
                    }
                    m_particleSystem.randomInitialPositions = randPositions;
                    updateListbox(listBoxSpawnPoints.Items, randPositions);
                }

                m_particleSystem.randomInitialVelocities = null;
                listBoxSpawnVelocities.Items.Clear();
                if(pd.RandomInitialVelocities.Count > 0)
                {
                    List<Vector2f> randVelocities = new List<Vector2f>();
                    foreach(var v in pd.RandomInitialVelocities)
                    {
                        randVelocities.Add(new Vector2f(v.X, v.Y));
                    }
                    m_particleSystem.randomInitialVelocities = randVelocities;
                    updateListbox(listBoxSpawnVelocities.Items, randVelocities);
                }

                listBoxAffectors.Items.Clear();
                m_particleSystem.Affectors.Clear();
                if(pd.Affectors.Count > 0)
                {
                    foreach(var ad in pd.Affectors)
                    {
                        if (ad.Type == AffectorType.Colour.ToString())
                        {
                            SFML.Graphics.Color start = intToColour((int)ad.Data[0]);
                            SFML.Graphics.Color end = intToColour((int)ad.Data[1]);
                            ColourAffector ca = new ColourAffector(start, end, ad.Data[2]);
                            m_particleSystem.Affectors.Add(ca);
                            m_particleSystem.colour = start;
                        }
                        else if (ad.Type == AffectorType.Force.ToString())
                        {
                            ForceAffector fa = new ForceAffector(new Vector2f(ad.Data[0], ad.Data[1]));
                            m_particleSystem.Affectors.Add(fa);
                        }
                        else if (ad.Type == AffectorType.Rotation.ToString())
                        {
                            RotationAffector ra = new RotationAffector(ad.Data[0]);
                            m_particleSystem.Affectors.Add(ra);
                        }
                        else if (ad.Type == AffectorType.Scale.ToString())
                        {
                            ScaleAffector sa = new ScaleAffector(new Vector2f(ad.Data[0], ad.Data[1]));
                            m_particleSystem.Affectors.Add(sa);
                        }
                        else if(ad.Type == AffectorType.Velocity.ToString())
                        {
                            VelocityAffector va = new VelocityAffector(new Vector2f(ad.Data[0], ad.Data[1]));
                            m_particleSystem.addAffector(va);
                        }
                        listBoxAffectors.Items.Add(ad.Type);
                    }
                }

                numericUpDownReleaseCount.Value = pd.ReleaseCount;
                if(!string.IsNullOrEmpty(pd.Texture))
                {
                    string path = pd.Texture;
                    if (!File.Exists(path))
                    {
                        //try reconstructing from known asset paths
                        path = path.Replace('/', '\\');
                        bool loaded = false;
                        foreach (string str in m_AssetPaths)
                        {
                            string temp = str + "\\" + path;
                            if (File.Exists(temp))
                            {
                                m_texture = new Texture(temp);
                                m_particleSystem.texture = m_texture;
                                textBoxTexturePath.Text = pd.Texture;
                                panelTexPreview.BackgroundImage = new Bitmap(temp);
                                loaded = true;
                                break;
                            }
                        }
                        if (!loaded)
                        {
                            //last ditch, try working dir
                            string temp = Path.GetFileName(pd.Texture);
                            if (File.Exists(temp))
                            {
                                m_texture = new Texture(temp);
                                m_particleSystem.texture = m_texture;
                                textBoxTexturePath.Text = pd.Texture;
                                panelTexPreview.BackgroundImage = new Bitmap(temp);
                            }
                        }
                    }
                    else
                    {
                        m_texture = new Texture(pd.Texture);
                        m_particleSystem.texture = m_texture;
                        textBoxTexturePath.Text = pd.Texture;
                        panelTexPreview.BackgroundImage = new Bitmap(pd.Texture);
                    }
                    fitPreviewImage();
                }
                else
                {
                    m_texture = null;
                    m_particleSystem.texture = null;
                    textBoxTexturePath.Text = string.Empty;
                    panelTexPreview.BackgroundImage = null;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Failed to open particle file");
                MessageBox.Show("make sure asset paths are valid\n(Options->Add Asset Folder)");
            }
        }