first commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.PlasticSCM.Editor.UI.Avatar
|
||||
{
|
||||
internal static class ApplyCircleMask
|
||||
{
|
||||
internal static Texture2D For(Texture2D sourceImage)
|
||||
{
|
||||
int centerx = sourceImage.width / 2;
|
||||
int centery = sourceImage.height / 2;
|
||||
|
||||
int radius = sourceImage.width / 2;
|
||||
|
||||
Texture2D result = Images.GetNewTextureFromTexture(sourceImage);
|
||||
|
||||
for (int i = (centerx - radius); i < centerx + radius; i++)
|
||||
{
|
||||
for (int j = (centery - radius); j < centery + radius; j++)
|
||||
{
|
||||
float dx = i - centerx;
|
||||
float dy = j - centery;
|
||||
|
||||
float d = Mathf.Sqrt(dx * dx + dy * dy);
|
||||
|
||||
float borderSize = 1f;
|
||||
|
||||
if (d <= (radius - borderSize))
|
||||
{
|
||||
result.SetPixel(
|
||||
i - (centerx - radius),
|
||||
j - (centery - radius),
|
||||
sourceImage.GetPixel(i, j));
|
||||
continue;
|
||||
}
|
||||
|
||||
Color color = sourceImage.GetPixel(i, j);
|
||||
|
||||
result.SetPixel(
|
||||
i - (centerx - radius),
|
||||
j - (centery - radius),
|
||||
Color.Lerp(Color.clear, color,
|
||||
GetAntialiasAlpha(radius, d, borderSize)));
|
||||
}
|
||||
}
|
||||
|
||||
result.Apply();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static float GetAntialiasAlpha(float radius, float d, float borderSize)
|
||||
{
|
||||
if (d >= (radius + borderSize))
|
||||
return 0f;
|
||||
|
||||
if (d - radius - borderSize == 0)
|
||||
return 0;
|
||||
|
||||
float proportion =
|
||||
Mathf.Abs(d - radius - borderSize) /
|
||||
(radius + borderSize) - (radius - borderSize);
|
||||
|
||||
return Mathf.Max(0, 1.0f - proportion);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e76ce7703143a924b9c0693ee66ebee7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.PlasticSCM.Editor.UI.Avatar
|
||||
{
|
||||
internal static class AvatarImages
|
||||
{
|
||||
internal static void Dispose()
|
||||
{
|
||||
foreach (Texture2D image in mAvatars.Values)
|
||||
UnityEngine.Object.DestroyImmediate(image, true);
|
||||
|
||||
mAvatars.Clear();
|
||||
}
|
||||
|
||||
internal static bool HasGravatar(string email)
|
||||
{
|
||||
return mAvatars.ContainsKey(email);
|
||||
}
|
||||
|
||||
internal static void AddGravatar(string email, Texture2D image)
|
||||
{
|
||||
if (mAvatars.ContainsKey(email))
|
||||
return;
|
||||
|
||||
mAvatars.Add(email, image);
|
||||
}
|
||||
|
||||
internal static void UpdateGravatar(string email, byte[] rawImage)
|
||||
{
|
||||
if (!mAvatars.ContainsKey(email))
|
||||
return;
|
||||
|
||||
Texture2D result = GetTexture(rawImage);
|
||||
|
||||
mAvatars[email] = result;
|
||||
}
|
||||
|
||||
internal static Texture2D GetAvatar(string email)
|
||||
{
|
||||
Texture2D image = GetGravatarImage(email);
|
||||
|
||||
if (image != null)
|
||||
return image;
|
||||
|
||||
return Images.GetEmptyGravatar();
|
||||
}
|
||||
|
||||
static Texture2D GetGravatarImage(string email)
|
||||
{
|
||||
Texture2D avatar;
|
||||
mAvatars.TryGetValue(email, out avatar);
|
||||
return avatar;
|
||||
}
|
||||
|
||||
static Texture2D GetTexture(byte[] rawImage)
|
||||
{
|
||||
Texture2D result = Images.GetNewTextureFromBytes(32, 32, rawImage);
|
||||
Texture2D maskImage = ApplyCircleMask.For(result);
|
||||
|
||||
UnityEngine.Object.DestroyImmediate(result, true);
|
||||
|
||||
return maskImage;
|
||||
}
|
||||
|
||||
static readonly Dictionary<string, Texture2D> mAvatars =
|
||||
new Dictionary<string, Texture2D>();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43c380b3a07cbf449a54619bb50096b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
using PlasticGui;
|
||||
using CodiceApp.Gravatar;
|
||||
|
||||
namespace Unity.PlasticSCM.Editor.UI.Avatar
|
||||
{
|
||||
internal static class GetAvatar
|
||||
{
|
||||
internal static Texture2D ForEmail(
|
||||
string email,
|
||||
Action avatarLoadedAction)
|
||||
{
|
||||
if (string.IsNullOrEmpty(email))
|
||||
return Images.GetEmptyGravatar();
|
||||
|
||||
if (AvatarImages.HasGravatar(email))
|
||||
return AvatarImages.GetAvatar(email);
|
||||
|
||||
Texture2D defaultImage =
|
||||
Images.GetEmptyGravatar();
|
||||
|
||||
AvatarImages.AddGravatar(email, defaultImage);
|
||||
|
||||
LoadAvatar.ForEmail(
|
||||
email, avatarLoadedAction,
|
||||
AfterDownloadSucceed);
|
||||
|
||||
return defaultImage;
|
||||
}
|
||||
|
||||
static void AfterDownloadSucceed(
|
||||
string email,
|
||||
GravatarImagesProvider.Result result,
|
||||
Action avatarLoadedAction)
|
||||
{
|
||||
if (result.ResultCode == GravatarImagesProvider.Result.OperationResult.OK)
|
||||
{
|
||||
AvatarImages.UpdateGravatar(email, result.RawGravatar);
|
||||
|
||||
avatarLoadedAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0afb4c6d979970647841ee14a55b8d25
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user