コード例 #1
0
        protected void loadLayerHandler(object sender, EventArgs args)
        {
            ToolStripButton button = sender as ToolStripButton;

            MapZone zone = button.Tag as MapZone;

            using (LoadLayerDialog dlg = new LoadLayerDialog())
            {
                DialogResult result;

                dlg.LayerNames = worldMap.LayerNames;

                result = dlg.ShowDialog();
                if (result != DialogResult.OK)
                {
                    return;
                }

                int tilesWidth       = zone.MaxTileCoord.x - zone.MinTileCoord.x + 1;
                int tilesHeight      = zone.MaxTileCoord.z - zone.MinTileCoord.z + 1;
                int zoneWidthMeters  = tilesWidth * worldMap.TileSize / worldMap.OneMeter;
                int zoneHeightMeters = tilesHeight * worldMap.TileSize / worldMap.OneMeter;

                int metersPerPixel = zoneWidthMeters / dlg.LayerMapImage.Width;
                Debug.Assert(metersPerPixel == (zoneHeightMeters / dlg.LayerMapImage.Height));


                int samplesPerTile = WorldMap.metersPerTile / metersPerPixel;

                MapLayer layer = worldMap.GetLayer(dlg.LayerName);

                for (int z = 0; z < tilesHeight; z++)
                {
                    int dz = z + zone.MinTileCoord.z;
                    for (int x = 0; x < tilesWidth; x++)
                    {
                        int dx = x + zone.MinTileCoord.x;

                        CoordXZ tileCoord = new CoordXZ(dx, dz, WorldMap.tileSize);

                        MapBuffer tileMap;

                        if (layer is ColorMapLayer)
                        {
                            ColorMapLayer tmpLayer = layer as ColorMapLayer;

                            tileMap = tmpLayer.CreateCompatibleMapBuffer(dlg.LayerMapImage, metersPerPixel,
                                                                         x * samplesPerTile, z * samplesPerTile, samplesPerTile);
                        }
                        else
                        {
                            ValueMapLayer tmpLayer = layer as ValueMapLayer;
                            tileMap = null;
                        }

                        MapTile tile = worldMap.GetTile(tileCoord);

                        if (tileMap.MetersPerSample != layer.MetersPerSample)
                        {
                            tileMap = tileMap.Scale(worldMap.MetersPerTile / layer.MetersPerSample);
                        }

                        layer.CopyIn(tileCoord, tileMap);
                    }
                }
            }
        }