Skip to content

Commit

Permalink
Merge pull request #9 from trulyspinach/master
Browse files Browse the repository at this point in the history
Support for adding space between buttons
  • Loading branch information
madsbangh authored Oct 20, 2018
2 parents 3bf7367 + d4bae0d commit 690ef85
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
32 changes: 31 additions & 1 deletion Assets/EasyButtons/ButtonAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ public enum ButtonMode
EnabledInPlayMode,
DisabledInPlayMode
}

[Flags]
public enum ButtonSpacing
{
None = 0,
Before = 1,
After = 2
}

/// <summary>
/// Attribute to create a button in the inspector for calling the method it is attached to.
/// The method must have no arguments.
Expand All @@ -24,9 +33,11 @@ public sealed class ButtonAttribute : Attribute
{
private string name = null;
private ButtonMode mode = ButtonMode.AlwaysEnabled;
private ButtonSpacing spacing = ButtonSpacing.None;

public string Name { get { return name; } }
public ButtonMode Mode { get { return mode; } }
public ButtonSpacing Spacing { get { return spacing; } }

public ButtonAttribute()
{
Expand All @@ -37,15 +48,34 @@ public ButtonAttribute(string name)
this.name = name;
}

public ButtonAttribute(ButtonMode mode)
{
this.mode = mode;
}

public ButtonAttribute(ButtonSpacing spacing)
{
this.spacing = spacing;
}

public ButtonAttribute(string name, ButtonMode mode)
{
this.name = name;
this.mode = mode;
}

public ButtonAttribute(ButtonMode mode)
public ButtonAttribute(string name, ButtonSpacing spacing)
{
this.name = name;
this.spacing = spacing;
}

public ButtonAttribute(string name, ButtonMode mode, ButtonSpacing spacing)
{
this.name = name;
this.mode = mode;
this.spacing = spacing;
}
}
}

16 changes: 14 additions & 2 deletions Assets/EasyButtons/ButtonsExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void SayHelloInRuntime()
}

// Example use of the ButtonAttribute with custom name
[Button("Special Name")]
[Button("Special Name", ButtonSpacing.Before)]
private void TestButtonName()
{
Debug.Log("Hello from special name button");
Expand All @@ -38,12 +38,24 @@ private void TestButtonNameEditorOnly()
{
Debug.Log("Hello from special name button for editor only");
}

// Example use of the ButtonAttribute with static method
[Button]
private static void TestStaticMethod()
{
Debug.Log("Hello from static method");
}

// Example use of the ButtonAttribute with ButtonSpacing, and mix two spacing together.
[Button("Space Before and After", ButtonSpacing.Before | ButtonSpacing.After)]
private void TestButtonSpaceBoth() {
Debug.Log("Hello from a button surround by spaces");
}

// Placeholder to show the last button have space after it.
[Button("Another Button")]
private void TestButtonEndSpace() {
Debug.Log("Hello I am here to show some spacing.");
}
}
}
5 changes: 5 additions & 0 deletions Assets/EasyButtons/Editor/EasyButtonsEditorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static void DrawEasyButtons(this Editor editor)
GUI.enabled = ba.Mode == ButtonMode.AlwaysEnabled
|| (EditorApplication.isPlaying ? ba.Mode == ButtonMode.EnabledInPlayMode : ba.Mode == ButtonMode.DisabledInPlayMode);


if (((int)ba.Spacing & 1) != 0) GUILayout.Space(10);

// Draw a button which invokes the method
var buttonName = String.IsNullOrEmpty(ba.Name) ? ObjectNames.NicifyVariableName(method.Name) : ba.Name;
if (GUILayout.Button(buttonName))
Expand All @@ -35,6 +38,8 @@ public static void DrawEasyButtons(this Editor editor)
}
}

if (((int)ba.Spacing & 2) != 0) GUILayout.Space(10);

GUI.enabled = true;
}
}
Expand Down

0 comments on commit 690ef85

Please sign in to comment.