diff --git a/SpriteLibrary/SpriteController.cs b/SpriteLibrary/SpriteController.cs index 6092469..04fbb24 100644 --- a/SpriteLibrary/SpriteController.cs +++ b/SpriteLibrary/SpriteController.cs @@ -116,6 +116,7 @@ namespace SpriteLibrary Image MyOriginalImage; //The untainted background PictureBox DrawingArea; //The PictureBox we draw ourselves on List Sprites = new List(); + List LinkedControllers = new List(); //Other sprite controllers that we share sprites with /// /// Since everything needs a random number generator, we make one that should be accessible throughout your program. @@ -638,6 +639,21 @@ namespace SpriteLibrary return newList; } + /// + /// Get a list of all your named sprites. These should just be your template sprites. + /// + /// A list containing all the named sprites + public List AllNamedSprites() + { + List tList = new List(); + foreach(Sprite one in Sprites) + { + if (one.SpriteName != "") + tList.Add(one); + } + return tList; + } + /// /// Return an adjustment ratio. This is the image-size to picture-box ratio. /// It is used for calculating precise pixels or picture-box locations. @@ -959,9 +975,35 @@ namespace SpriteLibrary public void AddSprite(Sprite SpriteToAdd) { Sprites.Add(SpriteToAdd); + AddSpriteToLinkedControllers(SpriteToAdd); SortSprites(); } + /// + /// This internal function is for adding named sprites from other controllers to keep them in sync + /// + /// The sprite to add if it does not exist yet on this controller + internal void AddSpriteIfNotExists(Sprite SpriteToAdd) + { + if (SpriteToAdd.SpriteName == "") return; //We only add named sprites + Sprite found = SpriteFromName(SpriteToAdd.SpriteName); + if(found == null) + Sprites.Add(SpriteToAdd); + } + + /// + /// If we are linked to other controllers, add this sprite template to the other controllers also + /// + /// The sprite we are trying to add + internal void AddSpriteToLinkedControllers(Sprite SpriteToAdd) + { + if (SpriteToAdd.SpriteName == "") return; //We only add named sprites + foreach (SpriteController one in LinkedControllers) + { + one.AddSpriteIfNotExists(SpriteToAdd); + } + } + /// /// Tell a sprite to destroy itself. The sprite will have Destroying property set to true from /// the time you destroy it until it vanishes. Whe you destroy a sprite, it will erase itself @@ -1000,6 +1042,64 @@ namespace SpriteLibrary What.SetName(Name); } + + /// + /// Link up a sprite controller so that it shares sprites with this other sprite controller. If one sprite controller + /// does not have the named sprite, it will query any linked controllers for that named sprite and copy it to the + /// controller that did not have it. This means you only need to create a sprite once, and you can use it on multiple + /// sprite controllers. In many games, you will want to have a sprite appear on different PictureBoxes, and this is + /// a way to do that. For example, you may want to have a bad-guy running around on the screen, but also have his sprite + /// appear in a bad-guy summary, along with his stats, on the side. Loading sprites can be slow, so this makes things a bit + /// faster by only needing to load them once. + /// + /// The sprite-controller to link. You only need to link it one direction, + /// the sprite controller will automatically create a bi-directional link + public void LinkControllersForSpriteTemplateSharing(SpriteController ControllerToLinkToThis) + { + if (ControllerToLinkToThis == null) return; + if(!LinkedControllers.Contains(ControllerToLinkToThis)) + { + LinkedControllers.Add(ControllerToLinkToThis); + } + ControllerToLinkToThis.LinkControllersForSpriteTemplateSharing(this); //link the other direction also + } + + /// + /// Unlink a previously linked controller. If you have linked a controller from a different window and are trying to + /// kill off the controller in a window you are closing, you want to unlink them as the window closes. We take a brief + /// moment to copy over any templates that have not yet been copied over. + /// + /// The + public void UnlinkControllersForSpriteTemplateSharing(SpriteController ControllerToUnlink) + { + if (ControllerToUnlink == null) return; //nothing to do. + if (LinkedControllers.Contains(ControllerToUnlink)) + { + LinkedControllers.Remove(ControllerToUnlink); + } + ControllerToUnlink.UnlinkControllersForSpriteTemplateSharingInternal(this); + List MySpriteTemplates = AllNamedSprites(); + List TheirSpriteTemplates = ControllerToUnlink.AllNamedSprites(); + foreach (Sprite one in MySpriteTemplates) + ControllerToUnlink.AddSpriteIfNotExists(one); + foreach (Sprite one in TheirSpriteTemplates) + AddSpriteIfNotExists(one); + } + + /// + /// This unlinks the second half. This is an internal function so people using SpriteController cannot accidentally + /// unlink half a controller. + /// + /// + internal void UnlinkControllersForSpriteTemplateSharingInternal(SpriteController ControllerToUnlink) + { + if (ControllerToUnlink == null) return; //nothing to do. + if (LinkedControllers.Contains(ControllerToUnlink)) + { + LinkedControllers.Remove(ControllerToUnlink); + } + } + /// /// This takes a point, as given by the mouse-click args, and returns the sprites at that point. Different /// functions use different coordinates, whether based off the background image, or based off the picturebox.