Example #1
0
        IEnumerable <MapGrh> GetFillMapGrhs(EditorMap map, Vector2 worldPos)
        {
            MapGrh           mapGrh = MapGrhPencilTool.GetGrhToSelect(map, worldPos);
            HashSet <MapGrh> ret    = new HashSet <MapGrh>();

            GetFillMapGrhs(map, mapGrh, ret);
            return(ret.ToArray());
        }
Example #2
0
        /// <summary>
        /// Removes a MapGrh from the map
        /// </summary>
        /// <param name="mg">MapGrh to remove from the map</param>
        public void RemoveMapGrh(MapGrh mg)
        {
            if (mg == null)
            {
                Debug.Fail("mg is null.");
                return;
            }

            Debug.Assert(Spatial.CollectionContains(mg), string.Format("MapGrh `{0}` isn't in the spatial collection.", mg));

            // Remove from the spatial
            Spatial.Remove(mg);
        }
Example #3
0
        void GetFillMapGrhs(EditorMap map, MapGrh mapGrh, HashSet <MapGrh> ret)
        {
            if (mapGrh == null || !ret.Add(mapGrh))
            {
                return;
            }

            Rectangle rect = new Rectangle(mapGrh.Position.X - 1, mapGrh.Position.Y - 1, mapGrh.Size.X + 2, mapGrh.Size.Y + 2);

            foreach (var mg in map.Spatial.GetMany <MapGrh>(rect, x => x.Grh != null && x.Grh.GrhData == mapGrh.Grh.GrhData))
            {
                GetFillMapGrhs(map, mg, ret);
            }
        }
Example #4
0
        /// <summary>
        /// Adds a MapGrh to the map
        /// </summary>
        /// <param name="mg">MapGrh to add to the map</param>
        public void AddMapGrh(MapGrh mg)
        {
            if (mg == null)
            {
                const string errmsg = "Parameter mg is null.";
                if (log.IsErrorEnabled)
                {
                    log.Error(errmsg);
                }
                Debug.Fail(errmsg);
                return;
            }

            // When in debug mode, ensure there are no duplicates
            Debug.Assert(!Spatial.CollectionContains(mg), string.Format("MapGrh `{0}` is already in the spatial collection.", mg));

            // Add to the spatial
            Spatial.Add(mg);
        }
        /// <summary>
        /// Places a <see cref="MapGrh"/> on the map.
        /// </summary>
        /// <param name="map">The map to place the <see cref="MapGrh"/> on.</param>
        /// <param name="camera">The <see cref="ICamera2D"/>.</param>
        /// <param name="screenPos">The screen position to place the <see cref="MapGrh"/>.</param>
        /// <param name="useTileMode">If TileMode should be used.</param>
        /// <param name="gd">The <see cref="GrhData"/> to place. Set to null to attempt to use the <see cref="GrhData"/> that is
        /// currently selected in the <see cref="GlobalState"/>.</param>
        /// <returns>The <see cref="MapGrh"/> instance that was added, or null if the the <see cref="MapGrh"/> could not be
        /// added for any reason.</returns>
        public static MapGrh PlaceMapGrh(Map map, ICamera2D camera, Vector2 screenPos, bool useTileMode, GrhData gd = null)
        {
            // Get the GrhData to place from the global state
            if (gd == null)
            {
                gd = GlobalState.Instance.Map.GrhToPlace.GrhData;
            }

            // Ensure we have a GrhData to place
            if (gd == null)
            {
                return(null);
            }

            // Get the world position to place it
            var drawPos = camera.ToWorld(screenPos);

            drawPos = GridAligner.Instance.Align(drawPos, useTileMode).Round();

            // Cache some other values
            var selGrhGrhIndex = gd.GrhIndex;
            var isForeground   = EditorSettings.Default.MapGrh_DefaultIsForeground;
            var depth          = EditorSettings.Default.MapGrh_DefaultDepth;
            var drawPosArea    = drawPos.ToRectangle(new Vector2(2), true);

            if (!useTileMode)
            {
                // Make sure the same MapGrh doesn't already exist at that position
                if (map.Spatial.Contains <MapGrh>(drawPosArea,
                                                  x =>
                                                  x.Grh.GrhData.GrhIndex == selGrhGrhIndex && x.IsForeground == isForeground &&
                                                  Math.Round(x.Position.QuickDistance(drawPos)) <= 1))
                {
                    return(null);
                }
            }
            else
            {
                // Make sure the same MapGrh doesn't already exist at that position on the same layer
                if (map.Spatial.Contains <MapGrh>(drawPosArea,
                                                  x =>
                                                  x.Grh.GrhData.GrhIndex == selGrhGrhIndex && x.IsForeground == isForeground &&
                                                  Math.Round(x.Position.QuickDistance(drawPos)) <= 1))
                {
                    return(null);
                }

                // In TileMode, do not allow ANY MapGrh at the same position and layer depth. And if it does exist, instead of aborting,
                // delete the existing one.
                var existingMapGrhs = map.Spatial.GetMany <MapGrh>(drawPosArea,
                                                                   x =>
                                                                   x.LayerDepth == depth && x.IsForeground == isForeground && Math.Round(x.Position.QuickDistance(drawPos)) <= 1);
                foreach (var toDelete in existingMapGrhs)
                {
                    Debug.Assert(toDelete != null);
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("TileMode caused MapGrh `{0}` to be overwritten.", toDelete);
                    }

                    map.RemoveMapGrh(toDelete);
                }

                Debug.Assert(
                    !map.Spatial.Contains <MapGrh>(drawPosArea,
                                                   x =>
                                                   x.LayerDepth == depth && x.IsForeground == isForeground &&
                                                   Math.Round(x.Position.QuickDistance(drawPos)) <= 1));
            }

            // Create the new MapGrh and add it to the map
            var g  = new Grh(gd, AnimType.Loop, map.GetTime());
            var mg = new MapGrh(g, drawPos, isForeground)
            {
                LayerDepth = depth
            };

            map.AddMapGrh(mg);

            return(mg);
        }
        public static bool DrawMapGrhTooltip(ISpriteBatch spriteBatch, IDrawableMap map, MapGrh mapGrh, Vector2 worldPos)
        {
            if (mapGrh == null || mapGrh.Grh == null || mapGrh.Grh.GrhData == null)
            {
                return(false);
            }

            Vector2 drawPos = worldPos - map.Camera.Min;
            var     font    = GlobalState.Instance.DefaultRenderFont;
            string  txt     = mapGrh.Grh.GrhData.Categorization.ToString();
            Vector2 txtSize = font.MeasureString(txt);
            Vector2 txtPos  = drawPos.Max(Vector2.Zero).Min(map.Camera.Size - txtSize);

            spriteBatch.DrawStringShaded(font, txt, txtPos, Color.White, Color.Black);

            return(true);
        }