Skip to content

Commit

Permalink
Big, uh, updatey-things
Browse files Browse the repository at this point in the history
  • Loading branch information
gamrguy committed Apr 3, 2017
1 parent 298eeee commit 4e4b3fd
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 71 deletions.
55 changes: 53 additions & 2 deletions Dyes/ModDyePlayer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.DataStructures;
using Terraria.Graphics.Shaders;
using Terraria.ModLoader;
using ShaderLib.Dyes;

namespace ShaderLib.Dyes
{
public class ModDyePlayer : ModPlayer
{
{
internal static List<Func<int, Item, Player, int>> heldItemHooks;
internal static List<Func<int, Item, Player, int>> heldItemFlameHooks;

public override void ModifyDrawInfo(ref PlayerDrawInfo drawInfo)
{
//Don't ask. This is the vanilla code that applies dye to the appropriate equip types.
Expand Down Expand Up @@ -189,6 +194,52 @@ public override void PreUpdateBuffs()
player.cYorai = player.cPet;
}

public PlayerLayer heldItemLayer = new PlayerLayer("ShaderLib", "HeldItemShader", (PlayerDrawInfo drawInfo) => {
Player player = drawInfo.drawPlayer;

//Selects the held mouse item if it's the client-side player AND is holding one; selected inventory item otherwise
Item heldItem = (Main.myPlayer == player.whoAmI) && Main.mouseItem.active && !Main.mouseItem.IsAir && Main.mouseItem.type > 0 ? Main.mouseItem : player.inventory[player.selectedItem];

//Don't cause crashes :-)
if(heldItem == null || !heldItem.active || heldItem.IsAir) return;
int index = Main.playerDrawData.Count - 1;

//Don't bother with items that aren't showing (e.g. Arkhalis)
if(heldItem.noUseGraphic) return;

//Handling for applying shaders to flame textures
if(heldItem.flame) {
int flameShaderID = 0;
heldItemHooks.ForEach((hook) => { flameShaderID = hook(flameShaderID, heldItem, player); });

if(player.itemAnimation > 0 && !player.pulley && flameShaderID > 0) {
DrawData data = Main.playerDrawData[index];
data.shader = flameShaderID;
Main.playerDrawData[index] = data;
}

index -= 1;
}

//Handling for applying shaders to the held item itself
int shaderID = 0;
heldItemHooks.ForEach((hook) => { shaderID = hook(shaderID, heldItem, player); });

//Only use shader when player is using/holding an item
if(player.itemAnimation > 0 && !player.pulley && shaderID > 0) {
DrawData data = Main.playerDrawData[index];
data.shader = shaderID;
Main.playerDrawData[index] = data;
}
});

public override void ModifyDrawLayers(List<PlayerLayer> layers)
{
if(!Main.gameMenu) {
layers.Insert(layers.IndexOf(PlayerLayer.HeldItem) + 1, heldItemLayer);
}
}

private int GetShaderID(Item i){
/*if(i.GetModInfo<MetaDyeInfo>(mod).fakeItemID < 0) {
return GameShaders.Armor.GetShaderIdFromItemId(i.GetModInfo<MetaDyeInfo>(mod).fakeItemID);
Expand Down
36 changes: 17 additions & 19 deletions ShaderLib-WINDOWS.csproj
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,9 @@
<ItemGroup>
<Folder Include="Customizer\" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Xna.Framework">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Game">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.Game.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Graphics">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.Graphics.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Xact">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.Xact.dll</HintPath>
</Reference>
<Reference Include="Terraria">
<HintPath>..\..\..\..\..\..\Downloads\tModLoader-1.3.4\ModCompile\tModLoaderWindows.exe</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ShaderLibMod.cs" />
<Compile Include="ShaderReflections.cs" />
<Compile Include="Dyes\MetaDyeInfo.cs" />
<Compile Include="Dyes\MetaDyeLoader.cs" />
<Compile Include="Dyes\ModDyePlayer.cs" />
<Compile Include="Shaders\ItemShader.cs" />
<Compile Include="Shaders\MetaArmorShaderData.cs" />
Expand All @@ -90,4 +71,21 @@
<None Include="build.txt" />
<None Include="description.txt" />
</ItemGroup>
<ItemGroup>
<Reference Include="tModLoaderWindows">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\tModLoaderWindows.exe</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Game">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.Game.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Graphics">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.Graphics.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Xact">
<HintPath>..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.Xact.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
15 changes: 9 additions & 6 deletions ShaderLib-WINDOWS.userprefs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<MonoDevelop.Ide.Workbench ActiveDocument="../SecretDyes/DyesMod.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="../ItemCustomizer/CustomizerMod.cs">
<Files>
<File FileName="../SecretDyes/DyesMod.cs" Line="37" Column="136" />
<File FileName="../SecretDyes/Dyes/ArmorFFDye.cs" Line="14" Column="4" />
<File FileName="TestRainbowShader.cs" Line="4" Column="16" />
<File FileName="ShaderLibMod.cs" Line="54" Column="27" />
<File FileName="ShaderLibMod.cs" Line="122" Column="9" />
<File FileName="../ItemCustomizer/CustomizerPlayer.cs" Line="31" Column="4" />
<File FileName="../ItemCustomizer/CustomizerMod.cs" Line="40" Column="25" />
<File FileName="../ItemCustomizer/Customizer/CustomizerItemInfo.cs" Line="12" Column="3" />
<File FileName="../SecretDyes/DyesMod.cs" Line="32" Column="4" />
<File FileName="../ItemCustomizer/description.txt" Line="17" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
<MultiItemStartupConfigurations />
</Properties>
33 changes: 9 additions & 24 deletions ShaderLib.userprefs
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
<Properties>
<MonoDevelop.Ide.Workbench>
<Pads>
<Pad Id="ProjectPad">
<State name="__root__">
<Node name="ArmorHairDye" expanded="True">
<Node name="ArmorHairDye" expanded="True" />
</Node>
<Node name="ItemCustomizer" expanded="True" selected="True">
<Node name="ItemCustomizer" expanded="True">
<Node name="References" expanded="True" />
<Node name="Customizer" expanded="True" />
</Node>
</Node>
<Node />
<Node name="ShaderLib" expanded="True">
<Node name="ShaderLib" expanded="True">
<Node name="References" expanded="True" />
<Node name="Dyes" expanded="True" />
<Node name="Shaders" expanded="True" />
</Node>
</Node>
</State>
</Pad>
</Pads>
<MonoDevelop.Ide.Workbench ActiveDocument="../ItemCustomizer/CustomizerMod.cs">
<Files>
<File FileName="ShaderLibMod.cs" Line="122" Column="9" />
<File FileName="../ItemCustomizer/CustomizerPlayer.cs" Line="31" Column="4" />
<File FileName="../ItemCustomizer/CustomizerMod.cs" Line="40" Column="25" />
<File FileName="../ItemCustomizer/Customizer/CustomizerItemInfo.cs" Line="12" Column="3" />
<File FileName="../SecretDyes/DyesMod.cs" Line="32" Column="4" />
<File FileName="../ItemCustomizer/description.txt" Line="17" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
Expand Down
43 changes: 42 additions & 1 deletion ShaderLibMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public override void Load()
ItemShader.preDrawInv = new List<Func<int, Item, int>>();
ItemShader.preDrawWorld = new List<Func<int, Item, int>>();
NPCShader.hooks = new List<Func<int, NPC, int>>();
Dyes.ModDyePlayer.heldItemHooks = new List<Func<int, Item, Player, int>>();

instance = this;
}
Expand Down Expand Up @@ -96,7 +97,7 @@ public ModArmorShaderData GetModShaderByNames(Tuple<string, string> data) {
/// <param name="shaderName">Shader name.</param>
/// <param name="data">ModArmorShaderData object.</param>
/// <param name="itemID">Item ID to associate with this shader (use for dyes).</param>
public int RegisterArmorShader(string modName, string shaderName, ModArmorShaderData data, int itemID = -1) {
public int RegisterArmorShader<T>(string modName, string shaderName, T data, int itemID = -1) where T : ModArmorShaderData {
int shaderID;
if(itemID > 0) {
ShaderReflections.BindArmorShaderWithID(itemID, data);
Expand Down Expand Up @@ -127,5 +128,45 @@ public int RegisterMetaArmorShader(MetaArmorShaderData data) {
metaShaderRegistry.Add(shaderID, data);
return shaderID;
}

//Hook setter methods, ignores server so mods don't have to :-)
//Also acts as security; no hacking the hook lists!
public static void AddProjectileShaderHook(Func<int, Projectile, int> hook) {
if(Main.netMode != NetmodeID.Server) {
ProjectileShader.hooks.Add(hook);
}
}

public static void AddNPCShaderHook(Func<int, NPC, int> hook) {
if(Main.netMode != NetmodeID.Server) {
NPCShader.hooks.Add(hook);
}
}

public static void AddItemShaderInventoryHook(Func<int, Item, int> hook) {
if(Main.netMode != NetmodeID.Server) {
ItemShader.preDrawInv.Add(hook);
}
}

public static void AddItemShaderWorldHook(Func<int, Item, int> hook) {
if(Main.netMode != NetmodeID.Server) {
ItemShader.preDrawWorld.Add(hook);
}
}

public static void AddHeldItemShaderHook(Func<int, Item, Player, int> hook)
{
if(Main.netMode != NetmodeID.Server) {
Dyes.ModDyePlayer.heldItemHooks.Add(hook);
}
}

public static void AddHeldItemShaderFlameHook(Func<int, Item, Player, int> hook)
{
if(Main.netMode != NetmodeID.Server) {
Dyes.ModDyePlayer.heldItemFlameHooks.Add(hook);
}
}
}
}
4 changes: 2 additions & 2 deletions Shaders/ItemShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace ShaderLib.Shaders
{
public class ItemShader : GlobalItem
{
public static List<Func<int, Item, int>> preDrawInv;
public static List<Func<int, Item, int>> preDrawWorld;
internal static List<Func<int, Item, int>> preDrawInv;
internal static List<Func<int, Item, int>> preDrawWorld;

public override bool PreDrawInInventory(Item item, SpriteBatch spriteBatch, Vector2 position, Rectangle frame, Color drawColor, Color itemColor, Vector2 origin, float scale)
{
Expand Down
33 changes: 20 additions & 13 deletions Shaders/ModArmorShaderData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ public class ModArmorShaderData : ArmorShaderData
public Func<Entity, ArmorShaderData> SecondaryShader;

public ModArmorShaderData(Ref<Effect> shader, string passName) : base(shader, passName){
//Set default delegates if not set by user
if(UpdatePrimary == null) {
UpdatePrimary = new Func<Entity, DrawData?, Color>(delegate(Entity e, DrawData? drawData) {
return primary;
});
}
if(UpdateSecondary == null) {
UpdateSecondary = new Func<Entity, DrawData?, Color>(delegate(Entity e, DrawData? drawData) {
return secondary;
});
}
//Set default delegate if not set by user
if(SecondaryShader == null) {
SecondaryShader = new Func<Entity, ArmorShaderData>(delegate(Entity e) {
return this;
Expand All @@ -67,8 +57,8 @@ public ModArmorShaderData(Ref<Effect> shader, string passName) : base(shader, pa

public override void Apply(Entity e, DrawData? drawData)
{
UseColor(UpdatePrimary(e, drawData));
UseSecondaryColor(UpdateSecondary(e, drawData));
if(UpdatePrimary != null) UseColor(UpdatePrimary(e, drawData));
if(UpdateSecondary != null) UseSecondaryColor(UpdateSecondary(e, drawData));
UseSaturation(saturation);
if(image != null) ShaderReflections.SetImage(this as ArmorShaderData, new Ref<Texture2D>(image));
SwapProgram(_passName);
Expand All @@ -86,5 +76,22 @@ public override ArmorShaderData GetSecondaryShader(Entity entity)
public string GetPassName() {
return _passName;
}

//New methods to replace the old ones, no more "as ModArmorShaderData" being appended all the time
new public ModArmorShaderData UseColor(Color color)
{
return base.UseColor(color) as ModArmorShaderData;
}

new public ModArmorShaderData UseSecondaryColor(Color color)
{
return base.UseSecondaryColor(color) as ModArmorShaderData;
}

new public ModArmorShaderData UseSaturation(float sat)
{
return base.UseSaturation(sat) as ModArmorShaderData;
}

}
}
2 changes: 1 addition & 1 deletion Shaders/NPCShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ShaderLib.Shaders
{
public class NPCShader : GlobalNPC
{
public static List<Func<int, NPC, int>> hooks;
internal static List<Func<int, NPC, int>> hooks;
public override bool PreDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor)
{
int shaderID = 0;
Expand Down
2 changes: 1 addition & 1 deletion Shaders/ProjectileShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ProjectileShader : GlobalProjectile
/// <summary>
/// List of hooks for changing shader on projectiles.
/// </summary>
public static List<Func<int, Projectile, int>> hooks;
internal static List<Func<int, Projectile, int>> hooks;

public override bool PreDraw(Projectile projectile, SpriteBatch spriteBatch, Color lightColor)
{
Expand Down
4 changes: 2 additions & 2 deletions Shaders/ShaderIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public static int ReadArmorShader(BinaryReader reader){
/// </summary>
/// <returns>The extracted shader ID.</returns>
/// <param name="compound">TagCompound to extract the shader from.</param>
public static int LoadShader(TagCompound compound) {
var savedShader = compound.GetCompound("SavedShader");
public static int LoadShader(TagCompound savedShader) {
string shaderType = savedShader.GetString("Type");

//Load vanilla shader; easily identified by unchanging shader ID
Expand Down Expand Up @@ -179,6 +178,7 @@ public static TagCompound SaveShader(int shaderID) {
if(shaderID <= ShaderLibMod.maxVanillaID) {
compound.Set("Type", "Vanilla");
compound.Set("ShaderID", shaderID);
ErrorLogger.Log("Saving shader of type Vanilla");
}

//Save mod name and shader name if modded shader
Expand Down

0 comments on commit 4e4b3fd

Please sign in to comment.