Beispiel #1
0
        private void btnAddLayer_Click(object sender, EventArgs e)
        {
            string    layerName;
            int       layerWidth;
            int       layerHeight;
            TileLayer layer;

            LayerForm Form = new LayerForm(LayerList.Items.Count);

            Form.ShowDialog();


            if (Form.OkPressed)
            {
                layerName   = Form.txtLayerName.Text;
                layerWidth  = int.Parse(Form.txtLayrWidth.Text);
                layerHeight = int.Parse(Form.txtLayHeight.Text);
                layer       = new TileLayer(layerWidth, layerHeight);
                dictLayer.Add(layerName, layer);
                LayerList.Items.Add(layerName);
                Map.Addlayer(layer);
                currentLayer = layer;
                foreach (string s in TextureList.Items)
                {
                    currentLayer.AddTexture(dictTextures[s]);
                }
                LayerList.SetSelected(LayerList.Items.Count - 1, true);
            }
        }
Beispiel #2
0
        private void quickLoadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog2.InitialDirectory = WorkspacePath;
            openFileDialog2.Filter           = "Layer File|*.layer|Map File|*.map|Xml File|*.xml|All Supported Files|*.layer;*.map;*.xml";
            Dictionary <int, string> texturesToLoad;
            string NameOfLayer;

            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                TileLayer layer = TileLayer.ReadInLayer(openFileDialog2.FileName, out texturesToLoad);
                NameOfLayer = layer.LayerName;
                string extensFound = "";

                try
                {
                    foreach (KeyValuePair <int, string> texturespaths in texturesToLoad)
                    {
                        foreach (string ext in Extensions)
                        {
                            if (File.Exists(texPathAddress.Text + "\\" + texturespaths.Value + ext))
                            {
                                extensFound = ext;
                                break;
                            }
                        }

                        FileStream stream  = new FileStream(texPathAddress.Text + "\\" + texturespaths.Value + extensFound, FileMode.Open);
                        Texture2D  texture = Texture2D.FromStream(GraphicsDevices, stream);
                        layer.AddTexture(texture);
                        stream.Dispose();
                        if (!dictTextures.ContainsKey(texturespaths.Value))
                        {
                            TextureList.Items.Add(texturespaths.Value);
                            dictTextures.Add(texturespaths.Value, texture);
                            Image img = Image.FromFile(texPathAddress.Text + "\\" + texturespaths.Value + extensFound);
                            dictImages.Add(texturespaths.Value, img);
                        }
                    }
                    dictLayer[NameOfLayer] = layer;
                    Map.Addlayer(layer);
                    LayerList.Items.Add(NameOfLayer);
                    currentLayer            = layer;
                    LayerList.SelectedIndex = LayerList.Items.Count - 1;
                    activateControls();
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show("Cannot Load Layer File. the texure: " +
                                    ex.FileName +
                                    " Could not be found in the content folder you have chosen. Content Folder Selector button has been enabled, you have can choose another Directory that has the resource you are trying to load.", "RESOURCE FILE NOT FOUND");
                    btnAddFiles.Enabled = true;
                }
            }
        }
Beispiel #3
0
        public override void Excute()
        {
            if (form.TextureList.Items.Count > 0 && form.TextureList.SelectedItem != null)
            {
                LayerStateBeforeRecursion = new TileLayer(currentLayer.TileMapArray);
                foreach (Texture2D text in currentLayer.TexturesList)
                {
                    LayerStateBeforeRecursion.AddTexture(text);
                }

                fillCounter = 5000;
                FillCellIndex((int)form.TileX, (int)form.TileY, currentLayer.HasTexture(form.dictTextures[form.TextureList.SelectedItem as string]));
            }
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter      = "Map File|*.map";
            openFileDialog1.Multiselect = false;

            //tileMap = null;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                layerDict.Clear();
                collDict.Clear();
                textureDict.Clear();
                previewDict.Clear();


                CollLayerListBox.Items.Clear();
                TilelayerListBox.Items.Clear();
                textureListBox.Items.Clear();

                tileMap.tile.Clear();
                tileMap.coll.Clear();

                tileMap.Layers.Clear();
                tileMap.colLayers.Clear();

                Logic();
                Render();

                currentLayer          = null;
                currentCollisionLayer = null;


                string filename = openFileDialog1.FileName;

                string[] textureNames;

                tileMap.OpenTileFile(filename);

                foreach (string tile in tileMap.tile) // Loop through List with foreach
                {
                    TileLayer tileLayer = TileLayer.FromFile(contentPathTextBox.Text + "/" + tile, out textureNames);

                    layerDict.Add(Path.GetFileName(contentPathTextBox.Text + "/" + tile), tileLayer);

                    tileMap.Layers.Add(tileLayer);

                    TilelayerListBox.Items.Add(Path.GetFileName(contentPathTextBox.Text + "/" + tile));

                    foreach (string textureName in textureNames)
                    {
                        if (textureDict.ContainsKey(textureName))
                        {
                            tileLayer.AddTexture(textureDict[textureName]);
                            continue;
                        }

                        string fullPath = contentPathTextBox.Text + "/" + textureName;

                        foreach (string ext in imageExtensions)
                        {
                            if (File.Exists(fullPath + ext))
                            {
                                fullPath += ext;
                                break;
                            }
                        }

                        Texture2D tex;

                        using (Stream stream = File.OpenRead(fullPath))
                        {
                            tex = Texture2D.FromStream(GraphicsDevice, stream);
                        }

                        Image image = Image.FromFile(fullPath);
                        textureDict.Add(textureName, tex);
                        previewDict.Add(textureName, image);

                        textureListBox.Items.Add(textureName);
                        tileLayer.AddTexture(tex);
                    }
                }

                foreach (string coll in tileMap.coll) // Loop through List with foreach
                {
                    CollisionLayer collLayer = CollisionLayer.FromFile(contentPathTextBox.Text + "/" + coll);
                    collDict.Add(Path.GetFileName(contentPathTextBox.Text + "/" + coll), collLayer);

                    tileMap.colLayers.Add(collLayer);
                    CollLayerListBox.Items.Add(Path.GetFileName(contentPathTextBox.Text + "/" + coll));
                }

                AdjustScrollBars();
            }
        }
        private void Logic()
        {
            camera.Position.X = hScrollBar1.Value * Engine.TileWidth;
            camera.Position.Y = vScrollBar1.Value * Engine.TileHeight;

            int mx = Mouse.GetState().X;
            int my = Mouse.GetState().Y;

            if (currentCollisionLayer != null)
            {
                if (mx >= 0 && mx < tileDisplay1.Width &&
                    my >= 0 && my < tileDisplay1.Height)
                {
                    collideCellX = mx / Engine.TileWidth;
                    collideCellY = my / Engine.TileHeight;

                    collideCellX += hScrollBar1.Value;
                    collideCellY += vScrollBar1.Value;

                    collideCellX = (int)MathHelper.Clamp(collideCellX, 0, currentCollisionLayer.Width - 1);
                    collideCellY = (int)MathHelper.Clamp(collideCellY, 0, currentCollisionLayer.Height - 1);
                }
            }

            if (currentLayer != null)
            {
                if (mx >= 0 && mx < tileDisplay1.Width &&
                    my >= 0 && my < tileDisplay1.Height)
                {
                    cellX = mx / Engine.TileWidth;
                    cellY = my / Engine.TileHeight;

                    cellX += hScrollBar1.Value;
                    cellY += vScrollBar1.Value;

                    cellX = (int)MathHelper.Clamp(cellX, 0, currentLayer.Width - 1);
                    cellY = (int)MathHelper.Clamp(cellY, 0, currentLayer.Height - 1);

                    if (MouseDown && coolBool == false)
                    {
                        if (drawRadioButton.Checked && textureListBox != null)
                        {
                            Texture2D texture = textureDict[textureListBox.SelectedItem as string];

                            int index = currentLayer.isUsingTexture(texture);

                            if (index == -1)
                            {
                                currentLayer.AddTexture(texture);
                                index = currentLayer.isUsingTexture(texture);
                            }

                            if (fillCheckBox.Checked)
                            {
                                FillCounter = 500;
                                FillCell(cellX, cellY, index);
                            }
                            else
                            {
                                currentLayer.SetCellIndex(cellX, cellY, index);
                            }
                        }
                        else if (eraseRadioButton.Checked)
                        {
                            if (fillCheckBox.Checked)
                            {
                                FillCounter = 500;
                                FillCell(cellX, cellY, -1);
                            }
                            else
                            {
                                currentLayer.SetCellIndex(cellX, cellY, -1);
                            }
                        }
                    }
                }
                else
                {
                    cellX = cellY = -1;
                }
            }

            if (MouseDown && coolBool == true)
            {
                tileNumber = currentCollisionLayer.GetCellIndex(collideCellX, collideCellY);

                if (drawRadioButton.Checked && textureListBox != null)
                {
                    int colIndex = collisionTiles.SelectedIndex;

                    if (colIndex == 0 && collisionTiles.SelectedItem != null && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    spriteBatch.Begin();

                    if (colIndex == 1 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 2 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 3 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 4 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, form.spawnNumber.ToString());
                    }

                    if (AssociateBox.SelectedIndex == 1)
                    {
                        string TileName = tileNumber + "#" + numForm.actualNumber.Text;
                        int    count    = TileName.Count(f => f == '#');

                        if (count <= 1 && int.Parse(tileNumber) > 3)
                        {
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, TileName);
                        }
                        else
                        {
                            TileName = null;
                        }
                    }

                    else if (AssociateBox.SelectedIndex == 2)
                    {
                        int  tileNumberType;
                        bool isNum = int.TryParse(tileNumber, out tileNumberType);

                        if (!isNum)
                        {
                            string[] numbers = tileNumber.Split('#');
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, numbers[0]);
                        }
                        else
                        {
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, tileNumber);
                        }
                    }
                    spriteBatch.End();
                }
            }
        }
Beispiel #6
0
        private void Logic()
        {
            int colIndex = collisionTiles.SelectedIndex;

            camera.Position.X = hScrollBar1.Value * TileInformation.TileWidth;
            camera.Position.Y = vScrollBar1.Value * TileInformation.TileHeight;

            //int mx = Mouse.GetState().X;
            //int my = Mouse.GetState().Y;

            Vector2 worldPosition = Vector2.Transform(new Vector2(Mouse.GetState().X, Mouse.GetState().Y), Matrix.Invert(camera.TransformMatrix));

            int mx = (int)worldPosition.X;
            int my = (int)worldPosition.Y;


            if (currentCollisionLayer != null)
            {
                if (mx >= 0 && mx < tileDisplay1.Width + (int)camera.Position.X &&
                    my >= 0 && my < tileDisplay1.Height + (int)camera.Position.Y)
                {
                    collideCellX = mx / TileInformation.TileWidth;
                    collideCellY = my / TileInformation.TileHeight;

                    collideCellX += hScrollBar1.Value;
                    collideCellY += vScrollBar1.Value;

                    collideCellX = (int)MathHelper.Clamp(collideCellX, 0, currentCollisionLayer.Width - 1);
                    collideCellY = (int)MathHelper.Clamp(collideCellY, 0, currentCollisionLayer.Height - 1);
                }
            }

            if (currentLayer != null)
            {
                if (mx >= 0 && mx < tileDisplay1.Width + (int)camera.Position.X &&
                    my >= 0 && my < tileDisplay1.Height + (int)camera.Position.Y)
                {
                    cellX = mx / TileInformation.TileWidth;
                    cellY = my / TileInformation.TileHeight;

                    cellX += hScrollBar1.Value;
                    cellY += vScrollBar1.Value;

                    cellX = (int)MathHelper.Clamp(cellX, 0, currentLayer.Width - 1);
                    cellY = (int)MathHelper.Clamp(cellY, 0, currentLayer.Height - 1);

                    if (MouseDown && coolBool == false)
                    {
                        if (drawRadioButton.Checked && textureListBox != null)
                        {
                            Texture2D texture = null;

                            if (textureListBox.SelectedIndex > -1)
                            {
                                texture = textureDict[textureListBox.SelectedItem as string];
                            }
                            else
                            {
                                return;
                            }

                            int index = currentLayer.isUsingTexture(texture);

                            if (index == -1)
                            {
                                currentLayer.AddTexture(texture);
                                index = currentLayer.isUsingTexture(texture);
                            }
                            else
                            {
                                currentLayer.SetCellIndex(cellX, cellY, index);
                            }

                            if (fillCheckBox.Checked)
                            {
                                FillCounter = 500;
                                FillCell(cellX, cellY, index);
                            }
                            else
                            {
                                currentLayer.SetCellIndex(cellX, cellY, index);
                            }
                        }
                        else if (eraseRadioButton.Checked)
                        {
                            if (fillCheckBox.Checked)
                            {
                                FillCounter = 500;
                                FillCell(cellX, cellY, -1);
                            }
                            else
                            {
                                currentLayer.SetCellIndex(cellX, cellY, -1);
                            }
                        }
                    }
                }
                else
                {
                    cellX = cellY = -1;
                }
            }

            if (MouseDown && coolBool == true)
            {
                tileNumber = currentCollisionLayer.GetCellIndex(collideCellX, collideCellY);

                if (drawRadioButton.Checked && textureListBox != null)
                {
                    //int colIndex = collisionTiles.SelectedIndex;

                    if (colIndex == 0 && collisionTiles.SelectedItem != null && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());


                        for (int i = 0; i < CamReferences.Count; i++)
                        {
                            if (CamReferences[i].X == collideCellX && CamReferences[i].Y == collideCellY)
                            {
                                CamReferences.RemoveAt(i);
                            }
                        }
                    }

                    spriteBatch.Begin(SpriteSortMode.Texture,
                                      BlendState.AlphaBlend,
                                      null, null, null, null,
                                      camera.TransformMatrix);

                    if (colIndex == 1 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }
                    if (colIndex == 2 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 3 && AssociateBox.SelectedIndex == 0)
                    {
                        CamReferences.Add(new CameraNodes(collideCellX, collideCellY));
                    }

                    if (colIndex == 4 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 5 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 6 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 7 && AssociateBox.SelectedIndex == 0)
                    {
                        currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, colIndex.ToString());
                    }

                    if (colIndex == 8 && AssociateBox.SelectedIndex == 0)
                    {
                        if (form.spawnNumber != "12")
                        {
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, form.spawnNumber.ToString());
                        }
                    }

                    if (AssociateBox.SelectedIndex == 1)
                    {
                        string TileName = tileNumber + "#" + numForm.actualNumber.Text;
                        int    count    = TileName.Count(f => f == '#');

                        if (count <= 1 && int.Parse(tileNumber) > 3)
                        {
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, TileName);
                        }
                        else
                        {
                            TileName = null;
                        }
                    }

                    else if (AssociateBox.SelectedIndex == 2)
                    {
                        int  tileNumberType;
                        bool isNum = int.TryParse(tileNumber, out tileNumberType);

                        if (!isNum)
                        {
                            string[] numbers = tileNumber.Split('#');
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, numbers[0]);
                        }
                        else
                        {
                            currentCollisionLayer.SetCellIndex(collideCellX, collideCellY, tileNumber);
                        }
                    }
                    spriteBatch.End();
                }
            }
        }
Beispiel #7
0
        private void btnAddTexture_Click(object sender, EventArgs e)
        {
            if (currentLayer != null)
            {
                string Path;
                openFileDialog1.InitialDirectory = WorkspacePath;
                openFileDialog1.Filter           = "Png|*.png|Jpeg|*.jpeg|Jpg|*.jpg|All Image Formats|*.png;*.jpeg;*.jpg";
                openFileDialog1.Multiselect      = true;
                openFileDialog1.InitialDirectory = texPathAddress.Text;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    foreach (string path in openFileDialog1.FileNames)
                    {
                        Path = path;
                        Texture2D text;
                        Image     img;
                        try
                        {
                            FileStream stream = new FileStream(Path, FileMode.Open);

                            text = Texture2D.FromStream(GraphicsDevices, stream);
                            img  = Image.FromStream(stream);
                            stream.Close();
                            stream.Dispose();

                            string TextureNameToBeSaved = "";
                            try
                            {
                                TextureNameToBeSaved = path.Substring(texPathAddress.Text.Length);
                            }
                            catch (ArgumentOutOfRangeException ex)
                            {
                                MessageBox.Show("Texture Was Not Found In Content Directory, Cannot Load Texture");
                                return;
                            }

                            currentLayer.AddTexture(text);
                            foreach (TileLayer layer in Map.Layers)
                            {
                                if (layer.HasTexture(text) == -1)
                                {
                                    layer.AddTexture(text);
                                }
                            }

                            string[] TextureNameSplit      = TextureNameToBeSaved.Split('.');
                            string   textureNameToWriteOut = TextureNameSplit[0];
                            textureNameToWriteOut = textureNameToWriteOut.Substring(1);

                            dictTextures[textureNameToWriteOut] = text;
                            dictImages[textureNameToWriteOut]   = img;

                            TextureList.Items.Add(textureNameToWriteOut);
                        }

                        catch (IOException ex)
                        {
                            MessageBox.Show("The File: " + Path + "Is already Added", "Error");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Select A Layer First Please");
            }
        }