a mod to change the terraria mouse cursor so its color is based on the background, but inverted so it is very visible
give it a name in the namespace
Assistant
Creating a TModLoader mod to dynamically adjust the Terraria mouse cursor color based on the background while inverting it for better visibility is a great idea to enhance user experience. Below is a step-by-step guide along with the necessary code to create this mod for Terraria version 1.4.4.
Mod Overview
Namespace Name:DynamicCursorMod
Mod Name:DynamicCursor
Functionality: The mod changes the mouse cursor color by sampling the background color beneath the cursor, inverting it to ensure high visibility.
Prerequisites
TModLoader Installed: Ensure you have TModLoader installed and set up correctly for Terraria 1.4.4.
Development Environment: Set up your preferred C# development environment (e.g., Visual Studio).
Step-by-Step Implementation
Create the Mod Structure:
In your Mods folder, create a new folder named DynamicCursor.
Inside the DynamicCursor folder, create a file named DynamicCursor.cs.
Add the Following Code to DynamicCursor.cs:
csharp
using Terraria;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.UI;
using System;
namespace DynamicCursorMod
{
public class DynamicCursor : Mod
{
public override void Load()
{
if (!Main.dedServ) // Ensure this runs only on the client
{
// Subscribe to the Update hooks
On_Main.DrawCursor += Main_DrawCursor;
}
}
private bool Main_DrawCursor(On_Main.orig_DrawCursor orig)
{
// Get cursor position
Vector2 cursorPosition = new Vector2(Main.mouseX, Main.mouseY);
// Clamp cursor position within screen bounds
cursorPosition.X = Math.Clamp(cursorPosition.X, 0, Main.screenWidth);
cursorPosition.Y = Math.Clamp(cursorPosition.Y, 0, Main.screenHeight);
// Convert screen coordinates to world coordinates
Vector2 worldPosition = Main.MouseWorld;
// Get the tile at the cursor position
int tileX = (int)(worldPosition.X / 16f);
int tileY = (int)(worldPosition.Y / 16f);
// Ensure tile indices are within bounds
if (tileX < 0 || tileX >= Main.maxTilesX || tileY < 0 || tileY >= Main.maxTilesY)
{
// Default cursor color if out of bounds
Main.cursorColor = Color.White;
}
else
{
// Get the color of the tile
Tile tile = Main.tile[tileX, tileY];
Color tileColor = Lighting.GetColor(tileX, tileY);
// Invert the color
Color invertedColor = new Color(255 - tileColor.R, 255 - tileColor.G, 255 - tileColor.B);
// Set the cursor color
Main.cursorColor = invertedColor;
}
// Proceed with the original cursor drawing
orig();
return false;
}
public override void Unload()
{
if (!Main.dedServ)
{
// Unsubscribe from the Update hooks when unloading
On_Main.DrawCursor -= Main_DrawCursor;
}
}
}
}
Explanation of the Code
Namespace and Class Definition:
The mod is encapsulated within the DynamicCursorMod namespace.
The main mod class DynamicCursor inherits from Mod.
Load Method:
The Load method ensures that the code runs only on the client side (not on the server).
It hooks into Terraria's Main.DrawCursor method by using the On_Main.DrawCursor delegate. This allows us to inject custom behavior when the cursor is drawn.
Main_DrawCursor Method:
Cursor Position: Retrieves the current cursor position in screen coordinates.
Clamping: Ensures the cursor position does not exceed screen boundaries.
World Position: Converts screen coordinates to world coordinates to know which tile the cursor is over.
Tile Retrieval: Determines which tile the cursor is currently hovering over and retrieves its color affected by lighting (Lighting.GetColor).
Color Inversion: Inverts the tile's color to ensure the cursor remains visible against varying backgrounds.
Setting Cursor Color: Assigns the inverted color to Main.cursorColor, which affects the cursor's rendering color.
Original Drawing: Calls the original DrawCursor method to handle the actual rendering with the modified color.
Unload Method:
Ensures that the event hook is properly removed when the mod is unloaded to prevent potential issues or conflicts.
Additional Considerations
Performance: Sampling the tile color and inverting it each frame could have performance implications, especially in areas with complex lighting. However, for most use cases, the impact should be negligible.
Cursor Visibility: Depending on game elements, you might want to add additional logic to handle extreme scenarios where inversion might not provide sufficient contrast. For instance, implementing a threshold or fallback color.
Customization: You can extend this mod to allow users to customize inversion behavior or thresholds via a configuration file or mod settings UI.
Building and Testing the Mod
Build the Mod:
Open your development environment and compile the mod. Ensure there are no compilation errors.
Place the Mod in the Mods Folder:
After a successful build, place the compiled .tmod file into the Mods folder of your TModLoader installation.
Enable and Test:
Launch TModLoader, enable the DynamicCursor mod, and start Terraria.
Move your cursor around different backgrounds to observe the dynamic color changes ensuring high visibility.
Conclusion
With this mod, the cursor dynamically adjusts its color based on the background, inverted to maintain visibility across various terrains and conditions in Terraria. You can further enhance this mod by adding configuration options or extending its functionality based on your preferences.
Feel free to modify and expand upon this basic implementation to better fit your needs!