Merge branch 'Button' into development
This commit is contained in:
commit
67c7088fe4
3 changed files with 54 additions and 21 deletions
|
@ -19,8 +19,12 @@ namespace CrosshairMod
|
||||||
public Vector2 position = new Vector2(0, 0);
|
public Vector2 position = new Vector2(0, 0);
|
||||||
public Vector2 dimensions = 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
|
// Label of the Button
|
||||||
public string label = "";
|
public string label { get; set; } = "";
|
||||||
|
|
||||||
// OnClick event
|
// OnClick event
|
||||||
public event EventHandler OnClick;
|
public event EventHandler OnClick;
|
||||||
|
@ -41,14 +45,29 @@ namespace CrosshairMod
|
||||||
// Empty
|
// 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.
|
// Updates and Draws the Button.
|
||||||
// TODO: Seperate PressChecking and Rendering in order to have Disabled Buttons
|
|
||||||
public void Update()
|
public void Update()
|
||||||
|
{
|
||||||
|
if (m_visible)
|
||||||
{
|
{
|
||||||
// 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 && m_active)
|
||||||
OnClick?.Invoke(this, EventArgs.Empty);
|
OnClick?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,7 @@ namespace CrosshairMod
|
||||||
private static bool m_visible = false;
|
private static bool m_visible = false;
|
||||||
|
|
||||||
// Stores all Buttons used in the interface.
|
// Stores all Buttons used in the interface.
|
||||||
// TODO: Create function to easily add Buttons
|
private static List<GUIButton> m_buttons = new List<GUIButton>();
|
||||||
private static Dictionary<string, GUIButton> m_buttons = new Dictionary<string, GUIButton>();
|
|
||||||
|
|
||||||
// Values of the RGBA Sliders
|
// Values of the RGBA Sliders
|
||||||
private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue;
|
private static int rSliderValue, gSliderValue, bSliderValue, aSliderValue;
|
||||||
|
@ -33,6 +32,19 @@ namespace CrosshairMod
|
||||||
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 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
|
// Initializes all Buttons, gives them their function etc
|
||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
|
@ -49,22 +61,24 @@ namespace CrosshairMod
|
||||||
// Apply Texture to Style
|
// Apply Texture to Style
|
||||||
m_style.normal.background = m_background;
|
m_style.normal.background = m_background;
|
||||||
|
|
||||||
|
AddButton(3, 3, 3, 3, "", (object sender, EventArgs e) => { });
|
||||||
|
|
||||||
// Create Crosshair Visibilty Button
|
// 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)
|
// 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"));
|
AddButton((uint)m_position.x + 20, (uint)m_position.y + 20, 200, 30,
|
||||||
m_buttons["visibility"].OnClick += (object sender, EventArgs e) => { Crosshair.Toggle(); };
|
"Toggle Crosshair", (object sender, EventArgs e) => { Crosshair.Toggle(); });
|
||||||
|
|
||||||
// Create Crosshair Size +/- Buttons
|
// Create Crosshair Size +/- Buttons
|
||||||
m_buttons.Add("size-", new GUIButton((uint)m_position.x + 20, (uint)m_position.y + 60, 30, 30, "-"));
|
AddButton((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, "+"));
|
"-", (object sender, EventArgs e) => { Crosshair.ChangeSize(-1); });
|
||||||
m_buttons["size-"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeSize(-1); };
|
AddButton((uint)m_position.x + 190, (uint)m_position.y + 60, 30, 30,
|
||||||
m_buttons["size+"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeSize(+1); };
|
"+", (object sender, EventArgs e) => { Crosshair.ChangeSize(+1); });
|
||||||
|
|
||||||
// Create Crosshair Thickness +/- Buttons
|
// Create Crosshair Thickness +/- Buttons
|
||||||
m_buttons.Add("thick-", new GUIButton((uint)m_position.x + 20, (uint)m_position.y + 100, 30, 30, "-"));
|
AddButton((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, "+"));
|
"-", (object sender, EventArgs e) => { Crosshair.ChangeThickness(-1); });
|
||||||
m_buttons["thick-"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeThickness(-1); };
|
AddButton((uint)m_position.x + 190, (uint)m_position.y + 100, 30, 30,
|
||||||
m_buttons["thick+"].OnClick += (object sender, EventArgs e) => { Crosshair.ChangeThickness(+1); };
|
"+", (object sender, EventArgs e) => { Crosshair.ChangeThickness(+1); });
|
||||||
|
|
||||||
// Assign setting values to sliders
|
// Assign setting values to sliders
|
||||||
rSliderValue = Settings.GetValue("crosshairColorRed");
|
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
|
// Calls the Update function on all Buttons to check if they were pressed, and execute their Action
|
||||||
private static void HandleButtons()
|
private static void HandleButtons()
|
||||||
{
|
{
|
||||||
foreach(KeyValuePair<string, GUIButton> pair in m_buttons)
|
foreach(GUIButton button in m_buttons)
|
||||||
{
|
{
|
||||||
pair.Value.Update();
|
button.Update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ namespace CrosshairMod
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* The class that is responsible for loading and storing all the
|
* 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
|
static class Settings
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue