Example #1
0
        /// <summary>
        /// Updates the map preview texture's position inside
        /// this control's display rectangle and the
        /// starting location indicators' positions.
        /// </summary>
        private void UpdateMap()
        {
            if (disposeTextures && texture != null && !texture.IsDisposed)
            {
                texture.Dispose();
            }

            if (Map == null)
            {
                texture = null;
                briefingBox.Disable();

                contextMenu.Disable();

                foreach (var indicator in startingLocationIndicators)
                {
                    indicator.Disable();
                }

                return;
            }

            if (Map.PreviewTexture == null)
            {
                texture         = Map.LoadPreviewTexture();
                disposeTextures = true;
            }
            else
            {
                texture         = Map.PreviewTexture;
                disposeTextures = false;
            }

            if (!string.IsNullOrEmpty(Map.Briefing))
            {
                briefingBox.SetText(Map.Briefing);
                briefingBox.Enable();
                if (IsActive)
                {
                    briefingBox.SetAlpha(0f);
                }
            }
            else
            {
                briefingBox.Disable();
            }

            double xRatio = (Width - 2) / (double)texture.Width;
            double yRatio = (Height - 2) / (double)texture.Height;

            double ratio;

            int texturePositionX = 1;
            int texturePositionY = 1;
            int textureHeight    = 0;
            int textureWidth     = 0;

            if (xRatio > yRatio)
            {
                ratio            = yRatio;
                textureHeight    = Height - 2;
                textureWidth     = (int)(texture.Width * ratio);
                texturePositionX = (int)(Width - 2 - textureWidth) / 2;
            }
            else
            {
                ratio            = xRatio;
                textureWidth     = Width - 2;
                textureHeight    = (int)(texture.Height * ratio);
                texturePositionY = (Height - 2 - textureHeight) / 2 + 1;
            }

            useNearestNeighbour = ratio < 1.0;

            textureRectangle = new Rectangle(texturePositionX, texturePositionY,
                                             textureWidth, textureHeight);

            List <Point> startingLocations = Map.GetStartingLocationPreviewCoords(new Point(texture.Width, texture.Height));

            for (int i = 0; i < startingLocations.Count && i < Map.MaxPlayers; i++)
            {
                PlayerLocationIndicator indicator = startingLocationIndicators[i];

                Point location = new Point(
                    texturePositionX + (int)(startingLocations[i].X * ratio),
                    texturePositionY + (int)(startingLocations[i].Y * ratio));

                indicator.SetPosition(location);
                indicator.Enabled = true;
                indicator.Visible = true;
            }

            for (int i = startingLocations.Count; i < MAX_STARTING_LOCATIONS; i++)
            {
                startingLocationIndicators[i].Disable();
            }
        }
        /// <summary>
        /// Updates the map preview texture's position inside
        /// this control's display rectangle and the
        /// starting location indicators' positions.
        /// </summary>
        private void UpdateMap()
        {
            if (disposeTextures && previewTexture != null && !previewTexture.IsDisposed)
            {
                previewTexture.Dispose();
            }

            extraTextures.Clear();

            if (Map == null)
            {
                previewTexture = null;
                briefingBox.Disable();

                contextMenu.Disable();

                foreach (var indicator in startingLocationIndicators)
                {
                    indicator.Disable();
                }

                return;
            }

            if (Map.PreviewTexture == null)
            {
                previewTexture  = Map.LoadPreviewTexture();
                disposeTextures = true;
            }
            else
            {
                previewTexture  = Map.PreviewTexture;
                disposeTextures = false;
            }

            if (!string.IsNullOrEmpty(Map.Briefing))
            {
                briefingBox.SetText(Map.Briefing);
                briefingBox.Enable();
                if (IsActive)
                {
                    briefingBox.SetAlpha(0f);
                }
            }
            else
            {
                briefingBox.Disable();
            }

            double xRatio = (Width - 2) / (double)previewTexture.Width;
            double yRatio = (Height - 2) / (double)previewTexture.Height;

            double ratio;

            int texturePositionX = 1;
            int texturePositionY = 1;
            int textureHeight    = 0;
            int textureWidth     = 0;

            if (xRatio > yRatio)
            {
                ratio            = yRatio;
                textureHeight    = Height - 2;
                textureWidth     = (int)(previewTexture.Width * ratio);
                texturePositionX = (int)(Width - 2 - textureWidth) / 2;
            }
            else
            {
                ratio            = xRatio;
                textureWidth     = Width - 2;
                textureHeight    = (int)(previewTexture.Height * ratio);
                texturePositionY = (Height - 2 - textureHeight) / 2 + 1;
            }

            useNearestNeighbour = ratio < 1.0;

            textureRectangle = new Rectangle(texturePositionX, texturePositionY,
                                             textureWidth, textureHeight);

            List <Point> startingLocations = Map.GetStartingLocationPreviewCoords(new Point(previewTexture.Width, previewTexture.Height));

            for (int i = 0; i < startingLocations.Count && i < Map.MaxPlayers; i++)
            {
                PlayerLocationIndicator indicator = startingLocationIndicators[i];

                Point location = new Point(
                    texturePositionX + (int)(startingLocations[i].X * ratio),
                    texturePositionY + (int)(startingLocations[i].Y * ratio));

                indicator.SetPosition(location);
                indicator.Enabled = true;
                indicator.Visible = true;
            }

            for (int i = startingLocations.Count; i < MAX_STARTING_LOCATIONS; i++)
            {
                startingLocationIndicators[i].Disable();
            }


            foreach (var mapExtraTexture in Map.GetExtraMapPreviewTextures())
            {
                // LoadTexture makes use of a texture cache
                // so we don't need to cache the textures manually
                Texture2D extraTexture = AssetLoader.LoadTexture(mapExtraTexture.TextureName);
                Point     location     = PreviewTexturePointToControlAreaPoint(
                    Map.MapPointToMapPreviewPoint(mapExtraTexture.Point,
                                                  new Point(previewTexture.Width - (extraTexture.Width / 2),
                                                            previewTexture.Height - (extraTexture.Height / 2)), mapExtraTexture.Level),
                    ratio);

                extraTextures.Add(new ExtraMapPreviewTexture(extraTexture, location));
            }
        }