Skip to content

Commit 85277b5

Browse files
committed
Rename file to match class name and extract other classes out of it for readability
1 parent 79ae71a commit 85277b5

File tree

6 files changed

+438
-373
lines changed

6 files changed

+438
-373
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright (c) The OpenRA Developers and Contributors
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, either version 3 of
7+
* the License, or (at your option) any later version. For more
8+
* information, see COPYING.
9+
*/
10+
#endregion
11+
12+
using OpenRA.Network;
13+
using OpenRA.Widgets;
14+
15+
namespace OpenRA.Mods.Common.Widgets.Logic
16+
{
17+
public class AnonymousProfileTooltipLogic : ChromeLogic
18+
{
19+
[ObjectCreator.UseCtor]
20+
public AnonymousProfileTooltipLogic(Widget widget, Session.Client client)
21+
{
22+
var nameLabel = widget.Get<LabelWidget>("NAME");
23+
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
24+
widget.Bounds.Width = nameFont.Measure(nameLabel.GetText()).X + 2 * nameLabel.Bounds.Left;
25+
26+
var locationLabel = widget.Get<LabelWidget>("LOCATION");
27+
var ipLabel = widget.Get<LabelWidget>("IP");
28+
var adminLabel = widget.Get("GAME_ADMIN");
29+
30+
if (client.Location != null)
31+
{
32+
var locationFont = Game.Renderer.Fonts[locationLabel.Font];
33+
var locationWidth = widget.Bounds.Width - 2 * locationLabel.Bounds.X;
34+
var location = WidgetUtils.TruncateText(client.Location, locationWidth, locationFont);
35+
locationLabel.IsVisible = () => true;
36+
locationLabel.GetText = () => location;
37+
widget.Bounds.Height += locationLabel.Bounds.Height;
38+
ipLabel.Bounds.Y += locationLabel.Bounds.Height;
39+
adminLabel.Bounds.Y += locationLabel.Bounds.Height;
40+
}
41+
42+
if (client.AnonymizedIPAddress != null)
43+
{
44+
ipLabel.IsVisible = () => true;
45+
ipLabel.GetText = () => client.AnonymizedIPAddress;
46+
widget.Bounds.Height += ipLabel.Bounds.Height;
47+
adminLabel.Bounds.Y += locationLabel.Bounds.Height;
48+
}
49+
50+
if (client.IsAdmin)
51+
{
52+
adminLabel.IsVisible = () => true;
53+
widget.Bounds.Height += adminLabel.Bounds.Height;
54+
}
55+
}
56+
}
57+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright (c) The OpenRA Developers and Contributors
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, either version 3 of
7+
* the License, or (at your option) any later version. For more
8+
* information, see COPYING.
9+
*/
10+
#endregion
11+
12+
using OpenRA.Network;
13+
using OpenRA.Widgets;
14+
15+
namespace OpenRA.Mods.Common.Widgets.Logic
16+
{
17+
public class BotTooltipLogic : ChromeLogic
18+
{
19+
[FluentReference("name")]
20+
const string BotManagedBy = "label-bot-managed-by-tooltip";
21+
22+
[ObjectCreator.UseCtor]
23+
public BotTooltipLogic(OrderManager orderManager, Widget widget, Session.Client client)
24+
{
25+
var nameLabel = widget.Get<LabelWidget>("NAME");
26+
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
27+
var controller = orderManager.LobbyInfo.Clients.FirstOrDefault(c => c.Index == client.BotControllerClientIndex);
28+
if (controller != null)
29+
nameLabel.GetText = () => FluentProvider.GetString(BotManagedBy, "name", controller.Name);
30+
31+
widget.Bounds.Width = nameFont.Measure(nameLabel.GetText()).X + 2 * nameLabel.Bounds.Left;
32+
}
33+
}
34+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright (c) The OpenRA Developers and Contributors
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, either version 3 of
7+
* the License, or (at your option) any later version. For more
8+
* information, see COPYING.
9+
*/
10+
#endregion
11+
12+
using System;
13+
using OpenRA.Graphics;
14+
using OpenRA.Widgets;
15+
16+
namespace OpenRA.Mods.Common.Widgets.Logic
17+
{
18+
public class LocalProfileLogic : ChromeLogic
19+
{
20+
readonly WorldRenderer worldRenderer;
21+
readonly LocalPlayerProfile localProfile;
22+
readonly Widget badgeContainer;
23+
readonly Widget widget;
24+
bool notFound;
25+
bool badgesVisible;
26+
27+
[ObjectCreator.UseCtor]
28+
public LocalProfileLogic(Widget widget, WorldRenderer worldRenderer, Func<bool> minimalProfile)
29+
{
30+
this.worldRenderer = worldRenderer;
31+
this.widget = widget;
32+
localProfile = Game.LocalPlayerProfile;
33+
34+
// Key registration
35+
widget.Get("GENERATE_KEYS").IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.Uninitialized && !minimalProfile();
36+
widget.Get("GENERATING_KEYS").IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.GeneratingKeys && !minimalProfile();
37+
38+
var lastProfileState = LocalPlayerProfile.LinkState.CheckingLink;
39+
widget.Get("REGISTER_FINGERPRINT").IsVisible = () =>
40+
{
41+
// Take a copy of the state to avoid race conditions
42+
var state = localProfile.State;
43+
44+
// Copy the key to the clipboard when displaying the link instructions
45+
if (state != lastProfileState && state == LocalPlayerProfile.LinkState.Unlinked)
46+
Game.SetClipboardText(localProfile.PublicKey);
47+
48+
lastProfileState = state;
49+
return localProfile.State == LocalPlayerProfile.LinkState.Unlinked && !notFound && !minimalProfile();
50+
};
51+
52+
widget.Get("CHECKING_FINGERPRINT").IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.CheckingLink && !minimalProfile();
53+
widget.Get("FINGERPRINT_NOT_FOUND").IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.Unlinked && notFound && !minimalProfile();
54+
widget.Get("CONNECTION_ERROR").IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.ConnectionFailed && !minimalProfile();
55+
56+
widget.Get<ButtonWidget>("GENERATE_KEY").OnClick = localProfile.GenerateKeypair;
57+
58+
widget.Get<ButtonWidget>("CHECK_KEY").OnClick = () => localProfile.RefreshPlayerData(() => RefreshComplete(true));
59+
60+
widget.Get<ButtonWidget>("DELETE_KEY").OnClick = () =>
61+
{
62+
localProfile.DeleteKeypair();
63+
Game.RunAfterTick(Ui.ResetTooltips);
64+
};
65+
66+
widget.Get<ButtonWidget>("FINGERPRINT_NOT_FOUND_CONTINUE").OnClick = () =>
67+
{
68+
notFound = false;
69+
Game.RunAfterTick(Ui.ResetTooltips);
70+
};
71+
72+
widget.Get<ButtonWidget>("CONNECTION_ERROR_RETRY").OnClick = () => localProfile.RefreshPlayerData(() => RefreshComplete(true));
73+
74+
// Profile view
75+
widget.Get("PROFILE_HEADER").IsVisible = () => localProfile.State == LocalPlayerProfile.LinkState.Linked;
76+
widget.Get<LabelWidget>("PROFILE_NAME").GetText = () => localProfile.ProfileData.ProfileName;
77+
widget.Get<LabelWidget>("PROFILE_RANK").GetText = () => localProfile.ProfileData.ProfileRank;
78+
79+
var destroyKey = widget.Get<ButtonWidget>("DESTROY_KEY");
80+
destroyKey.OnClick = localProfile.DeleteKeypair;
81+
destroyKey.IsDisabled = minimalProfile;
82+
83+
badgeContainer = widget.Get("BADGES_CONTAINER");
84+
badgeContainer.IsVisible = () => badgesVisible && !minimalProfile()
85+
&& localProfile.State == LocalPlayerProfile.LinkState.Linked;
86+
87+
localProfile.RefreshPlayerData(() => RefreshComplete(false));
88+
}
89+
90+
public void RefreshComplete(bool updateNotFound)
91+
{
92+
if (updateNotFound)
93+
notFound = localProfile.State == LocalPlayerProfile.LinkState.Unlinked;
94+
95+
Game.RunAfterTick(() =>
96+
{
97+
badgesVisible = false;
98+
99+
if (localProfile.State == LocalPlayerProfile.LinkState.Linked && localProfile.ProfileData.Badges.Count > 0)
100+
{
101+
Func<int, int> negotiateWidth = _ => widget.Get("PROFILE_HEADER").Bounds.Width;
102+
103+
// Remove any stale badges that may be left over from a previous session
104+
badgeContainer.RemoveChildren();
105+
106+
var badges = Ui.LoadWidget("PLAYER_PROFILE_BADGES_INSERT", badgeContainer, new WidgetArgs()
107+
{
108+
{ "worldRenderer", worldRenderer },
109+
{ "profile", localProfile.ProfileData },
110+
{ "negotiateWidth", negotiateWidth }
111+
});
112+
113+
if (badges.Bounds.Height > 0)
114+
{
115+
badgeContainer.Bounds.Height = badges.Bounds.Height;
116+
badgesVisible = true;
117+
}
118+
}
119+
120+
Ui.ResetTooltips();
121+
});
122+
}
123+
}
124+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright (c) The OpenRA Developers and Contributors
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, either version 3 of
7+
* the License, or (at your option) any later version. For more
8+
* information, see COPYING.
9+
*/
10+
#endregion
11+
12+
using System;
13+
using OpenRA.Widgets;
14+
15+
namespace OpenRA.Mods.Common.Widgets.Logic
16+
{
17+
public class PlayerProfileBadgesLogic : ChromeLogic
18+
{
19+
[ObjectCreator.UseCtor]
20+
public PlayerProfileBadgesLogic(Widget widget, PlayerProfile profile, Func<int, int> negotiateWidth)
21+
{
22+
var showBadges = profile.Badges.Count > 0;
23+
widget.IsVisible = () => showBadges;
24+
25+
var badgeTemplate = widget.Get("BADGE_TEMPLATE");
26+
widget.RemoveChild(badgeTemplate);
27+
28+
// Negotiate the label length that the tooltip will allow
29+
var maxLabelWidth = 0;
30+
var templateIcon = badgeTemplate.Get("ICON");
31+
var templateLabel = badgeTemplate.Get<LabelWidget>("LABEL");
32+
var templateLabelFont = Game.Renderer.Fonts[templateLabel.Font];
33+
foreach (var badge in profile.Badges)
34+
maxLabelWidth = Math.Max(maxLabelWidth, templateLabelFont.Measure(badge.Label).X);
35+
36+
widget.Bounds.Width = negotiateWidth(2 * templateLabel.Bounds.Left - templateIcon.Bounds.Right + maxLabelWidth);
37+
38+
var badgeOffset = badgeTemplate.Bounds.Y;
39+
if (profile.Badges.Count > 0)
40+
badgeOffset += 3;
41+
42+
foreach (var badge in profile.Badges)
43+
{
44+
var b = badgeTemplate.Clone();
45+
var icon = b.Get<BadgeWidget>("ICON");
46+
icon.Badge = badge;
47+
48+
var label = b.Get<LabelWidget>("LABEL");
49+
var labelFont = Game.Renderer.Fonts[label.Font];
50+
51+
var labelText = WidgetUtils.TruncateText(badge.Label, widget.Bounds.Width - label.Bounds.X - icon.Bounds.X, labelFont);
52+
label.GetText = () => labelText;
53+
54+
b.Bounds.Y = badgeOffset;
55+
widget.AddChild(b);
56+
57+
badgeOffset += badgeTemplate.Bounds.Height;
58+
}
59+
60+
if (badgeOffset > badgeTemplate.Bounds.Y)
61+
badgeOffset += 5;
62+
63+
widget.Bounds.Height = badgeOffset;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)