Pushed vor Version v0.2.1

This commit is contained in:
Robert Altner 2019-08-01 20:15:40 +02:00
parent 74d122a9a6
commit fedf27da79
5 changed files with 198 additions and 158 deletions

View file

@ -9,28 +9,33 @@ using UnityEngine;
namespace CrosshairMod
{
// Button Wrapper class, utilizing GUI.Button()
public class Button
class GUIButton
{
public Vector2 position = new Vector2(0, 0);
public Vector2 dimensions = new Vector2(0, 0);
public string label = "";
public Action OnClick = new Action(() => { });
public event EventHandler OnClick;
public Button(uint x, uint y, uint width, uint height, string label, Action e)
public GUIButton(uint x, uint y, uint width, uint height, string label)
{
Logging.Log("Button Constructor");
this.position = new Vector2(x, y);
this.dimensions = new Vector2(width, height);
this.label = label;
this.OnClick = e;
}
public Button() { /*Empty*/ }
public GUIButton()
{
// Empty
}
public void Update()
{
bool buttonClicked = GUI.Button(new Rect(position, dimensions), label);
if (buttonClicked)
OnClick();
bool buttonPressed = GUI.Button(new Rect(position, dimensions), label);
if (buttonPressed)
OnClick?.Invoke(this, EventArgs.Empty);
}
}
}

102
CrosshairMod/Crosshair.cs Normal file
View file

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace CrosshairMod
{
static class Crosshair
{
private static Texture2D m_texture = new Texture2D(0, 0);
private static GUIStyle m_style;
private static bool m_enabled = true;
// Toggles visibilty of the crosshair
public static void Toggle()
{
m_enabled = !m_enabled;
}
// This must be called, or else no crosshair will be rendered
public static void Create()
{
// 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");
// Construct color object from RGBA values
Color m_crosshairColor = new Color(m_crosshairColorRed / 255f,
m_crosshairColorGreen / 255f,
m_crosshairColorBlue / 255f,
m_crosshairColorAlpha / 255f);
// Lazily add 1 to thickness if it's even. If it is even the crosshair can't be symmetrical
m_crosshairThickness += (m_crosshairThickness % 2 == 0) ? 1 : 0;
// Create Texture with approprate dimensions. The m_crosshairLength is the distance
// between center and end-of-line (end-points included)
m_texture = new Texture2D((m_crosshairLength - 1) * 2 + 1, (m_crosshairLength - 1) * 2 + 1);
// Fill Texture with some color where alpha = 0
for (int y = 0; y < m_texture.height; y++)
for (int x = 0; x < m_texture.width; x++)
m_texture.SetPixel(x, y, new Color(0, 0, 0, 0));
// The texture will always have odd dimensions, so we can be sure that there will be a definite center (which is m_crossheirLength)
// Draw vertical line:
for (int y = 0; y < m_texture.height; y++)
{
for (int i = 0; i < m_crosshairThickness; i++)
{
m_texture.SetPixel(m_crosshairLength - (int)Math.Floor((double)m_crosshairThickness / 2) + i - 1, y, m_crosshairColor);
}
}
// Draw horizontal line:
for (int x = 0; x < m_texture.height; x++)
{
for (int i = 0; i < m_crosshairThickness; i++)
{
m_texture.SetPixel(x, m_crosshairLength - (int)Math.Floor((double)m_crosshairThickness / 2) + i - 1, m_crosshairColor);
}
}
// Set texture settings and apply changes
m_texture.wrapMode = TextureWrapMode.Repeat;
m_texture.Apply();
// Create GUIStyle to render the crosshair
m_style = new GUIStyle();
m_style.normal.background = m_texture;
}
public static void Render()
{
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);
}
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);
}
}
}

View file

@ -45,9 +45,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Button.cs" />
<Compile Include="Crosshair.cs" />
<Compile Include="Logging.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs" />
</ItemGroup>
<ItemGroup>
<None Include="chSettings.sett" />

View file

@ -17,173 +17,37 @@ namespace CrosshairMod
{
public class Main : MonoBehaviour
{
// Initialize Settings dictionary
private Dictionary<string, int> m_settings = new Dictionary<string, int>();
// Initialize Crosshair texture
private Texture2D crosshair = new Texture2D(-1, -1);
// 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 bool readSettings(string filepath, ref Dictionary<string, int> sett_dict)
{
Logging.Log("Accessing Settings at " + filepath);
// Try to read file contents into string
string settings = "";
try
{
settings = System.IO.File.ReadAllText(filepath);
} catch(Exception e)
{
// Log error and return invalid state
Logging.LogError(e.Message);
return false;
}
// Empty settings dictionary
sett_dict.Clear();
// Splice settings string by newline characters, to get each line into a new array member
string[] lines = settings.Split('\n');
foreach (string line in lines) // Loop through all lines
{
if (line == "") // If line is empty, ignore it (some editors add newlines when saving, this will combat that)
continue;
string[] vals = line.Split('='); // Split each line by the = character, to get a key and a value
sett_dict.Add(vals[0], Int32.Parse(vals[1])); // Store key and value in settings dictionary
}
Logging.Log("Successfully loaded settings!");
return true; // Return valid state
}
// Creates a crosshair texture
private bool createCrossTexture(ref Texture2D texture)
{
// Assign dictionary values to variables
int m_crosshairLength = 0, m_crosshairThickness = 0, m_crosshairColorRed = 0, m_crosshairColorGreen = 0, m_crosshairColorBlue = 0, m_crosshairColorAlpha = 0;
// Checks wether the key is in the dictionary, and assigns it
// If the function finds that a necessary setting is missing, it will
// return an invalid state
if (!m_settings.TryGetValue("crosshairLength", out m_crosshairLength))
{
Logging.LogError("Missing Setting: crosshairLength");
return false;
}
if (!m_settings.TryGetValue("crosshairThickness", out m_crosshairThickness))
{
Logging.LogError("Missing Setting: crosshairThickness");
return false;
}
if (!m_settings.TryGetValue("crosshairColorRed", out m_crosshairColorRed))
{
Logging.LogError("Missing Setting: crosshairColorRed");
return false;
}
if (!m_settings.TryGetValue("crosshairColorGreen", out m_crosshairColorGreen))
{
Logging.LogError("Missing Setting: crosshairColorGreen");
return false;
}
if (!m_settings.TryGetValue("crosshairColorBlue", out m_crosshairColorBlue))
{
Logging.LogError("Missing Setting: crosshairColorBlue");
return false;
}
if (!m_settings.TryGetValue("crosshairColorAlpha", out m_crosshairColorAlpha))
{
Logging.LogError("Missing Setting: crosshairColorAlpha");
return false;
}
// Construct color object from RGBA values
Color m_crosshairColor = new Color(m_crosshairColorRed / 255f,
m_crosshairColorGreen / 255f,
m_crosshairColorBlue / 255f,
m_crosshairColorAlpha / 255f);
// Lazily add 1 to thickness if it's even. If it is even the crosshair can't be symmetrical
m_crosshairThickness += (m_crosshairThickness % 2 == 0) ? 1 : 0;
// Create Texture with approprate dimensions. The m_crosshairLength is the distance
// between center and end-of-line (end-points included)
texture = new Texture2D((m_crosshairLength - 1) * 2 + 1, (m_crosshairLength - 1) * 2 + 1);
// Fill Texture with some color where alpha = 0
for (int y = 0; y < texture.height; y++)
for (int x = 0; x < texture.width; x++)
texture.SetPixel(x, y, new Color(0, 0, 0, 0));
// The texture will always have odd dimensions, so we can be sure that there will be a definite center (which is m_crossheirLength)
// Draw vertical line:
for (int y = 0; y < texture.height; y++)
{
for (int i = 0; i < m_crosshairThickness; i++)
{
texture.SetPixel(m_crosshairLength - (int)Math.Floor((double)m_crosshairThickness / 2) + i - 1, y, m_crosshairColor);
}
}
// Draw horizontal line:
for (int x = 0; x < texture.height; x++)
{
for (int i = 0; i < m_crosshairThickness; i++)
{
texture.SetPixel(x, m_crosshairLength - (int)Math.Floor((double)m_crosshairThickness / 2) + i - 1, m_crosshairColor);
}
}
// Set texture settings and apply changes
texture.wrapMode = TextureWrapMode.Repeat;
texture.Apply();
return true; // Return valid state
}
// This will be executed first
void Start()
{
// Read settings, if this fails, the state will be invalid
m_validState = readSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett", ref m_settings);
foreach (KeyValuePair<string, int> setting in m_settings)
Logging.Log(setting.Key + ": " + setting.Value);
// Update the settings
Settings.LoadSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett");
// Create Crosshair
Crosshair.Create();
// Add function to Button
crosshairButton.OnClick += (object sender, EventArgs e) => { Crosshair.Toggle(); };
}
private Button testBtn = new Button(500, 500, 300, 200, "Test Button", () => { Logging.Log("Test Button pressed"); });
private GUIButton crosshairButton = new GUIButton(200, 10, 100, 20, "Toggle Crosshair");
void OnGUI()
{
testBtn.Update();
crosshairButton.Update();
// If the mod is in an invalid state, the OnGUI function won't execute
if (m_validState)
{
// If the state is valid, construct crosshair texture, and update state
// TODO: Remove createCrossTexture, this doesn't need to be constructed each time
m_validState = createCrossTexture(ref crosshair);
// Create GUIStyle to render the crosshair
GUIStyle style = new GUIStyle();
style.normal.background = crosshair;
// Render the crosshair
GUI.Label(new Rect(Screen.width / 2 - crosshair.width / 2, Screen.height / 2 - crosshair.height / 2, crosshair.width, crosshair.height),
crosshair, style);
}
//Render Crosshair
Crosshair.Render();
}
}
}

67
CrosshairMod/Settings.cs Normal file
View file

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CrosshairMod
{
static class Settings
{
// Initialize Settings dictionary
private static Dictionary<string, int> m_settings = new Dictionary<string, int>();
public static void LoadSettings(string filepath)
{
Logging.Log("Accessing Settings at " + filepath);
// Try to read file contents into string
string settings = "";
try
{
settings = System.IO.File.ReadAllText(filepath);
}
catch (Exception e)
{
// Log error and return invalid state
Logging.LogError(e.Message);
return;
}
// Empty settings dictionary
m_settings.Clear();
// Splice settings string by newline characters, to get each line into a new array member
string[] lines = settings.Split('\n');
foreach (string line in lines) // Loop through all lines
{
if (line == "") // If line is empty, ignore it (some editors add newlines when saving, this will combat that)
continue;
string[] vals = line.Split('='); // Split each line by the = character, to get a key and a value
m_settings.Add(vals[0], Int32.Parse(vals[1])); // Store key and value in settings dictionary
}
Logging.Log("Successfully loaded settings!");
}
public static void writeSettings(string filepath)
{
//TODO: Implement saving
}
// 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)
{
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.");
return value;
}
}
}