Commented and Updated Logging
This commit is contained in:
parent
052378e377
commit
10ebcbd822
|
@ -8,19 +8,29 @@ using UnityEngine;
|
|||
|
||||
namespace CrosshairMod
|
||||
{
|
||||
// Button Wrapper class, utilizing GUI.Button()
|
||||
/*
|
||||
* A button wrapper class that is used right now as I don't have access to
|
||||
* the games buttons. Since UnityEngine.GUI only has a function to draw Buttons,
|
||||
* I made this class for easy handling.
|
||||
*/
|
||||
class GUIButton
|
||||
{
|
||||
|
||||
// Position / Dimension of the Button.
|
||||
public Vector2 position = new Vector2(0, 0);
|
||||
public Vector2 dimensions = new Vector2(0, 0);
|
||||
|
||||
// Label of the Button
|
||||
public string label = "";
|
||||
|
||||
// OnClick event
|
||||
public event EventHandler OnClick;
|
||||
|
||||
// Initialize Button
|
||||
public GUIButton(uint x, uint y, uint width, uint height, string label)
|
||||
{
|
||||
Logging.Log("Button Constructor");
|
||||
Logging.Debug.Log("Button Constructor");
|
||||
|
||||
// Assign position, dimension and label
|
||||
this.position = new Vector2(x, y);
|
||||
this.dimensions = new Vector2(width, height);
|
||||
this.label = label;
|
||||
|
@ -31,8 +41,11 @@ namespace CrosshairMod
|
|||
// Empty
|
||||
}
|
||||
|
||||
// Updates and Draws the Button.
|
||||
// TODO: Seperate PressChecking and Rendering in order to have Disabled Buttons
|
||||
public void Update()
|
||||
{
|
||||
// Get if the Button was pressed and invoke OnClick event accordingly
|
||||
bool buttonPressed = GUI.Button(new Rect(position, dimensions), label);
|
||||
if (buttonPressed)
|
||||
OnClick?.Invoke(this, EventArgs.Empty);
|
||||
|
|
|
@ -8,10 +8,17 @@ using UnityEngine;
|
|||
|
||||
namespace CrosshairMod
|
||||
{
|
||||
/* The class responsible for drawing/creating/administrating the crosshair.
|
||||
*
|
||||
* This is where settings are applied to the crosshair.
|
||||
*/
|
||||
static class Crosshair
|
||||
{
|
||||
// Crosshair Texture / Style
|
||||
private static Texture2D m_texture = new Texture2D(0, 0);
|
||||
private static GUIStyle m_style;
|
||||
|
||||
// If crosshair is visible or hidden
|
||||
private static bool m_enabled = true;
|
||||
private static bool m_validState = true;
|
||||
|
||||
|
@ -112,8 +119,11 @@ namespace CrosshairMod
|
|||
m_style.normal.background = m_texture;
|
||||
}
|
||||
|
||||
// Render the Crosshair
|
||||
public static void Render()
|
||||
{
|
||||
// If the crosshair is faulty, then don't execute this code
|
||||
// This is here to stop the Logger from spamming the console.
|
||||
if (m_validState)
|
||||
{
|
||||
if (InvalidCrosshair())
|
||||
|
@ -122,13 +132,14 @@ namespace CrosshairMod
|
|||
return;
|
||||
}
|
||||
|
||||
// Don't draw a hidden crosshair. Duh.
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check Crosshair State
|
||||
private static bool InvalidCrosshair()
|
||||
{
|
||||
// Check if the texture is bigger than (0, 0) to see if it was initialized.
|
||||
|
|
|
@ -8,45 +8,65 @@ using UnityEngine;
|
|||
|
||||
namespace CrosshairMod
|
||||
{
|
||||
|
||||
/* A class that handles the Crosshair GUI.
|
||||
*
|
||||
* Contains all Buttons, Sliders etc. that are able to modify the crosshair.
|
||||
*/
|
||||
static class Interface
|
||||
{
|
||||
// Saves wether the interface is visible or not
|
||||
private static bool m_visible = false;
|
||||
|
||||
// Stores all Buttons used in the interface.
|
||||
// TODO: Create function to easily add Buttons
|
||||
private static Dictionary<string, GUIButton> m_buttons = new Dictionary<string, GUIButton>();
|
||||
|
||||
// Values of the RGBA Sliders
|
||||
private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue;
|
||||
|
||||
// Texture and Styles for the GUI background
|
||||
private static Texture2D m_background = new Texture2D(1, 1);
|
||||
private static GUIStyle m_style = new GUIStyle();
|
||||
|
||||
|
||||
// Position ind dimension of the GUI background
|
||||
private static Vector2 m_position;
|
||||
private static Vector2 m_dimension;
|
||||
|
||||
// Initializes all Buttons, gives them their function etc
|
||||
public static void Init()
|
||||
{
|
||||
// Set dimension to 0.25 of the screen width/height
|
||||
m_dimension = new Vector2(Screen.width / 4, Screen.height / 4);
|
||||
// Center the interface
|
||||
m_position = new Vector2((Screen.width - m_dimension.x) / 2, (Screen.height - m_dimension.y) / 2);
|
||||
|
||||
// Create Texture that is dark gray and slightly see-through
|
||||
m_background.SetPixel(0, 0, new Color(0.4f, 0.4f, 0.4f, 0.4f));
|
||||
m_background.wrapMode = TextureWrapMode.Repeat;
|
||||
m_background.Apply();
|
||||
|
||||
// Apply Texture to Style
|
||||
m_style.normal.background = m_background;
|
||||
|
||||
// Create Crosshair Visibilty Button
|
||||
// TODO: Make Button change label depending on Crosshait State (e.g. if it's hidden the label should be "Show", and vice versa)
|
||||
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(); };
|
||||
|
||||
// Create Crosshair Size +/- Buttons
|
||||
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); };
|
||||
|
||||
// Create Crosshair Thickness +/- Buttons
|
||||
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); };
|
||||
|
||||
// Assign setting values to sliders
|
||||
rSliderValue = Settings.GetValue("crosshairColorRed");
|
||||
gSliderValue = Settings.GetValue("crosshairColorGreen");
|
||||
bSliderValue = Settings.GetValue("crosshairColorBlue");
|
||||
|
@ -64,11 +84,15 @@ namespace CrosshairMod
|
|||
{
|
||||
if(m_visible)
|
||||
{
|
||||
// Draw the background
|
||||
GUI.Label(new Rect(m_position, m_dimension), m_background, m_style);
|
||||
|
||||
// Draw the Length and Thickness Labels
|
||||
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"));
|
||||
|
||||
// Draw the RGBA Labels and Sliders
|
||||
// TODO: Find better way to handle Sliders. Maybe make some InputInterface class that handles Buttons/Sliders etc
|
||||
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);
|
||||
|
||||
|
@ -81,6 +105,7 @@ namespace CrosshairMod
|
|||
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);
|
||||
|
||||
// Set crosshair Colour after getting slider values
|
||||
Crosshair.SetColor(rSliderValue, gSliderValue, bSliderValue, aSliderValue);
|
||||
|
||||
// Update Buttons
|
||||
|
@ -88,6 +113,7 @@ namespace CrosshairMod
|
|||
}
|
||||
}
|
||||
|
||||
// Calls the Update function on all Buttons to check if they were pressed, and execute their Action
|
||||
private static void HandleButtons()
|
||||
{
|
||||
foreach(KeyValuePair<string, GUIButton> pair in m_buttons)
|
||||
|
|
|
@ -13,18 +13,50 @@ namespace CrosshairMod
|
|||
// However, I prefer this over Debug.Log() since it doesn't include a stacktrace (Except for Errors)
|
||||
public static class Logging
|
||||
{
|
||||
// The Prefix that gets put in front of every Log
|
||||
public const string PREFIX = "[CROSSHAIRMOD]";
|
||||
|
||||
// A kind of sub-class that is used to Log messages that will only appear
|
||||
// when the User installs a Debug build of the Mod. Release versions will
|
||||
// not log Debug messages this way.
|
||||
public static class Debug
|
||||
{
|
||||
public static void Log(string message)
|
||||
{
|
||||
#if DEBUG
|
||||
Logging.Log(message);
|
||||
#endif // DEBUG
|
||||
}
|
||||
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
#if DEBUG
|
||||
Logging.LogError(message);
|
||||
#endif // DEBUG
|
||||
}
|
||||
|
||||
public static void LogError(string message)
|
||||
{
|
||||
#if DEBUG
|
||||
Debug.Log(PREFIX + "Error: " + message);
|
||||
#endif // DEBUG
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Logs information
|
||||
public static void Log(string message)
|
||||
{
|
||||
Console.WriteLine(PREFIX + "Info: " + message);
|
||||
}
|
||||
|
||||
// Logs warnings
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
Console.WriteLine(PREFIX + "Warning: " + message);
|
||||
}
|
||||
|
||||
// Logs errors
|
||||
public static void LogError(string message)
|
||||
{
|
||||
Debug.Log(PREFIX + "Error: " + message);
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
* the game Blackwake.
|
||||
*
|
||||
* @author Lauchmelder
|
||||
* @version v0.2
|
||||
* @version v0.3
|
||||
*/
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -15,8 +16,16 @@ using UnityEngine;
|
|||
|
||||
namespace CrosshairMod
|
||||
{
|
||||
|
||||
/*
|
||||
* This is the Main class that is responsible for
|
||||
* handling initializing and updating the components
|
||||
* of the crosshair mod.
|
||||
*/
|
||||
public class Main : MonoBehaviour
|
||||
{
|
||||
// Define Hotkeys for Menu and Crosshair Toggle
|
||||
// TODO: Make Hotkeys editable for the User
|
||||
private const string MENU_OPEN_KEY = "H";
|
||||
private const string CH_TOGGLE_KEY = "J";
|
||||
|
||||
|
@ -31,16 +40,19 @@ namespace CrosshairMod
|
|||
Interface.Init();
|
||||
}
|
||||
|
||||
// This gets called on every GUI Update (Can be multiple tiems per Frame)
|
||||
void OnGUI()
|
||||
{
|
||||
// Check for Key press
|
||||
// Check for Key presses
|
||||
if(Event.current.Equals(Event.KeyboardEvent(MENU_OPEN_KEY)))
|
||||
{
|
||||
// Toggle Crosshair GUI
|
||||
Interface.Toggle();
|
||||
}
|
||||
|
||||
if (Event.current.Equals(Event.KeyboardEvent(CH_TOGGLE_KEY)))
|
||||
{
|
||||
// Toggle Crosshair
|
||||
Crosshair.Toggle();
|
||||
}
|
||||
|
||||
|
@ -50,11 +62,12 @@ namespace CrosshairMod
|
|||
Crosshair.Render();
|
||||
}
|
||||
|
||||
// Will be called when the application is closed
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
// Save settings
|
||||
Settings.SaveSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett");
|
||||
Logging.Log("Saved Settings");
|
||||
Logging.Debug.Log("Saved Settings");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,14 +6,19 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace CrosshairMod
|
||||
{
|
||||
/*
|
||||
* The class that is responsible for loading and storing all the
|
||||
* necessary settings. There is much room for improvement.
|
||||
*/
|
||||
static class Settings
|
||||
{
|
||||
// Initialize Settings dictionary
|
||||
private static Dictionary<string, int> m_settings = new Dictionary<string, int>();
|
||||
|
||||
// Load settings from file
|
||||
public static void LoadSettings(string filepath)
|
||||
{
|
||||
Logging.Log("Accessing Settings at " + filepath);
|
||||
Logging.Debug.Log("Accessing Settings at " + filepath);
|
||||
|
||||
// Try to read file contents into string
|
||||
string settings = "";
|
||||
|
@ -43,7 +48,7 @@ namespace CrosshairMod
|
|||
m_settings.Add(vals[0], Int32.Parse(vals[1])); // Store key and value in settings dictionary
|
||||
}
|
||||
|
||||
Logging.Log("Successfully loaded settings!");
|
||||
Logging.Debug.Log("Successfully loaded settings!");
|
||||
}
|
||||
|
||||
// Converts the dictionary to a sett file
|
||||
|
@ -67,10 +72,10 @@ namespace CrosshairMod
|
|||
m_settings.Add(key, value);
|
||||
}
|
||||
|
||||
// Changes a settings value
|
||||
// Changes a settings value, and adds it if specified
|
||||
public static void SetSetting(string key, int newVal, bool addIfDoesntExist = false)
|
||||
{
|
||||
|
||||
// If the setting doesn't exist, either add and set it, or print a Debug.Warning
|
||||
if(!m_settings.ContainsKey(key))
|
||||
{
|
||||
if (!addIfDoesntExist)
|
||||
|
@ -81,7 +86,7 @@ namespace CrosshairMod
|
|||
else
|
||||
{
|
||||
AddSetting(key, newVal);
|
||||
Logging.LogWarning("Tried to change a setting with key \"" + key + "\" that doesn't exist. It has been added now.");
|
||||
Logging.Debug.LogWarning("Tried to change a setting with key \"" + key + "\" that doesn't exist. It has been added now.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,7 +110,7 @@ namespace CrosshairMod
|
|||
else
|
||||
{
|
||||
AddSetting(key, initialValue);
|
||||
Logging.LogWarning("Tried to access unknown setting: \"" + key + "\". A new setting with this key was created.");
|
||||
Logging.Debug.LogWarning("Tried to access unknown setting: \"" + key + "\". A new setting with this key was created.");
|
||||
return initialValue;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue