diff --git a/CrosshairMod/Crosshair.cs b/CrosshairMod/Crosshair.cs index 7d36e26..1b6c7ee 100644 --- a/CrosshairMod/Crosshair.cs +++ b/CrosshairMod/Crosshair.cs @@ -13,11 +13,44 @@ namespace CrosshairMod private static Texture2D m_texture = new Texture2D(0, 0); private static GUIStyle m_style; private static bool m_enabled = true; + private static bool m_validState = true; // Toggles visibilty of the crosshair public static void Toggle() { m_enabled = !m_enabled; + Settings.SetSetting("crosshairVisible", 1, true); + } + + // Change Color + public static void SetColor(int r, int g, int b, int a) + { + Settings.SetSetting("crosshairColorRed", r); + Settings.SetSetting("crosshairColorGreen", g); + Settings.SetSetting("crosshairColorBlue", b); + Settings.SetSetting("crosshairColorAlpha", a); + + Create(); + } + + // Change Size + public static void ChangeSize(int difference) + { + int currentLength = Settings.GetValue("crosshairLength"); + Settings.SetSetting("crosshairLength", currentLength + difference); + + // Re-create crosshair with new settings + Create(); + } + + // Change Thickness + public static void ChangeThickness(int difference) + { + int currentThickness = Settings.GetValue("crosshairThickness"); + Settings.SetSetting("crosshairThickness", currentThickness + difference); + + // Re-create crosshair with new settings + Create(); } // This must be called, or else no crosshair will be rendered @@ -25,12 +58,12 @@ namespace CrosshairMod { // Creates a crosshair texture // Assign dictionary values to variables - int m_crosshairLength = Settings.GetValue("crosshairLength"); - int m_crosshairThickness = Settings.GetValue("crosshairThickness"); - int m_crosshairColorRed = Settings.GetValue("crosshairColorRed"); - int m_crosshairColorGreen = Settings.GetValue("crosshairColorGreen"); - int m_crosshairColorBlue = Settings.GetValue("crosshairColorBlue"); - int m_crosshairColorAlpha = Settings.GetValue("crosshairColorAlpha"); + int m_crosshairLength = Settings.GetValue("crosshairLength", true, 15); + int m_crosshairThickness = Settings.GetValue("crosshairThickness", true, 3); + int m_crosshairColorRed = Settings.GetValue("crosshairColorRed", true, 255); + int m_crosshairColorGreen = Settings.GetValue("crosshairColorGreen", true, 94); + int m_crosshairColorBlue = Settings.GetValue("crosshairColorBlue", true, 244); + int m_crosshairColorAlpha = Settings.GetValue("crosshairColorAlpha", true, 255); // Construct color object from RGBA values Color m_crosshairColor = new Color(m_crosshairColorRed / 255f, @@ -81,22 +114,26 @@ namespace CrosshairMod public static void Render() { - if(InvalidCrosshair()) + if (m_validState) { - Logging.LogWarning("Crosshair was either not initialized, or has an invalid size of (0, 0). Check your settings file and adjust your settings accordingly"); - return; - } + if (InvalidCrosshair()) + { + Logging.LogWarning("Crosshair was either not initialized, or has an invalid size of (0, 0). Check your settings file and adjust your settings accordingly"); + return; + } - if (m_enabled) - GUI.Label(new Rect(Screen.width / 2 - m_texture.width / 2, Screen.height / 2 - m_texture.height / 2, m_texture.width, m_texture.height), - m_texture, m_style); + if (m_enabled) + GUI.Label(new Rect(Screen.width / 2 - m_texture.width / 2, Screen.height / 2 - m_texture.height / 2, m_texture.width, m_texture.height), + m_texture, m_style); + } } private static bool InvalidCrosshair() { // Check if the texture is bigger than (0, 0) to see if it was initialized. - return (m_texture.width == 0 && m_texture.height == 0); + m_validState = (m_texture.width != 0 && m_texture.height != 0); + return !m_validState; } } } diff --git a/CrosshairMod/CrosshairMod.csproj b/CrosshairMod/CrosshairMod.csproj index fda3ef3..1f918ef 100644 --- a/CrosshairMod/CrosshairMod.csproj +++ b/CrosshairMod/CrosshairMod.csproj @@ -46,6 +46,7 @@ + diff --git a/CrosshairMod/Interface.cs b/CrosshairMod/Interface.cs new file mode 100644 index 0000000..d8e4634 --- /dev/null +++ b/CrosshairMod/Interface.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using UnityEngine; + +namespace CrosshairMod +{ + static class Interface + { + private static bool m_visible = false; + + private static Dictionary m_buttons = new Dictionary(); + private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue; + + private static Texture2D m_background = new Texture2D(1, 1); + private static GUIStyle m_style = new GUIStyle(); + + + private static Vector2 m_position; + private static Vector2 m_dimension; + + // Initializes all Buttons, gives them their function etc + public static void Init() + { + m_dimension = new Vector2(Screen.width / 4, Screen.height / 4); + m_position = new Vector2((Screen.width - m_dimension.x) / 2, (Screen.height - m_dimension.y) / 2); + + m_background.SetPixel(0, 0, new Color(0.4f, 0.4f, 0.4f, 0.4f)); + m_background.wrapMode = TextureWrapMode.Repeat; + m_background.Apply(); + + m_style.normal.background = m_background; + + m_buttons.Add("visibility", new GUIButton((uint)m_position.x + 20, (uint)m_position.y + 20, 200, 30, "Toggle Crosshair")); + m_buttons["visibility"].OnClick += (object sender, EventArgs e) => { Crosshair.Toggle(); }; + + m_buttons.Add("size-", new GUIButton((uint)m_position.x + 20, (uint)m_position.y + 60, 30, 30, "-")); + m_buttons.Add("size+", new GUIButton((uint)m_position.x + 190, (uint)m_position.y + 60, 30, 30, "+")); + m_buttons["size-"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeSize(-1); }; + m_buttons["size+"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeSize(+1); }; + + m_buttons.Add("thick-", new GUIButton((uint)m_position.x + 20, (uint)m_position.y + 100, 30, 30, "-")); + m_buttons.Add("thick+", new GUIButton((uint)m_position.x + 190, (uint)m_position.y + 100, 30, 30, "+")); + m_buttons["thick-"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeThickness(-1); }; + m_buttons["thick+"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeThickness(+1); }; + + rSliderValue = Settings.GetValue("crosshairColorRed"); + gSliderValue = Settings.GetValue("crosshairColorGreen"); + bSliderValue = Settings.GetValue("crosshairColorBlue"); + aSliderValue = Settings.GetValue("crosshairColorAlpha"); + } + + // Displays / Hides the menu + public static void Toggle() + { + m_visible = !m_visible; + } + + // Renders the Panel, but also handles Updating the buttons + public static void Render() + { + if(m_visible) + { + GUI.Label(new Rect(m_position, m_dimension), m_background, m_style); + + GUI.Label(new Rect(m_position.x + 60, m_position.y + 70, 120, 30), "Length: " + Settings.GetValue("crosshairLength")); + GUI.Label(new Rect(m_position.x + 60, m_position.y + 110, 120, 30), "Thickness: " + Settings.GetValue("crosshairThickness")); + + GUI.Label(new Rect(m_position.x + m_dimension.x / 2 + 20, m_position.y + 30, 200, 30), "R: " + rSliderValue); + rSliderValue = (int)GUI.HorizontalSlider(new Rect(m_position.x + m_dimension.x / 2 + 60, m_position.y + 20, 200, 30), (int)rSliderValue, 0f, 255f); + + GUI.Label(new Rect(m_position.x + m_dimension.x / 2 + 20, m_position.y + 70, 200, 30), "G: " + gSliderValue); + gSliderValue = (int)GUI.HorizontalSlider(new Rect(m_position.x + m_dimension.x / 2 + 60, m_position.y + 60, 200, 30), (int)gSliderValue, 0f, 255f); + + GUI.Label(new Rect(m_position.x + m_dimension.x / 2 + 20, m_position.y + 110, 200, 30), "B: " + bSliderValue); + bSliderValue = (int)GUI.HorizontalSlider(new Rect(m_position.x + m_dimension.x / 2 + 60, m_position.y + 100, 200, 30), (int)bSliderValue, 0f, 255f); + + GUI.Label(new Rect(m_position.x + m_dimension.x / 2 + 20, m_position.y + 150, 200, 30), "A: " + aSliderValue); + aSliderValue = (int)GUI.HorizontalSlider(new Rect(m_position.x + m_dimension.x / 2 + 60, m_position.y + 140, 200, 30), (int)aSliderValue, 0f, 255f); + + Crosshair.SetColor(rSliderValue, gSliderValue, bSliderValue, aSliderValue); + + // Update Buttons + HandleButtons(); + } + } + + private static void HandleButtons() + { + foreach(KeyValuePair pair in m_buttons) + { + pair.Value.Update(); + } + } + } +} diff --git a/CrosshairMod/Main.cs b/CrosshairMod/Main.cs index 3c07673..f6fb2ea 100644 --- a/CrosshairMod/Main.cs +++ b/CrosshairMod/Main.cs @@ -17,16 +17,8 @@ namespace CrosshairMod { public class Main : MonoBehaviour { - // Initialize State checker. If this is false, the crosshair won't be drawn - // This is a temporary fix to stop this mod from spamming errors in the log - private bool m_validState = true; - private bool m_renderCrosshair = true; - - // Reads settings file and sets all variables - - - - + private const string MENU_OPEN_KEY = "H"; + private const string CH_TOGGLE_KEY = "J"; // This will be executed first void Start() @@ -35,19 +27,34 @@ namespace CrosshairMod Settings.LoadSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett"); // Create Crosshair Crosshair.Create(); - - // Add function to Button - crosshairButton.OnClick += (object sender, EventArgs e) => { Crosshair.Toggle(); }; + // Create Panel + Interface.Init(); } - private GUIButton crosshairButton = new GUIButton(200, 10, 100, 20, "Toggle Crosshair"); - void OnGUI() { - crosshairButton.Update(); + // Check for Key press + if(Event.current.Equals(Event.KeyboardEvent(MENU_OPEN_KEY))) + { + Interface.Toggle(); + } + if (Event.current.Equals(Event.KeyboardEvent(CH_TOGGLE_KEY))) + { + Crosshair.Toggle(); + } + + //Render GUI + Interface.Render(); //Render Crosshair Crosshair.Render(); } + + void OnApplicationQuit() + { + // Save settings + Settings.SaveSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett"); + Logging.Log("Saved Settings"); + } } } diff --git a/CrosshairMod/Settings.cs b/CrosshairMod/Settings.cs index f4e283c..7bf7b2e 100644 --- a/CrosshairMod/Settings.cs +++ b/CrosshairMod/Settings.cs @@ -46,20 +46,69 @@ namespace CrosshairMod Logging.Log("Successfully loaded settings!"); } - public static void writeSettings(string filepath) + // Converts the dictionary to a sett file + public static void SaveSettings(string filepath) { - //TODO: Implement saving + string filecontent = ""; + foreach(KeyValuePair pair in m_settings) + { + filecontent += (pair.Key + "=" + pair.Value + "\n"); + } + + System.IO.File.WriteAllText(filepath, filecontent); + } + + // Adds a setting to the settings + public static void AddSetting(string key, int value) + { + if (m_settings.ContainsKey(key)) + return; + + m_settings.Add(key, value); + } + + // Changes a settings value + public static void SetSetting(string key, int newVal, bool addIfDoesntExist = false) + { + + if(!m_settings.ContainsKey(key)) + { + if (!addIfDoesntExist) + { + Logging.LogError("Tried to change a setting with key \"" + key + "\" that doesn't exist."); + return; + } + else + { + AddSetting(key, newVal); + Logging.LogWarning("Tried to change a setting with key \"" + key + "\" that doesn't exist. It has been added now."); + } + } + + m_settings[key] = newVal; } // Tries to return the value belonging to a certain key // If the specified key doesn't exist, it returns 0 and logs an error - public static int GetValue(string key) + // One can also specify that the setting should be created with some initial value + public static int GetValue(string key, bool addIfDoesntExist = false, int initialValue = 0) { int value = 0; bool valExists = m_settings.TryGetValue(key, out value); if (!valExists) - Logging.LogError("Tried to access unknown setting: \"" + key + "\". Check your chSettings.sett for errors."); + { + if (!addIfDoesntExist) + { + Logging.LogError("Tried to access unknown setting: \"" + key + "\". Check your chSettings.sett for errors."); + } + else + { + AddSetting(key, initialValue); + Logging.LogWarning("Tried to access unknown setting: \"" + key + "\". A new setting with this key was created."); + return initialValue; + } + } return value; } diff --git a/CrosshairMod/bin/Release/CrosshairMod.dll b/CrosshairMod/bin/Release/CrosshairMod.dll new file mode 100644 index 0000000..1080c7e Binary files /dev/null and b/CrosshairMod/bin/Release/CrosshairMod.dll differ