Add lobby tooltips for registered / anonymous players.
This commit is contained in:
@@ -296,15 +296,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
return "Poor";
|
||||
}
|
||||
|
||||
public static string DescriptiveIpAddress(string ip)
|
||||
{
|
||||
if (ip == null)
|
||||
return "Unknown Host";
|
||||
if (ip == IPAddress.Loopback.ToString())
|
||||
return "Local Host";
|
||||
return ip;
|
||||
}
|
||||
|
||||
public static void SetupLatencyWidget(Widget parent, Session.Client c, OrderManager orderManager, bool visible)
|
||||
{
|
||||
var block = parent.GetOrNull("LATENCY");
|
||||
@@ -323,6 +314,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
tooltip.Bind(orderManager, null, c);
|
||||
}
|
||||
|
||||
public static void SetupProfileWidget(Widget parent, Session.Client c, OrderManager orderManager, WorldRenderer worldRenderer)
|
||||
{
|
||||
var profile = parent.GetOrNull<ImageWidget>("PROFILE");
|
||||
if (profile != null && c.Bot == null)
|
||||
{
|
||||
var imageName = (c != null && c.IsAdmin ? "admin-" : "player-")
|
||||
+ (c.Fingerprint != null ? "registered" : "anonymous");
|
||||
|
||||
profile.GetImageName = () => imageName;
|
||||
profile.IsVisible = () => true;
|
||||
}
|
||||
|
||||
var profileTooltip = parent.GetOrNull<ClientTooltipRegionWidget>("PROFILE_TOOLTIP");
|
||||
if (profileTooltip != null && c.Bot == null)
|
||||
{
|
||||
if (c != null && c.Fingerprint != null)
|
||||
profileTooltip.Template = "REGISTERED_PLAYER_TOOLTIP";
|
||||
|
||||
profileTooltip.Bind(orderManager, worldRenderer, c);
|
||||
profileTooltip.IsVisible = () => true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetupEditableNameWidget(Widget parent, Session.Slot s, Session.Client c, OrderManager orderManager, WorldRenderer worldRenderer)
|
||||
{
|
||||
var name = parent.Get<TextFieldWidget>("NAME");
|
||||
@@ -360,6 +374,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
return true;
|
||||
};
|
||||
|
||||
SetupProfileWidget(name, c, orderManager, worldRenderer);
|
||||
|
||||
HideChildWidget(parent, "SLOT_OPTIONS");
|
||||
}
|
||||
|
||||
@@ -370,6 +386,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
var font = Game.Renderer.Fonts[name.Font];
|
||||
var label = WidgetUtils.TruncateText(c.Name, name.Bounds.Width, font);
|
||||
name.GetText = () => label;
|
||||
|
||||
SetupProfileWidget(parent, c, orderManager, worldRenderer);
|
||||
}
|
||||
|
||||
public static void SetupEditableSlotWidget(Widget parent, Session.Slot s, Session.Client c,
|
||||
@@ -404,6 +422,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
slot.GetText = () => c != null ? c.Name : string.Empty;
|
||||
slot.OnMouseDown = _ => ShowPlayerActionDropDown(slot, s, c, orderManager, lobby, before, after);
|
||||
|
||||
SetupProfileWidget(slot, c, orderManager, worldRenderer);
|
||||
|
||||
// Ensure Name selector (if present) is hidden
|
||||
HideChildWidget(parent, "NAME");
|
||||
}
|
||||
@@ -573,12 +593,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
playerName.GetColor = () => player.Color.RGB;
|
||||
}
|
||||
|
||||
public static string GetExternalIP(int clientIndex, OrderManager orderManager)
|
||||
public static string GetExternalIP(Session.Client client, OrderManager orderManager)
|
||||
{
|
||||
var client = orderManager.LobbyInfo.ClientWithIndex(clientIndex);
|
||||
var address = client != null ? client.IpAddress : "";
|
||||
var lc = orderManager.LocalClient;
|
||||
if (lc != null && lc.Index == clientIndex && address == IPAddress.Loopback.ToString())
|
||||
if (lc != null && lc.Index == client.Index && address == IPAddress.Loopback.ToString())
|
||||
{
|
||||
var externalIP = UPnP.ExternalIP;
|
||||
if (externalIP != null)
|
||||
|
||||
@@ -10,7 +10,11 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Network;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
@@ -18,7 +22,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
public class LocalProfileLogic : ChromeLogic
|
||||
{
|
||||
readonly LocalPlayerProfile localProfile;
|
||||
readonly Widget detailsContainer;
|
||||
bool notFound;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
@@ -75,8 +78,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
destroyKey.OnClick = localProfile.DeleteKeypair;
|
||||
destroyKey.IsDisabled = minimalProfile;
|
||||
|
||||
detailsContainer = widget.Get("PROFILE_DETAILS");
|
||||
detailsContainer.IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.Linked && !minimalProfile();
|
||||
localProfile.RefreshPlayerData(() => RefreshComplete(false));
|
||||
}
|
||||
|
||||
@@ -88,4 +89,129 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
Game.RunAfterTick(Ui.ResetTooltips);
|
||||
}
|
||||
}
|
||||
|
||||
public class RegisteredProfileTooltipLogic : ChromeLogic
|
||||
{
|
||||
readonly PlayerDatabase playerDatabase;
|
||||
PlayerProfile profile;
|
||||
bool profileLoaded;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public RegisteredProfileTooltipLogic(Widget widget, WorldRenderer worldRenderer, ModData modData, Session.Client client)
|
||||
{
|
||||
playerDatabase = modData.Manifest.Get<PlayerDatabase>();
|
||||
|
||||
var header = widget.Get("HEADER");
|
||||
|
||||
var profileHeader = header.Get("PROFILE_HEADER");
|
||||
var messageHeader = header.Get("MESSAGE_HEADER");
|
||||
var message = messageHeader.Get<LabelWidget>("MESSAGE");
|
||||
var messageFont = Game.Renderer.Fonts[message.Font];
|
||||
|
||||
profileHeader.IsVisible = () => profileLoaded;
|
||||
messageHeader.IsVisible = () => !profileLoaded;
|
||||
|
||||
var profileWidth = 0;
|
||||
var messageText = "Loading player profile...";
|
||||
var messageWidth = messageFont.Measure(messageText).X + 2 * message.Bounds.Left;
|
||||
|
||||
Action<DownloadDataCompletedEventArgs> onQueryComplete = i =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (i.Error == null)
|
||||
{
|
||||
var yaml = MiniYaml.FromString(Encoding.UTF8.GetString(i.Result)).First();
|
||||
if (yaml.Key == "Player")
|
||||
{
|
||||
profile = FieldLoader.Load<PlayerProfile>(yaml.Value);
|
||||
Game.RunAfterTick(() =>
|
||||
{
|
||||
var nameLabel = profileHeader.Get<LabelWidget>("PROFILE_NAME");
|
||||
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
|
||||
var rankLabel = profileHeader.Get<LabelWidget>("PROFILE_RANK");
|
||||
var rankFont = Game.Renderer.Fonts[rankLabel.Font];
|
||||
|
||||
var adminContainer = profileHeader.Get("GAME_ADMIN");
|
||||
var adminLabel = adminContainer.Get<LabelWidget>("LABEL");
|
||||
var adminFont = Game.Renderer.Fonts[adminLabel.Font];
|
||||
|
||||
var headerSizeOffset = profileHeader.Bounds.Height - messageHeader.Bounds.Height;
|
||||
|
||||
nameLabel.GetText = () => profile.ProfileName;
|
||||
rankLabel.GetText = () => profile.ProfileRank;
|
||||
|
||||
profileWidth = Math.Max(profileWidth, nameFont.Measure(profile.ProfileName).X + 2 * nameLabel.Bounds.Left);
|
||||
profileWidth = Math.Max(profileWidth, rankFont.Measure(profile.ProfileRank).X + 2 * rankLabel.Bounds.Left);
|
||||
|
||||
header.Bounds.Height += headerSizeOffset;
|
||||
if (client.IsAdmin)
|
||||
{
|
||||
profileWidth = Math.Max(profileWidth, adminFont.Measure(adminLabel.Text).X + 2 * adminLabel.Bounds.Left);
|
||||
|
||||
adminContainer.IsVisible = () => true;
|
||||
profileHeader.Bounds.Height += adminLabel.Bounds.Height;
|
||||
header.Bounds.Height += adminLabel.Bounds.Height;
|
||||
}
|
||||
|
||||
profileWidth = Math.Min(profileWidth, widget.Bounds.Width);
|
||||
header.Bounds.Width = widget.Bounds.Width = profileWidth;
|
||||
widget.Bounds.Height = header.Bounds.Height;
|
||||
|
||||
profileLoaded = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Write("debug", "Failed to parse player data result with exception: {0}", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (profile == null)
|
||||
{
|
||||
messageText = "Failed to load player profile.";
|
||||
messageWidth = messageFont.Measure(messageText).X + 2 * message.Bounds.Left;
|
||||
header.Bounds.Width = widget.Bounds.Width = messageWidth;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
message.GetText = () => messageText;
|
||||
header.Bounds.Height += messageHeader.Bounds.Height;
|
||||
header.Bounds.Width = widget.Bounds.Width = messageWidth;
|
||||
widget.Bounds.Height = header.Bounds.Height;
|
||||
|
||||
new Download(playerDatabase.Profile + client.Fingerprint, _ => { }, onQueryComplete);
|
||||
}
|
||||
}
|
||||
|
||||
public class AnonymousProfileTooltipLogic : ChromeLogic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public AnonymousProfileTooltipLogic(Widget widget, OrderManager orderManager, Session.Client client)
|
||||
{
|
||||
var address = LobbyUtils.GetExternalIP(client, orderManager);
|
||||
var cachedDescriptiveIP = address ?? "Unknown IP";
|
||||
|
||||
var nameLabel = widget.Get<LabelWidget>("NAME");
|
||||
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
|
||||
widget.Bounds.Width = nameFont.Measure(nameLabel.Text).X + 2 * nameLabel.Bounds.Left;
|
||||
|
||||
var ipLabel = widget.Get<LabelWidget>("IP");
|
||||
ipLabel.GetText = () => cachedDescriptiveIP;
|
||||
|
||||
var locationLabel = widget.Get<LabelWidget>("LOCATION");
|
||||
var cachedCountryLookup = GeoIP.LookupCountry(address);
|
||||
locationLabel.GetText = () => cachedCountryLookup;
|
||||
|
||||
if (client.IsAdmin)
|
||||
{
|
||||
var adminLabel = widget.Get("GAME_ADMIN");
|
||||
adminLabel.IsVisible = () => client.IsAdmin;
|
||||
widget.Bounds.Height += adminLabel.Bounds.Height;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user