diff --git a/CrosshairMod/Button.cs b/CrosshairMod/Button.cs index 2fb4dbb..50a85f7 100644 --- a/CrosshairMod/Button.cs +++ b/CrosshairMod/Button.cs @@ -19,8 +19,12 @@ namespace CrosshairMod public Vector2 position = new Vector2(0, 0); public Vector2 dimensions = new Vector2(0, 0); + // Visibilty variables + private bool m_visible = true; + private bool m_active = true; + // Label of the Button - public string label = ""; + public string label { get; set; } = ""; // OnClick event public event EventHandler OnClick; @@ -41,14 +45,29 @@ namespace CrosshairMod // Empty } + + // Changes visibilty of the Button + public void Toggle() + { + m_visible = !m_visible; + } + + // Changes Usabilty of the button + public void Activate() + { + m_active = !m_active; + } + // 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); + if (m_visible) + { + // Get if the Button was pressed and invoke OnClick event accordingly + bool buttonPressed = GUI.Button(new Rect(position, dimensions), label); + if (buttonPressed && m_active) + OnClick?.Invoke(this, EventArgs.Empty); + } } } } diff --git a/CrosshairMod/Interface.cs b/CrosshairMod/Interface.cs index 43fab20..35206af 100644 --- a/CrosshairMod/Interface.cs +++ b/CrosshairMod/Interface.cs @@ -19,8 +19,7 @@ namespace CrosshairMod private static bool m_visible = false; // Stores all Buttons used in the interface. - // TODO: Create function to easily add Buttons - private static Dictionary m_buttons = new Dictionary(); + private static List m_buttons = new List(); // Values of the RGBA Sliders private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue; @@ -33,6 +32,19 @@ namespace CrosshairMod private static Vector2 m_position; private static Vector2 m_dimension; + + // Creates a new button object and adds it to the ButtonList + private static void AddButton(uint x, uint y, uint width, uint height, string label, params EventHandler[] onClickEvent) + { + GUIButton buttonObj = new GUIButton(x, y, width, height, label); + foreach(EventHandler e in onClickEvent) + { + buttonObj.OnClick += e; + } + + m_buttons.Add(buttonObj); + } + // Initializes all Buttons, gives them their function etc public static void Init() { @@ -49,22 +61,24 @@ namespace CrosshairMod // Apply Texture to Style m_style.normal.background = m_background; + AddButton(3, 3, 3, 3, "", (object sender, EventArgs e) => { }); + // 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(); }; + AddButton((uint)m_position.x + 20, (uint)m_position.y + 20, 200, 30, + "Toggle Crosshair", (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); }; + AddButton((uint)m_position.x + 20, (uint)m_position.y + 60, 30, 30, + "-", (object sender, EventArgs e) => { Crosshair.ChangeSize(-1); }); + AddButton((uint)m_position.x + 190, (uint)m_position.y + 60, 30, 30, + "+", (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); }; + AddButton((uint)m_position.x + 20, (uint)m_position.y + 100, 30, 30, + "-", (object sender, EventArgs e) => { Crosshair.ChangeThickness(-1); }); + AddButton((uint)m_position.x + 190, (uint)m_position.y + 100, 30, 30, + "+", (object sender, EventArgs e) => { Crosshair.ChangeThickness(+1); }); // Assign setting values to sliders rSliderValue = Settings.GetValue("crosshairColorRed"); @@ -116,9 +130,9 @@ 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 pair in m_buttons) + foreach(GUIButton button in m_buttons) { - pair.Value.Update(); + button.Update(); } } } diff --git a/CrosshairMod/Settings.cs b/CrosshairMod/Settings.cs index 16fe35b..f274f7c 100644 --- a/CrosshairMod/Settings.cs +++ b/CrosshairMod/Settings.cs @@ -8,7 +8,7 @@ namespace CrosshairMod { /* * The class that is responsible for loading and storing all the - * necessary settings. There's much room for improvement. + * necessary settings. There is much room for improvement. */ static class Settings {