Commented everything properly

This commit is contained in:
Robert 2019-08-04 15:50:04 +02:00
parent 147f53a52d
commit 85a4c7b254
8 changed files with 188 additions and 117 deletions

View file

@ -8,34 +8,31 @@ using UnityEngine;
namespace CrosshairMod namespace CrosshairMod
{ {
/* The class responsible for drawing/creating/administrating the crosshair. /// <summary>
* /// Contains Crosshair information and draws/creates it
* This is where settings are applied to the crosshair. /// </summary>
*/
static class Crosshair static class Crosshair
{ {
// Crosshair Texture / Style /// <summary>
/// Graphics information for the crosshair
/// </summary>
private static Texture2D m_texture = new Texture2D(0, 0); private static Texture2D m_texture = new Texture2D(0, 0);
private static GUIStyle m_style; private static GUIStyle m_style;
// If crosshair is visible or hidden
private static bool m_enabled = true; private static bool m_enabled = true;
private static bool m_validState = true; private static bool m_validState = true;
// Toggles visibilty of the crosshair
public static void Toggle() public static void Toggle()
{ {
m_enabled = !m_enabled; m_enabled = !m_enabled;
Settings.SetSetting("crosshairVisible", 1, true); Settings.SetSetting("crosshairVisible", 1, true);
} }
// Returns wether the crosshair is enabled
public static bool Enabled() public static bool Enabled()
{ {
return m_enabled; return m_enabled;
} }
// Change Color
public static void SetColor(int r, int g, int b, int a) public static void SetColor(int r, int g, int b, int a)
{ {
Settings.SetSetting("crosshairColorRed", r); Settings.SetSetting("crosshairColorRed", r);
@ -46,7 +43,6 @@ namespace CrosshairMod
Create(); Create();
} }
// Change Size
public static void ChangeSize(int difference) public static void ChangeSize(int difference)
{ {
int currentLength = Settings.GetValue("crosshairLength"); int currentLength = Settings.GetValue("crosshairLength");
@ -56,7 +52,6 @@ namespace CrosshairMod
Create(); Create();
} }
// Change Thickness
public static void ChangeThickness(int difference) public static void ChangeThickness(int difference)
{ {
int currentThickness = Settings.GetValue("crosshairThickness"); int currentThickness = Settings.GetValue("crosshairThickness");
@ -66,7 +61,9 @@ namespace CrosshairMod
Create(); Create();
} }
// This must be called, or else no crosshair will be rendered /// <summary>
/// Creates a new crosshair texture with the current settings
/// </summary>
public static void Create() public static void Create()
{ {
// Creates a crosshair texture // Creates a crosshair texture
@ -125,7 +122,6 @@ namespace CrosshairMod
m_style.normal.background = m_texture; m_style.normal.background = m_texture;
} }
// Render the Crosshair
public static void Render() public static void Render()
{ {
// If the crosshair is faulty, then don't execute this code // If the crosshair is faulty, then don't execute this code

View file

@ -8,45 +8,65 @@ using UnityEngine;
namespace CrosshairMod namespace CrosshairMod
{ {
/// <summary>
/* A class that handles the Crosshair GUI. /// Handles and contains every Object needed for the interface
* /// </summary>
* Contains all Buttons, Sliders etc. that are able to modify the crosshair.
*/
// TODO: Create GUILayout.Window to make a less crappy version of the settings window
static class Interface static class Interface
{ {
// Saves wether the interface is visible or not
private static bool m_visible = false; private static bool m_visible = false;
// Stores all Buttons used in the interface. /// <summary>
/// A list of all Objects (That includes buttons, sliders etc) inside the GUI Window
/// This makes it easy to add more components if it is needed.
/// </summary>
private static List<InputObject> m_inputs = new List<InputObject>(); private static List<InputObject> m_inputs = new List<InputObject>();
// TODO: Replace with dictionary, the whole concept of having IDs inside the objects is stupid and unnecessary
// Values of the RGBA Sliders /// <summary>
/// RGBA Slider values
/// </summary>
private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue; private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue;
// Position ind dimension of the GUI background
private static Vector2 m_position; private static Vector2 m_position;
private static Vector2 m_dimension; private static Vector2 m_dimension;
// Creates a new button object and adds it to the List /// <summary>
/// Creates a new Button and adds it to the content list
/// </summary>
/// <param name="x">X-Position</param>
/// <param name="y">Y-position</param>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <param name="label">Text to be displayed on the button</param>
/// <param name="ID">Key of the Button</param>
/// <param name="onClickEvent">Action to be executed when the button is pressed</param>
private static void AddButton(float x, float y, float width, float height, string label, string ID, params EventHandler[] onClickEvent) private static void AddButton(float x, float y, float width, float height, string label, string ID, params EventHandler[] onClickEvent)
{ {
GUIButton buttonObj = new GUIButton(x, y, width, height, label, ID, onClickEvent); Input.Button buttonObj = new Input.Button(x, y, width, height, label, ID, onClickEvent);
m_inputs.Add(buttonObj); m_inputs.Add(buttonObj);
} }
// Creates a new slider object and adds it to the List /// <summary>
// Returns the index of the button /// Creates a new Slider and adds ot to the content list
/// </summary>
/// <param name="x">X-Position</param>
/// <param name="y">Y-Position/param>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <param name="min">Minimum value of the slider</param>
/// <param name="max">Maximum value of the slider</param>
/// <param name="init">Starting value of the slider</param>
/// <param name="ID">Key of the slider</param>
private static void AddSlider(float x, float y, float width, float height, float min, float max, float init, string ID) private static void AddSlider(float x, float y, float width, float height, float min, float max, float init, string ID)
{ {
GUISlider sliderObj = new GUISlider(x, y, width, height, min, max, init, ID); Input.Slider sliderObj = new Input.Slider(x, y, width, height, min, max, init, ID);
m_inputs.Add(sliderObj); m_inputs.Add(sliderObj);
} }
// Initializes all Buttons, gives them their function etc /// <summary>
/// Creates all needed input objects and initializes window
/// </summary>
public static void Init() public static void Init()
{ {
// Set dimension to 0.25 of the screen width/height // Set dimension to 0.25 of the screen width/height
@ -58,7 +78,7 @@ namespace CrosshairMod
// Create Crosshair Visibilty Button // Create Crosshair Visibilty Button
AddButton(20, 20, 200, 30, AddButton(20, 20, 200, 30,
(Crosshair.Enabled() ? "Hide Crosshair" : "Show Crosshair"), "Toggle", (object sender, EventArgs e) => { Crosshair.Toggle(); }, (Crosshair.Enabled() ? "Hide Crosshair" : "Show Crosshair"), "Toggle", (object sender, EventArgs e) => { Crosshair.Toggle(); },
(object sender, EventArgs e) => { GUIButton btn = (GUIButton)sender; btn.label = (Crosshair.Enabled() ? "Hide Crosshair" : "Show Crosshair"); }); (object sender, EventArgs e) => { Input.Button btn = (Input.Button)sender; btn.Label = (Crosshair.Enabled() ? "Hide Crosshair" : "Show Crosshair"); });
// Create Crosshair Size +/- Buttons // Create Crosshair Size +/- Buttons
AddButton(20, 60, 30, 30, AddButton(20, 60, 30, 30,
@ -84,20 +104,21 @@ namespace CrosshairMod
AddSlider(m_dimension.x / 2 + 60, 150, 200, 30, 0, 255, aSliderValue, "alpha"); AddSlider(m_dimension.x / 2 + 60, 150, 200, 30, 0, 255, aSliderValue, "alpha");
} }
// Displays / Hides the menu
public static void Toggle() public static void Toggle()
{ {
m_visible = !m_visible; m_visible = !m_visible;
} }
// Renders the window
public static void Render() public static void Render()
{ {
// TODO: Think about making this a GUILayout.ModalWindow
if (m_visible) if (m_visible)
GUI.Window(420, new Rect(m_position, m_dimension), RenderFunc, "Crosshair Settings"); GUI.Window(420, new Rect(m_position, m_dimension), RenderFunc, "Crosshair Settings");
} }
// Renders the Panel, but also handles Updating the buttons /// <summary>
/// Where actual rendering happens. Draws all objects and updated them
/// </summary>
private static void RenderFunc(int windowID) private static void RenderFunc(int windowID)
{ {
// Make Window draggable // Make Window draggable
@ -114,7 +135,7 @@ namespace CrosshairMod
GUI.Label(new Rect(m_dimension.x / 2 + 20, 150, 200, 30), "A: " + aSliderValue); GUI.Label(new Rect(m_dimension.x / 2 + 20, 150, 200, 30), "A: " + aSliderValue);
// Set crosshair Colour after getting slider values // Set crosshair Colour after getting slider values
IEnumerable<GUISlider> it = m_inputs.OfType<GUISlider>(); IEnumerable<Input.Slider> it = m_inputs.OfType<Input.Slider>();
rSliderValue = (int)it.First(slider => slider.ID == "red").Value; rSliderValue = (int)it.First(slider => slider.ID == "red").Value;
gSliderValue = (int)it.First(slider => slider.ID == "green").Value; gSliderValue = (int)it.First(slider => slider.ID == "green").Value;
bSliderValue = (int)it.First(slider => slider.ID == "blue").Value; bSliderValue = (int)it.First(slider => slider.ID == "blue").Value;
@ -123,12 +144,14 @@ namespace CrosshairMod
Crosshair.SetColor(rSliderValue, gSliderValue, bSliderValue, aSliderValue); Crosshair.SetColor(rSliderValue, gSliderValue, bSliderValue, aSliderValue);
// Update Buttons // Update Buttons
HandleButtons(); HandleInputObjects();
} }
// Calls the Update function on all Buttons to check if they were pressed, and execute their Action /// <summary>
private static void HandleButtons() /// Calls the Update function of each input object
/// </summary>
private static void HandleInputObjects()
{ {
foreach(InputObject obj in m_inputs) foreach(InputObject obj in m_inputs)
{ {

View file

@ -6,51 +6,62 @@ using System.Text;
using UnityEngine; using UnityEngine;
namespace CrosshairMod namespace CrosshairMod.Input
{ {
/* /// <summary>
* A button wrapper class that is used right now as I don't have access to /// Button wrapper for Unitys GUI.Button() function. I made this in order
* the games buttons. Since UnityEngine.GUI only has a function to draw Buttons, /// to store buttons and render them in Lists
* I made this class for easy handling. /// </summary>
* class Button : InputObject
*/
class GUIButton : InputObject
{ {
// da_google thinks this Button Wrapper is stupid, so let's see what ths Button Wrapper thinks about him /// <summary>
private const bool IS_DA_GOOGLE_STUPID = true; /// Event Handler that contains the actions to execute when the Button was clicked
// Interesting. /// </summary>
// OnClick event
public event EventHandler OnClick; public event EventHandler OnClick;
// Label of the Button public string Label = "";
public string label { get; set; } = "";
// Initialize Button /// <summary>
public GUIButton(float x, float y, float width, float height, string label, string ID, params EventHandler[] OnClickEvent) /// Create new Button
/// </summary>
/// <param name="x">X-Position</param>
/// <param name="y">Y-Position</param>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <param name="label">Text inside the button</param>
/// <param name="ID">Button ID</param>
/// <param name="OnClickEvent">Action to execute when button is pressed</param>
public Button(float x, float y, float width, float height, string label, string ID, params EventHandler[] OnClickEvent)
: base(x, y, width, height, ID) : base(x, y, width, height, ID)
{ {
Logging.Debug.Log("Button Constructor"); Logging.Debug.Log("Button Constructor: " + label);
// Assign position, dimension and label // Assign position, dimension and label
this.label = label; this.Label = label;
// Push OnClickEvents // Push OnClickEvents
foreach(EventHandler e in OnClickEvent) foreach(EventHandler e in OnClickEvent)
OnClick += e; OnClick += e;
} }
public GUIButton(string ID) /// <summary>
/// Sad, pathetic default constructor
/// </summary>
/// <param name="ID"></param>
public Button(string ID)
: base(0, 0, 0, 0, ID) : base(0, 0, 0, 0, ID)
{ {
// Empty // Empty
} }
// Updates and Draws the Button. /// <summary>
/// Draws button and returns button state / executes button action
/// </summary>
/// <returns>1f if the button is pressed, 0f else</returns>
public override float Update() public override float Update()
{ {
// Get if the Button was pressed and invoke OnClick event accordingly // Get if the Button was pressed and invoke OnClick event accordingly
bool buttonPressed = GUI.Button(new Rect(position, dimensions), label); bool buttonPressed = GUI.Button(new Rect(position, dimensions), Label);
if (buttonPressed) if (buttonPressed)
OnClick?.Invoke(this, EventArgs.Empty); OnClick?.Invoke(this, EventArgs.Empty);

View file

@ -8,21 +8,25 @@ using UnityEngine;
namespace CrosshairMod namespace CrosshairMod
{ {
/* /// <summary>
* Base of all Input Objects. /// Base class for all InputObjects. Defines basic attributes that
* /// every input object needs
* Any Input Object that wants to be displayed in the Interface must /// </summary>
* inherit from this class.
*/
abstract class InputObject abstract class InputObject
{ {
// position and dimension of the object
public Vector2 position, dimensions; public Vector2 position, dimensions;
// ID of the Object // TODO: Remove once dictionary was implemented
public readonly string ID; public readonly string ID;
// constructor to set position and size /// <summary>
/// Create new InputObject
/// </summary>
/// <param name="x">X-Position</param>
/// <param name="y">Y-Position</param>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <param name="ID">ID</param>
public InputObject(float x, float y, float width, float height, string ID) public InputObject(float x, float y, float width, float height, string ID)
{ {
this.position = new Vector2(x, y); this.position = new Vector2(x, y);
@ -30,7 +34,11 @@ namespace CrosshairMod
this.ID = ID; this.ID = ID;
} }
// the update method (that works as renderer) must be overriden by each object /// <summary>
/// The Update method every Input Object needs.
/// This is needed to store all kinds of input objects in one List for example
/// </summary>
/// <returns>Value of the Object</returns>
public abstract float Update(); public abstract float Update();
} }
} }

View file

@ -6,18 +6,33 @@ using System.Text;
using UnityEngine; using UnityEngine;
namespace CrosshairMod namespace CrosshairMod.Input
{ {
class GUISlider : InputObject class Slider : InputObject
{ {
// Min/Max values for the slider /// <summary>
public float Min { get; set; } = 0; /// Min/Max Slider values
public float Max { get; set; } = 0; /// </summary>
public float Min = 0;
public float Max = 0;
// Current slider value /// <summary>
public float Value { get; set; } = 0; /// Current slider value
/// </summary>
public float Value = 0;
public GUISlider(float x, float y, float width, float height, float min, float max, float init, string ID) /// <summary>
/// Creates a new Slider
/// </summary>
/// <param name="x">X-Position</param>
/// <param name="y">Y-Position</param>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <param name="min">Minimum Slider value</param>
/// <param name="max">Maximum Slider value</param>
/// <param name="init">Initial Slider value</param>
/// <param name="ID">ID</param>
public Slider(float x, float y, float width, float height, float min, float max, float init, string ID)
: base(x, y, width, height, ID) : base(x, y, width, height, ID)
{ {
Min = min; Min = min;
@ -25,12 +40,20 @@ namespace CrosshairMod
Value = init; Value = init;
} }
public GUISlider(string ID) /// <summary>
/// Sad, pathetic default constructor
/// </summary>
/// <param name="ID"></param>
public Slider(string ID)
:base (0, 0, 0, 0, ID) :base (0, 0, 0, 0, ID)
{ {
} }
/// <summary>
/// Draws and Updates the slider
/// </summary>
/// <returns>Value of the Slider</returns>
public override float Update() public override float Update()
{ {
Value = GUI.HorizontalSlider(new Rect(position, dimensions), Value, Min, Max); Value = GUI.HorizontalSlider(new Rect(position, dimensions), Value, Min, Max);

View file

@ -8,17 +8,17 @@ using UnityEngine;
namespace CrosshairMod namespace CrosshairMod
{ {
// CrosshairMod's Logging class /// <summary>
// Simply writes messages to the Unity Debug output /// Handles Logging in the Crosshair mod
// However, I prefer this over Debug.Log() since it doesn't include a stacktrace (Except for Errors) /// </summary>
public static class Logging public static class Logging
{ {
// The Prefix that gets put in front of every Log
public const string PREFIX = "[CROSSHAIRMOD]"; public const string PREFIX = "[CROSSHAIRMOD]";
// A kind of sub-class that is used to Log messages that will only appear /// <summary>
// when the User installs a Debug build of the Mod. Release versions will /// Logging's subclass for Debug Logging.
// not log Debug messages this way. /// This only logs messages in the debug build of the mod.
/// </summary>
public static class Debug public static class Debug
{ {
public static void Log(string message) public static void Log(string message)
@ -44,19 +44,17 @@ namespace CrosshairMod
} }
// Logs information
public static void Log(string message) public static void Log(string message)
{ {
Console.WriteLine(PREFIX + "Info: " + message); Console.WriteLine(PREFIX + "Info: " + message);
} }
// Logs warnings
public static void LogWarning(string message) public static void LogWarning(string message)
{ {
Console.WriteLine(PREFIX + "Warning: " + message); Console.WriteLine(PREFIX + "Warning: " + message);
} }
// Logs errors
public static void LogError(string message) public static void LogError(string message)
{ {
UnityEngine.Debug.Log(PREFIX + "Error: " + message); UnityEngine.Debug.Log(PREFIX + "Error: " + message);

View file

@ -1,12 +1,11 @@
/* /*
* Source code of a mode that adds a crosshair into * Source code of a mod that adds a crosshair into
* the game Blackwake. * the game Blackwake.
* *
* @author Lauchmelder * @author Lauchmelder
* @version v0.3 * @version v0.3.1
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -16,24 +15,22 @@ using UnityEngine;
namespace CrosshairMod namespace CrosshairMod
{ {
/// <summary>
/* /// Class that gets loaded by the ModLoader. Calls every component of the Mod
* This is the Main class that is responsible for /// </summary>
* handling initializing and updating the components
* of the crosshair mod.
*/
public class CrosshairMod : MonoBehaviour public class CrosshairMod : MonoBehaviour
{ {
// Define Hotkeys for Menu and Crosshair Toggle /// <summary>
/// Default Hotkey Definitions
/// </summary>
private char MENU_OPEN_KEY = 'H'; private char MENU_OPEN_KEY = 'H';
private char CH_TOGGLE_KEY = 'J'; private char CH_TOGGLE_KEY = 'J';
// This will be executed first
void Start() void Start()
{ {
// Update the settings // Update the settings
Settings.LoadSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett"); Settings.LoadSettings(".\\Blackwake_Data\\Managed\\Mods\\Assets\\chSettings.sett");
// Create Crosshair // Create Crosshair
Crosshair.Create(); Crosshair.Create();
// Create Panel // Create Panel
@ -44,7 +41,6 @@ namespace CrosshairMod
CH_TOGGLE_KEY = (char)Settings.GetValue("hotkeyGUIToggle", true, CH_TOGGLE_KEY); CH_TOGGLE_KEY = (char)Settings.GetValue("hotkeyGUIToggle", true, CH_TOGGLE_KEY);
} }
// This gets called on every GUI Update (Can be multiple tiems per Frame)
void OnGUI() void OnGUI()
{ {
// Check for Key presses // Check for Key presses
@ -66,11 +62,10 @@ namespace CrosshairMod
Crosshair.Render(); Crosshair.Render();
} }
// Will be called when the application is closed
void OnApplicationQuit() void OnApplicationQuit()
{ {
// Save settings // Save settings
Settings.SaveSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett"); Settings.SaveSettings(".\\Blackwake_Data\\Managed\\Mods\\Assets\\chSettings.sett");
Logging.Debug.Log("Saved Settings"); Logging.Debug.Log("Saved Settings");
} }
} }

View file

@ -6,16 +6,17 @@ using System.Text;
namespace CrosshairMod namespace CrosshairMod
{ {
/* /// <summary>
* The class that is responsible for loading and storing all the /// Loads, contains and handles settings
* necessary settings. There is much room for improvement. /// </summary>
*/
static class Settings static class Settings
{ {
// Initialize Settings dictionary
private static Dictionary<string, int> m_settings = new Dictionary<string, int>(); private static Dictionary<string, int> m_settings = new Dictionary<string, int>();
// Load settings from file /// <summary>
/// Load settings from file
/// </summary>
/// <param name="filepath"></param>
public static void LoadSettings(string filepath) public static void LoadSettings(string filepath)
{ {
Logging.Debug.Log("Accessing Settings at " + filepath); Logging.Debug.Log("Accessing Settings at " + filepath);
@ -75,7 +76,10 @@ namespace CrosshairMod
Logging.Log("Settings loaded."); Logging.Log("Settings loaded.");
} }
// Converts the dictionary to a sett file /// <summary>
/// Converts dictionary to a string to be stored in the settings file
/// </summary>
/// <param name="filepath">Save location</param>
public static void SaveSettings(string filepath) public static void SaveSettings(string filepath)
{ {
string filecontent = ""; string filecontent = "";
@ -87,7 +91,11 @@ namespace CrosshairMod
System.IO.File.WriteAllText(filepath, filecontent); System.IO.File.WriteAllText(filepath, filecontent);
} }
// Adds a setting to the settings /// <summary>
/// Create new setting and add it to the dictionary
/// </summary>
/// <param name="key">Setting name</param>
/// <param name="value">Setting value</param>
public static void AddSetting(string key, int value) public static void AddSetting(string key, int value)
{ {
if (m_settings.ContainsKey(key)) if (m_settings.ContainsKey(key))
@ -96,7 +104,12 @@ namespace CrosshairMod
m_settings.Add(key, value); m_settings.Add(key, value);
} }
// Changes a settings value, and adds it if specified /// <summary>
/// Change a settings value
/// </summary>
/// <param name="key">Setting name</param>
/// <param name="newVal">New setting value</param>
/// <param name="addIfDoesntExist">If the setting doesn't exist, create it</param>
public static void SetSetting(string key, int newVal, bool addIfDoesntExist = false) 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 the setting doesn't exist, either add and set it, or print a Debug.Warning
@ -117,9 +130,13 @@ namespace CrosshairMod
m_settings[key] = newVal; m_settings[key] = newVal;
} }
// Tries to return the value belonging to a certain key /// <summary>
// If the specified key doesn't exist, it returns 0 and logs an error /// Returns the value of a setting
// One can also specify that the setting should be created with some initial value /// </summary>
/// <param name="key">Setting name</param>
/// <param name="addIfDoesntExist">Add the setting if it doesn't exist</param>
/// <param name="initialValue">Initial value of the setting in case it doesn't exist</param>
/// <returns></returns>
public static int GetValue(string key, bool addIfDoesntExist = false, int initialValue = 0) public static int GetValue(string key, bool addIfDoesntExist = false, int initialValue = 0)
{ {
int value = 0; int value = 0;