Exemple #1
0
        /// <summary>
        /// Creates a new map.
        /// </summary>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        /// <returns>The <see cref="MapID"/> of the newly created map, or null if no map was created.</returns>
        public static MapID? CreateNewMap(bool showConfirmation = true)
        {
            try
            {
                // Show the confirmation message
                if (showConfirmation)
                {
                    const string confirmMsg = "Are you sure you wish to create a new map?";
                    if (MessageBox.Show(confirmMsg, "Create new map?", MessageBoxButtons.YesNo) == DialogResult.No)
                        return null;
                }

                // Get the map ID to use
                var id = MapBase.GetNextFreeIndex(ContentPaths.Dev);

                // Create the map and save it
                using (
                    var map = new EditorMap(id, new Camera2D(new Vector2(800, 600)), GetTimeDummy.Instance) { Name = "New map" })
                {
                    map.SetDimensions(new Vector2(960, 960));
                    SaveMap(map, false);
                }

                return id;
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to create a new map. Exception: {0}";
                if (log.IsErrorEnabled)
                    log.ErrorFormat(errmsg, ex);
                Debug.Fail(string.Format(errmsg, ex));
            }

            return null;
        }
        // TODO: Class is currently unused because displaying persistent Characters in the editor is not yet supported

        /// <summary>
        /// Initializes a new instance of the <see cref="EditorCharacter"/> class.
        /// </summary>
        /// <param name="table">The <see cref="ICharacterTable"/> describing the character.</param>
        /// <param name="map">The <see cref="Map"/> to place the character on.</param>
        /// <exception cref="ArgumentNullException"><paramref name="table" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="map" /> is <c>null</c>.</exception>
        public EditorCharacter(ICharacterTable table, EditorMap map)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }

            _table       = table;
            _characterID = table.ID;

            var dbController = DbControllerBase.GetInstance();

            var charInfo = dbController.GetQuery <SelectCharacterByIDQuery>().Execute(_characterID);

            BodyID = charInfo.BodyID;
            Teleport(new Vector2(charInfo.RespawnX, charInfo.RespawnY));
            Resize(BodyInfo.Size);
            Name = charInfo.Name;

            Initialize(map, SkeletonManager.Create(ContentPaths.Build));
        }
Exemple #3
0
        /// <summary>
        /// Checks if the given map differs from the copy saved on disk.
        /// </summary>
        public static bool DiffersFromSaved(EditorMap map)
        {
            // Add the MapGrh-bound walls
            var mapGrhs    = map.Spatial.GetMany <MapGrh>().Distinct();
            var extraWalls = GlobalState.Instance.MapGrhWalls.CreateWallList(mapGrhs);

            foreach (var wall in extraWalls)
            {
                map.AddEntity(wall);
            }

            bool differs;

            try
            {
                // Compare
                differs = map.DiffersFromSaved(ContentPaths.Dev, EditorDynamicEntityFactory.Instance);
            }
            finally
            {
                // Pull the MapGrh-bound walls back out
                foreach (var wall in extraWalls)
                {
                    map.RemoveEntity(wall);
                }
            }

            return(differs);
        }
Exemple #4
0
        /// <summary>
        /// Goes through each map and removes invalid GrhData references.
        /// </summary>
        public static List <KeyValuePair <MapID, int> > RemoveInvalidGrhDatasFromMaps()
        {
            var ret = new List <KeyValuePair <MapID, int> >();

            var grhIndexes = new HashSet <int>(GrhInfo.GrhDatas.Select(x => (int)x.GrhIndex));

            foreach (var mapId in FindAllMaps().Select(x => x.ID))
            {
                // Load the map
                EditorMap map = new EditorMap(mapId, new Camera2D(new Vector2(800, 600)), GetTimeDummy.Instance);
                map.Load(ContentPaths.Dev, true, EditorDynamicEntityFactory.Instance);
                RemoveBoundWalls(map);

                // Remove invalid
                var removed = RemoveInvalidGrhDatasFromMap(map, grhIndexes);

                if (removed > 0)
                {
                    // Save
                    SaveMap(map, false);
                    SaveMiniMap(map.ID, ContentPaths.Dev.Grhs + "MiniMap");
                    ret.Add(new KeyValuePair <MapID, int>(mapId, removed));
                }
            }

            return(ret);
        }
Exemple #5
0
        /// <summary>
        /// Goes through each map and removes invalid GrhData references.
        /// </summary>
        public static List<KeyValuePair<MapID, int>> RemoveInvalidGrhDatasFromMaps()
        {
            var ret = new List<KeyValuePair<MapID, int>>();

            var grhIndexes = new HashSet<int>(GrhInfo.GrhDatas.Select(x => (int)x.GrhIndex));

            foreach (var mapId in FindAllMaps().Select(x => x.ID))
            {
                // Load the map
                EditorMap map = new EditorMap(mapId, new Camera2D(new Vector2(800, 600)), GetTimeDummy.Instance);
                map.Load(ContentPaths.Dev, true, EditorDynamicEntityFactory.Instance);
                RemoveBoundWalls(map);

                // Remove invalid
                var removed = RemoveInvalidGrhDatasFromMap(map, grhIndexes);

                if (removed > 0)
                {
                    // Save
                    SaveMap(map, false);
                    SaveMiniMap(map.ID, ContentPaths.Dev.Grhs + "MiniMap");
                    ret.Add(new KeyValuePair<MapID, int>(mapId, removed));
                }
            }

            return ret;
        }
Exemple #6
0
        /// <summary>
        /// Saves a map as a new map.
        /// </summary>
        /// <param name="map">The map to save.</param>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        public static void SaveMapAs(EditorMap map, bool showConfirmation = true)
        {
            try
            {
                if (map == null)
                {
                    return;
                }

                var newID = MapBase.GetNextFreeIndex(ContentPaths.Dev);

                int newSetID;
                if (!int.TryParse(InputBox.Show("Save as...", "Save map ID as:", newID.ToString()), out newSetID))
                {
                    return;
                }

                if (!MapBase.MapIDExists((MapID)newSetID))
                {
                    newID = ((MapID)newSetID);

                    // Confirm save
                    if (showConfirmation)
                    {
                        const string confirmMsg = "Are you sure you wish to save map `{0}` as a new map (with ID `{1}`)?";
                        if (MessageBox.Show(string.Format(confirmMsg, map, newID), "Save map as?", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    const string confirmMsgOverWrite = "Are you sure you wish to save map `{0}` and overwrite map (with ID `{1}`)?";
                    if (MessageBox.Show(string.Format(confirmMsgOverWrite, map.ID, newSetID), "Save map as?", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }

                    newID = ((MapID)newSetID);
                }

                // Change the map ID
                map.ChangeID(newID);

                // Save
                SaveMap(map, false);
                SaveMiniMap(map.ID, ContentPaths.Dev.Grhs + "MiniMap");
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to save map `{0}` as a new map. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, map, ex);
                }
                Debug.Fail(string.Format(errmsg, map, ex));
            }
        }
        /// <summary>
        /// Changes the current map being displayed.
        /// </summary>
        /// <param name="mapID">The <see cref="MapID"/> to change to.</param>
        public void ChangeMap(MapID mapID)
        {
            if (Map != null && Map.ID == mapID)
            {
                return;
            }

            Map = new EditorMap(mapID, Camera, this);
        }
Exemple #8
0
        public ShiftContentsForm(EditorMap map)
        {
            if (map == null)
                throw new ArgumentNullException("map");

            _map = map;

            InitializeComponent();
        }
Exemple #9
0
        /// <summary>
        /// Removes the MapGrh bound walls from a map.
        /// </summary>
        public static void RemoveBoundWalls(EditorMap map)
        {
            var toRemove = map.Spatial.GetMany <WallEntityBase>(x => x.BoundGrhIndex > 0).Distinct().ToArray();

            foreach (var wall in toRemove)
            {
                map.RemoveEntity(wall);
            }
        }
Exemple #10
0
        /// <summary>
        /// Handles the <see cref="CommandLineSwitch.SaveMaps"/> switch.
        /// </summary>
        /// <param name="values">The values passed to the switch at the command line.</param>
        /// <returns>True if completed without errors; false if there were any errors.</returns>
        static bool HandleSwitch_SaveMaps(string[] values)
        {
            var ret = true;

            var camera = new Camera2D(GameData.ScreenSize);
            var dynamicEntityFactory = EditorDynamicEntityFactory.Instance;
            var contentPath          = ContentPaths.Dev;

            // Get the maps
            var mapInfos = MapHelper.FindAllMaps();

            // For each map, load it then save it
            foreach (var mapInfo in mapInfos)
            {
                // Load
                EditorMap map;
                try
                {
                    map = new EditorMap(mapInfo.ID, camera, GetTimeDummy.Instance);
                    map.Load(contentPath, true, dynamicEntityFactory);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to load map ID `{0}`. Exception: {1}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, mapInfo.ID, ex);
                    }
                    Debug.Fail(string.Format(errmsg, mapInfo.ID, ex));
                    map = null;
                    ret = false;
                }

                // Save
                try
                {
                    if (map != null)
                    {
                        MapHelper.SaveMap(map, false);
                    }
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to save map `{0}`. Exception: {1}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, mapInfo, ex);
                    }
                    Debug.Fail(string.Format(errmsg, mapInfo, ex));
                    ret = false;
                }
            }

            return(ret);
        }
        /// <summary>
        /// Handles the <see cref="MapScreenControl.MapChanged"/> event.
        /// </summary>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        protected virtual void OnMapChanged(EditorMap oldValue, EditorMap newValue)
        {
            // Update some references
            Camera.Map = newValue;

            // Remove all of the walls previously created from the MapGrhs
            if (newValue != null)
            {
                var newMapGrhs = newValue.Spatial.GetMany <MapGrh>().Distinct();
                var grhWalls   = GlobalState.Instance.MapGrhWalls.CreateWallList(newMapGrhs);
                var dupeWalls  = newValue.FindDuplicateWalls(grhWalls);
                foreach (var dupeWall in dupeWalls)
                {
                    newValue.RemoveEntity(dupeWall);
                }
            }

            // Reset some of the state values
            Camera.Min = Vector2.Zero;

            // Remove all of the old lights and add the new ones
            if (oldValue != null)
            {
                foreach (var light in oldValue.Lights)
                {
                    DrawingManager.LightManager.Remove(light);
                }
            }

            if (newValue != null)
            {
                foreach (var light in newValue.Lights)
                {
                    DrawingManager.LightManager.Add(light);
                }
            }

            // Remove all of the old refraction effects and add the new ones
            if (oldValue != null)
            {
                foreach (var fx in oldValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Remove(fx);
                }
            }

            // Add the refraction effects for the new map
            if (newValue != null)
            {
                foreach (var fx in newValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Add(fx);
                }
            }
        }
        public ShiftContentsForm(EditorMap map)
        {
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }

            _map = map;

            InitializeComponent();
        }
Exemple #13
0
        /// <summary>
        /// Deletes a map.
        /// </summary>
        /// <param name="map">The map to delete.</param>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        public static void DeleteMap(EditorMap map, bool showConfirmation = true)
        {
            try
            {
                if (map == null)
                {
                    return;
                }

                // Show the confirmation message
                if (showConfirmation)
                {
                    const string confirmMsg = "Are you sure you wish to delete map `{0}`? This cannot be undone!";
                    if (MessageBox.Show(string.Format(confirmMsg, map), "Delete map?", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }
                }

                // If a MapScreenControl is open for this map, set the map on it to null then dispose of the control
                var msc = MapScreenControl.TryFindInstance(map);
                if (msc != null)
                {
                    msc.Map = null;
                    msc.Dispose();
                }

                // Delete the map file
                var path = MapBase.GetMapFilePath(ContentPaths.Dev, map.ID);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                // Update the database
                GlobalState.Instance.DbController.GetQuery <DeleteMapQuery>().Execute(map.ID);

                // Delete successful
                if (showConfirmation)
                {
                    const string deletedMsg = "Successfully deleted map `{0}`!";
                    MessageBox.Show(string.Format(deletedMsg, map), "Map deleted", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to delete map `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, map, ex);
                }
                Debug.Fail(string.Format(errmsg, map, ex));
            }
        }
Exemple #14
0
        /// <summary>
        /// Handles the <see cref="CommandLineSwitch.SaveMaps"/> switch.
        /// </summary>
        /// <param name="values">The values passed to the switch at the command line.</param>
        /// <returns>True if completed without errors; false if there were any errors.</returns>
        static bool HandleSwitch_SaveMaps(string[] values)
        {
            var ret = true;

            var camera = new Camera2D(GameData.ScreenSize);
            var dynamicEntityFactory = EditorDynamicEntityFactory.Instance;
            var contentPath = ContentPaths.Dev;

            // Get the maps
            var mapInfos = MapHelper.FindAllMaps();

            // For each map, load it then save it
            foreach (var mapInfo in mapInfos)
            {
                // Load
                EditorMap map;
                try
                {
                    map = new EditorMap(mapInfo.ID, camera, GetTimeDummy.Instance);
                    map.Load(contentPath, true, dynamicEntityFactory);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to load map ID `{0}`. Exception: {1}";
                    if (log.IsErrorEnabled)
                        log.ErrorFormat(errmsg, mapInfo.ID, ex);
                    Debug.Fail(string.Format(errmsg, mapInfo.ID, ex));
                    map = null;
                    ret = false;
                }

                // Save
                try
                {
                    if (map != null)
                        MapHelper.SaveMap(map, false);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to save map `{0}`. Exception: {1}";
                    if (log.IsErrorEnabled)
                        log.ErrorFormat(errmsg, mapInfo, ex);
                    Debug.Fail(string.Format(errmsg, mapInfo, ex));
                    ret = false;
                }
            }

            return ret;
        }
Exemple #15
0
        /// <summary>
        /// Handles the <see cref="MapScreenControl.MapChanged"/> event.
        /// </summary>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        protected virtual void OnMapChanged(EditorMap oldValue, EditorMap newValue)
        {
            // Update some references
            Camera.Map = newValue;

            // Remove all of the walls previously created from the MapGrhs
            if (newValue != null)
            {
                MapHelper.RemoveBoundWalls(newValue);
            }

            // Reset some of the state values
            Camera.Min = Vector2.Zero;

            // Remove all of the old lights and add the new ones
            if (oldValue != null)
            {
                foreach (var light in oldValue.Lights)
                {
                    DrawingManager.LightManager.Remove(light);
                }
            }

            if (newValue != null)
            {
                foreach (var light in newValue.Lights)
                {
                    DrawingManager.LightManager.Add(light);
                }
            }

            // Remove all of the old refraction effects and add the new ones
            if (oldValue != null)
            {
                foreach (var fx in oldValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Remove(fx);
                }
            }

            // Add the refraction effects for the new map
            if (newValue != null)
            {
                foreach (var fx in newValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Add(fx);
                }
            }
        }
Exemple #16
0
        /// <summary>
        /// Removes the invalid GrhData references in a map.
        /// </summary>
        /// <param name="map">The map to remove the invalid GrhData references from.</param>
        /// <param name="grhIndexes">HashSet of the GrhIndexes that exist.</param>
        /// <returns>Number of MapGrhs removed.</returns>
        static int RemoveInvalidGrhDatasFromMap(EditorMap map, HashSet <int> grhIndexes)
        {
            int removed = 0;

            foreach (MapGrh mg in map.Spatial.GetMany <MapGrh>().Distinct().ToArray())
            {
                if (mg.Grh == null || mg.Grh.GrhData == null || !grhIndexes.Contains((int)mg.Grh.GrhData.GrhIndex))
                {
                    // Remove
                    map.Spatial.Remove(mg);
                    ++removed;
                }
            }

            return(removed);
        }
Exemple #17
0
        /// <summary>
        /// Deletes a map.
        /// </summary>
        /// <param name="map">The map to delete.</param>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        public static void DeleteMap(EditorMap map, bool showConfirmation = true)
        {
            try
            {
                if (map == null)
                    return;

                // Show the confirmation message
                if (showConfirmation)
                {
                    const string confirmMsg = "Are you sure you wish to delete map `{0}`? This cannot be undone!";
                    if (MessageBox.Show(string.Format(confirmMsg, map), "Delete map?", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                }
                
                // If a MapScreenControl is open for this map, set the map on it to null then dispose of the control
                var msc = MapScreenControl.TryFindInstance(map);
                if (msc != null)
                {
                    msc.Map = null;
                    msc.Dispose();
                }

                // Delete the map file
                var path = MapBase.GetMapFilePath(ContentPaths.Dev, map.ID);
                if (File.Exists(path))
                    File.Delete(path);

                // Update the database
                GlobalState.Instance.DbController.GetQuery<DeleteMapQuery>().Execute(map.ID);

                // Delete successful
                if (showConfirmation)
                {
                    const string deletedMsg = "Successfully deleted map `{0}`!";
                    MessageBox.Show(string.Format(deletedMsg, map), "Map deleted", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to delete map `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                    log.ErrorFormat(errmsg, map, ex);
                Debug.Fail(string.Format(errmsg, map, ex));
            }
        }
Exemple #18
0
        /// <summary>
        /// Creates a new map.
        /// </summary>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        /// <returns>The <see cref="MapID"/> of the newly created map, or null if no map was created.</returns>
        public static MapID?CreateNewMap(bool showConfirmation = true)
        {
            try
            {
                // Show the confirmation message
                if (showConfirmation)
                {
                    const string confirmMsg = "Are you sure you wish to create a new map?";
                    if (MessageBox.Show(confirmMsg, "Create new map?", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return(null);
                    }
                }

                // Get the map ID to use
                var id = MapBase.GetNextFreeIndex(ContentPaths.Dev);

                // Create the map and save it
                using (var map = new EditorMap(id, new Camera2D(new Vector2(800, 600)), GetTimeDummy.Instance)
                {
                    Name = "New map"
                })
                {
                    map.SetDimensions(new Vector2(960, 960));
                    SaveMap(map, false);
                    SaveMiniMap(map.ID, ContentPaths.Dev.Grhs + "MiniMap");
                }

                return(id);
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to create a new map. Exception: {0}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, ex);
                }
                Debug.Fail(string.Format(errmsg, ex));
            }

            return(null);
        }
        // TODO: Class is currently unused because displaying persistent Characters in the editor is not yet supported

        /// <summary>
        /// Initializes a new instance of the <see cref="EditorCharacter"/> class.
        /// </summary>
        /// <param name="table">The <see cref="ICharacterTable"/> describing the character.</param>
        /// <param name="map">The <see cref="Map"/> to place the character on.</param>
        /// <exception cref="ArgumentNullException"><paramref name="table" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="map" /> is <c>null</c>.</exception>
        public EditorCharacter(ICharacterTable table, EditorMap map)
        {
            if (table == null)
                throw new ArgumentNullException("table");
            if (map == null)
                throw new ArgumentNullException("map");

            _table = table;
            _characterID = table.ID;

            var dbController = DbControllerBase.GetInstance();

            var charInfo = dbController.GetQuery<SelectCharacterByIDQuery>().Execute(_characterID);
            BodyID = charInfo.BodyID;
            Teleport(new Vector2(charInfo.RespawnX, charInfo.RespawnY));
            Resize(BodyInfo.Size);
            Name = charInfo.Name;

            Initialize(map, SkeletonManager.Create(ContentPaths.Build));
        }
Exemple #20
0
 public MapUndoManager(EditorMap map, IDynamicEntityFactory dynamicEntityFactory)
 {
     Map = map;
     DynamicEntityFactory = dynamicEntityFactory;
 }
Exemple #21
0
 public MapUndoManager(EditorMap map, IDynamicEntityFactory dynamicEntityFactory)
 {
     Map = map;
     DynamicEntityFactory = dynamicEntityFactory;
 }
Exemple #22
0
 /// <summary>
 /// Removes the MapGrh bound walls from a map.
 /// </summary>
 public static void RemoveBoundWalls(EditorMap map)
 {
     var toRemove = map.Spatial.GetMany<WallEntityBase>(x => x.BoundGrhIndex > 0).Distinct().ToArray();
     foreach (var wall in toRemove)
         map.RemoveEntity(wall);
 }
        /// <summary>
        /// Handles the <see cref="MapScreenControl.MapChanged"/> event.
        /// </summary>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        protected virtual void OnMapChanged(EditorMap oldValue, EditorMap newValue)
        {
            // Update some references
            Camera.Map = newValue;

            // Remove all of the walls previously created from the MapGrhs
            if (newValue != null)
            {
                MapHelper.RemoveBoundWalls(newValue);
            }

            // Reset some of the state values
            Camera.Min = Vector2.Zero;

            // Remove all of the old lights and add the new ones
            if (oldValue != null)
            {
                foreach (var light in oldValue.Lights)
                {
                    DrawingManager.LightManager.Remove(light);
                }
            }

            if (newValue != null)
            {
                foreach (var light in newValue.Lights)
                {
                    DrawingManager.LightManager.Add(light);
                }
            }

            // Remove all of the old refraction effects and add the new ones
            if (oldValue != null)
            {
                foreach (var fx in oldValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Remove(fx);
                }
            }

            // Add the refraction effects for the new map
            if (newValue != null)
            {
                foreach (var fx in newValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Add(fx);
                }
            }
        }
        /// <summary>
        /// Changes the current map being displayed.
        /// </summary>
        /// <param name="mapID">The <see cref="MapID"/> to change to.</param>
        public void ChangeMap(MapID mapID)
        {
            if (Map != null && Map.ID == mapID)
                return;

            Map = new EditorMap(mapID, Camera, this);
        }
Exemple #25
0
        /// <summary>
        /// Checks if the given map differs from the copy saved on disk.
        /// </summary>
        public static bool DiffersFromSaved(EditorMap map)
        {
            // Add the MapGrh-bound walls
            var mapGrhs = map.Spatial.GetMany<MapGrh>().Distinct();
            var extraWalls = GlobalState.Instance.MapGrhWalls.CreateWallList(mapGrhs);
            foreach (var wall in extraWalls)
            {
                map.AddEntity(wall);
            }

            bool differs;
            try
            {
                // Compare
                differs = map.DiffersFromSaved(ContentPaths.Dev, EditorDynamicEntityFactory.Instance);
            }
            finally
            {
                // Pull the MapGrh-bound walls back out
                foreach (var wall in extraWalls)
                {
                    map.RemoveEntity(wall);
                }
            }

            return differs;
        }
Exemple #26
0
 /// <summary>
 /// Removes the invalid GrhData references in a map.
 /// </summary>
 /// <param name="map">The map to remove the invalid GrhData references from.</param>
 /// <returns>Number of MapGrhs removed.</returns>
 public static int RemoveInvalidGrhDatasFromMap(EditorMap map)
 {
     return RemoveInvalidGrhDatasFromMap(map, new HashSet<int>(GrhInfo.GrhDatas.Select(x => (int)x.GrhIndex)));
 }
Exemple #27
0
 /// <summary>
 /// Removes the invalid GrhData references in a map.
 /// </summary>
 /// <param name="map">The map to remove the invalid GrhData references from.</param>
 /// <returns>Number of MapGrhs removed.</returns>
 public static int RemoveInvalidGrhDatasFromMap(EditorMap map)
 {
     return(RemoveInvalidGrhDatasFromMap(map, new HashSet <int>(GrhInfo.GrhDatas.Select(x => (int)x.GrhIndex))));
 }
Exemple #28
0
        /// <summary>
        /// Removes the invalid GrhData references in a map.
        /// </summary>
        /// <param name="map">The map to remove the invalid GrhData references from.</param>
        /// <param name="grhIndexes">HashSet of the GrhIndexes that exist.</param>
        /// <returns>Number of MapGrhs removed.</returns>
        static int RemoveInvalidGrhDatasFromMap(EditorMap map, HashSet<int> grhIndexes)
        {
            int removed = 0;

            foreach (MapGrh mg in map.Spatial.GetMany<MapGrh>().Distinct().ToArray())
            {
                if (mg.Grh == null || mg.Grh.GrhData == null || !grhIndexes.Contains((int)mg.Grh.GrhData.GrhIndex))
                {
                    // Remove
                    map.Spatial.Remove(mg);
                    ++removed;
                }
            }

            return removed;
        }
Exemple #29
0
        /// <summary>
        /// Saves a map.
        /// </summary>
        /// <param name="map">The map to save.</param>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        public static void SaveMap(EditorMap map, bool showConfirmation = true)
        {
            try
            {
                if (map == null)
                    return;

                // Show confirmation
                if (showConfirmation)
                {
                    const string confirmMsg = "Are you sure you wish to save the changes to map `{0}`?";
                    if (MessageBox.Show(string.Format(confirmMsg, map), "Save map?", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                    // Allow specifying a new map ID
                    bool firstEnterIDAttempt = true;
                    while (true)
                    {
                        string displayText = "Save as map id:";
                        if (!firstEnterIDAttempt)
                            displayText = "Invalid map id entered. Please enter a numeric value greater than 0." + Environment.NewLine + Environment.NewLine + displayText;

                        firstEnterIDAttempt = false;

                        int newId;
                        if (int.TryParse(InputBox.Show("Save as...", displayText, map.ID.ToString()), out newId))
                        {
                            if (newId >= 0)
                            {
                                try
                                {
                                    map.ChangeID((MapID)newId);
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(string.Format("Failed to change map ID to `{0}`. Exception: {1}", newId, ex));
                                }
                            }
                        }
                    }
                }

                // Add the MapGrh-bound walls
                var mapGrhs = map.Spatial.GetMany<MapGrh>().Distinct();
                var extraWalls = GlobalState.Instance.MapGrhWalls.CreateWallList(mapGrhs);
                foreach (var wall in extraWalls)
                {
                    map.AddEntity(wall);
                }

                // Save the map
                map.Save(ContentPaths.Dev, EditorDynamicEntityFactory.Instance);

                // Update the database
                GlobalState.Instance.DbController.GetQuery<InsertMapQuery>().Execute(map);

                // Pull the MapGrh-bound walls back out
                foreach (var wall in extraWalls)
                {
                    map.RemoveEntity(wall);
                }

                // Save successful
                if (showConfirmation)
                {
                    const string savedMsg = "Successfully saved the changes to map `{0}`!";
                    MessageBox.Show(string.Format(savedMsg, map), "Map saved", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to save map `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                    log.ErrorFormat(errmsg, map, ex);
                Debug.Fail(string.Format(errmsg, map, ex));
            }
        }
Exemple #30
0
        /// <summary>
        /// Saves a map.
        /// </summary>
        /// <param name="map">The map to save.</param>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        public static void SaveMap(EditorMap map, bool showConfirmation = true)
        {
            try
            {
                if (map == null)
                {
                    return;
                }

                // Show confirmation
                if (showConfirmation)
                {
                    const string confirmMsg = "Are you sure you wish to save the changes to map `{0}`?";
                    if (MessageBox.Show(string.Format(confirmMsg, map), "Save map?", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }

                    // Allow specifying a new map ID
                    bool firstEnterIDAttempt = true;
                    while (true)
                    {
                        string displayText = "Save as map id:";
                        if (!firstEnterIDAttempt)
                        {
                            displayText = "Invalid map id entered. Please enter a numeric value greater than 0." + Environment.NewLine + Environment.NewLine + displayText;
                        }

                        firstEnterIDAttempt = false;

                        int newId;
                        if (int.TryParse(InputBox.Show("Save as...", displayText, map.ID.ToString()), out newId))
                        {
                            if (newId >= 0)
                            {
                                try
                                {
                                    map.ChangeID((MapID)newId);
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(string.Format("Failed to change map ID to `{0}`. Exception: {1}", newId, ex));
                                }
                            }
                        }
                    }
                }

                // Add the MapGrh-bound walls
                var mapGrhs    = map.Spatial.GetMany <MapGrh>().Distinct();
                var extraWalls = GlobalState.Instance.MapGrhWalls.CreateWallList(mapGrhs);
                foreach (var wall in extraWalls)
                {
                    map.AddEntity(wall);
                }

                try
                {
                    // Save the map
                    map.Save(ContentPaths.Dev, EditorDynamicEntityFactory.Instance);
                    SaveMiniMap(map.ID, ContentPaths.Dev.Grhs + "MiniMap");

                    // Update the database
                    GlobalState.Instance.DbController.GetQuery <InsertMapQuery>().Execute(map);
                }
                finally
                {
                    // Pull the MapGrh-bound walls back out
                    foreach (var wall in extraWalls)
                    {
                        Debug.Assert(wall.BoundGrhIndex > 0);
                        map.RemoveEntity(wall);
                    }
                }

                // Save successful
                const string savedMsg = "Successfully saved the changes to map id {0}: {1}";
                MainForm.SetStatusMessage(string.Format(savedMsg, map.ID, map.Name ?? string.Empty));
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to save map `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, map, ex);
                }
                Debug.Fail(string.Format(errmsg, map, ex));
            }
        }
Exemple #31
0
        /// <summary>
        /// Saves a map as a new map.
        /// </summary>
        /// <param name="map">The map to save.</param>
        /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to
        /// perform this operation.</param>
        public static void SaveMapAs(EditorMap map, bool showConfirmation = true)
        {
            try
            {
                if (map == null)
                    return;

                var newID = MapBase.GetNextFreeIndex(ContentPaths.Dev);

                var newSetID = 0;
                if (int.TryParse(InputBox.Show("Save as...", "Save map ID as:", newID.ToString()), out newSetID))
                {
                    if (!MapBase.MapIDExists((MapID)newSetID))
                    {
                        newID = ((MapID)newSetID);
                        // Confirm save

                        if (showConfirmation)
                        {
                            const string confirmMsg = "Are you sure you wish to save map `{0}` as a new map (with ID `{1}`)?";
                            if (MessageBox.Show(string.Format(confirmMsg, map, newID), "Save map as?", MessageBoxButtons.YesNo) ==
                                DialogResult.No)
                                return;
                        }
                    }
                    else
                    {
                        const string confirmMsgOverWrite =
                            "Are you sure you wish to save map `{0}` and overwrite map (with ID `{1}`)?";
                        if (
                            MessageBox.Show(string.Format(confirmMsgOverWrite, map.ID, newSetID), "Save map as?",
                                MessageBoxButtons.YesNo) == DialogResult.No)
                            return;

                        newID = ((MapID)newSetID);
                    }
                }

                // Change the map ID
                map.ChangeID(newID);

                // Save
                SaveMap(map, false);

                // Save successful
                if (showConfirmation)
                {
                    const string savedMsg = "Successfully saved the map `{0}` as a new map!";
                    MessageBox.Show(string.Format(savedMsg, map), "Map successfully saved as a new map", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to save map `{0}` as a new map. Exception: {1}";
                if (log.IsErrorEnabled)
                    log.ErrorFormat(errmsg, map, ex);
                Debug.Fail(string.Format(errmsg, map, ex));
            }
        }
Exemple #32
0
        /// <summary>
        /// Handles the <see cref="MapScreenControl.MapChanged"/> event.
        /// </summary>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        protected virtual void OnMapChanged(EditorMap oldValue, EditorMap newValue)
        {
            // Update some references
            Camera.Map = newValue;

            // Remove all of the walls previously created from the MapGrhs
            if (newValue != null)
            {
                var newMapGrhs = newValue.Spatial.GetMany<MapGrh>().Distinct();
                var grhWalls = GlobalState.Instance.MapGrhWalls.CreateWallList(newMapGrhs);
                var dupeWalls = newValue.FindDuplicateWalls(grhWalls);
                foreach (var dupeWall in dupeWalls)
                {
                    newValue.RemoveEntity(dupeWall);
                }
            }

            // Reset some of the state values
            Camera.Min = Vector2.Zero;

            // Remove all of the old lights and add the new ones
            if (oldValue != null)
            {
                foreach (var light in oldValue.Lights)
                {
                    DrawingManager.LightManager.Remove(light);
                }
            }

            if (newValue != null)
            {
                foreach (var light in newValue.Lights)
                {
                    DrawingManager.LightManager.Add(light);
                }
            }

            // Remove all of the old refraction effects and add the new ones
            if (oldValue != null)
            {
                foreach (var fx in oldValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Remove(fx);
                }
            }

            // Add the refraction effects for the new map
            if (newValue != null)
            {
                foreach (var fx in newValue.RefractionEffects)
                {
                    DrawingManager.RefractionManager.Add(fx);
                }
            }
        }