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

Add 9-slice support for Images #70

Open
wants to merge 1 commit 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
16 changes: 16 additions & 0 deletions CommunityEntity.UI.ImageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UnityEngine;
using UnityEngine.UI;

#if CLIENT

public partial class CommunityEntity
{
public struct ImageRequest
{
public Vector4? slice;

public MaskableGraphic graphic;
}
}

#endif
20 changes: 14 additions & 6 deletions CommunityEntity.UI.ServerImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public partial class CommunityEntity
{
private static Dictionary<uint, List<UnityEngine.UI.MaskableGraphic>> requestingTextureImages = new Dictionary<uint, List<UnityEngine.UI.MaskableGraphic>>();
private static Dictionary<uint, List<ImageRequest>> requestingTextureImages = new Dictionary<uint, List<ImageRequest>>();
private static Dictionary<uint, CachedTexture> _textureCache = new Dictionary<uint, CachedTexture>();

private class CachedTexture
Expand Down Expand Up @@ -56,7 +56,7 @@ public void CL_ReceiveFilePng( BaseEntity.RPCMessage msg )

foreach ( var c in components )
{
ApplyCachedTextureToImage( c, texture );
ApplyCachedTextureToImage( c.graphic, texture, c.slice );
}
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ private CachedTexture StoreCachedTexture( uint textureId, byte[] bytes )
return texture;
}

private void ApplyTextureToImage( UnityEngine.UI.MaskableGraphic component, uint textureID )
private void ApplyTextureToImage( UnityEngine.UI.MaskableGraphic component, uint textureID, Vector4? slice = null )
{
var texture = GetCachedTexture( textureID );

Expand All @@ -107,19 +107,27 @@ private void ApplyTextureToImage( UnityEngine.UI.MaskableGraphic component, uint
requestingTextureImages[ textureID ] = components;
RequestFileFromServer( textureID, FileStorage.Type.png, "CL_ReceiveFilePng" );
}
components.Add( component );
components.Add( new ImageRequest()
{
graphic = component,
slice = slice
});
return;
}
}

ApplyCachedTextureToImage( component, texture );
ApplyCachedTextureToImage( component, texture, slice );
}

private void ApplyCachedTextureToImage( UnityEngine.UI.MaskableGraphic component, CachedTexture texture )
private void ApplyCachedTextureToImage( UnityEngine.UI.MaskableGraphic component, CachedTexture texture, Vector4? slice = null )
{
var image = component as UnityEngine.UI.Image;
if ( image )
{
if(slice != null){
image.sprite = Sprite.Create( texture.Texture, new Rect( 0, 0, texture.Texture.width, texture.Texture.height ), new Vector2( 0.5f, 0.5f ), 100, 0, SpriteMeshType.Tight, slice.Value);
return;
}
if ( texture.Sprite == null )
{
texture.Sprite = Sprite.Create( texture.Texture, new Rect( 0, 0, texture.Texture.width, texture.Texture.height ), new Vector2( 0.5f, 0.5f ) );
Expand Down
10 changes: 9 additions & 1 deletion CommunityEntity.UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,18 @@ T GetOrAddComponent<T>() where T : Component
c.color = ColorEx.Parse( obj.GetString( "color", "1.0 1.0 1.0 1.0" ) );
if ( ShouldUpdateField( "imagetype" ) )
c.type = ParseEnum( obj.GetString( "imagetype", "Simple" ), UnityEngine.UI.Image.Type.Simple );
if( ShouldUpdateField( "fillCenter" ) )
c.fillCenter = obj.GetBoolean("fillCenter", c.fillCenter);

if ( obj.ContainsKey( "png" ) && uint.TryParse( obj.GetString( "png" ), out var id ) )
{
ApplyTextureToImage( c, id );
Vector4? slice = null;
if (obj.ContainsKey("slice"))
{
var sliceAsCol = ColorEx.Parse(obj.GetString("slice", "0 0 0 0"));
slice = new Vector4(sliceAsCol.r, sliceAsCol.g, sliceAsCol.b, sliceAsCol.a);
}
ApplyTextureToImage( c, id, slice );
}

if ( obj.ContainsKey( "itemid" ) )
Expand Down