|
| 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 | +} |
0 commit comments