Calling a method after a Textbox is closed

From The Stardew Modding Wiki
Jump to navigation Jump to search
   //This class is a subclass to the DialogueBox and can thus be used in place of it
   //The DialogueBox is being used whenever a question pops up or a message is being displayed until you click it away
   //This subclass is only for messages though, because the question variant already calls a
   //function when an answer has been selected.
   class ResponsiveDialogueBox : DialogueBox
   {
       //The function that should be executed, after the dialogue is being closed
       //A method with no parameters which returns void
       //If you want to execute a function with parameters or a return type build a 'shell' around it
       //and set the shell as the AfterDialogueFunction:
       //(in this example 'this' is the object you want your function to be called in)
       //
       // AfterDialogueFunction()
       // {
       //  this.someValueToSet = yourOwnFunction(this.ParameterFromSomewhere);
       // }
       
       //The property which holds the function which should be invoked after the box is closed
        private Action AfterDialogueFunction { get; set; }
        
       //The default constructor. It adds the given function to the class and the relays the
       //message to the base class, so that a normal DialogueBox is being displayed
       public ResponsiveDialogueBox(string sMessage, Action fuFunction)
           : base(sMessage)
       {
           this.AfterDialogueFunction = fuFunction;
       }
       
       //The function which reacts to your leftclick. It is the same as the one in DialogueBox, but with everything not needed
       //for a simple box kicked out
       public override void receiveLeftClick(int x, int y, bool playSound = true)
       {
           if (this.characterIndexInDialogue < this.getCurrentString().Length - 1)
           {
               this.characterIndexInDialogue = this.getCurrentString().Length - 1;
           }
           this.questionFinishPauseTimer = (Game1.eventUp ? 600 : 200);
           this.transitioning = true;
           this.transitionInitialized = false;
           this.transitioningBigger = true;
           Game1.dialogueUp = false;

           //Calls your own function
           AfterDialogueFunction();

           Game1.objectDialoguePortraitPerson = null;
           Game1.playSound("smallSelect");
           this.selectedResponse = -1;
           if (Game1.activeClickableMenu != null && Game1.activeClickableMenu.Equals(this))
           {
               this.beginOutro();
           }
       }
   }