diff --git a/Intersect (Core)/Enums/EventResponseType.cs b/Intersect (Core)/Enums/EventResponseType.cs new file mode 100644 index 0000000000..6e93daa887 --- /dev/null +++ b/Intersect (Core)/Enums/EventResponseType.cs @@ -0,0 +1,12 @@ +namespace Intersect.Enums; + +public enum EventResponseType +{ + OneOption = 1, + + TwoOption, + + ThreeOption, + + FourOption, + } diff --git a/Intersect.Client/Core/Input.cs b/Intersect.Client/Core/Input.cs index 925bc4ce06..e60de89883 100644 --- a/Intersect.Client/Core/Input.cs +++ b/Intersect.Client/Core/Input.cs @@ -11,6 +11,7 @@ using Intersect.Client.Maps; using Intersect.Client.Networking; using Intersect.Configuration; +using Intersect.Enums; using Intersect.Utilities; namespace Intersect.Client.Core; @@ -97,7 +98,7 @@ public static void OnKeyPressed(Keys modifier, Keys key) var eventWindow = (EventWindow)Interface.Interface.InputBlockingElements[i]; if (eventWindow != null && !eventWindow.IsHidden && Globals.EventDialogs.Count > 0) { - eventWindow.EventResponse1_Clicked(null, null); + eventWindow.CloseEventResponse(EventResponseType.OneOption); canFocusChat = false; break; diff --git a/Intersect.Client/Entities/Events/Dialog.cs b/Intersect.Client/Entities/Events/Dialog.cs index d9f118cb66..5fdffee620 100644 --- a/Intersect.Client/Entities/Events/Dialog.cs +++ b/Intersect.Client/Entities/Events/Dialog.cs @@ -4,17 +4,17 @@ public partial class Dialog { public Guid EventId; - public string Face = ""; + public string Face = string.Empty; - public string Opt1 = ""; + public string Opt1 = string.Empty; - public string Opt2 = ""; + public string Opt2 = string.Empty; - public string Opt3 = ""; + public string Opt3 = string.Empty; - public string Opt4 = ""; + public string Opt4 = string.Empty; - public string Prompt = ""; + public string Prompt = string.Empty; public int ResponseSent; diff --git a/Intersect.Client/Interface/Game/EventWindow.cs b/Intersect.Client/Interface/Game/EventWindow.cs index e329bde0db..f8c04b073e 100644 --- a/Intersect.Client/Interface/Game/EventWindow.cs +++ b/Intersect.Client/Interface/Game/EventWindow.cs @@ -1,296 +1,190 @@ using Intersect.Client.Core; using Intersect.Client.Core.Controls; +using Intersect.Client.Entities.Events; +using Intersect.Client.Framework.Content; using Intersect.Client.Framework.File_Management; using Intersect.Client.Framework.Gwen.Control; -using Intersect.Client.Framework.Gwen.Control.EventArguments; using Intersect.Client.General; using Intersect.Client.Interface.Game.Typewriting; using Intersect.Client.Localization; using Intersect.Client.Networking; using Intersect.Configuration; +using Intersect.Enums; using Intersect.Utilities; namespace Intersect.Client.Interface.Game; - -public partial class EventWindow : Base +public partial class EventWindow : ImagePanel { + private readonly ImagePanel _panelEventFace; + private readonly ScrollControl _areaEventDialog; + private readonly Label _labelEventDialog; + private readonly RichLabel _richLabelEventDialog; + private readonly ScrollControl _areaEventDialogNoFace; + private readonly Label _labelEventDialogNoFace; + private readonly RichLabel _richLabelEventDialogNoFace; + private readonly Button _buttonEventResponse1; + private readonly Button _buttonEventResponse2; + private readonly Button _buttonEventResponse3; + private readonly Button _buttonEventResponse4; - private ScrollControl mEventDialogArea; + private readonly Typewriter _writer; + private bool _isTypewriting = false; + private readonly long _typewriterResponseDelay = ClientConfiguration.Instance.TypewriterResponseDelay; - private ScrollControl mEventDialogAreaNoFace; + private Dialog _currentDialog => Globals.EventDialogs[0]; + private bool _hasOneOption => _currentDialog.Opt1.Length > 0; + private bool _hasTwoOptions => _currentDialog.Opt2.Length > 0; + private bool _hasThreeOptions => _currentDialog.Opt3.Length > 0; + private bool _hasFourOptions => _currentDialog.Opt4.Length > 0; - private RichLabel mEventDialogLabel; + public EventWindow(Canvas gameCanvas) : base(gameCanvas, nameof(EventWindow)) + { + _panelEventFace = new ImagePanel(this, "EventFacePanel"); - private RichLabel mEventDialogLabelNoFace; + _areaEventDialog = new ScrollControl(this, "EventDialogArea"); + _labelEventDialog = new Label(_areaEventDialog, "EventDialogLabel"); + _richLabelEventDialog = new RichLabel(_areaEventDialog); - private Label mEventDialogLabelNoFaceTemplate; + _areaEventDialogNoFace = new ScrollControl(this, "EventDialogAreaNoFace"); + _labelEventDialogNoFace = new Label(_areaEventDialogNoFace, "EventDialogLabel"); + _richLabelEventDialogNoFace = new RichLabel(_areaEventDialogNoFace); - private Label mEventDialogLabelTemplate; + _buttonEventResponse1 = new Button(this, "Response1Button"); + _buttonEventResponse1.Clicked += (s, e) => CloseEventResponse(EventResponseType.OneOption); - //Window Controls - private ImagePanel mEventDialogWindow; + _buttonEventResponse2 = new Button(this, "Response2Button"); + _buttonEventResponse2.Clicked += (s, e) => CloseEventResponse(EventResponseType.TwoOption); - private ImagePanel mEventFace; + _buttonEventResponse3 = new Button(this, "Response3Button"); + _buttonEventResponse3.Clicked += (s, e) => CloseEventResponse(EventResponseType.ThreeOption); - private Button mEventResponse1; + _buttonEventResponse4 = new Button(this, "Response4Button"); + _buttonEventResponse4.Clicked += (s, e) => CloseEventResponse(EventResponseType.FourOption); - private Button mEventResponse2; + _writer = new Typewriter(); - private Button mEventResponse3; + Clicked += (s, e) => SkipTypewriting(); + Hide(); + } - private Button mEventResponse4; + public void Update() + { + if (IsHidden) + { + _ = Interface.InputBlockingElements.Remove(this); + } + else + { + if (!Interface.InputBlockingElements.Contains(this)) + { + Interface.InputBlockingElements.Add(this); + } + } - private bool _typewriting = false; + if (Globals.EventDialogs.Count <= 0) + { + return; + } - private readonly Typewriter _writer; + // Handle typewriting + if (_isTypewriting && !IsHidden) + { + var voiceIdx = Randomization.Next(0, ClientConfiguration.Instance.TypewriterSounds.Count); - private readonly long _typewriterResponseDelay = ClientConfiguration.Instance.TypewriterResponseDelay; + // Always show option 1 ("continue" if options empty) + _buttonEventResponse1.IsHidden = !_writer.IsDone; + _buttonEventResponse2.IsHidden = !_writer.IsDone || string.IsNullOrEmpty(_currentDialog.Opt2); + _buttonEventResponse3.IsHidden = !_writer.IsDone || string.IsNullOrEmpty(_currentDialog.Opt3); + _buttonEventResponse4.IsHidden = !_writer.IsDone || string.IsNullOrEmpty(_currentDialog.Opt4); - //Init - public EventWindow(Canvas gameCanvas) - { - //Event Dialog Window - mEventDialogWindow = new ImagePanel(gameCanvas, "EventDialogueWindow"); - mEventDialogWindow.Hide(); - Interface.InputBlockingElements.Add(mEventDialogWindow); + _writer.Write(ClientConfiguration.Instance.TypewriterSounds.ElementAtOrDefault(voiceIdx)); + if (_writer.IsDone) + { + var disableResponse = Timing.Global.MillisecondsUtc - _writer.DoneAtMilliseconds < _typewriterResponseDelay; + _buttonEventResponse1.IsDisabled = disableResponse; + _buttonEventResponse2.IsDisabled = disableResponse; + _buttonEventResponse3.IsDisabled = disableResponse; + _buttonEventResponse4.IsDisabled = disableResponse; + } + else if (Controls.KeyDown(Control.AttackInteract)) + { + SkipTypewriting(); + } - mEventFace = new ImagePanel(mEventDialogWindow, "EventFacePanel"); + return; + } - mEventDialogArea = new ScrollControl(mEventDialogWindow, "EventDialogArea"); - mEventDialogLabelTemplate = new Label(mEventDialogArea, "EventDialogLabel"); - mEventDialogLabel = new RichLabel(mEventDialogArea); + // We have dialog to show let's setup + Show(); + MakeModal(); + BringToFront(); + _areaEventDialog.ScrollToTop(); - mEventDialogAreaNoFace = new ScrollControl(mEventDialogWindow, "EventDialogAreaNoFace"); - mEventDialogLabelNoFaceTemplate = new Label(mEventDialogAreaNoFace, "EventDialogLabel"); - mEventDialogLabelNoFace = new RichLabel(mEventDialogAreaNoFace); + var responseCount = 0; + var maxResponse = 1; - mEventResponse1 = new Button(mEventDialogWindow, "EventResponse1"); - mEventResponse1.Clicked += EventResponse1_Clicked; + if (_hasOneOption) + { + responseCount++; + } - mEventResponse2 = new Button(mEventDialogWindow, "EventResponse2"); - mEventResponse2.Clicked += EventResponse2_Clicked; + if (_hasTwoOptions) + { + responseCount++; + maxResponse++; + } - mEventResponse3 = new Button(mEventDialogWindow, "EventResponse3"); - mEventResponse3.Clicked += EventResponse3_Clicked; + if (_hasThreeOptions) + { + responseCount++; + maxResponse++; + } - mEventResponse4 = new Button(mEventDialogWindow, "EventResponse4"); - mEventResponse4.Clicked += EventResponse4_Clicked; + if (_hasFourOptions) + { + responseCount++; + maxResponse++; + } - _writer = new Typewriter(); + _isTypewriting = ClientConfiguration.Instance.TypewriterEnabled && Globals.Database.TypewriterBehavior == TypewriterBehavior.Word; - mEventDialogWindow.Clicked += Dialog_Clicked; - } + Name = $"EventDialogWindow_{maxResponse}Response{(maxResponse == 1 ? string.Empty : 's')}"; + LoadJsonUi(GameContentManager.UI.InGame, Graphics.Renderer?.GetResolutionString()); - private void Dialog_Clicked(Base sender, ClickedEventArgs arguments) - { - SkipTypewriting(); - } + var faceTex = Globals.ContentManager.GetTexture(TextureType.Face, _currentDialog.Face); + _panelEventFace.Texture = faceTex; + _panelEventFace.IsHidden = faceTex == null; + _areaEventDialog.IsHidden = faceTex == null; + _areaEventDialogNoFace.IsHidden = faceTex != null; - //Update - public void Update() - { - if (mEventDialogWindow.IsHidden) + if (responseCount == 0) { - Interface.InputBlockingElements.Remove(this); + _buttonEventResponse1.Show(); + _buttonEventResponse1.SetText(Strings.EventWindow.Continue); + _buttonEventResponse2.Hide(); + _buttonEventResponse3.Hide(); + _buttonEventResponse4.Hide(); } else { - if (!Interface.InputBlockingElements.Contains(this)) - { - Interface.InputBlockingElements.Add(this); - } + _buttonEventResponse1.IsHidden = !_hasOneOption; + _buttonEventResponse1.SetText(_currentDialog.Opt1); + _buttonEventResponse2.IsHidden = !_hasTwoOptions; + _buttonEventResponse2.SetText(_currentDialog.Opt2); + _buttonEventResponse3.IsHidden = !_hasThreeOptions; + _buttonEventResponse3.SetText(_currentDialog.Opt3); + _buttonEventResponse4.IsHidden = !_hasFourOptions; + _buttonEventResponse4.SetText(_currentDialog.Opt4); } - if (Globals.EventDialogs.Count > 0) + if (faceTex != null) { - if (mEventDialogWindow.IsHidden) - { - base.Show(); - mEventDialogWindow.Show(); - mEventDialogWindow.MakeModal(); - mEventDialogArea.ScrollToTop(); - mEventDialogWindow.BringToFront(); - var faceTex = Globals.ContentManager.GetTexture( - Framework.Content.TextureType.Face, Globals.EventDialogs[0].Face - ); - - var responseCount = 0; - var maxResponse = 1; - if (Globals.EventDialogs[0].Opt1.Length > 0) - { - responseCount++; - } - - if (Globals.EventDialogs[0].Opt2.Length > 0) - { - responseCount++; - maxResponse = 2; - } - - if (Globals.EventDialogs[0].Opt3.Length > 0) - { - responseCount++; - maxResponse = 3; - } - - if (Globals.EventDialogs[0].Opt4.Length > 0) - { - responseCount++; - maxResponse = 4; - } - - _typewriting = ClientConfiguration.Instance.TypewriterEnabled && Globals.Database.TypewriterBehavior == Enums.TypewriterBehavior.Word; - - mEventResponse1.Name = ""; - mEventResponse2.Name = ""; - mEventResponse3.Name = ""; - mEventResponse4.Name = ""; - switch (maxResponse) - { - case 1: - mEventDialogWindow.Name = "EventDialogWindow_1Response"; - mEventResponse1.Name = "Response1Button"; - - break; - case 2: - mEventDialogWindow.Name = "EventDialogWindow_2Responses"; - mEventResponse1.Name = "Response1Button"; - mEventResponse2.Name = "Response2Button"; - - break; - case 3: - mEventDialogWindow.Name = "EventDialogWindow_3Responses"; - mEventResponse1.Name = "Response1Button"; - mEventResponse2.Name = "Response2Button"; - mEventResponse3.Name = "Response3Button"; - - break; - case 4: - mEventDialogWindow.Name = "EventDialogWindow_4Responses"; - mEventResponse1.Name = "Response1Button"; - mEventResponse2.Name = "Response2Button"; - mEventResponse3.Name = "Response3Button"; - mEventResponse4.Name = "Response4Button"; - - break; - } - - mEventDialogWindow.LoadJsonUi( - GameContentManager.UI.InGame, Graphics.Renderer.GetResolutionString() - ); - - if (faceTex != null) - { - mEventFace.Show(); - mEventFace.Texture = faceTex; - mEventDialogArea.Show(); - mEventDialogAreaNoFace.Hide(); - } - else - { - mEventFace.Hide(); - mEventDialogArea.Hide(); - mEventDialogAreaNoFace.Show(); - } - - if (responseCount == 0) - { - mEventResponse1.Show(); - mEventResponse1.SetText(Strings.EventWindow.Continue); - mEventResponse2.Hide(); - mEventResponse3.Hide(); - mEventResponse4.Hide(); - } - else - { - if (Globals.EventDialogs[0].Opt1 != "") - { - mEventResponse1.Show(); - mEventResponse1.SetText(Globals.EventDialogs[0].Opt1); - } - else - { - mEventResponse1.Hide(); - } - - if (Globals.EventDialogs[0].Opt2 != "") - { - mEventResponse2.Show(); - mEventResponse2.SetText(Globals.EventDialogs[0].Opt2); - } - else - { - mEventResponse2.Hide(); - } - - if (Globals.EventDialogs[0].Opt3 != "") - { - mEventResponse3.Show(); - mEventResponse3.SetText(Globals.EventDialogs[0].Opt3); - } - else - { - mEventResponse3.Hide(); - } - - if (Globals.EventDialogs[0].Opt4 != "") - { - mEventResponse4.Show(); - mEventResponse4.SetText(Globals.EventDialogs[0].Opt4); - } - else - { - mEventResponse4.Hide(); - } - } - - mEventDialogWindow.SetSize( - mEventDialogWindow.Texture.GetWidth(), mEventDialogWindow.Texture.GetHeight() - ); - - var prompt = Globals.EventDialogs[0].Prompt; - if (faceTex != null) - { - ShowDialog(mEventDialogLabel, - mEventDialogLabelTemplate, - mEventDialogArea, - prompt); - } - else - { - ShowDialog(mEventDialogLabelNoFace, - mEventDialogLabelNoFaceTemplate, - mEventDialogAreaNoFace, - prompt); - } - } - else if (_typewriting) - { - var voiceIdx = Randomization.Next(0, ClientConfiguration.Instance.TypewriterSounds.Count); - - var dialog = Globals.EventDialogs[0]; - - // Always show option 1 ("continue" if options empty) - mEventResponse1.IsHidden = !_writer.IsDone; - mEventResponse2.IsHidden = !_writer.IsDone || string.IsNullOrEmpty(dialog.Opt2); - mEventResponse3.IsHidden = !_writer.IsDone || string.IsNullOrEmpty(dialog.Opt3); - mEventResponse4.IsHidden = !_writer.IsDone || string.IsNullOrEmpty(dialog.Opt4); - - _writer.Write(ClientConfiguration.Instance.TypewriterSounds.ElementAtOrDefault(voiceIdx)); - if (_writer.IsDone) - { - var disableResponse = Timing.Global.MillisecondsUtc - _writer.DoneAtMilliseconds < _typewriterResponseDelay; - mEventResponse1.IsDisabled = disableResponse; - mEventResponse2.IsDisabled = disableResponse; - mEventResponse3.IsDisabled = disableResponse; - mEventResponse4.IsDisabled = disableResponse; - } - else if (Controls.KeyDown(Control.AttackInteract)) - { - SkipTypewriting(); - } - } + ShowDialog(_richLabelEventDialog, _labelEventDialog, _areaEventDialog, _currentDialog.Prompt); + } + else + { + ShowDialog(_richLabelEventDialogNoFace, _labelEventDialogNoFace, _areaEventDialogNoFace, _currentDialog.Prompt); } } @@ -306,80 +200,39 @@ private void ShowDialog(RichLabel dialogLabel, Label dialogLabelTemplate, Scroll dialogLabel.AddText(prompt, dialogLabelTemplate); - dialogLabel.SizeToChildren(false, true); + _ = dialogLabel.SizeToChildren(false, true); // Do this _after_ sizing so we have lines broken up - if (_typewriting) + if (_isTypewriting) { _writer.Initialize(dialogLabel.FormattedLabels); - mEventResponse1.Hide(); - mEventResponse2.Hide(); - mEventResponse3.Hide(); - mEventResponse4.Hide(); + _buttonEventResponse1.Hide(); + _buttonEventResponse2.Hide(); + _buttonEventResponse3.Hide(); + _buttonEventResponse4.Hide(); } dialogArea.ScrollToTop(); } - //Input Handlers - void EventResponse4_Clicked(Base sender, ClickedEventArgs arguments) + public void CloseEventResponse(EventResponseType response) { - var ed = Globals.EventDialogs[0]; - if (ed.ResponseSent != 0) + if (!_writer.IsDone) { + SkipTypewriting(); return; - } - - PacketSender.SendEventResponse(4, ed); - mEventDialogWindow.RemoveModal(); - mEventDialogWindow.IsHidden = true; - ed.ResponseSent = 1; - base.Hide(); - } - - void EventResponse3_Clicked(Base sender, ClickedEventArgs arguments) - { - var ed = Globals.EventDialogs[0]; - if (ed.ResponseSent != 0) - { - return; - } - - PacketSender.SendEventResponse(3, ed); - mEventDialogWindow.RemoveModal(); - mEventDialogWindow.IsHidden = true; - ed.ResponseSent = 1; - base.Hide(); - } + }; - void EventResponse2_Clicked(Base sender, ClickedEventArgs arguments) - { - var ed = Globals.EventDialogs[0]; - if (ed.ResponseSent != 0) - { - return; - } - - PacketSender.SendEventResponse(2, ed); - mEventDialogWindow.RemoveModal(); - mEventDialogWindow.IsHidden = true; - ed.ResponseSent = 1; - base.Hide(); - } - - public void EventResponse1_Clicked(Base? sender, ClickedEventArgs? arguments) - { - var ed = Globals.EventDialogs[0]; - if (ed.ResponseSent != 0) + var eventDialog = _currentDialog; + if (eventDialog.ResponseSent != 0) { return; } - PacketSender.SendEventResponse(1, ed); - mEventDialogWindow.RemoveModal(); - mEventDialogWindow.IsHidden = true; - ed.ResponseSent = 1; - base.Hide(); + PacketSender.SendEventResponse((byte)response, eventDialog); + RemoveModal(); + Hide(); + eventDialog.ResponseSent = 1; } private void SkipTypewriting() diff --git a/Intersect.Client/Interface/Game/Typewriting/Typewriter.cs b/Intersect.Client/Interface/Game/Typewriting/Typewriter.cs index 4743e7dffa..39b71fe0a2 100644 --- a/Intersect.Client/Interface/Game/Typewriting/Typewriter.cs +++ b/Intersect.Client/Interface/Game/Typewriting/Typewriter.cs @@ -51,7 +51,7 @@ private void NewLine() _lastChar = null; } - public void Write(string voice) + public void Write(string? voice) { if (IsDone) {