Beispiel #1
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)");
            }
        }
Beispiel #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)");
            }
        }