Ejemplo n.º 1
0
        private void LoadDefault()
        {
            int ID = m_mTM.LoadTexture(m_strDefFileName, Color.FromArgb(255, 255, 255).ToArgb());
            m_nCurrImageID[m_nCurrTilesetIndex] = ID;
            int imageWidth = m_mTM.GetTextureWidth(ID);
            int imageHeight= m_mTM.GetTextureHeight(ID);

            splitContainer1.Panel1.AutoScrollMinSize = new Size(imageWidth, imageHeight + m_nTilesetYoffset + 5);

            // set up default starting selected tile
            ResetCurrTile();

            // set up default Map
            m_mMap = new CMap(64, 32, (int)nudMapNumCols.Value, (int)nudMapNumRows.Value, m_nZoomIncrement, true, 0, this.Height, this.Width);
            m_mMap.GMapGrid.SetDotID(dotID);
            m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth; // panel size is size of map + an extra cell on top and bottom
            m_mMap.NPanelHeight = m_mMap.NNumRows * m_mMap.NCellHeight + m_mMap.NCellHeight;
            splitContainer1.Panel2.AutoScrollMinSize = new Size(m_mMap.NPanelWidth, m_mMap.NPanelHeight);
            m_bIsIsometric = true;
            nudMapNumCols.Enabled = false;
            nudMapNumRows.Enabled = false;
            nudMapCellSize.Enabled = false;
            //nudMapZoom.Enabled = false;

            m_clrKey[m_nCurrTilesetIndex] = Color.White;
            m_mMap.ClrTilesetKey = m_clrKey[m_nCurrTilesetIndex];

            m_strTilesetFilenames[m_nCurrTilesetIndex] = Path.GetFullPath(m_strDefFileName);

            AddTileset(ID, m_clrKey[m_nCurrTilesetIndex], imageWidth, imageHeight, 64, 45, m_strTilesetFilenames[m_nCurrTilesetIndex]);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles binary, text, and xml loading depending on which was chosen
        /// 
        ///     <LoadParams>
        /// 
        ///     Save Variables:
        ///     Version number.
        ///     Map Name.
        ///     Is it isometric?
        ///     Total number of tiles.
        ///     Columns.
        ///     Rows.
        ///     Cell Width.
        ///     Cell Height.
        ///     "Layer Name".
        ///     
        ///         <IndividualTiles>
        ///         Map ID.
        ///         Image ID.
        ///         ID (tileset tile ID).
        ///         Flag.
        ///         (Free Placed adds x and y, source rect is used for tileset x and y).
        ///         Tile Width.
        ///         Tile Height.
        ///         Trigger.
        ///         </IndividualTiles>
        ///     
        ///     </SaveParams>
        /// </summary>
        /// <param name="fileName">The file to load from</param>
        /// <returns>void</returns>
        private void OpenFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();

            switch (loadType)
            {
                case (int)LOAD_TYPE.XML:
                    {
                        ofd.FileName = ".xml";
                        ofd.Filter = "XML Files|*.xml";
                        if (DialogResult.OK == ofd.ShowDialog(this))
                        {
                            if (Path.GetExtension(ofd.FileName) == ".xml" ||
                                Path.GetExtension(ofd.FileName) == ".XML")
                            {
                                this.Text = "TED - " + Path.GetFileNameWithoutExtension(ofd.FileName);
                                m_mMap = null;
                                for (int i = 0; i < MAX_NUM_TILESETS && m_tsTileset[i] != null; ++i)
                                {
                                    m_tsTileset[i] = null;
                                    m_tsComponents[i] = null;
                                    m_strTilesetFilenames[i] = null;
                                    m_mTM.ReleaseTexture(m_nCurrImageID[i]);
                                    m_nCurrImageID[i] = -1;
                                    m_clrKey[i] = Color.White;
                                }
                                XmlReaderSettings settings = new XmlReaderSettings();
                                settings.ConformanceLevel = ConformanceLevel.Document;
                                settings.IgnoreComments = true;
                                settings.IgnoreWhitespace = true;

                                using (XmlReader reader = XmlReader.Create(ofd.FileName, settings))
                                {
                                    reader.MoveToContent();
                                    if (reader.Name == m_strVersionNumber)
                                    {
                                        int nNumTiles;
                                        int nNumCols;
                                        int nNumRows;
                                        int nCellWidth;
                                        int nCellHeight;
                                        int nNumTilesets = 0;
                                        int type;
                                        reader.ReadStartElement();
                                        if (reader.Name == "Map")
                                        {
                                            m_strSaveFileName = reader.GetAttribute("Name");
                                            type         = Convert.ToInt32(reader.GetAttribute("Type"));
                                            m_bIsIsometric = Convert.ToBoolean(reader.GetAttribute("Isometric"));
                                            nNumTiles    = Convert.ToInt32(reader.GetAttribute("TotalTiles"));
                                            nNumCols     = Convert.ToInt32(reader.GetAttribute("Columns"));
                                            nNumRows     = Convert.ToInt32(reader.GetAttribute("Rows"));
                                            nCellWidth   = Convert.ToInt32(reader.GetAttribute("CellWidth"));
                                            nCellHeight  = Convert.ToInt32(reader.GetAttribute("CellHeight"));
                                            nNumTilesets = Convert.ToInt32(reader.GetAttribute("NumTilesets"));

                                            // set up the map
                                            if (m_bIsIsometric)
                                            {
                                                m_mMap = new CMap(nCellWidth, nCellHeight, nNumCols, nNumRows, m_nZoomIncrement, true, type, this.Height, Width);
                                                nudMapCellSize.Enabled = false;

                                                nudMapNumCols.Enabled = false;
                                                nudMapNumRows.Enabled = false;
                                                nudMapCellSize.Enabled = false;
                                                nudMapZoom.Enabled = false;
                                                m_bIsIsometric = true;

                                                switch (type)
                                                {
                                                    case (int)IsoType.ISO_DIAMOND:
                                                        m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth;
                                                        m_mMap.NPanelHeight = m_mMap.NNumRows * m_mMap.NCellHeight + m_mMap.NCellHeight;
                                                        splitContainer1.Panel2.AutoScrollMinSize = new Size(0, 0);
                                                        splitContainer1.Panel2.AutoScrollMinSize = new Size(m_mMap.NPanelWidth, m_mMap.NPanelHeight);
                                                        break;
                                                    case (int)IsoType.ISO_SLIDE:
                                                        m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth;
                                                        m_mMap.NPanelHeight = (m_mMap.NNumRows >> 1) * m_mMap.NCellHeight + m_mMap.NCellHeight;
                                                        splitContainer1.Panel2.AutoScrollMinSize = new Size(0, 0);
                                                        splitContainer1.Panel2.AutoScrollMinSize = new Size(m_mMap.NPanelWidth + 100, m_mMap.NPanelHeight + 100);
                                                        break;
                                                    case (int)IsoType.ISO_STAG:
                                                        int tempWidth, tempHeight;
                                                        tempWidth = m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth + 100;
                                                        tempHeight = m_mMap.NPanelHeight = (m_mMap.NNumRows >> 1) * m_mMap.NCellHeight + m_mMap.NCellHeight + m_mMap.GMapGrid.NIsoTopY;
                                                        splitContainer1.Panel2.AutoScrollMinSize = new Size(0, 0);
                                                        splitContainer1.Panel2.AutoScrollMinSize = new Size(tempWidth, m_mMap.NPanelHeight);

                                                        if (tempWidth > splitContainer1.Panel2.ClientSize.Width)
                                                            m_mMap.NPanelWidth = tempWidth;
                                                        if (tempHeight > splitContainer1.Panel2.ClientSize.Height)
                                                            m_mMap.NPanelHeight = tempHeight;

                                                        break;
                                                }
                                            }
                                            else
                                            {
                                                m_mMap = new CMap(nCellWidth, nNumCols, nNumRows, m_nZoomIncrement, false);
                                                nudMapCellSize.Value = nCellWidth;
                                                nudMapCellSize.Enabled = true;
                                                splitContainer1.Panel2.AutoScrollMinSize = new Size(nNumCols * nCellWidth, nNumRows * nCellHeight);
                                                m_mMap.NPanelWidth = splitContainer1.Panel2.Width;
                                                m_mMap.NPanelHeight = splitContainer1.Panel2.Height;
                                                nudMapNumCols.Value = nNumCols;
                                                nudMapNumRows.Value = nNumRows;
                                            }
                                            m_mMap.NZoom = (int)nudMapZoom.Value;
                                            m_mMap.ClrTilesetKey = Color.White;
                                        }

                                        m_nCurrTilesetIndex = 0;

                                        // set up all the tilesets
                                        {
                                            string filename = "";
                                            int imageID;
                                            int red, green, blue;
                                            Color clrKey = Color.White;
                                            int width;
                                            int height;
                                            int cellwidth = 0;
                                            int cellheight = 0;
                                            for (int i = 0; i < tabControl1.TabCount - 1; ++i )
                                            {
                                                tabControl1.TabPages.RemoveAt(i + 1);
                                            }
                                            for (int i = 0; i < nNumTilesets; ++i)
                                            {
                                                reader.ReadStartElement();
                                                if (reader.Name == "Tileset")
                                                {
                                                    filename = reader.GetAttribute("FileName");
                                                    m_strTilesetFilenames[i] = filename;
                                                    red = Convert.ToInt32(reader.GetAttribute("ColorKeyRed"));
                                                    green = Convert.ToInt32(reader.GetAttribute("ColorKeyGreen"));
                                                    blue = Convert.ToInt32(reader.GetAttribute("ColorKeyBlue"));
                                                    width = Convert.ToInt32(reader.GetAttribute("Width"));
                                                    height = Convert.ToInt32(reader.GetAttribute("Height"));
                                                    cellwidth = Convert.ToInt32(reader.GetAttribute("CellWidth"));
                                                    cellheight = Convert.ToInt32(reader.GetAttribute("CellHeight"));
                                                    clrKey = Color.FromArgb(red, green, blue);
                                                    imageID = m_mTM.LoadTexture((filename), Color.FromArgb(clrKey.R, clrKey.G, clrKey.B).ToArgb());
                                                    m_nCurrImageID[i] = imageID;
                                                    bNewTS = true;
                                                    AddTileset(imageID, clrKey, width, height, cellwidth, cellheight, filename);
                                                    bNewTS = false;
                                                    m_clrKey[i] = clrKey;
                                                }
                                            }
                                            Rectangle SourceRect = new Rectangle(0, 0, cellwidth, cellheight);
                                            m_tCurrTile = new CTILE(0, SourceRect, m_nCurrTileFlag, m_nCurrImageID[m_nCurrTilesetIndex], 0, "None", filename);
                                        }
                                        // set up the map tiles
                                        int mapID = 0;
                                        int tileImageID = 0;
                                        int sourceID = 0;
                                        int flag = 0;
                                        int tileX = 0;
                                        int tileY = 0;
                                        int tileWidth = 0;
                                        int tileHeight = 0;
                                        int cost = 0;
                                        float rotation = 0.0f;
                                        string trigger = "";
                                        string fileName = "";
                                        Rectangle srcRect;
                                        m_mMap.NCurrLayer = (int)LAYER.LAYER_ONE;
                                        reader.ReadToFollowing("Layer1");

                                        reader.Read();
                                        m_mMap.ContainsTile[(int)LAYER.LAYER_ONE] = true;
                                        while (reader.Name == "TILE")
                                        {
                                            reader.Read();
                                            mapID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            fileName = reader.ReadString();
                                            //tileImageID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            sourceID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            flag = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileX = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileY = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileWidth = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileHeight = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            trigger = reader.ReadString();
                                            reader.Read();
                                            cost = Convert.ToInt32(reader.ReadString());

                                            // find the imageId of the tile's texture
                                            for (int i = 1; i < nNumTilesets + 1; ++i)
                                            {
                                                if (fileName == m_mTM.GetTextureFilename(i))
                                                { tileImageID = i; break; }
                                            }

                                            srcRect = new Rectangle(tileX, tileY, tileWidth, tileHeight);
                                            CTILE tempTile = new CTILE(sourceID, srcRect, flag, tileImageID, cost, trigger, fileName);
                                            tempTile.Trigger = trigger;

                                            m_mMap.AddTile(tempTile, mapID);
                                            reader.Read();
                                            reader.Read();
                                        }
                                        m_mMap.NCurrLayer = (int)LAYER.LAYER_TWO;
                                        reader.ReadToFollowing("Layer2");
                                        reader.Read();
                                        while (reader.Name == "TILE")
                                        {
                                            reader.Read();
                                            mapID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            fileName = reader.ReadString();
                                            //tileImageID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            sourceID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            flag = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileX = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileY = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileWidth = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileHeight = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            trigger = reader.ReadString();
                                            reader.Read();
                                            cost = Convert.ToInt32(reader.ReadString());

                                            // find the imageId of the tile's texture, start at [1] (dot is [0]
                                            for (int i = 1; i < nNumTilesets+1; ++i)
                                            {
                                                if (fileName == m_mTM.GetTextureFilename(i))
                                                { tileImageID = i; break; }
                                            }
                                            srcRect = new Rectangle(tileX, tileY, tileWidth, tileHeight);
                                            CTILE tempTile = new CTILE(sourceID, srcRect, flag, tileImageID, cost, trigger, fileName);
                                            tempTile.Trigger = trigger;

                                            m_mMap.AddTile(tempTile, mapID);
                                            reader.Read();
                                            reader.Read();
                                        }
                                        m_mMap.NCurrLayer = (int)LAYER.LAYER_ONE;
                                        if (reader.Name != "FreePlace")
                                        {
                                            reader.ReadToFollowing("FreePlace");
                                        }
                                        reader.Read();
                                        int posX, posY, count = 0;
                                        if (reader.Name == "FREETILE")
                                            m_mMap.ContainsTile[(int)LAYER.LAYER_FREE] = true;
                                        while (reader.Name == "FREETILE")
                                        {
                                            reader.Read();
                                            fileName = reader.ReadString();
                                            //tileImageID = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileX = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileY = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            flag = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            posX = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            posY = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileWidth = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            tileHeight = Convert.ToInt32(reader.ReadString());
                                            reader.Read();
                                            trigger = reader.ReadString();
                                            reader.Read();
                                            rotation = (float)Convert.ToDouble(reader.ReadString());
                                            reader.Read();

                                            // find the imageId of the tile's texture
                                            for (int i = 0; i < nNumTilesets; ++i)
                                            {
                                                if (fileName == m_strTilesetFilenames[i])
                                                { tileImageID = i; break; }
                                            }
                                            srcRect = new Rectangle(tileX, tileY, tileWidth, tileHeight);
                                            CFREETILE tempFree = new CFREETILE(posX, posY, srcRect, flag, tileImageID, rotation, fileName);
                                            tempFree.Trigger = trigger;
                                            // TODO:: save and load rotation
                                            m_mMap.LoadFreePlacedTiles(tempFree, count++);
                                            reader.Read();
                                            //reader.Read();
                                        }
                                    }
                                }
                                m_bDontDraw = false;
                                m_bInPanelTwo = true;
                                m_bInPanelOne = false;
                                return;
                            }
                            else // try again
                            {
                                MessageBox.Show("You have entered an invalid file extension.",
                                                "Failed to load", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                OpenFile();
                            }
                        }
                    }
                    break;
                case (int)LOAD_TYPE.BINARY:
                    {
                        ofd.FileName = ".dat";
                        ofd.Filter = "DATA Files|*.dat";
                        if (DialogResult.OK == ofd.ShowDialog(this))
                        {
                            if (Path.GetExtension(ofd.FileName) == ".dat" ||
                                Path.GetExtension(ofd.FileName) == ".DAT")
                            {
                                m_mMap = null;
                                for (int i = 0; i < MAX_NUM_TILESETS; ++i)
                                {
                                    this.Text = "TED - " + Path.GetFileNameWithoutExtension(ofd.FileName);
                                    m_tsTileset[i] = null;
                                    m_tsComponents[i] = null;
                                    m_strTilesetFilenames[i] = null;
                                    m_nCurrImageID[i] = -1;
                                    m_clrKey[i] = Color.White;
                                }
                                return;
                            }
                            else // try again
                            {
                                MessageBox.Show("You have entered an invalid file extension.",
                                                "Failed to load", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                OpenFile();
                            }
                        }
                    }
                    break;
            }
        }
Ejemplo n.º 3
0
        void newMap_createPushed(object sender, EventArgs e)
        {
            NewMap newMap = sender as NewMap;
            if (newMap != null)
            {
                m_bInPanelTwo = true;
                m_bDontDraw = false;
                m_bIsIsometric = newMap.BIsIsometric;
                nudMapNumCols.Value = newMap.NColumns;
                nudMapNumRows.Value = newMap.NRows;
                if (!newMap.BIsIsometric)
                {
                    m_mMap = new CMap(newMap.NCellSize, newMap.NColumns, newMap.NRows, m_nZoomIncrement, false);
                    nudMapCellSize.Value = newMap.NCellSize;
                    nudMapNumCols.Enabled = true;
                    nudMapNumRows.Enabled = true;
                    nudMapCellSize.Enabled = true;
                    nudMapZoom.Enabled = true;
                    m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth;
                    m_mMap.NPanelHeight = m_mMap.NNumRows * m_mMap.NCellHeight + m_mMap.NCellHeight;
                    splitContainer1.Panel2.AutoScrollMinSize = new Size(m_mMap.NPanelWidth, m_mMap.NPanelHeight);
                }
                else // is an isometric map
                {
                    m_mMap = new CMap(newMap.NIsoWidth, newMap.NIsoHeight, newMap.NColumns, newMap.NRows,
                        m_nZoomIncrement, true, newMap.NIsoType,
                        this.Height, this.Width);
                    m_mMap.GMapGrid.SetDotID(dotID);

                    nudMapNumCols.Enabled = false;
                    nudMapNumRows.Enabled = false;
                    nudMapCellSize.Enabled = false;
                    nudMapZoom.Enabled = false;
                    m_bIsIsometric = true;

                    switch (newMap.NIsoType)
                    {
                        case (int)IsoType.ISO_DIAMOND:
                            m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth;
                            m_mMap.NPanelHeight = m_mMap.NNumRows * m_mMap.NCellHeight + m_mMap.NCellHeight;
                            if (m_mMap.NPanelHeight < this.Height)
                                m_mMap.NPanelHeight = this.Height;
                            if (m_mMap.NPanelWidth < this.Width)
                                m_mMap.NPanelWidth = this.Width;
                            splitContainer1.Panel2.AutoScrollMinSize = new Size(0, 0);
                            splitContainer1.Panel2.AutoScrollMinSize = new Size(m_mMap.NPanelWidth, m_mMap.NPanelHeight);
                            break;
                        case (int)IsoType.ISO_SLIDE:
                            m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth;
                            m_mMap.NPanelHeight = (m_mMap.NNumRows >> 1) * m_mMap.NCellHeight + m_mMap.NCellHeight;
                            if (m_mMap.NPanelHeight < this.Height)
                                m_mMap.NPanelHeight = this.Height;
                            if (m_mMap.NPanelWidth < this.Width)
                                m_mMap.NPanelWidth = this.Width;
                            splitContainer1.Panel2.AutoScrollMinSize = new Size(0, 0);
                            splitContainer1.Panel2.AutoScrollMinSize = new Size(m_mMap.NPanelWidth + 100, m_mMap.NPanelHeight + 100);
                            break;
                        case (int)IsoType.ISO_STAG:
                            int tempWidth, tempHeight;
                            tempWidth = m_mMap.NPanelWidth = m_mMap.NNumCols * m_mMap.NCellWidth + m_mMap.NCellWidth + 100;
                            tempHeight = m_mMap.NPanelHeight = (m_mMap.NNumRows >> 1) * m_mMap.NCellHeight + m_mMap.NCellHeight + m_mMap.GMapGrid.NIsoTopY;
                            if (m_mMap.NPanelHeight < this.Height)
                                m_mMap.NPanelHeight = this.Height;
                            if (m_mMap.NPanelWidth < this.Width)
                                m_mMap.NPanelWidth = this.Width;
                            splitContainer1.Panel2.AutoScrollMinSize = new Size(0, 0);
                            splitContainer1.Panel2.AutoScrollMinSize = new Size(tempWidth, m_mMap.NPanelHeight);

                            if (tempWidth > splitContainer1.Panel2.ClientSize.Width)
                                m_mMap.NPanelWidth = tempWidth;
                            if (tempHeight > splitContainer1.Panel2.ClientSize.Height)
                                m_mMap.NPanelHeight = tempHeight;

                            break;
                    }
                }

            //                 if (m_nCurrTilesetIndex == -1)
            //                     m_nCurrTilesetIndex = 0;
                if (m_nCurrTilesetIndex > -1 && m_tsTileset[m_nCurrTilesetIndex] != null)
                {
                    m_nCurrImageID[m_nCurrTilesetIndex] = m_tsTileset[m_nCurrTilesetIndex].NTilesetImageID;
                    m_mMap.ClrTilesetKey = m_clrKey[m_nCurrTilesetIndex];
                }
                else
                    m_mMap.ClrTilesetKey = Color.White;

                m_strSaveFileName = "MyMap";
                this.Text = "TED - " + m_strSaveFileName;
                newMap.Close();
            }
        }