Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button improvements #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CommunityEntity.UI.Countdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ void UpdateDisplay(float time)
TimerFormat.MinutesSecondsHundreth => t.ToString("mm\\:ss\\.ff"),
TimerFormat.HoursMinutes => t.ToString("hh\\:mm"),
TimerFormat.HoursMinutesSeconds => t.ToString("hh\\:mm\\:ss"),
TimerFormat.DaysHoursMinutes => t.ToString("d\\.hh\\:mm"),
TimerFormat.DaysHoursMinutesSeconds => t.ToString("d\\.hh\\:mm\\:ss"),
TimerFormat.Custom => time.ToString(numberFormat),
_ => time.ToString(numberFormat)
};
textComponent.text = tempText.Replace( "%TIME_LEFT%", formattedTime );
Expand All @@ -106,7 +109,10 @@ public enum TimerFormat
MinutesSeconds,
MinutesSecondsHundreth,
HoursMinutes,
HoursMinutesSeconds
HoursMinutesSeconds,
DaysHoursMinutes,
DaysHoursMinutesSeconds,
Custom
}
}

Expand Down
38 changes: 38 additions & 0 deletions CommunityEntity.UI.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ public static void cui_test( ConsoleSystem.Arg args )
}
]
},
{
""name"": ""ButtonLink"",
""parent"": ""TestPanel7766"",
""components"":
[
{
""type"":""UnityEngine.UI.Button"",
""url"":""https://facepunch.com/"",
""normalColor"": ""0.55 0.78 0.24 1"",
""highlightColor"": ""0.8 0.28 0.2 1"",
""pressedColor"": ""0.204 0.596 0.859 1"",
""fadeDuration"": 0.4,
""imagetype"": ""Tiled""
},
{
""type"":""RectTransform"",
""anchormin"": ""0.3 0.05"",
""anchormax"": ""0.7 0.1""
}
]
},
{
""parent"": ""ButtonLink"",
""components"":
[
{
""type"":""UnityEngine.UI.Text"",
""text"":""Go to Facepunch.com"",
""fontSize"":15,
""align"": ""MiddleCenter""
}
]
},
{
""name"": ""ItemIcon"",
""parent"": ""TestPanel7766"",
Expand Down Expand Up @@ -132,6 +165,11 @@ public static void cui_test( ConsoleSystem.Arg args )
""fontSize"":32,
""align"": ""MiddleRight"",
},
{
""type"":""RectTransform"",
""anchormin"": ""0.3 0.1"",
""anchormax"": ""0.7 0.15""
}
]
},
]
Expand Down
44 changes: 35 additions & 9 deletions CommunityEntity.UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,37 +274,63 @@ T GetOrAddComponent<T>() where T : Component

case "UnityEngine.UI.Button":
{
var c = GetOrAddComponent<UnityEngine.UI.Button>();
HandleEnableState( obj, c );
var button = GetOrAddComponent<UnityEngine.UI.Button>();
HandleEnableState( obj, button );

if ( obj.ContainsKey( "command" ) )
{
var cmd = obj.GetString( "command" );
if ( allowUpdate )
c.onClick.RemoveAllListeners();
c.onClick.AddListener( () => { ConsoleNetwork.ClientRunOnServer( cmd ); } );
button.onClick.RemoveAllListeners();
button.onClick.AddListener( () => { ConsoleNetwork.ClientRunOnServer( cmd ); } );
}

if ( obj.ContainsKey( "close" ) )
{
var pnlName = obj.GetString( "close" );
if ( allowUpdate )
c.onClick.RemoveAllListeners();
c.onClick.AddListener( () => { DestroyPanel( pnlName ); } );
button.onClick.RemoveAllListeners();
button.onClick.AddListener( () => { DestroyPanel( pnlName ); } );
}

if (obj.ContainsKey("url"))
{
string url = obj.GetString("url");
if (url.StartsWith("http://") || url.StartsWith("https://")) // This is necessary for safety. It won't be able to open local files.
{
if (allowUpdate)
button.onClick.RemoveAllListeners();
button.onClick.AddListener(() => { Application.OpenURL(url); });
}
}

// style the button
var block = button.colors;
if ( ShouldUpdateField( "normalColor" ) )
block.normalColor = ColorEx.Parse( obj.GetString( "normalColor", "1.0 1.0 1.0 1.0" ) );
if ( ShouldUpdateField( "highlightColor" ) )
block.highlightedColor = ColorEx.Parse( obj.GetString( "highlightColor", "1.0 1.0 1.0 1.0" ) );
if ( ShouldUpdateField("pressedColor") )
{
block.pressedColor = ColorEx.Parse( obj.GetString( "pressedColor", "0.78 0.78 0.78 1" ) );
block.selectedColor = block.pressedColor;
}
if ( ShouldUpdateField("fadeDuration") )
block.fadeDuration = obj.GetFloat("fadeDuration", 0.1f); //Smoothness of color change
button.colors = block;

// bg image
var img = GetOrAddComponent<UnityEngine.UI.Image>();
if ( ShouldUpdateField( "sprite" ) )
img.sprite = FileSystem.Load<Sprite>( obj.GetString( "sprite", "Assets/Content/UI/UI.Background.Tile.psd" ) );
if ( ShouldUpdateField( "material" ) )
img.material = FileSystem.Load<Material>( obj.GetString( "material", "Assets/Icons/IconMaterial.mat" ) );
if ( ShouldUpdateField( "color" ) )
img.color = ColorEx.Parse( obj.GetString( "color", "1.0 1.0 1.0 1.0" ) );
if ( ShouldUpdateField( "color" ) ) // This is necessary for proper operation in conjunction with button.colors (You can remove it, it's not necessary.)
img.color = obj.ContainsKey("normalColor") ? ColorEx.Parse("1.0 1.0 1.0 1.0") : ColorEx.Parse( obj.GetString( "color", "1.0 1.0 1.0 1.0" ) );
if ( ShouldUpdateField( "imagetype" ) )
img.type = ParseEnum( obj.GetString( "imagetype", "Simple" ), UnityEngine.UI.Image.Type.Simple );

c.image = img;
button.image = img;

GraphicComponentCreated( img, obj );

Expand Down