Updated texture manager to support content packs but also load in textures without having to hard code them.
This commit is contained in:
parent
c026702f09
commit
596aedac77
|
@ -1,6 +1,7 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using StardewModdingAPI;
|
using StardewModdingAPI;
|
||||||
|
using StardewValley;
|
||||||
|
|
||||||
namespace Revitalize.Framework.Graphics
|
namespace Revitalize.Framework.Graphics
|
||||||
{
|
{
|
||||||
|
@ -12,6 +13,7 @@ namespace Revitalize.Framework.Graphics
|
||||||
public string modID;
|
public string modID;
|
||||||
public ContentSource source;
|
public ContentSource source;
|
||||||
private readonly IModHelper helper;
|
private readonly IModHelper helper;
|
||||||
|
private readonly IContentPack contentPack;
|
||||||
|
|
||||||
/// <summary>Empty/null constructor.</summary>
|
/// <summary>Empty/null constructor.</summary>
|
||||||
public Texture2DExtended()
|
public Texture2DExtended()
|
||||||
|
@ -25,13 +27,15 @@ namespace Revitalize.Framework.Graphics
|
||||||
|
|
||||||
public Texture2DExtended(Texture2D Texture)
|
public Texture2DExtended(Texture2D Texture)
|
||||||
{
|
{
|
||||||
this.Name = "";
|
this.Name = Texture.Name;
|
||||||
this.texture = Texture;
|
this.texture = Texture;
|
||||||
this.path = "";
|
this.path = "";
|
||||||
this.helper = null;
|
this.helper = null;
|
||||||
this.modID = "";
|
this.modID = "";
|
||||||
|
this.contentPack = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>Construct an instance.</summary>
|
/// <summary>Construct an instance.</summary>
|
||||||
/// <param name="path">The relative path to file on disk. See StardustCore.Utilities.getRelativePath(modname,path);
|
/// <param name="path">The relative path to file on disk. See StardustCore.Utilities.getRelativePath(modname,path);
|
||||||
public Texture2DExtended(IModHelper helper, IManifest manifest, string path, ContentSource contentSource = ContentSource.ModFolder)
|
public Texture2DExtended(IModHelper helper, IManifest manifest, string path, ContentSource contentSource = ContentSource.ModFolder)
|
||||||
|
@ -42,6 +46,7 @@ namespace Revitalize.Framework.Graphics
|
||||||
this.helper = helper;
|
this.helper = helper;
|
||||||
this.modID = manifest.UniqueID;
|
this.modID = manifest.UniqueID;
|
||||||
this.source = contentSource;
|
this.source = contentSource;
|
||||||
|
this.contentPack = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Texture2DExtended(IModHelper helper, string modID, string path, ContentSource contentSource = ContentSource.ModFolder)
|
public Texture2DExtended(IModHelper helper, string modID, string path, ContentSource contentSource = ContentSource.ModFolder)
|
||||||
|
@ -52,11 +57,45 @@ namespace Revitalize.Framework.Graphics
|
||||||
this.helper = helper;
|
this.helper = helper;
|
||||||
this.modID = modID;
|
this.modID = modID;
|
||||||
this.source = contentSource;
|
this.source = contentSource;
|
||||||
|
this.contentPack = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Texture2DExtended(IContentPack ContentPack, IManifest manifest, string path)
|
||||||
|
{
|
||||||
|
this.Name = Path.GetFileNameWithoutExtension(path);
|
||||||
|
this.path = path;
|
||||||
|
this.texture = ContentPack.LoadAsset<Texture2D>(path);
|
||||||
|
this.helper = null;
|
||||||
|
this.modID = manifest.UniqueID;
|
||||||
|
this.source = ContentSource.ModFolder;
|
||||||
|
this.contentPack = null;
|
||||||
|
}
|
||||||
|
public Texture2DExtended(IContentPack ContentPack, string modID, string path)
|
||||||
|
{
|
||||||
|
this.Name = Path.GetFileNameWithoutExtension(path);
|
||||||
|
this.path = path;
|
||||||
|
this.texture = ContentPack.LoadAsset<Texture2D>(path);
|
||||||
|
this.helper = null;
|
||||||
|
this.modID = modID;
|
||||||
|
this.source = ContentSource.ModFolder;
|
||||||
|
this.contentPack = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Texture2DExtended Copy()
|
public Texture2DExtended Copy()
|
||||||
{
|
{
|
||||||
return new Texture2DExtended(this.helper, this.modID, this.path);
|
//return this;
|
||||||
|
if (this.helper != null)
|
||||||
|
{
|
||||||
|
return new Texture2DExtended(this.helper, this.modID, this.path);
|
||||||
|
}
|
||||||
|
else if(this.contentPack!=null)
|
||||||
|
{
|
||||||
|
return new Texture2DExtended(this.contentPack,this.modID,this.path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IModHelper getHelper()
|
public IModHelper getHelper()
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using StardewModdingAPI;
|
||||||
|
using StardewValley;
|
||||||
|
|
||||||
namespace Revitalize.Framework.Graphics
|
namespace Revitalize.Framework.Graphics
|
||||||
{
|
{
|
||||||
|
@ -16,6 +21,19 @@ namespace Revitalize.Framework.Graphics
|
||||||
public TextureManager()
|
public TextureManager()
|
||||||
{
|
{
|
||||||
this.textures = new Dictionary<string, Texture2DExtended>();
|
this.textures = new Dictionary<string, Texture2DExtended>();
|
||||||
|
this.searchForTextures();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextureManager(bool doNothing=false)
|
||||||
|
{
|
||||||
|
this.textures = new Dictionary<string, Texture2DExtended>();
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextureManager(IContentPack ContentPack)
|
||||||
|
{
|
||||||
|
this.textures = new Dictionary<string, Texture2DExtended>();
|
||||||
|
this.searchForTextures(ContentPack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTexture(string name, Texture2DExtended texture)
|
public void addTexture(string name, Texture2DExtended texture)
|
||||||
|
@ -36,9 +54,101 @@ namespace Revitalize.Framework.Graphics
|
||||||
|
|
||||||
public static void addTexture(string managerName, string textureName, Texture2DExtended Texture)
|
public static void addTexture(string managerName, string textureName, Texture2DExtended Texture)
|
||||||
{
|
{
|
||||||
Texture.texture.Name = managerName + '.' + textureName;
|
Texture.texture.Name = textureName;
|
||||||
TextureManagers[managerName].addTexture(textureName, Texture);
|
TextureManagers[managerName].addTexture(textureName, Texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Content pack search.
|
||||||
|
/// </summary>
|
||||||
|
public void searchForTextures(IContentPack content)
|
||||||
|
{
|
||||||
|
string path = content.DirectoryPath;
|
||||||
|
this.searchDirectories(path,"",content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Non-Content pack search.
|
||||||
|
/// </summary>
|
||||||
|
public void searchForTextures()
|
||||||
|
{
|
||||||
|
string path = ModCore.ModHelper.DirectoryPath;
|
||||||
|
this.searchDirectories(path,"",ModCore.Manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void searchDirectories(string path,string relativePath,IManifest manifest)
|
||||||
|
{
|
||||||
|
string[] dirs = Directory.GetDirectories(path);
|
||||||
|
string[] files = Directory.GetFiles(path);
|
||||||
|
|
||||||
|
string[] extensions = new string[2]
|
||||||
|
{
|
||||||
|
".png",
|
||||||
|
".jpg"
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (string directory in dirs)
|
||||||
|
{
|
||||||
|
string combo = string.IsNullOrEmpty(relativePath) ? Path.GetFileName(directory) : Path.Combine(relativePath, Path.GetFileName(directory));
|
||||||
|
this.searchDirectories(directory,combo,manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (string file in files)
|
||||||
|
{
|
||||||
|
if (extensions.Contains(Path.GetExtension(file)))
|
||||||
|
{
|
||||||
|
this.processFoundTexture(file,relativePath);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
private void searchDirectories(string path, string relativePath,IContentPack ContentPack)
|
||||||
|
{
|
||||||
|
string[] dirs = Directory.GetDirectories(path);
|
||||||
|
string[] files = Directory.GetFiles(path);
|
||||||
|
|
||||||
|
string[] extensions = new string[2]
|
||||||
|
{
|
||||||
|
".png",
|
||||||
|
".jpg"
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (string directory in dirs)
|
||||||
|
{
|
||||||
|
string combo = string.IsNullOrEmpty(relativePath) ? Path.GetFileName(directory) : Path.Combine(relativePath, Path.GetFileName(directory));
|
||||||
|
this.searchDirectories(directory, combo, ContentPack);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (string file in files)
|
||||||
|
{
|
||||||
|
if (extensions.Contains(Path.GetExtension(file)))
|
||||||
|
{
|
||||||
|
this.processFoundTexture(file, relativePath,ContentPack);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processFoundTexture(string file,string relativePath)
|
||||||
|
{
|
||||||
|
Texture2DExtended textureExtended = new Texture2DExtended(ModCore.ModHelper,ModCore.Manifest,Path.Combine(relativePath,Path.GetFileName(file)));
|
||||||
|
|
||||||
|
ModCore.log("Found texture: " + textureExtended.Name);
|
||||||
|
|
||||||
|
this.addTexture(textureExtended.Name, textureExtended);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processFoundTexture(string file, string relativePath, IContentPack ContentPack)
|
||||||
|
{
|
||||||
|
Texture2DExtended textureExtended = new Texture2DExtended(ContentPack, ContentPack.Manifest, Path.Combine(relativePath, Path.GetFileName(file)));
|
||||||
|
|
||||||
|
ModCore.log("Found texture: " + textureExtended.Name);
|
||||||
|
|
||||||
|
this.addTexture(textureExtended.Name, textureExtended);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,6 @@ namespace Revitalize.Framework.Objects
|
||||||
|
|
||||||
public BasicItemInformation(string name, string description, string categoryName, Color categoryColor,int edibility, int fragility, bool isLamp, int price, Vector2 TileLocation, bool canBeSetOutdoors, bool canBeSetIndoors, string id, string data, Texture2D texture, Color color, int tileIndex, bool bigCraftable, Type type, CraftingData craftingData, AnimationManager animationManager, Color drawColor, bool ignoreBoundingBox, InventoryManager Inventory, LightManager Lights) : base(id, data, texture, color, tileIndex, bigCraftable, type, craftingData)
|
public BasicItemInformation(string name, string description, string categoryName, Color categoryColor,int edibility, int fragility, bool isLamp, int price, Vector2 TileLocation, bool canBeSetOutdoors, bool canBeSetIndoors, string id, string data, Texture2D texture, Color color, int tileIndex, bool bigCraftable, Type type, CraftingData craftingData, AnimationManager animationManager, Color drawColor, bool ignoreBoundingBox, InventoryManager Inventory, LightManager Lights) : base(id, data, texture, color, tileIndex, bigCraftable, type, craftingData)
|
||||||
{
|
{
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.categoryName = categoryName;
|
this.categoryName = categoryName;
|
||||||
|
@ -79,10 +78,10 @@ namespace Revitalize.Framework.Objects
|
||||||
if (this.animationManager.IsNull)
|
if (this.animationManager.IsNull)
|
||||||
{
|
{
|
||||||
this.animationManager = new AnimationManager(new Graphics.Texture2DExtended(), new Animation(new Rectangle(0, 0, 16, 16)), false);
|
this.animationManager = new AnimationManager(new Graphics.Texture2DExtended(), new Animation(new Rectangle(0, 0, 16, 16)), false);
|
||||||
this.animationManager.getExtendedTexture().texture = this.texture;
|
this.animationManager.getExtendedTexture().texture = texture;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
this.texture = this.animationManager.getTexture();
|
this.texture = texture;
|
||||||
|
|
||||||
this.drawPosition = Vector2.Zero;
|
this.drawPosition = Vector2.Zero;
|
||||||
this.drawColor = drawColor;
|
this.drawColor = drawColor;
|
||||||
|
|
|
@ -25,6 +25,7 @@ namespace Revitalize
|
||||||
//Bugs:
|
//Bugs:
|
||||||
// -Chair tops cut off objects
|
// -Chair tops cut off objects
|
||||||
// -load content MUST be enabled for the table to be placed?????? WTF
|
// -load content MUST be enabled for the table to be placed?????? WTF
|
||||||
|
//-paths for extensive texture search are too long. need to be relative?
|
||||||
// TODO:
|
// TODO:
|
||||||
//
|
//
|
||||||
// -Get way to read in textures at runtime without having to load them in...
|
// -Get way to read in textures at runtime without having to load them in...
|
||||||
|
@ -113,6 +114,7 @@ namespace Revitalize
|
||||||
{
|
{
|
||||||
public static IModHelper ModHelper;
|
public static IModHelper ModHelper;
|
||||||
public static IMonitor ModMonitor;
|
public static IMonitor ModMonitor;
|
||||||
|
public static IManifest Manifest;
|
||||||
|
|
||||||
public static Dictionary<string, CustomObject> customObjects;
|
public static Dictionary<string, CustomObject> customObjects;
|
||||||
|
|
||||||
|
@ -128,6 +130,7 @@ namespace Revitalize
|
||||||
{
|
{
|
||||||
ModHelper = helper;
|
ModHelper = helper;
|
||||||
ModMonitor = this.Monitor;
|
ModMonitor = this.Monitor;
|
||||||
|
Manifest = this.ModManifest;
|
||||||
|
|
||||||
this.createDirectories();
|
this.createDirectories();
|
||||||
this.initailizeComponents();
|
this.initailizeComponents();
|
||||||
|
@ -138,10 +141,14 @@ namespace Revitalize
|
||||||
ModHelper.Events.GameLoop.ReturnedToTitle += this.GameLoop_ReturnedToTitle;
|
ModHelper.Events.GameLoop.ReturnedToTitle += this.GameLoop_ReturnedToTitle;
|
||||||
playerInfo = new PlayerInfo();
|
playerInfo = new PlayerInfo();
|
||||||
|
|
||||||
|
//Framework.Graphics.TextureManager.TextureManagers.Add("Furniture", new TextureManager(this.Helper.DirectoryPath, Path.Combine("Content", "Graphics", "Furniture")));
|
||||||
Framework.Graphics.TextureManager.TextureManagers.Add("Furniture", new TextureManager());
|
Framework.Graphics.TextureManager.TextureManagers.Add("Furniture", new TextureManager());
|
||||||
TextureManager.addTexture("Furniture","Oak Chair", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content","Graphics","Furniture", "Chairs", "OakChair.png")));
|
//Rename graphic files tohave spaces and comment out below lines
|
||||||
TextureManager.addTexture("Furniture", "Oak Table", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content", "Graphics", "Furniture", "Tables", "OakTable.png")));
|
|
||||||
TextureManager.addTexture("Furniture", "Oak Lamp", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content", "Graphics", "Furniture", "Lamps", "OakLamp.png")));
|
//TextureManager.addTexture("Furniture","Oak Chair", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content","Graphics","Furniture", "Chairs", "Oak Chair.png")));
|
||||||
|
//
|
||||||
|
//TextureManager.addTexture("Furniture", "Oak Table", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content", "Graphics", "Furniture", "Tables", "Oak Table.png")));
|
||||||
|
//TextureManager.addTexture("Furniture", "Oak Lamp", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content", "Graphics", "Furniture", "Lamps", "Oak Lamp.png")));
|
||||||
customObjects = new Dictionary<string, CustomObject>();
|
customObjects = new Dictionary<string, CustomObject>();
|
||||||
ObjectGroups = new Dictionary<string, MultiTiledObject>();
|
ObjectGroups = new Dictionary<string, MultiTiledObject>();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue