Пример #1
0
        public void ImportMapConfig()
        {
            #region 逻辑准备
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "map files(*.map)|*.map";
            ofd.FilterIndex = 2;
            ofd.RestoreDirectory = true;
            #endregion

            checkCanMove.Checked = false;
            checkShelter.Checked = false;

            if (ofd.ShowDialog() == DialogResult.OK) {
                using (Stream s = new FileStream(ofd.FileName, FileMode.Open)) {
                    mapConfig = new MapConfig();
                    _points = new Dictionary<Point, PointInfo>();
                    mapConfig = Serializer.Deserialize<MapConfig>(s);

                    int max45Pos = 2 * maxSize / tileHeight;
                    for (int i = 0; i < max45Pos; i++) {
                        for (int j = 0; j < max45Pos; j++) {
                            PointInfo pointInfo = new PointInfo();
                            pointInfo.X = i;
                            pointInfo.Y = j;
                            if (!Is45PointInScreen(i, j)) {
                                continue;
                            }
                            else {
                                pointInfo.mark = CellMark.CanNotMove;
                            }
                            _points[new Point(i, j)] = pointInfo;
                        }
                    }
                    for (int i = 0;i< mapConfig.posXList.Count;i++) {
                        PointInfo point = new PointInfo();
                        point.X = mapConfig.posXList[i];
                        point.Y = mapConfig.posYList[i];
                        point.mark = (CellMark)mapConfig.posMarks[i];
                        _points[new Point(point.X,point.Y)] = point;
                    }

                    splitWidth = mapConfig.tileInfo.splitWidth;
                    splitHeight = mapConfig.tileInfo.splitHeight;
                    tileHeight = mapConfig.tileInfo.tileHeight;
                    tileWidth = 2 * tileHeight;
                    HeightBox.Text = tileHeight.ToString();
                    WidthBox.Text = tileWidth.ToString();
                    splitHeightBox.Text = splitHeight.ToString();
                    splitWidthBox.Text = splitWidth.ToString();
                    SaveConfig();

                    mapConfig.tileInfo.mapHeight = pictureBox1.Image.Height;
                    mapConfig.tileInfo.mapWidth = pictureBox1.Image.Width;
                    ClearHistory();
                    RefreshMesh();
                    s.Close();
                }
            }
        }
Пример #2
0
        private void DrawMesh(Bitmap bm, bool isRestPoint)
        {
            pictureBox1.Image = bm;
            if (tileHeight <= 0) {
                MessageBox.Show("网格宽度必须大于0");
                return;
            }
            Dictionary<Point, PointInfo> mapDic = new Dictionary<Point, PointInfo>();
            backgroundGraphics = Graphics.FromImage(bm);
            maxSize = Math.Max(bm.Height, bm.Width);
            mapHeight = bm.Height;

            #region 遍历坐标,画菱形网格
            // 获取屏幕坐标为(0,0)时,45度坐标系的坐标点
            Point zeroP = ScreenTo45(0, 0);
            // 根据45度坐标点获取改菱形中点的屏幕坐标
            Point screenZeroCenterP = GetDiamondScreenCenterBy45(zeroP);

            for (int i = screenZeroCenterP.Y - tileHeight / 2 - (maxSize / tileHeight) * tileHeight; i <= maxSize; i += tileHeight) {
                backgroundGraphics.DrawLine(Pens.Green, new Point(screenZeroCenterP.X - tileWidth / 2, i - tileHeight / 2), new Point(screenZeroCenterP.X + maxSize * tileWidth / 2, i + maxSize * tileHeight / 2));
            }

            for (int i = screenZeroCenterP.Y + tileHeight / 2; i <= maxSize + (maxSize / tileHeight) * tileHeight; i += tileHeight) {
                backgroundGraphics.DrawLine(Pens.Green, new Point(screenZeroCenterP.X - tileWidth / 2, i + tileHeight / 2), new Point(screenZeroCenterP.X + maxSize * tileWidth / 2, i - maxSize * tileHeight / 2));
            }

            #region 重置地图信息
            if (isRestPoint) {
                mapConfig = new MapConfig();
                mapConfig.tileInfo = new TileInfo();
                mapConfig.tileInfo.mapHeight = bm.Height;
                mapConfig.tileInfo.mapWidth = bm.Width;
                mapConfig.tileInfo.splitHeight = splitHeight;
                mapConfig.tileInfo.splitWidth = splitWidth;
                mapConfig.tileInfo.tileHeight = tileHeight;
                ClearHistory();
                int max45Pos = 2 * maxSize / tileHeight;
                _points.Clear();
                for (int i = 0; i < max45Pos; i++) {
                    for (int j = 0; j < max45Pos; j++) {
                        PointInfo pointInfo = new PointInfo();
                        pointInfo.X = i;
                        pointInfo.Y = j;
                        if (!Is45PointInScreen(i, j)) {
                            continue;
                        }
                        else {
                            pointInfo.mark = CellMark.CanNotMove;
                        }
                        _points[new Point(i,j)] = pointInfo;
                        FillDiamon(GetDiamondScreenCenterBy45(pointInfo.X, pointInfo.Y), false, true,pointInfo.mark);
                    }
                }
            }
            #endregion

            isMapLoaded = true;
            this.保存配置文件ToolStripMenuItem.Enabled = isMapLoaded;
            this.打开配置文件ToolStripMenuItem.Enabled = isMapLoaded;
            this.切割图片ToolStripMenuItem.Enabled = isMapLoaded;
            if (checkCanMove.Checked) {
                Bitmap a = new Bitmap(Properties.Resources.can_not_move);
                SetCursor(a, new Point(0, 0));
            }
            #endregion
        }